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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. if (txt.length() > 0 && !txt.startsWith("#") && !txt.equals("/")) { //$NON-NLS-1$ //$NON-NLS-2$
  103. FastIgnoreRule rule = new FastIgnoreRule(txt);
  104. if (!rule.isEmpty()) {
  105. rules.add(rule);
  106. }
  107. }
  108. }
  109. }
  110. private static BufferedReader asReader(InputStream in) {
  111. return new BufferedReader(new InputStreamReader(in, Constants.CHARSET));
  112. }
  113. /** @return list of all ignore rules held by this node. */
  114. public List<FastIgnoreRule> getRules() {
  115. return Collections.unmodifiableList(rules);
  116. }
  117. /**
  118. * Determine if an entry path matches an ignore rule.
  119. *
  120. * @param entryPath
  121. * the path to test. The path must be relative to this ignore
  122. * node's own repository path, and in repository path format
  123. * (uses '/' and not '\').
  124. * @param isDirectory
  125. * true if the target item is a directory.
  126. * @return status of the path.
  127. */
  128. public MatchResult isIgnored(String entryPath, boolean isDirectory) {
  129. return isIgnored(entryPath, isDirectory, false);
  130. }
  131. /**
  132. * Determine if an entry path matches an ignore rule.
  133. *
  134. * @param entryPath
  135. * the path to test. The path must be relative to this ignore
  136. * node's own repository path, and in repository path format
  137. * (uses '/' and not '\').
  138. * @param isDirectory
  139. * true if the target item is a directory.
  140. * @param negateFirstMatch
  141. * true if the first match should be negated
  142. * @return status of the path.
  143. * @since 3.6
  144. */
  145. public MatchResult isIgnored(String entryPath, boolean isDirectory,
  146. boolean negateFirstMatch) {
  147. if (rules.isEmpty())
  148. if (negateFirstMatch)
  149. return MatchResult.CHECK_PARENT_NEGATE_FIRST_MATCH;
  150. else
  151. return MatchResult.CHECK_PARENT;
  152. // Parse rules in the reverse order that they were read
  153. for (int i = rules.size() - 1; i > -1; i--) {
  154. FastIgnoreRule rule = rules.get(i);
  155. if (rule.isMatch(entryPath, isDirectory)) {
  156. if (rule.getResult()) {
  157. // rule matches: path could be ignored
  158. if (negateFirstMatch)
  159. // ignore current match, reset "negate" flag, continue
  160. negateFirstMatch = false;
  161. else
  162. // valid match, just return
  163. return MatchResult.IGNORED;
  164. } else {
  165. // found negated rule
  166. if (negateFirstMatch)
  167. // not possible to re-include excluded ignore rule
  168. return MatchResult.NOT_IGNORED;
  169. else
  170. // set the flag and continue
  171. negateFirstMatch = true;
  172. }
  173. }
  174. }
  175. if (negateFirstMatch)
  176. // negated rule found but there is no previous rule in *this* file
  177. return MatchResult.CHECK_PARENT_NEGATE_FIRST_MATCH;
  178. // *this* file has no matching rules
  179. return MatchResult.CHECK_PARENT;
  180. }
  181. @Override
  182. public String toString() {
  183. return rules.toString();
  184. }
  185. }