Mercurial > jhg
comparison hg4j/src/main/java/org/tmatesoft/hg/repo/HgIgnore.java @ 213:6ec4af642ba8 gradle
Project uses Gradle for build - actual changes
author | Alexander Kitaev <kitaev@gmail.com> |
---|---|
date | Tue, 10 May 2011 10:52:53 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
212:edb2e2829352 | 213:6ec4af642ba8 |
---|---|
1 /* | |
2 * Copyright (c) 2010-2011 TMate Software Ltd | |
3 * | |
4 * This program is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
6 * the Free Software Foundation; version 2 of the License. | |
7 * | |
8 * This program is distributed in the hope that it will be useful, | |
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 * GNU General Public License for more details. | |
12 * | |
13 * For information on how to redistribute this software under | |
14 * the terms of a license other than GNU General Public License | |
15 * contact TMate Software at support@hg4j.com | |
16 */ | |
17 package org.tmatesoft.hg.repo; | |
18 | |
19 import java.io.BufferedReader; | |
20 import java.io.File; | |
21 import java.io.FileReader; | |
22 import java.io.IOException; | |
23 import java.util.ArrayList; | |
24 import java.util.Collections; | |
25 import java.util.List; | |
26 import java.util.regex.Pattern; | |
27 | |
28 import org.tmatesoft.hg.util.Path; | |
29 | |
30 /** | |
31 * Handling of ignored paths according to .hgignore configuration | |
32 * | |
33 * @author Artem Tikhomirov | |
34 * @author TMate Software Ltd. | |
35 */ | |
36 public class HgIgnore { | |
37 | |
38 private List<Pattern> entries; | |
39 | |
40 HgIgnore() { | |
41 entries = Collections.emptyList(); | |
42 } | |
43 | |
44 /* package-local */void read(File hgignoreFile) throws IOException { | |
45 if (!hgignoreFile.exists()) { | |
46 return; | |
47 } | |
48 ArrayList<Pattern> result = new ArrayList<Pattern>(entries); // start with existing | |
49 String syntax = "regex"; // or "glob" | |
50 BufferedReader fr = new BufferedReader(new FileReader(hgignoreFile)); | |
51 String line; | |
52 while ((line = fr.readLine()) != null) { | |
53 line = line.trim(); | |
54 if (line.startsWith("syntax:")) { | |
55 syntax = line.substring("syntax:".length()).trim(); | |
56 if (!"regex".equals(syntax) && !"glob".equals(syntax)) { | |
57 throw new IllegalStateException(line); | |
58 } | |
59 } else if (line.length() > 0) { | |
60 // shall I account for local paths in the file (i.e. | |
61 // back-slashed on windows)? | |
62 int x; | |
63 if ((x = line.indexOf('#')) >= 0) { | |
64 line = line.substring(0, x).trim(); | |
65 if (line.length() == 0) { | |
66 continue; | |
67 } | |
68 } | |
69 if ("glob".equals(syntax)) { | |
70 // hgignore(5) | |
71 // (http://www.selenic.com/mercurial/hgignore.5.html) says slashes '\' are escape characters, | |
72 // hence no special treatment of Windows path | |
73 // however, own attempts make me think '\' on Windows are not treated as escapes | |
74 line = glob2regex(line); | |
75 } | |
76 result.add(Pattern.compile(line)); // case-sensitive | |
77 } | |
78 } | |
79 result.trimToSize(); | |
80 entries = result; | |
81 } | |
82 | |
83 // note, #isIgnored(), even if queried for directories and returned positive reply, may still get | |
84 // a file from that ignored folder to get examined. Thus, patterns like "bin" shall match not only a folder, | |
85 // but any file under that folder as well | |
86 // Alternatively, file walker may memorize folder is ignored and uses this information for all nested files. However, | |
87 // this approach would require walker (a) return directories (b) provide nesting information. This may become | |
88 // troublesome when one walks not over io.File, but Eclipse's IResource or any other custom VFS. | |
89 // | |
90 // | |
91 // might be interesting, although looks like of no direct use in my case | |
92 // @see http://stackoverflow.com/questions/1247772/is-there-an-equivalent-of-java-util-regex-for-glob-type-patterns | |
93 private String glob2regex(String line) { | |
94 assert line.length() > 0; | |
95 StringBuilder sb = new StringBuilder(line.length() + 10); | |
96 sb.append('^'); // help avoid matcher.find() to match 'bin' pattern in the middle of the filename | |
97 int start = 0, end = line.length() - 1; | |
98 // '*' at the beginning and end of a line are useless for Pattern | |
99 // XXX although how about **.txt - such globs can be seen in a config, are they valid for HgIgnore? | |
100 while (start <= end && line.charAt(start) == '*') start++; | |
101 while (end > start && line.charAt(end) == '*') end--; | |
102 | |
103 for (int i = start; i <= end; i++) { | |
104 char ch = line.charAt(i); | |
105 if (ch == '.' || ch == '\\') { | |
106 sb.append('\\'); | |
107 } else if (ch == '?') { | |
108 // simple '.' substitution might work out, however, more formally | |
109 // a char class seems more appropriate to avoid accidentally | |
110 // matching a subdirectory with ? char (i.e. /a/b?d against /a/bad, /a/bed and /a/b/d) | |
111 // @see http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_13_03 | |
112 // quote: "The slash character in a pathname shall be explicitly matched by using one or more slashes in the pattern; | |
113 // it shall neither be matched by the asterisk or question-mark special characters nor by a bracket expression" | |
114 sb.append("[^/]"); | |
115 continue; | |
116 } else if (ch == '*') { | |
117 sb.append("[^/]*?"); | |
118 continue; | |
119 } | |
120 sb.append(ch); | |
121 } | |
122 return sb.toString(); | |
123 } | |
124 | |
125 // TODO use PathGlobMatcher | |
126 public boolean isIgnored(Path path) { | |
127 for (Pattern p : entries) { | |
128 if (p.matcher(path).find()) { | |
129 return true; | |
130 } | |
131 } | |
132 return false; | |
133 } | |
134 } |