Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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("!")) {
  82. startIndex++;
  83. negation = true;
  84. }
  85. if (pattern.endsWith("/")) {
  86. endIndex --;
  87. dirOnly = true;
  88. }
  89. boolean hasSlash = pattern.contains("/");
  90. pattern = pattern.substring(startIndex, endIndex);
  91. if (!hasSlash)
  92. nameOnly = true;
  93. else if (!pattern.startsWith("/")) {
  94. //Contains "/" but does not start with one
  95. //Adding / to the start should not interfere with matching
  96. pattern = "/" + pattern;
  97. }
  98. if (pattern.contains("*") || pattern.contains("?") || pattern.contains("[")) {
  99. try {
  100. matcher = new FileNameMatcher(pattern, new Character('/'));
  101. } catch (InvalidPatternException e) {
  102. e.printStackTrace();
  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("/"))
  154. target = "/" + target;
  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 + "/"))
  171. return true;
  172. if (nameOnly) {
  173. //Iterate through each sub-name
  174. for (String folderName : target.split("/")) {
  175. if (folderName.equals(pattern))
  176. return true;
  177. }
  178. }
  179. } else {
  180. matcher.append(target);
  181. if (matcher.isMatch())
  182. return true;
  183. if (nameOnly) {
  184. for (String folderName : target.split("/")) {
  185. //Iterate through each sub-directory
  186. matcher.reset();
  187. matcher.append(folderName);
  188. if (matcher.isMatch())
  189. return true;
  190. }
  191. } else {
  192. //TODO: This is the slowest operation
  193. //This matches e.g. "/src/ne?" to "/src/new/file.c"
  194. matcher.reset();
  195. for (String folderName : target.split("/")) {
  196. if (folderName.length() > 0)
  197. matcher.append("/" + folderName);
  198. if (matcher.isMatch())
  199. return true;
  200. }
  201. }
  202. }
  203. return false;
  204. }
  205. /**
  206. * If a call to <code>isMatch(String, boolean)</code> was previously
  207. * made, this will return whether or not the target was ignored. Otherwise
  208. * this just indicates whether the rule is non-negation or negation.
  209. *
  210. * @return
  211. * True if the target is to be ignored, false otherwise.
  212. */
  213. public boolean getResult() {
  214. return !negation;
  215. }
  216. }