You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FastIgnoreRule.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (C) 2014, Andrey Loskutov <loskutov@gmx.de>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.ignore;
  44. import static org.eclipse.jgit.ignore.internal.Strings.stripTrailing;
  45. import static org.eclipse.jgit.ignore.internal.IMatcher.NO_MATCH;
  46. import org.eclipse.jgit.errors.InvalidPatternException;
  47. import org.eclipse.jgit.ignore.internal.IMatcher;
  48. import org.eclipse.jgit.ignore.internal.PathMatcher;
  49. /**
  50. * "Fast" (compared with IgnoreRule) git ignore rule implementation supporting
  51. * also double star <code>**<code> pattern.
  52. * <p>
  53. * This class is immutable and thread safe.
  54. *
  55. * @since 3.6
  56. */
  57. public class FastIgnoreRule {
  58. /**
  59. * Character used as default path separator for ignore entries
  60. */
  61. public static final char PATH_SEPARATOR = '/';
  62. private final IMatcher matcher;
  63. private final boolean inverse;
  64. private final boolean dirOnly;
  65. /**
  66. *
  67. * @param pattern
  68. * ignore pattern as described in <a href=
  69. * "https://www.kernel.org/pub/software/scm/git/docs/gitignore.html"
  70. * >git manual</a>. If pattern is invalid or is not a pattern
  71. * (comment), this rule doesn't match anything.
  72. */
  73. public FastIgnoreRule(String pattern) {
  74. if (pattern == null)
  75. throw new IllegalArgumentException("Pattern must not be null!"); //$NON-NLS-1$
  76. if (pattern.length() == 0) {
  77. dirOnly = false;
  78. inverse = false;
  79. this.matcher = NO_MATCH;
  80. return;
  81. }
  82. inverse = pattern.charAt(0) == '!';
  83. if (inverse) {
  84. pattern = pattern.substring(1);
  85. if (pattern.length() == 0) {
  86. dirOnly = false;
  87. this.matcher = NO_MATCH;
  88. return;
  89. }
  90. }
  91. if (pattern.charAt(0) == '#') {
  92. this.matcher = NO_MATCH;
  93. dirOnly = false;
  94. } else {
  95. dirOnly = pattern.charAt(pattern.length() - 1) == PATH_SEPARATOR;
  96. if (dirOnly) {
  97. pattern = stripTrailing(pattern, PATH_SEPARATOR);
  98. if (pattern.length() == 0) {
  99. this.matcher = NO_MATCH;
  100. return;
  101. }
  102. }
  103. IMatcher m;
  104. try {
  105. m = PathMatcher.createPathMatcher(pattern,
  106. Character.valueOf(PATH_SEPARATOR), dirOnly);
  107. } catch (InvalidPatternException e) {
  108. m = NO_MATCH;
  109. }
  110. this.matcher = m;
  111. }
  112. }
  113. /**
  114. * Returns true if a match was made. <br>
  115. * This function does NOT return the actual ignore status of the target!
  116. * Please consult {@link #getResult()} for the negation status. The actual
  117. * ignore status may be true or false depending on whether this rule is an
  118. * ignore rule or a negation rule.
  119. *
  120. * @param path
  121. * Name pattern of the file, relative to the base directory of
  122. * this rule
  123. * @param directory
  124. * Whether the target file is a directory or not
  125. * @return True if a match was made. This does not necessarily mean that the
  126. * target is ignored. Call {@link #getResult() getResult()} for the
  127. * result.
  128. */
  129. public boolean isMatch(String path, boolean directory) {
  130. if (path == null)
  131. return false;
  132. if (path.length() == 0)
  133. return false;
  134. boolean match = matcher.matches(path, directory);
  135. return match;
  136. }
  137. /**
  138. * @return True if the pattern is just a file name and not a path
  139. */
  140. public boolean getNameOnly() {
  141. return !(matcher instanceof PathMatcher);
  142. }
  143. /**
  144. *
  145. * @return True if the pattern should match directories only
  146. */
  147. public boolean dirOnly() {
  148. return dirOnly;
  149. }
  150. /**
  151. * Indicates whether the rule is non-negation or negation.
  152. *
  153. * @return True if the pattern had a "!" in front of it
  154. */
  155. public boolean getNegation() {
  156. return inverse;
  157. }
  158. /**
  159. * Indicates whether the rule is non-negation or negation.
  160. *
  161. * @return True if the target is to be ignored, false otherwise.
  162. */
  163. public boolean getResult() {
  164. return !inverse;
  165. }
  166. @Override
  167. public String toString() {
  168. StringBuilder sb = new StringBuilder();
  169. if (inverse)
  170. sb.append('!');
  171. sb.append(matcher);
  172. if (dirOnly)
  173. sb.append(PATH_SEPARATOR);
  174. return sb.toString();
  175. }
  176. @Override
  177. public int hashCode() {
  178. final int prime = 31;
  179. int result = 1;
  180. result = prime * result + (inverse ? 1231 : 1237);
  181. result = prime * result + (dirOnly ? 1231 : 1237);
  182. result = prime * result + ((matcher == null) ? 0 : matcher.hashCode());
  183. return result;
  184. }
  185. @Override
  186. public boolean equals(Object obj) {
  187. if (this == obj)
  188. return true;
  189. if (!(obj instanceof FastIgnoreRule))
  190. return false;
  191. FastIgnoreRule other = (FastIgnoreRule) obj;
  192. if (inverse != other.inverse)
  193. return false;
  194. if (dirOnly != other.dirOnly)
  195. return false;
  196. return matcher.equals(other.matcher);
  197. }
  198. }