1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.ignore;
11
12 import static java.nio.charset.StandardCharsets.UTF_8;
13
14 import java.io.BufferedReader;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.InputStreamReader;
18 import java.text.MessageFormat;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22
23 import org.eclipse.jgit.annotations.Nullable;
24 import org.eclipse.jgit.errors.InvalidPatternException;
25 import org.eclipse.jgit.internal.JGitText;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29
30
31
32
33
34 public class IgnoreNode {
35
36 private static final Logger LOG = LoggerFactory.getLogger(IgnoreNode.class);
37
38
39 public enum MatchResult {
40
41 NOT_IGNORED,
42
43
44 IGNORED,
45
46
47 CHECK_PARENT,
48
49
50
51
52
53
54
55 CHECK_PARENT_NEGATE_FIRST_MATCH;
56 }
57
58
59 private final List<FastIgnoreRule> rules;
60
61
62
63
64 public IgnoreNode() {
65 this(new ArrayList<>());
66 }
67
68
69
70
71
72
73
74 public IgnoreNode(List<FastIgnoreRule> rules) {
75 this.rules = rules;
76 }
77
78
79
80
81
82
83
84
85
86
87 public void parse(InputStream in) throws IOException {
88 parse(null, in);
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102
103 public void parse(String sourceName, InputStream in) throws IOException {
104 BufferedReader br = asReader(in);
105 String txt;
106 int lineNumber = 1;
107 while ((txt = br.readLine()) != null) {
108 if (txt.length() > 0 && !txt.startsWith("#") && !txt.equals("/")) {
109 FastIgnoreRule rule = new FastIgnoreRule();
110 try {
111 rule.parse(txt);
112 } catch (InvalidPatternException e) {
113 if (sourceName != null) {
114 LOG.error(MessageFormat.format(
115 JGitText.get().badIgnorePatternFull, sourceName,
116 Integer.toString(lineNumber), e.getPattern(),
117 e.getLocalizedMessage()), e);
118 } else {
119 LOG.error(MessageFormat.format(
120 JGitText.get().badIgnorePattern,
121 e.getPattern()), e);
122 }
123 }
124 if (!rule.isEmpty()) {
125 rules.add(rule);
126 }
127 }
128 lineNumber++;
129 }
130 }
131
132 private static BufferedReader asReader(InputStream in) {
133 return new BufferedReader(new InputStreamReader(in, UTF_8));
134 }
135
136
137
138
139
140
141 public List<FastIgnoreRule> getRules() {
142 return Collections.unmodifiableList(rules);
143 }
144
145
146
147
148
149
150
151
152
153
154
155
156 public MatchResult isIgnored(String entryPath, boolean isDirectory) {
157 final Boolean result = checkIgnored(entryPath, isDirectory);
158 if (result == null) {
159 return MatchResult.CHECK_PARENT;
160 }
161
162 return result.booleanValue() ? MatchResult.IGNORED
163 : MatchResult.NOT_IGNORED;
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180 public @Nullable Boolean checkIgnored(String entryPath,
181 boolean isDirectory) {
182
183
184 for (int i = rules.size() - 1; i > -1; i--) {
185 FastIgnoreRule rule = rules.get(i);
186 if (rule.isMatch(entryPath, isDirectory, true)) {
187 return Boolean.valueOf(rule.getResult());
188 }
189 }
190 return null;
191 }
192
193
194 @Override
195 public String toString() {
196 return rules.toString();
197 }
198 }