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.

IgnoreNode.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 java.io.BufferedReader;
  45. import java.io.IOException;
  46. import java.io.InputStream;
  47. import java.io.InputStreamReader;
  48. import java.util.ArrayList;
  49. import java.util.Collections;
  50. import java.util.List;
  51. import org.eclipse.jgit.lib.Constants;
  52. /**
  53. * Represents a bundle of ignore rules inherited from a base directory.
  54. *
  55. * This class is not thread safe, it maintains state about the last match.
  56. */
  57. public class IgnoreNode {
  58. /** Result from {@link IgnoreNode#isIgnored(String, boolean)}. */
  59. public static enum MatchResult {
  60. /** The file is not ignored, due to a rule saying its not ignored. */
  61. NOT_IGNORED,
  62. /** The file is ignored due to a rule in this node. */
  63. IGNORED,
  64. /** The ignore status is unknown, check inherited rules. */
  65. CHECK_PARENT,
  66. /**
  67. * The first previous (parent) ignore rule match (if any) should be
  68. * negated, and then inherited rules applied.
  69. *
  70. * @since 3.6
  71. */
  72. CHECK_PARENT_NEGATE_FIRST_MATCH;
  73. }
  74. /** The rules that have been parsed into this node. */
  75. private final List<FastIgnoreRule> rules;
  76. /** Create an empty ignore node with no rules. */
  77. public IgnoreNode() {
  78. rules = new ArrayList<FastIgnoreRule>();
  79. }
  80. /**
  81. * Create an ignore node with given rules.
  82. *
  83. * @param rules
  84. * list of rules.
  85. **/
  86. public IgnoreNode(List<FastIgnoreRule> rules) {
  87. this.rules = rules;
  88. }
  89. /**
  90. * Parse files according to gitignore standards.
  91. *
  92. * @param in
  93. * input stream holding the standard ignore format. The caller is
  94. * responsible for closing the stream.
  95. * @throws IOException
  96. * Error thrown when reading an ignore file.
  97. */
  98. public void parse(InputStream in) throws IOException {
  99. BufferedReader br = asReader(in);
  100. String txt;
  101. while ((txt = br.readLine()) != null) {
  102. txt = txt.trim();
  103. if (txt.length() > 0 && !txt.startsWith("#") && !txt.equals("/")) //$NON-NLS-1$ //$NON-NLS-2$
  104. rules.add(new FastIgnoreRule(txt));
  105. }
  106. }
  107. private static BufferedReader asReader(InputStream in) {
  108. return new BufferedReader(new InputStreamReader(in, Constants.CHARSET));
  109. }
  110. /** @return list of all ignore rules held by this node. */
  111. public List<FastIgnoreRule> getRules() {
  112. return Collections.unmodifiableList(rules);
  113. }
  114. /**
  115. * Determine if an entry path matches an ignore rule.
  116. *
  117. * @param entryPath
  118. * the path to test. The path must be relative to this ignore
  119. * node's own repository path, and in repository path format
  120. * (uses '/' and not '\').
  121. * @param isDirectory
  122. * true if the target item is a directory.
  123. * @return status of the path.
  124. */
  125. public MatchResult isIgnored(String entryPath, boolean isDirectory) {
  126. return isIgnored(entryPath, isDirectory, false);
  127. }
  128. /**
  129. * Determine if an entry path matches an ignore rule.
  130. *
  131. * @param entryPath
  132. * the path to test. The path must be relative to this ignore
  133. * node's own repository path, and in repository path format
  134. * (uses '/' and not '\').
  135. * @param isDirectory
  136. * true if the target item is a directory.
  137. * @param negateFirstMatch
  138. * true if the first match should be negated
  139. * @return status of the path.
  140. * @since 3.6
  141. */
  142. public MatchResult isIgnored(String entryPath, boolean isDirectory,
  143. boolean negateFirstMatch) {
  144. if (rules.isEmpty())
  145. if (negateFirstMatch)
  146. return MatchResult.CHECK_PARENT_NEGATE_FIRST_MATCH;
  147. else
  148. return MatchResult.CHECK_PARENT;
  149. // Parse rules in the reverse order that they were read
  150. for (int i = rules.size() - 1; i > -1; i--) {
  151. FastIgnoreRule rule = rules.get(i);
  152. if (rule.isMatch(entryPath, isDirectory)) {
  153. if (rule.getResult()) {
  154. // rule matches: path could be ignored
  155. if (negateFirstMatch)
  156. // ignore current match, reset "negate" flag, continue
  157. negateFirstMatch = false;
  158. else
  159. // valid match, just return
  160. return MatchResult.IGNORED;
  161. } else {
  162. // found negated rule
  163. if (negateFirstMatch)
  164. // not possible to re-include excluded ignore rule
  165. return MatchResult.NOT_IGNORED;
  166. else
  167. // set the flag and continue
  168. negateFirstMatch = true;
  169. }
  170. }
  171. }
  172. if (negateFirstMatch)
  173. // negated rule found but there is no previous rule in *this* file
  174. return MatchResult.CHECK_PARENT_NEGATE_FIRST_MATCH;
  175. // *this* file has no matching rules
  176. return MatchResult.CHECK_PARENT;
  177. }
  178. @Override
  179. public String toString() {
  180. return rules.toString();
  181. }
  182. }