WildMatcher.java

  1. /*
  2.  * Copyright (C) 2014, Andrey Loskutov <loskutov@gmx.de> and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.ignore.internal;

  11. /**
  12.  * Wildmatch matcher for "double star" (<code>**</code>) pattern only. This
  13.  * matcher matches any path.
  14.  * <p>
  15.  * This class is immutable and thread safe.
  16.  */
  17. public final class WildMatcher extends AbstractMatcher {

  18.     static final String WILDMATCH = "**"; //$NON-NLS-1$

  19.     // double star for the beginning of pattern
  20.     static final String WILDMATCH2 = "/**"; //$NON-NLS-1$

  21.     WildMatcher(boolean dirOnly) {
  22.         super(WILDMATCH, dirOnly);
  23.     }

  24.     /** {@inheritDoc} */
  25.     @Override
  26.     public final boolean matches(String path, boolean assumeDirectory,
  27.             boolean pathMatch) {
  28.         return !dirOnly || assumeDirectory
  29.                 || (!pathMatch && isSubdirectory(path));
  30.     }

  31.     /** {@inheritDoc} */
  32.     @Override
  33.     public final boolean matches(String segment, int startIncl, int endExcl) {
  34.         return true;
  35.     }

  36.     private static boolean isSubdirectory(String path) {
  37.         final int slashIndex = path.indexOf('/');
  38.         return slashIndex >= 0 && slashIndex < path.length() - 1;
  39.     }
  40. }