Mercurial > hg4j
annotate src/org/tmatesoft/hg/repo/HgIgnore.java @ 467:51d682cf9cdc
Cleaned experimental tag and updated comments/javadoc
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Fri, 22 Jun 2012 17:39:31 +0200 |
parents | 7f136a3fa671 |
children | e6407313bab7 |
rev | line source |
---|---|
15
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
1 /* |
408
e732521a9eb4
Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
343
diff
changeset
|
2 * Copyright (c) 2010-2012 TMate Software Ltd |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
15
diff
changeset
|
3 * |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
15
diff
changeset
|
4 * This program is free software; you can redistribute it and/or modify |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
15
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
15
diff
changeset
|
6 * the Free Software Foundation; version 2 of the License. |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
15
diff
changeset
|
7 * |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
15
diff
changeset
|
8 * This program is distributed in the hope that it will be useful, |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
15
diff
changeset
|
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
15
diff
changeset
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
15
diff
changeset
|
11 * GNU General Public License for more details. |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
15
diff
changeset
|
12 * |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
15
diff
changeset
|
13 * For information on how to redistribute this software under |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
15
diff
changeset
|
14 * the terms of a license other than GNU General Public License |
102
a3a2e5deb320
Updated contact address to support@hg4j.com
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
91
diff
changeset
|
15 * contact TMate Software at support@hg4j.com |
15
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
16 */ |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
15
diff
changeset
|
17 package org.tmatesoft.hg.repo; |
15
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
18 |
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
19 import java.io.BufferedReader; |
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
20 import java.io.File; |
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
21 import java.io.FileReader; |
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
22 import java.io.IOException; |
91
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
23 import java.util.ArrayList; |
15
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
24 import java.util.Collections; |
91
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
25 import java.util.List; |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
26 import java.util.regex.Pattern; |
335
3d41dc148d14
Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
289
diff
changeset
|
27 import java.util.regex.PatternSyntaxException; |
15
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
28 |
141
8248aae33f7d
Adopt FileIterator moving towards WCStatusCollector parameterizing. Improved path handling, move 'em around
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
114
diff
changeset
|
29 import org.tmatesoft.hg.util.Path; |
409
0f5696623512
Support glob path pattern rewrite to facilitate use of globs with Windows path separator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
408
diff
changeset
|
30 import org.tmatesoft.hg.util.PathRewrite; |
141
8248aae33f7d
Adopt FileIterator moving towards WCStatusCollector parameterizing. Improved path handling, move 'em around
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
114
diff
changeset
|
31 |
15
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
32 /** |
141
8248aae33f7d
Adopt FileIterator moving towards WCStatusCollector parameterizing. Improved path handling, move 'em around
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
114
diff
changeset
|
33 * Handling of ignored paths according to .hgignore configuration |
91
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
34 * |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
15
diff
changeset
|
35 * @author Artem Tikhomirov |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
15
diff
changeset
|
36 * @author TMate Software Ltd. |
15
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
37 */ |
289
086a326f181f
Provide public access to ignored files configuration to use in alternative file walkers
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
271
diff
changeset
|
38 public class HgIgnore implements Path.Matcher { |
15
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
39 |
91
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
40 private List<Pattern> entries; |
409
0f5696623512
Support glob path pattern rewrite to facilitate use of globs with Windows path separator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
408
diff
changeset
|
41 private final PathRewrite globPathHelper; |
15
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
42 |
409
0f5696623512
Support glob path pattern rewrite to facilitate use of globs with Windows path separator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
408
diff
changeset
|
43 HgIgnore(PathRewrite globPathRewrite) { |
91
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
44 entries = Collections.emptyList(); |
409
0f5696623512
Support glob path pattern rewrite to facilitate use of globs with Windows path separator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
408
diff
changeset
|
45 globPathHelper = globPathRewrite; |
15
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
46 } |
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
47 |
409
0f5696623512
Support glob path pattern rewrite to facilitate use of globs with Windows path separator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
408
diff
changeset
|
48 /* package-local */ List<String> read(File hgignoreFile) throws IOException { |
15
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
49 if (!hgignoreFile.exists()) { |
335
3d41dc148d14
Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
289
diff
changeset
|
50 return null; |
15
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
51 } |
269
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
52 BufferedReader fr = new BufferedReader(new FileReader(hgignoreFile)); |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
53 try { |
335
3d41dc148d14
Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
289
diff
changeset
|
54 return read(fr); |
269
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
55 } finally { |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
56 fr.close(); |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
57 } |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
58 } |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
59 |
409
0f5696623512
Support glob path pattern rewrite to facilitate use of globs with Windows path separator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
408
diff
changeset
|
60 /* package-local */ List<String> read(BufferedReader content) throws IOException { |
408
e732521a9eb4
Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
343
diff
changeset
|
61 final String REGEXP = "regexp", GLOB = "glob"; |
e732521a9eb4
Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
343
diff
changeset
|
62 final String REGEXP_PREFIX = REGEXP + ":", GLOB_PREFIX = GLOB + ":"; |
335
3d41dc148d14
Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
289
diff
changeset
|
63 ArrayList<String> errors = new ArrayList<String>(); |
91
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
64 ArrayList<Pattern> result = new ArrayList<Pattern>(entries); // start with existing |
408
e732521a9eb4
Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
343
diff
changeset
|
65 String syntax = REGEXP; |
91
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
66 String line; |
269
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
67 while ((line = content.readLine()) != null) { |
91
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
68 line = line.trim(); |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
69 if (line.startsWith("syntax:")) { |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
70 syntax = line.substring("syntax:".length()).trim(); |
408
e732521a9eb4
Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
343
diff
changeset
|
71 if (!REGEXP.equals(syntax) && !GLOB.equals(syntax)) { |
335
3d41dc148d14
Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
289
diff
changeset
|
72 errors.add(line); |
3d41dc148d14
Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
289
diff
changeset
|
73 continue; |
3d41dc148d14
Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
289
diff
changeset
|
74 //throw new IllegalStateException(line); |
91
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
75 } |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
76 } else if (line.length() > 0) { |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
77 // shall I account for local paths in the file (i.e. |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
78 // back-slashed on windows)? |
269
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
79 int x, s = 0; |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
80 while ((x = line.indexOf('#', s)) >= 0) { |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
81 if (x > 0 && line.charAt(x-1) == '\\') { |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
82 // remove escape char |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
83 line = line.substring(0, x-1).concat(line.substring(x)); |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
84 s = x; // with exclusion of char at [x], s now points to what used to be at [x+1] |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
85 } else { |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
86 line = line.substring(0, x).trim(); |
91
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
87 } |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
88 } |
408
e732521a9eb4
Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
343
diff
changeset
|
89 // due to the nature of Mercurial implementation, lines prefixed with syntax kind |
e732521a9eb4
Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
343
diff
changeset
|
90 // are processed correctly (despite the fact hgignore(5) suggest "syntax:<kind>" as the |
e732521a9eb4
Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
343
diff
changeset
|
91 // only way to specify it). lineSyntax below leaves a chance for the line to switch |
e732521a9eb4
Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
343
diff
changeset
|
92 // syntax in use without affecting default kind. |
e732521a9eb4
Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
343
diff
changeset
|
93 String lineSyntax; |
e732521a9eb4
Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
343
diff
changeset
|
94 if (line.startsWith(GLOB_PREFIX)) { |
e732521a9eb4
Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
343
diff
changeset
|
95 line = line.substring(GLOB_PREFIX.length()).trim(); |
e732521a9eb4
Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
343
diff
changeset
|
96 lineSyntax = GLOB; |
e732521a9eb4
Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
343
diff
changeset
|
97 } else if (line.startsWith(REGEXP_PREFIX)) { |
e732521a9eb4
Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
343
diff
changeset
|
98 line = line.substring(REGEXP_PREFIX.length()).trim(); |
e732521a9eb4
Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
343
diff
changeset
|
99 lineSyntax = REGEXP; |
e732521a9eb4
Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
343
diff
changeset
|
100 } else { |
e732521a9eb4
Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
343
diff
changeset
|
101 lineSyntax = syntax; |
e732521a9eb4
Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
343
diff
changeset
|
102 } |
269
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
103 if (line.length() == 0) { |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
104 continue; |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
105 } |
408
e732521a9eb4
Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
343
diff
changeset
|
106 if (GLOB.equals(lineSyntax)) { |
409
0f5696623512
Support glob path pattern rewrite to facilitate use of globs with Windows path separator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
408
diff
changeset
|
107 // hgignore(5) says slashes '\' are escape characters, |
0f5696623512
Support glob path pattern rewrite to facilitate use of globs with Windows path separator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
408
diff
changeset
|
108 // however, for glob patterns on Windows first get backslashes converted to slashes |
0f5696623512
Support glob path pattern rewrite to facilitate use of globs with Windows path separator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
408
diff
changeset
|
109 if (globPathHelper != null) { |
0f5696623512
Support glob path pattern rewrite to facilitate use of globs with Windows path separator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
408
diff
changeset
|
110 line = globPathHelper.rewrite(line).toString(); |
0f5696623512
Support glob path pattern rewrite to facilitate use of globs with Windows path separator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
408
diff
changeset
|
111 } |
91
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
112 line = glob2regex(line); |
342
516b817415ba
HgIgnore: regex patterns to match part of the filename do not work
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
339
diff
changeset
|
113 } else { |
408
e732521a9eb4
Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
343
diff
changeset
|
114 assert REGEXP.equals(lineSyntax); |
342
516b817415ba
HgIgnore: regex patterns to match part of the filename do not work
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
339
diff
changeset
|
115 // regular expression patterns need not match start of the line unless demanded explicitly |
516b817415ba
HgIgnore: regex patterns to match part of the filename do not work
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
339
diff
changeset
|
116 line = line.charAt(0) == '^' ? line : ".*" + line; |
91
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
117 } |
335
3d41dc148d14
Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
289
diff
changeset
|
118 try { |
3d41dc148d14
Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
289
diff
changeset
|
119 result.add(Pattern.compile(line)); // case-sensitive |
3d41dc148d14
Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
289
diff
changeset
|
120 } catch (PatternSyntaxException ex) { |
3d41dc148d14
Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
289
diff
changeset
|
121 errors.add(line + "@" + ex.getMessage()); |
3d41dc148d14
Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
289
diff
changeset
|
122 } |
15
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
123 } |
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
124 } |
91
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
125 result.trimToSize(); |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
126 entries = result; |
335
3d41dc148d14
Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
289
diff
changeset
|
127 return errors.isEmpty() ? null : errors; |
15
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
128 } |
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
129 |
91
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
130 // note, #isIgnored(), even if queried for directories and returned positive reply, may still get |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
131 // a file from that ignored folder to get examined. Thus, patterns like "bin" shall match not only a folder, |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
132 // but any file under that folder as well |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
133 // Alternatively, file walker may memorize folder is ignored and uses this information for all nested files. However, |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
134 // this approach would require walker (a) return directories (b) provide nesting information. This may become |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
135 // troublesome when one walks not over io.File, but Eclipse's IResource or any other custom VFS. |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
136 // |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
137 // |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
138 // might be interesting, although looks like of no direct use in my case |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
139 // @see http://stackoverflow.com/questions/1247772/is-there-an-equivalent-of-java-util-regex-for-glob-type-patterns |
289
086a326f181f
Provide public access to ignored files configuration to use in alternative file walkers
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
271
diff
changeset
|
140 // |
086a326f181f
Provide public access to ignored files configuration to use in alternative file walkers
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
271
diff
changeset
|
141 // TODO consider refactoring to reuse in PathGlobMatcher#glob2regexp |
086a326f181f
Provide public access to ignored files configuration to use in alternative file walkers
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
271
diff
changeset
|
142 private static String glob2regex(String line) { |
91
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
143 assert line.length() > 0; |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
144 StringBuilder sb = new StringBuilder(line.length() + 10); |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
145 int start = 0, end = line.length() - 1; |
342
516b817415ba
HgIgnore: regex patterns to match part of the filename do not work
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
339
diff
changeset
|
146 sb.append("(?:|.*/)"); // glob patterns shall match file in any directory |
91
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
147 |
269
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
148 int inCurly = 0; |
91
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
149 for (int i = start; i <= end; i++) { |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
150 char ch = line.charAt(i); |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
151 if (ch == '.' || ch == '\\') { |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
152 sb.append('\\'); |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
153 } else if (ch == '?') { |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
154 // simple '.' substitution might work out, however, more formally |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
155 // a char class seems more appropriate to avoid accidentally |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
156 // matching a subdirectory with ? char (i.e. /a/b?d against /a/bad, /a/bed and /a/b/d) |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
157 // @see http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_13_03 |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
158 // quote: "The slash character in a pathname shall be explicitly matched by using one or more slashes in the pattern; |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
159 // it shall neither be matched by the asterisk or question-mark special characters nor by a bracket expression" |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
160 sb.append("[^/]"); |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
161 continue; |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
162 } else if (ch == '*') { |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
163 sb.append("[^/]*?"); |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
164 continue; |
269
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
165 } else if (ch == '{') { |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
166 // XXX in fact, need to respect if last char was escaping ('\\'), then don't need to treat this as special |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
167 // see link at javadoc above for reasonable example |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
168 inCurly++; |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
169 sb.append('('); |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
170 continue; |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
171 } else if (ch == '}') { |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
172 if (inCurly > 0) { |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
173 inCurly--; |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
174 sb.append(')'); |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
175 continue; |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
176 } |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
177 } else if (ch == ',' && inCurly > 0) { |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
178 sb.append('|'); |
7af843ecc378
Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
225
diff
changeset
|
179 continue; |
91
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
180 } |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
181 sb.append(ch); |
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
182 } |
409
0f5696623512
Support glob path pattern rewrite to facilitate use of globs with Windows path separator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
408
diff
changeset
|
183 // Python impl doesn't keep empty segments in directory names (ntpath.normpath and posixpath.normpath), |
0f5696623512
Support glob path pattern rewrite to facilitate use of globs with Windows path separator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
408
diff
changeset
|
184 // effectively removing trailing separators, thus patterns like "bin/" get translated into "bin$" |
0f5696623512
Support glob path pattern rewrite to facilitate use of globs with Windows path separator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
408
diff
changeset
|
185 // Our glob rewriter doesn't strip last empty segment, and "bin/$" would be incorrect pattern, |
0f5696623512
Support glob path pattern rewrite to facilitate use of globs with Windows path separator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
408
diff
changeset
|
186 // (e.g. isIgnored("bin/file") performs two matches, against "bin/file" and "bin") hence the check. |
0f5696623512
Support glob path pattern rewrite to facilitate use of globs with Windows path separator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
408
diff
changeset
|
187 if (sb.charAt(sb.length() - 1) != '/') { |
0f5696623512
Support glob path pattern rewrite to facilitate use of globs with Windows path separator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
408
diff
changeset
|
188 sb.append('$'); |
0f5696623512
Support glob path pattern rewrite to facilitate use of globs with Windows path separator
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
408
diff
changeset
|
189 } |
91
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
190 return sb.toString(); |
15
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
191 } |
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
192 |
289
086a326f181f
Provide public access to ignored files configuration to use in alternative file walkers
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
271
diff
changeset
|
193 /** |
086a326f181f
Provide public access to ignored files configuration to use in alternative file walkers
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
271
diff
changeset
|
194 * @param path file or directory name in question |
086a326f181f
Provide public access to ignored files configuration to use in alternative file walkers
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
271
diff
changeset
|
195 * @return <code>true</code> if matches repository configuration of ignored files. |
086a326f181f
Provide public access to ignored files configuration to use in alternative file walkers
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
271
diff
changeset
|
196 */ |
141
8248aae33f7d
Adopt FileIterator moving towards WCStatusCollector parameterizing. Improved path handling, move 'em around
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
114
diff
changeset
|
197 public boolean isIgnored(Path path) { |
342
516b817415ba
HgIgnore: regex patterns to match part of the filename do not work
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
339
diff
changeset
|
198 String ps = path.toString(); |
91
c2ce1cfaeb9e
ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
199 for (Pattern p : entries) { |
342
516b817415ba
HgIgnore: regex patterns to match part of the filename do not work
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
339
diff
changeset
|
200 int x = ps.indexOf('/'); // reset for each pattern |
516b817415ba
HgIgnore: regex patterns to match part of the filename do not work
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
339
diff
changeset
|
201 if (p.matcher(ps).find()) { |
15
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
202 return true; |
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
203 } |
342
516b817415ba
HgIgnore: regex patterns to match part of the filename do not work
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
339
diff
changeset
|
204 while (x != -1 && x+1 != ps.length() /*skip very last segment not to check complete string twice*/) { |
516b817415ba
HgIgnore: regex patterns to match part of the filename do not work
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
339
diff
changeset
|
205 String fragment = ps.substring(0, x); |
516b817415ba
HgIgnore: regex patterns to match part of the filename do not work
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
339
diff
changeset
|
206 if (p.matcher(fragment).matches()) { |
516b817415ba
HgIgnore: regex patterns to match part of the filename do not work
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
339
diff
changeset
|
207 return true; |
339
863356c2847e
Issue 16: respect glob patterns in HgIgnore for sub-directories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
335
diff
changeset
|
208 } |
342
516b817415ba
HgIgnore: regex patterns to match part of the filename do not work
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
339
diff
changeset
|
209 x = ps.indexOf('/', x+1); |
339
863356c2847e
Issue 16: respect glob patterns in HgIgnore for sub-directories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
335
diff
changeset
|
210 } |
15
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
211 } |
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
212 return false; |
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
213 } |
289
086a326f181f
Provide public access to ignored files configuration to use in alternative file walkers
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
271
diff
changeset
|
214 |
086a326f181f
Provide public access to ignored files configuration to use in alternative file walkers
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
271
diff
changeset
|
215 /** |
419
7f136a3fa671
Clean javadoc to fix obvious warnings
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
409
diff
changeset
|
216 * A handy wrap of {@link #isIgnored(Path)} into {@link org.tmatesoft.hg.util.Path.Matcher}. Yields same result as {@link #isIgnored(Path)}. |
289
086a326f181f
Provide public access to ignored files configuration to use in alternative file walkers
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
271
diff
changeset
|
217 * @return <code>true</code> if file is deemed ignored. |
086a326f181f
Provide public access to ignored files configuration to use in alternative file walkers
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
271
diff
changeset
|
218 */ |
086a326f181f
Provide public access to ignored files configuration to use in alternative file walkers
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
271
diff
changeset
|
219 public boolean accept(Path path) { |
086a326f181f
Provide public access to ignored files configuration to use in alternative file walkers
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
271
diff
changeset
|
220 return isIgnored(path); |
086a326f181f
Provide public access to ignored files configuration to use in alternative file walkers
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
271
diff
changeset
|
221 } |
15
865bf07f381f
Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
222 } |