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

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