annotate src/org/tmatesoft/hg/repo/HgIgnore.java @ 610:5c68567b3645

Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 09 May 2013 21:06:48 +0200
parents e6407313bab7
children 7efabe0cddcf
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
610
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
19 import static org.tmatesoft.hg.repo.HgRepositoryFiles.HgIgnore;
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
20 import static org.tmatesoft.hg.util.LogFacility.Severity.Warn;
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
21
15
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import java.io.BufferedReader;
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import java.io.File;
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 import java.io.FileReader;
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 import java.io.IOException;
91
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
26 import java.util.ArrayList;
15
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 import java.util.Collections;
91
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
28 import java.util.List;
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
29 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
30 import java.util.regex.PatternSyntaxException;
15
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31
610
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
32 import org.tmatesoft.hg.internal.FileChangeMonitor;
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
33 import org.tmatesoft.hg.internal.Internals;
141
8248aae33f7d Adopt FileIterator moving towards WCStatusCollector parameterizing. Improved path handling, move 'em around
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 114
diff changeset
34 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
35 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
36
15
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 /**
141
8248aae33f7d Adopt FileIterator moving towards WCStatusCollector parameterizing. Improved path handling, move 'em around
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 114
diff changeset
38 * 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
39 *
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 15
diff changeset
40 * @author Artem Tikhomirov
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 15
diff changeset
41 * @author TMate Software Ltd.
15
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 */
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
43 public class HgIgnore implements Path.Matcher {
15
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44
91
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
45 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
46 private final PathRewrite globPathHelper;
610
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
47 private FileChangeMonitor ignoreFileTracker;
15
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48
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
49 HgIgnore(PathRewrite globPathRewrite) {
91
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
50 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
51 globPathHelper = globPathRewrite;
15
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 }
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53
610
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
54 /* package-local */ void read(Internals repo) throws HgInvalidControlFileException {
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
55 File ignoreFile = repo.getRepositoryFile(HgIgnore);
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
56 BufferedReader fr = null;
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
57 try {
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
58 if (ignoreFile.canRead() && ignoreFile.isFile()) {
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
59 fr = new BufferedReader(new FileReader(ignoreFile));
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
60 final List<String> errors = read(fr);
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
61 if (errors != null) {
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
62 repo.getLog().dump(getClass(), Warn, "Syntax errors parsing %s:\n%s", ignoreFile.getName(), Internals.join(errors, ",\n"));
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
63 }
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
64 }
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
65 if (ignoreFileTracker == null) {
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
66 ignoreFileTracker = new FileChangeMonitor(ignoreFile);
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
67 }
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
68 ignoreFileTracker.touch(this);
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
69 } catch (IOException ex) {
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
70 final String m = String.format("Error reading %s file", ignoreFile);
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
71 throw new HgInvalidControlFileException(m, ex, ignoreFile);
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
72 } finally {
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
73 try {
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
74 if (fr != null) {
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
75 fr.close();
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
76 }
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
77 } catch (IOException ex) {
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
78 repo.getLog().dump(getClass(), Warn, ex, null); // it's read, don't treat as error
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
79 }
15
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 }
610
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
81 }
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
82
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
83 /*package-local*/ void reloadIfChanged(Internals repo) throws HgInvalidControlFileException {
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
84 assert ignoreFileTracker != null;
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
85 if (ignoreFileTracker.changed(this)) {
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
86 entries = Collections.emptyList();
5c68567b3645 Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 564
diff changeset
87 read(repo);
269
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
88 }
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
89 }
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
90
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
91 /* 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
92 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
93 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
94 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
95 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
96 String syntax = REGEXP;
91
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
97 String line;
269
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
98 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
99 line = line.trim();
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
100 if (line.startsWith("syntax:")) {
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
101 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
102 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
103 errors.add(line);
3d41dc148d14 Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 289
diff changeset
104 continue;
3d41dc148d14 Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 289
diff changeset
105 //throw new IllegalStateException(line);
91
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
106 }
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
107 } else if (line.length() > 0) {
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
108 // 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
109 // back-slashed on windows)?
269
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
110 int x, s = 0;
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
111 while ((x = line.indexOf('#', s)) >= 0) {
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
112 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
113 // remove escape char
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
114 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
115 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
116 } else {
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
117 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
118 }
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
119 }
408
e732521a9eb4 Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 343
diff changeset
120 // 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
121 // 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
122 // 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
123 // 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
124 String lineSyntax;
e732521a9eb4 Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 343
diff changeset
125 if (line.startsWith(GLOB_PREFIX)) {
e732521a9eb4 Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 343
diff changeset
126 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
127 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
128 } 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
129 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
130 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
131 } 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
132 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
133 lineSyntax = REGEXP;
e732521a9eb4 Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 343
diff changeset
134 } else {
e732521a9eb4 Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 343
diff changeset
135 lineSyntax = syntax;
e732521a9eb4 Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 343
diff changeset
136 }
269
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
137 if (line.length() == 0) {
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
138 continue;
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
139 }
408
e732521a9eb4 Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 343
diff changeset
140 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
141 // 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
142 // 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
143 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
144 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
145 }
91
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
146 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
147 } else {
408
e732521a9eb4 Issue 28: support hgignore entries with syntax prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 343
diff changeset
148 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
149 // 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
150 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
151 }
335
3d41dc148d14 Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 289
diff changeset
152 try {
3d41dc148d14 Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 289
diff changeset
153 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
154 } catch (PatternSyntaxException ex) {
3d41dc148d14 Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 289
diff changeset
155 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
156 }
15
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
157 }
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
158 }
91
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
159 result.trimToSize();
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
160 entries = result;
335
3d41dc148d14 Do not fail with exception on syntax errors in .hgignore
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 289
diff changeset
161 return errors.isEmpty() ? null : errors;
15
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
162 }
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
163
91
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
164 // 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
165 // 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
166 // 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
167 // 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
168 // 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
169 // 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
170 //
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
171 //
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
172 // 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
173 // @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
174 //
086a326f181f Provide public access to ignored files configuration to use in alternative file walkers
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 271
diff changeset
175 // 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
176 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
177 assert line.length() > 0;
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
178 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
179 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
180 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
181
269
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
182 int inCurly = 0;
91
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
183 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
184 char ch = line.charAt(i);
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
185 if (ch == '.' || ch == '\\') {
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
186 sb.append('\\');
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
187 } else if (ch == '?') {
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
188 // 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
189 // 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
190 // 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
191 // @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
192 // 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
193 // 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
194 sb.append("[^/]");
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
195 continue;
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
196 } else if (ch == '*') {
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
197 sb.append("[^/]*?");
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
198 continue;
269
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
199 } else if (ch == '{') {
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
200 // 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
201 // 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
202 inCurly++;
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
203 sb.append('(');
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
204 continue;
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
205 } else if (ch == '}') {
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
206 if (inCurly > 0) {
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
207 inCurly--;
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
208 sb.append(')');
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
209 continue;
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
210 }
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
211 } else if (ch == ',' && inCurly > 0) {
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
212 sb.append('|');
7af843ecc378 Respect glob pattern with alternatives {a,b}
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 225
diff changeset
213 continue;
91
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
214 }
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
215 sb.append(ch);
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
216 }
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
217 // 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
218 // 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
219 // 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
220 // (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
221 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
222 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
223 }
91
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
224 return sb.toString();
15
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
225 }
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
226
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
227 /**
086a326f181f Provide public access to ignored files configuration to use in alternative file walkers
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 271
diff changeset
228 * @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
229 * @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
230 */
141
8248aae33f7d Adopt FileIterator moving towards WCStatusCollector parameterizing. Improved path handling, move 'em around
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 114
diff changeset
231 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
232 String ps = path.toString();
91
c2ce1cfaeb9e ignore file with regex and 'honest' glob support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
233 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
234 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
235 if (p.matcher(ps).find()) {
15
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
236 return true;
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
237 }
342
516b817415ba HgIgnore: regex patterns to match part of the filename do not work
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 339
diff changeset
238 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
239 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
240 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
241 return true;
339
863356c2847e Issue 16: respect glob patterns in HgIgnore for sub-directories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 335
diff changeset
242 }
342
516b817415ba HgIgnore: regex patterns to match part of the filename do not work
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 339
diff changeset
243 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
244 }
15
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
245 }
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
246 return false;
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
247 }
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
248
086a326f181f Provide public access to ignored files configuration to use in alternative file walkers
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 271
diff changeset
249 /**
419
7f136a3fa671 Clean javadoc to fix obvious warnings
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 409
diff changeset
250 * 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
251 * @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
252 */
086a326f181f Provide public access to ignored files configuration to use in alternative file walkers
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 271
diff changeset
253 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
254 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
255 }
15
865bf07f381f Basic hgignore handling
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
256 }