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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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;
  11. import static org.eclipse.jgit.ignore.IMatcher.NO_MATCH;
  12. import static org.eclipse.jgit.ignore.internal.Strings.isDirectoryPattern;
  13. import static org.eclipse.jgit.ignore.internal.Strings.stripTrailing;
  14. import static org.eclipse.jgit.ignore.internal.Strings.stripTrailingWhitespace;
  15. import org.eclipse.jgit.errors.InvalidPatternException;
  16. import org.eclipse.jgit.ignore.internal.PathMatcher;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. /**
  20. * "Fast" (compared with IgnoreRule) git ignore rule implementation supporting
  21. * also double star {@code **} pattern.
  22. * <p>
  23. * This class is immutable and thread safe.
  24. *
  25. * @since 3.6
  26. */
  27. public class FastIgnoreRule {
  28. private static final Logger LOG = LoggerFactory
  29. .getLogger(FastIgnoreRule.class);
  30. /**
  31. * Character used as default path separator for ignore entries
  32. */
  33. public static final char PATH_SEPARATOR = '/';
  34. private final IMatcher matcher;
  35. private final boolean inverse;
  36. private final boolean dirOnly;
  37. /**
  38. * Constructor for FastIgnoreRule
  39. *
  40. * @param pattern
  41. * ignore pattern as described in <a href=
  42. * "https://www.kernel.org/pub/software/scm/git/docs/gitignore.html"
  43. * >git manual</a>. If pattern is invalid or is not a pattern
  44. * (comment), this rule doesn't match anything.
  45. */
  46. public FastIgnoreRule(String pattern) {
  47. if (pattern == null)
  48. throw new IllegalArgumentException("Pattern must not be null!"); //$NON-NLS-1$
  49. if (pattern.length() == 0) {
  50. dirOnly = false;
  51. inverse = false;
  52. this.matcher = NO_MATCH;
  53. return;
  54. }
  55. inverse = pattern.charAt(0) == '!';
  56. if (inverse) {
  57. pattern = pattern.substring(1);
  58. if (pattern.length() == 0) {
  59. dirOnly = false;
  60. this.matcher = NO_MATCH;
  61. return;
  62. }
  63. }
  64. if (pattern.charAt(0) == '#') {
  65. this.matcher = NO_MATCH;
  66. dirOnly = false;
  67. return;
  68. }
  69. if (pattern.charAt(0) == '\\' && pattern.length() > 1) {
  70. char next = pattern.charAt(1);
  71. if (next == '!' || next == '#') {
  72. // remove backslash escaping first special characters
  73. pattern = pattern.substring(1);
  74. }
  75. }
  76. dirOnly = isDirectoryPattern(pattern);
  77. if (dirOnly) {
  78. pattern = stripTrailingWhitespace(pattern);
  79. pattern = stripTrailing(pattern, PATH_SEPARATOR);
  80. if (pattern.length() == 0) {
  81. this.matcher = NO_MATCH;
  82. return;
  83. }
  84. }
  85. IMatcher m;
  86. try {
  87. m = PathMatcher.createPathMatcher(pattern,
  88. Character.valueOf(PATH_SEPARATOR), dirOnly);
  89. } catch (InvalidPatternException e) {
  90. m = NO_MATCH;
  91. LOG.error(e.getMessage(), e);
  92. }
  93. this.matcher = m;
  94. }
  95. /**
  96. * Returns true if a match was made. <br>
  97. * This function does NOT return the actual ignore status of the target!
  98. * Please consult {@link #getResult()} for the negation status. The actual
  99. * ignore status may be true or false depending on whether this rule is an
  100. * ignore rule or a negation rule.
  101. *
  102. * @param path
  103. * Name pattern of the file, relative to the base directory of
  104. * this rule
  105. * @param directory
  106. * Whether the target file is a directory or not
  107. * @return True if a match was made. This does not necessarily mean that the
  108. * target is ignored. Call {@link #getResult() getResult()} for the
  109. * result.
  110. */
  111. public boolean isMatch(String path, boolean directory) {
  112. return isMatch(path, directory, false);
  113. }
  114. /**
  115. * Returns true if a match was made. <br>
  116. * This function does NOT return the actual ignore status of the target!
  117. * Please consult {@link #getResult()} for the negation status. The actual
  118. * ignore status may be true or false depending on whether this rule is an
  119. * ignore rule or a negation rule.
  120. *
  121. * @param path
  122. * Name pattern of the file, relative to the base directory of
  123. * this rule
  124. * @param directory
  125. * Whether the target file is a directory or not
  126. * @param pathMatch
  127. * {@code true} if the match is for the full path: see
  128. * {@link IMatcher#matches(String, int, int)}
  129. * @return True if a match was made. This does not necessarily mean that the
  130. * target is ignored. Call {@link #getResult() getResult()} for the
  131. * result.
  132. * @since 4.11
  133. */
  134. public boolean isMatch(String path, boolean directory, boolean pathMatch) {
  135. if (path == null)
  136. return false;
  137. if (path.length() == 0)
  138. return false;
  139. boolean match = matcher.matches(path, directory, pathMatch);
  140. return match;
  141. }
  142. /**
  143. * Whether the pattern is just a file name and not a path
  144. *
  145. * @return {@code true} if the pattern is just a file name and not a path
  146. */
  147. public boolean getNameOnly() {
  148. return !(matcher instanceof PathMatcher);
  149. }
  150. /**
  151. * Whether the pattern should match directories only
  152. *
  153. * @return {@code true} if the pattern should match directories only
  154. */
  155. public boolean dirOnly() {
  156. return dirOnly;
  157. }
  158. /**
  159. * Indicates whether the rule is non-negation or negation.
  160. *
  161. * @return True if the pattern had a "!" in front of it
  162. */
  163. public boolean getNegation() {
  164. return inverse;
  165. }
  166. /**
  167. * Indicates whether the rule is non-negation or negation.
  168. *
  169. * @return True if the target is to be ignored, false otherwise.
  170. */
  171. public boolean getResult() {
  172. return !inverse;
  173. }
  174. /**
  175. * Whether the rule never matches
  176. *
  177. * @return {@code true} if the rule never matches (comment line or broken
  178. * pattern)
  179. * @since 4.1
  180. */
  181. public boolean isEmpty() {
  182. return matcher == NO_MATCH;
  183. }
  184. /** {@inheritDoc} */
  185. @Override
  186. public String toString() {
  187. StringBuilder sb = new StringBuilder();
  188. if (inverse)
  189. sb.append('!');
  190. sb.append(matcher);
  191. if (dirOnly)
  192. sb.append(PATH_SEPARATOR);
  193. return sb.toString();
  194. }
  195. /** {@inheritDoc} */
  196. @Override
  197. public int hashCode() {
  198. final int prime = 31;
  199. int result = 1;
  200. result = prime * result + (inverse ? 1231 : 1237);
  201. result = prime * result + (dirOnly ? 1231 : 1237);
  202. result = prime * result + ((matcher == null) ? 0 : matcher.hashCode());
  203. return result;
  204. }
  205. /** {@inheritDoc} */
  206. @Override
  207. public boolean equals(Object obj) {
  208. if (this == obj)
  209. return true;
  210. if (!(obj instanceof FastIgnoreRule))
  211. return false;
  212. FastIgnoreRule other = (FastIgnoreRule) obj;
  213. if (inverse != other.inverse)
  214. return false;
  215. if (dirOnly != other.dirOnly)
  216. return false;
  217. return matcher.equals(other.matcher);
  218. }
  219. }