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.

IgnoreRule.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright (C) 2010, Red Hat Inc.
  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 org.eclipse.jgit.errors.InvalidPatternException;
  45. import org.eclipse.jgit.fnmatch.FileNameMatcher;
  46. /**
  47. * A single ignore rule corresponding to one line in a .gitignore or ignore
  48. * file. Parses the ignore pattern
  49. *
  50. * Inspiration from: Ferry Huberts
  51. *
  52. * @deprecated this rule does not support double star pattern and is slow
  53. * parsing glob expressions. Consider to use {@link FastIgnoreRule}
  54. * instead. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=440732
  55. */
  56. @Deprecated
  57. public class IgnoreRule {
  58. private String pattern;
  59. private boolean negation;
  60. private boolean nameOnly;
  61. private boolean dirOnly;
  62. private FileNameMatcher matcher;
  63. /**
  64. * Create a new ignore rule with the given pattern. Assumes that
  65. * the pattern is already trimmed.
  66. *
  67. * @param pattern
  68. * Base pattern for the ignore rule. This pattern will
  69. * be parsed to generate rule parameters.
  70. */
  71. public IgnoreRule (String pattern) {
  72. this.pattern = pattern;
  73. negation = false;
  74. nameOnly = false;
  75. dirOnly = false;
  76. matcher = null;
  77. setup();
  78. }
  79. /**
  80. * Remove leading/trailing characters as needed. Set up
  81. * rule variables for later matching.
  82. */
  83. private void setup() {
  84. int startIndex = 0;
  85. int endIndex = pattern.length();
  86. if (pattern.startsWith("!")) { //$NON-NLS-1$
  87. startIndex++;
  88. negation = true;
  89. }
  90. if (pattern.endsWith("/")) { //$NON-NLS-1$
  91. endIndex --;
  92. dirOnly = true;
  93. }
  94. pattern = pattern.substring(startIndex, endIndex);
  95. boolean hasSlash = pattern.contains("/"); //$NON-NLS-1$
  96. if (!hasSlash)
  97. nameOnly = true;
  98. else if (!pattern.startsWith("/")) { //$NON-NLS-1$
  99. //Contains "/" but does not start with one
  100. //Adding / to the start should not interfere with matching
  101. pattern = "/" + pattern; //$NON-NLS-1$
  102. }
  103. if (pattern.contains("*") || pattern.contains("?") || pattern.contains("[")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  104. try {
  105. matcher = new FileNameMatcher(pattern, Character.valueOf('/'));
  106. } catch (InvalidPatternException e) {
  107. // Ignore pattern exceptions
  108. }
  109. }
  110. }
  111. /**
  112. * @return
  113. * True if the pattern is just a file name and not a path
  114. */
  115. public boolean getNameOnly() {
  116. return nameOnly;
  117. }
  118. /**
  119. *
  120. * @return
  121. * True if the pattern should match directories only
  122. */
  123. public boolean dirOnly() {
  124. return dirOnly;
  125. }
  126. /**
  127. *
  128. * @return
  129. * True if the pattern had a "!" in front of it
  130. */
  131. public boolean getNegation() {
  132. return negation;
  133. }
  134. /**
  135. * @return
  136. * The blob pattern to be used as a matcher
  137. */
  138. public String getPattern() {
  139. return pattern;
  140. }
  141. /**
  142. * Returns true if a match was made.
  143. * <br>
  144. * This function does NOT return the actual ignore status of the
  145. * target! Please consult {@link #getResult()} for the ignore status. The actual
  146. * ignore status may be true or false depending on whether this rule is
  147. * an ignore rule or a negation rule.
  148. *
  149. * @param target
  150. * Name pattern of the file, relative to the base directory of this rule
  151. * @param isDirectory
  152. * Whether the target file is a directory or not
  153. * @return
  154. * True if a match was made. This does not necessarily mean that
  155. * the target is ignored. Call {@link IgnoreRule#getResult() getResult()} for the result.
  156. */
  157. public boolean isMatch(String target, boolean isDirectory) {
  158. if (!target.startsWith("/")) //$NON-NLS-1$
  159. target = "/" + target; //$NON-NLS-1$
  160. if (matcher == null) {
  161. if (target.equals(pattern)) {
  162. //Exact match
  163. if (dirOnly && !isDirectory)
  164. //Directory expectations not met
  165. return false;
  166. else
  167. //Directory expectations met
  168. return true;
  169. }
  170. /*
  171. * Add slashes for startsWith check. This avoids matching e.g.
  172. * "/src/new" to /src/newfile" but allows "/src/new" to match
  173. * "/src/new/newfile", as is the git standard
  174. */
  175. if ((target).startsWith(pattern + "/")) //$NON-NLS-1$
  176. return true;
  177. if (nameOnly) {
  178. //Iterate through each sub-name
  179. final String[] segments = target.split("/"); //$NON-NLS-1$
  180. for (int idx = 0; idx < segments.length; idx++) {
  181. final String segmentName = segments[idx];
  182. // String.split("/") creates empty segment for leading slash
  183. if (segmentName.length() == 0)
  184. continue;
  185. if (segmentName.equals(pattern) &&
  186. doesMatchDirectoryExpectations(isDirectory, idx, segments.length))
  187. return true;
  188. }
  189. }
  190. } else {
  191. matcher.reset();
  192. matcher.append(target);
  193. if (matcher.isMatch())
  194. return true;
  195. final String[] segments = target.split("/"); //$NON-NLS-1$
  196. if (nameOnly) {
  197. for (int idx = 0; idx < segments.length; idx++) {
  198. final String segmentName = segments[idx];
  199. // String.split("/") creates empty segment for leading slash
  200. if (segmentName.length() == 0)
  201. continue;
  202. //Iterate through each sub-directory
  203. matcher.reset();
  204. matcher.append(segmentName);
  205. if (matcher.isMatch() &&
  206. doesMatchDirectoryExpectations(isDirectory, idx, segments.length))
  207. return true;
  208. }
  209. } else {
  210. //TODO: This is the slowest operation
  211. //This matches e.g. "/src/ne?" to "/src/new/file.c"
  212. matcher.reset();
  213. for (int idx = 0; idx < segments.length; idx++) {
  214. final String segmentName = segments[idx];
  215. // String.split("/") creates empty segment for leading slash
  216. if (segmentName.length() == 0)
  217. continue;
  218. matcher.append("/" + segmentName); //$NON-NLS-1$
  219. if (matcher.isMatch()
  220. && doesMatchDirectoryExpectations(isDirectory, idx,
  221. segments.length))
  222. return true;
  223. }
  224. }
  225. }
  226. return false;
  227. }
  228. /**
  229. * If a call to <code>isMatch(String, boolean)</code> was previously
  230. * made, this will return whether or not the target was ignored. Otherwise
  231. * this just indicates whether the rule is non-negation or negation.
  232. *
  233. * @return
  234. * True if the target is to be ignored, false otherwise.
  235. */
  236. public boolean getResult() {
  237. return !negation;
  238. }
  239. private boolean doesMatchDirectoryExpectations(boolean isDirectory, int segmentIdx, int segmentLength) {
  240. // The segment we are checking is a directory, expectations are met.
  241. if (segmentIdx < segmentLength - 1) {
  242. return true;
  243. }
  244. // We are checking the last part of the segment for which isDirectory has to be considered.
  245. return !dirOnly || isDirectory;
  246. }
  247. @Override
  248. public String toString() {
  249. return pattern;
  250. }
  251. }