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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 rules that have been parsed into this node. */
  68. private final List<IgnoreRule> rules;
  69. /** Create an empty ignore node with no rules. */
  70. public IgnoreNode() {
  71. rules = new ArrayList<IgnoreRule>();
  72. }
  73. /**
  74. * Create an ignore node with given rules.
  75. *
  76. * @param rules
  77. * list of rules.
  78. **/
  79. public IgnoreNode(List<IgnoreRule> rules) {
  80. this.rules = rules;
  81. }
  82. /**
  83. * Parse files according to gitignore standards.
  84. *
  85. * @param in
  86. * input stream holding the standard ignore format. The caller is
  87. * responsible for closing the stream.
  88. * @throws IOException
  89. * Error thrown when reading an ignore file.
  90. */
  91. public void parse(InputStream in) throws IOException {
  92. BufferedReader br = asReader(in);
  93. String txt;
  94. while ((txt = br.readLine()) != null) {
  95. txt = txt.trim();
  96. if (txt.length() > 0 && !txt.startsWith("#") && !txt.equals("/")) //$NON-NLS-1$ //$NON-NLS-2$
  97. rules.add(new IgnoreRule(txt));
  98. }
  99. }
  100. private static BufferedReader asReader(InputStream in) {
  101. return new BufferedReader(new InputStreamReader(in, Constants.CHARSET));
  102. }
  103. /** @return list of all ignore rules held by this node. */
  104. public List<IgnoreRule> getRules() {
  105. return Collections.unmodifiableList(rules);
  106. }
  107. /**
  108. * Determine if an entry path matches an ignore rule.
  109. *
  110. * @param entryPath
  111. * the path to test. The path must be relative to this ignore
  112. * node's own repository path, and in repository path format
  113. * (uses '/' and not '\').
  114. * @param isDirectory
  115. * true if the target item is a directory.
  116. * @return status of the path.
  117. */
  118. public MatchResult isIgnored(String entryPath, boolean isDirectory) {
  119. if (rules.isEmpty())
  120. return MatchResult.CHECK_PARENT;
  121. // Parse rules in the reverse order that they were read
  122. for (int i = rules.size() - 1; i > -1; i--) {
  123. IgnoreRule rule = rules.get(i);
  124. if (rule.isMatch(entryPath, isDirectory)) {
  125. if (rule.getResult())
  126. return MatchResult.IGNORED;
  127. else
  128. return MatchResult.NOT_IGNORED;
  129. }
  130. }
  131. return MatchResult.CHECK_PARENT;
  132. }
  133. }