comparison src/org/tmatesoft/hg/repo/HgIgnore.java @ 342:516b817415ba

HgIgnore: regex patterns to match part of the filename do not work
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 18 Nov 2011 05:10:33 +0100
parents 863356c2847e
children 58016b1b8554
comparison
equal deleted inserted replaced
341:75c452fdd76a 342:516b817415ba
88 // hgignore(5) 88 // hgignore(5)
89 // (http://www.selenic.com/mercurial/hgignore.5.html) says slashes '\' are escape characters, 89 // (http://www.selenic.com/mercurial/hgignore.5.html) says slashes '\' are escape characters,
90 // hence no special treatment of Windows path 90 // hence no special treatment of Windows path
91 // however, own attempts make me think '\' on Windows are not treated as escapes 91 // however, own attempts make me think '\' on Windows are not treated as escapes
92 line = glob2regex(line); 92 line = glob2regex(line);
93 } else {
94 assert "regexp".equals(syntax);
95 // regular expression patterns need not match start of the line unless demanded explicitly
96 line = line.charAt(0) == '^' ? line : ".*" + line;
93 } 97 }
94 try { 98 try {
95 result.add(Pattern.compile(line)); // case-sensitive 99 result.add(Pattern.compile(line)); // case-sensitive
96 } catch (PatternSyntaxException ex) { 100 } catch (PatternSyntaxException ex) {
97 errors.add(line + "@" + ex.getMessage()); 101 errors.add(line + "@" + ex.getMessage());
117 // TODO consider refactoring to reuse in PathGlobMatcher#glob2regexp 121 // TODO consider refactoring to reuse in PathGlobMatcher#glob2regexp
118 private static String glob2regex(String line) { 122 private static String glob2regex(String line) {
119 assert line.length() > 0; 123 assert line.length() > 0;
120 StringBuilder sb = new StringBuilder(line.length() + 10); 124 StringBuilder sb = new StringBuilder(line.length() + 10);
121 int start = 0, end = line.length() - 1; 125 int start = 0, end = line.length() - 1;
126 sb.append("(?:|.*/)"); // glob patterns shall match file in any directory
122 127
123 int inCurly = 0; 128 int inCurly = 0;
124 for (int i = start; i <= end; i++) { 129 for (int i = start; i <= end; i++) {
125 char ch = line.charAt(i); 130 char ch = line.charAt(i);
126 if (ch == '.' || ch == '\\') { 131 if (ch == '.' || ch == '\\') {
161 /** 166 /**
162 * @param path file or directory name in question 167 * @param path file or directory name in question
163 * @return <code>true</code> if matches repository configuration of ignored files. 168 * @return <code>true</code> if matches repository configuration of ignored files.
164 */ 169 */
165 public boolean isIgnored(Path path) { 170 public boolean isIgnored(Path path) {
166 boolean isDeep = path.toString().indexOf('/') != -1; 171 String ps = path.toString();
167 for (Pattern p : entries) { 172 for (Pattern p : entries) {
168 if (p.matcher(path).matches()) { 173 int x = ps.indexOf('/'); // reset for each pattern
174 if (p.matcher(ps).find()) {
169 return true; 175 return true;
170 } 176 }
171 if (isDeep) { 177 while (x != -1 && x+1 != ps.length() /*skip very last segment not to check complete string twice*/) {
172 for (String segment : path.segments()) { 178 String fragment = ps.substring(0, x);
173 if (p.matcher(segment).matches()) { 179 if (p.matcher(fragment).matches()) {
174 return true; 180 return true;
175 }
176 } 181 }
182 x = ps.indexOf('/', x+1);
177 } 183 }
178 } 184 }
179 return false; 185 return false;
180 } 186 }
181 187