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

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