forked from jenkinsci/git-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBranchSpec.java
More file actions
137 lines (114 loc) · 3.91 KB
/
Copy pathBranchSpec.java
File metadata and controls
137 lines (114 loc) · 3.91 KB
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
package hudson.plugins.git;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Pattern;
import org.kohsuke.stapler.DataBoundConstructor;
/**
* A specification of branches to build. Rather like a refspec.
*
* eg:
* <pre>
* master
* origin/master
* origin/*
* origin/*/thing
* </pre>
*/
public class BranchSpec implements Serializable {
private static final long serialVersionUID = -6177158367915899356L;
private String name;
private transient Pattern pattern;
public String getName() {
return name;
}
public void setName(String name) {
if(name == null)
throw new IllegalArgumentException();
else if(name.length() == 0)
this.name = "**";
else
this.name = name.trim();
}
@DataBoundConstructor
public BranchSpec(String name) {
setName(name);
}
public String toString() {
return pattern + " (" + name + ")";
}
public boolean matches(String item) {
return getPattern().matcher(item).matches();
}
public List<String> filterMatching(Collection<String> branches) {
List<String> items = new ArrayList<String>();
for(String b : branches) {
if(matches(b))
items.add(b);
}
return items;
}
public List<Branch> filterMatchingBranches(Collection<Branch> branches) {
List<Branch> items = new ArrayList<Branch>();
for(Branch b : branches) {
if(matches(b.getName()))
items.add(b);
}
return items;
}
private Pattern getPattern() {
// return the saved pattern if available
if (pattern != null)
return pattern;
// if an unqualified branch was given add a "*/" so it will match branches
// from remote repositories as the user probably intended
String qualifiedName;
if (!name.contains("**") && !name.contains("/"))
qualifiedName = "*/" + name;
else
qualifiedName = name;
// build a pattern into this builder
StringBuilder builder = new StringBuilder();
// was the last token a wildcard?
boolean foundWildcard = false;
// split the string at the wildcards
StringTokenizer tokenizer = new StringTokenizer(qualifiedName, "*", true);
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
// is this token is a wildcard?
if (token.equals("*")) {
// yes, was the previous token a wildcard?
if (foundWildcard) {
// yes, we found "**"
// match over any number of characters
builder.append(".*");
foundWildcard = false;
}
else {
// no, set foundWildcard to true and go on
foundWildcard = true;
}
}
else {
// no, was the previous token a wildcard?
if (foundWildcard) {
// yes, we found "*" followed by a non-wildcard
// match any number of characters other than a "/"
builder.append("[^/]*");
foundWildcard = false;
}
// quote the non-wildcard token before adding it to the phrase
builder.append(Pattern.quote(token));
}
}
// if the string ended with a wildcard add it now
if (foundWildcard) {
builder.append("[^/]*");
}
// save the pattern
pattern = Pattern.compile(builder.toString());
return pattern;
}
}