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

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