annotate src/org/tmatesoft/hg/repo/HgIgnore.java @ 603:707b5c7c6fa4

Refactor HgBlameFacility: relevant action methods moved to proper home (HgDataFile), as facility doesn't provide anything but packaging of relevant methods/interfaces
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 06 May 2013 18:29:57 +0200
parents e6407313bab7
children 5c68567b3645
rev   line source
15
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
564
e6407313bab7 Recognize re: in-line prefix for regular expressions as native client does, despite silence in hgingore.5
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 419
diff changeset
2 * Copyright (c) 2010-2013 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";
564
e6407313bab7 Recognize re: in-line prefix for regular expressions as native client does, despite silence in hgingore.5
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 419
diff changeset
62 final String REGEXP_PREFIX1 = REGEXP + ":", REGEXP_PREFIX2 = "re:", 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;
564
e6407313bab7 Recognize re: in-line prefix for regular expressions as native client does, despite silence in hgingore.5
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 419
diff changeset
97 } else if (line.startsWith(REGEXP_PREFIX1)) {
e6407313bab7 Recognize re: in-line prefix for regular expressions as native client does, despite silence in hgingore.5
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 419
diff changeset
98 line = line.substring(REGEXP_PREFIX1.length()).trim();
e6407313bab7 Recognize re: in-line prefix for regular expressions as native client does, despite silence in hgingore.5
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 419
diff changeset
99 lineSyntax = REGEXP;
e6407313bab7 Recognize re: in-line prefix for regular expressions as native client does, despite silence in hgingore.5
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 419
diff changeset
100 } else if (line.startsWith(REGEXP_PREFIX2)) {
e6407313bab7 Recognize re: in-line prefix for regular expressions as native client does, despite silence in hgingore.5
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 419
diff changeset
101 line = line.substring(REGEXP_PREFIX2.length()).trim();
408
e732521a9eb4 Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 343
diff changeset
102 lineSyntax = REGEXP;
e732521a9eb4 Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 343
diff changeset
103 } else {
e732521a9eb4 Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 343
diff changeset
104 lineSyntax = syntax;
e732521a9eb4 Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 343
diff changeset
105 }
269
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
106 if (line.length() == 0) {
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
107 continue;
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
108 }
408
e732521a9eb4 Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 343
diff changeset
109 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
110 // 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
111 // 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
112 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
113 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
114 }
91
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
115 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
116 } else {
408
e732521a9eb4 Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 343
diff changeset
117 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
118 // 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
119 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
120 }
335
3d41dc148d14 Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 289
diff changeset
121 try {
3d41dc148d14 Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 289
diff changeset
122 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
123 } catch (PatternSyntaxException ex) {
3d41dc148d14 Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 289
diff changeset
124 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
125 }
15
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
126 }
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
127 }
91
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
128 result.trimToSize();
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
129 entries = result;
335
3d41dc148d14 Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 289
diff changeset
130 return errors.isEmpty() ? null : errors;
15
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
131 }
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
132
91
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
133 // 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
134 // 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
135 // 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
136 // 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
137 // 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
138 // 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
139 //
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
140 //
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
141 // 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
142 // @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
143 //
086a326f181f Provide public access to ignored files configuration to use in alternative file walkers
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 271
diff changeset
144 // 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
145 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
146 assert line.length() > 0;
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
147 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
148 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
149 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
150
269
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
151 int inCurly = 0;
91
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
152 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
153 char ch = line.charAt(i);
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
154 if (ch == '.' || ch == '\\') {
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
155 sb.append('\\');
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
156 } else if (ch == '?') {
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
157 // 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
158 // 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
159 // 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
160 // @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
161 // 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
162 // 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
163 sb.append("[^/]");
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
164 continue;
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
165 } else if (ch == '*') {
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
166 sb.append("[^/]*?");
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
167 continue;
269
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
168 } else if (ch == '{') {
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
169 // 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
170 // 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
171 inCurly++;
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
172 sb.append('(');
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
173 continue;
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
174 } else if (ch == '}') {
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
175 if (inCurly > 0) {
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
176 inCurly--;
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
177 sb.append(')');
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
178 continue;
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
179 }
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
180 } else if (ch == ',' && inCurly > 0) {
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
181 sb.append('|');
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
182 continue;
91
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
183 }
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
184 sb.append(ch);
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
185 }
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
186 // 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
187 // 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
188 // 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
189 // (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
190 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
191 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
192 }
91
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
193 return sb.toString();
15
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
194 }
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
195
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
196 /**
086a326f181f Provide public access to ignored files configuration to use in alternative file walkers
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 271
diff changeset
197 * @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
198 * @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
199 */
141
8248aae33f7d Adopt FileIterator moving towards WCStatusCollector parameterizing. Improved path handling, move 'em around
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 114
diff changeset
200 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
201 String ps = path.toString();
91
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
202 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
203 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
204 if (p.matcher(ps).find()) {
15
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
205 return true;
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
206 }
342
516b817415ba HgIgnore: regex patterns to match part of the filename do not work
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 339
diff changeset
207 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
208 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
209 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
210 return true;
339
863356c2847e Issue 16: respect glob patterns in HgIgnore for sub-directories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 335
diff changeset
211 }
342
516b817415ba HgIgnore: regex patterns to match part of the filename do not work
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 339
diff changeset
212 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
213 }
15
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
214 }
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
215 return false;
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
216 }
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
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 /**
419
7f136a3fa671 Clean javadoc to fix obvious warnings
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 409
diff changeset
219 * 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
220 * @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
221 */
086a326f181f Provide public access to ignored files configuration to use in alternative file walkers
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 271
diff changeset
222 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
223 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
224 }
15
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
225 }