/
AbstractFileSet.java
141 lines (129 loc) · 5.29 KB
/
AbstractFileSet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*******************************************************************************
* Copyright (c) 2012, 2022 SAP AG and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* SAP AG - initial API and implementation
*******************************************************************************/
package org.eclipse.tycho.p2.impl.publisher.rootfiles;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import org.eclipse.core.runtime.IPath;
public abstract class AbstractFileSet {
private static final String ZERO_OR_MORE_DIRS = "([^/]+/)*";
private static final String ZERO_OR_MORE_FILE_CHARACTERS = "[^/]*";
private static final String ONE_CHARACTER = ".";
private static final String QUOTE_BEGIN = "\\Q";
private static final String QUOTE_END = "\\E";
private static final String[] DEFAULTEXCLUDES = {
// Miscellaneous typical temporary files
"**/*~", "**/#*#", "**/.#*", "**/%*%", "**/._*",
// CVS
"**/CVS", "**/CVS/**", "**/.cvsignore",
// RCS
"**/RCS", "**/RCS/**",
// SCCS
"**/SCCS", "**/SCCS/**",
// Visual SourceSafe
"**/vssver.scc",
// Subversion
"**/.svn", "**/.svn/**",
// Arch
"**/.arch-ids", "**/.arch-ids/**",
//Bazaar
"**/.bzr", "**/.bzr/**",
//SurroundSCM
"**/.MySCMServerInfo",
// Mac
"**/.DS_Store",
// Serena Dimensions Version 10
"**/.metadata", "**/.metadata/**",
// Mercurial
"**/.hg", "**/.hg/**",
// git
"**/.git", "**/.git/**",
// BitKeeper
"**/BitKeeper", "**/BitKeeper/**", "**/ChangeSet", "**/ChangeSet/**",
// darcs
"**/_darcs", "**/_darcs/**", "**/.darcsrepo", "**/.darcsrepo/**", "**/-darcs-backup*",
"**/.darcs-temp-mail" };
protected Pattern includePattern;
protected List<Pattern> defaultExcludePatterns;
private boolean useDefaultExcludes;
public AbstractFileSet(String pattern, boolean useDefaultExcludes) {
this.useDefaultExcludes = useDefaultExcludes;
this.includePattern = convertToRegexPattern(pattern);
this.defaultExcludePatterns = createDefaultExcludePatterns();
this.defaultExcludePatterns = createDefaultExcludePatterns();
}
private List<Pattern> createDefaultExcludePatterns() {
List<Pattern> defaultExcludePatterns = new ArrayList<>();
for (String exclude : DEFAULTEXCLUDES) {
defaultExcludePatterns.add(convertToRegexPattern(exclude));
}
// keep ignoring the following files after https://github.com/codehaus-plexus/plexus-utils/pull/174
defaultExcludePatterns.add(convertToRegexPattern("**/.gitignore"));
defaultExcludePatterns.add(convertToRegexPattern("**/.gitattributes"));
return defaultExcludePatterns;
}
private Pattern convertToRegexPattern(String antFilePattern) {
StringBuilder sb = new StringBuilder();
// always quote to make sure we don't interpret normal file
// characters as special regexp characters
sb.append(QUOTE_BEGIN);
char[] chars = antFilePattern.toCharArray();
for (int i = 0; i < chars.length; i++) {
switch (chars[i]) {
case '?':
sb.append(QUOTE_END + ONE_CHARACTER + QUOTE_BEGIN);
break;
case '*':
sb.append(QUOTE_END);
if ((i + 1 < chars.length) && chars[i + 1] == '*') {
// "**"
sb.append(ZERO_OR_MORE_DIRS);
i++;
if ((i + 1 < chars.length) && chars[i + 1] == '/') {
// "**/"
// eat up slash since it is matched by ZERO_OR_MORE_DIRS
i++;
}
if (i == chars.length - 1) {
// "**" at end means we also match files
sb.append(ZERO_OR_MORE_FILE_CHARACTERS);
}
} else {
// "*"
sb.append(ZERO_OR_MORE_FILE_CHARACTERS);
}
sb.append(QUOTE_BEGIN);
break;
default:
sb.append(chars[i]);
}
}
sb.append(QUOTE_END);
return Pattern.compile(sb.toString());
}
/**
* @return <code>true</code> if the specified path matches the include pattern of this fileset
* and not one of the default exclude patterns.
*/
protected boolean matches(IPath path) {
String slashifiedPath = path.toPortableString();
if (useDefaultExcludes) {
for (Pattern excludePattern : defaultExcludePatterns) {
if (excludePattern.matcher(slashifiedPath).matches()) {
return false;
}
}
}
return includePattern.matcher(slashifiedPath).matches();
}
}