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

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