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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 static java.nio.charset.StandardCharsets.UTF_8;
  45. import java.io.BufferedReader;
  46. import java.io.IOException;
  47. import java.io.InputStream;
  48. import java.io.InputStreamReader;
  49. import java.util.ArrayList;
  50. import java.util.Collections;
  51. import java.util.List;
  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. /**
  77. * Create an empty ignore node with no rules.
  78. */
  79. public IgnoreNode() {
  80. rules = new ArrayList<>();
  81. }
  82. /**
  83. * Create an ignore node with given rules.
  84. *
  85. * @param rules
  86. * list of rules.
  87. */
  88. public IgnoreNode(List<FastIgnoreRule> rules) {
  89. this.rules = rules;
  90. }
  91. /**
  92. * Parse files according to gitignore standards.
  93. *
  94. * @param in
  95. * input stream holding the standard ignore format. The caller is
  96. * responsible for closing the stream.
  97. * @throws java.io.IOException
  98. * Error thrown when reading an ignore file.
  99. */
  100. public void parse(InputStream in) throws IOException {
  101. BufferedReader br = asReader(in);
  102. String txt;
  103. while ((txt = br.readLine()) != null) {
  104. if (txt.length() > 0 && !txt.startsWith("#") && !txt.equals("/")) { //$NON-NLS-1$ //$NON-NLS-2$
  105. FastIgnoreRule rule = new FastIgnoreRule(txt);
  106. if (!rule.isEmpty()) {
  107. rules.add(rule);
  108. }
  109. }
  110. }
  111. }
  112. private static BufferedReader asReader(InputStream in) {
  113. return new BufferedReader(new InputStreamReader(in, UTF_8));
  114. }
  115. /**
  116. * Get list of all ignore rules held by this node
  117. *
  118. * @return list of all ignore rules held by this node
  119. */
  120. public List<FastIgnoreRule> getRules() {
  121. return Collections.unmodifiableList(rules);
  122. }
  123. /**
  124. * Determine if an entry path matches an ignore rule.
  125. *
  126. * @param entryPath
  127. * the path to test. The path must be relative to this ignore
  128. * node's own repository path, and in repository path format
  129. * (uses '/' and not '\').
  130. * @param isDirectory
  131. * true if the target item is a directory.
  132. * @return status of the path.
  133. */
  134. public MatchResult isIgnored(String entryPath, boolean isDirectory) {
  135. final Boolean result = checkIgnored(entryPath, isDirectory);
  136. if (result == null) {
  137. return MatchResult.CHECK_PARENT;
  138. }
  139. return result.booleanValue() ? MatchResult.IGNORED
  140. : MatchResult.NOT_IGNORED;
  141. }
  142. /**
  143. * Determine if an entry path matches an ignore rule.
  144. *
  145. * @param entryPath
  146. * the path to test. The path must be relative to this ignore
  147. * node's own repository path, and in repository path format
  148. * (uses '/' and not '\').
  149. * @param isDirectory
  150. * true if the target item is a directory.
  151. * @return Boolean.TRUE, if the entry is ignored; Boolean.FALSE, if the
  152. * entry is forced to be not ignored (negated match); or null, if
  153. * undetermined
  154. * @since 4.11
  155. */
  156. public Boolean checkIgnored(String entryPath, boolean isDirectory) {
  157. // Parse rules in the reverse order that they were read because later
  158. // rules have higher priority
  159. for (int i = rules.size() - 1; i > -1; i--) {
  160. FastIgnoreRule rule = rules.get(i);
  161. if (rule.isMatch(entryPath, isDirectory, true)) {
  162. return Boolean.valueOf(rule.getResult());
  163. }
  164. }
  165. return null;
  166. }
  167. /** {@inheritDoc} */
  168. @Override
  169. public String toString() {
  170. return rules.toString();
  171. }
  172. }