/
HgExe.java
237 lines (207 loc) · 7.93 KB
/
HgExe.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* The MIT License
*
* Copyright (c) 2004-2010, InfraDNA, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.plugins.mercurial;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import hudson.AbortException;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Launcher.ProcStarter;
import hudson.model.AbstractBuild;
import hudson.model.Node;
import hudson.model.TaskListener;
import hudson.util.ArgumentListBuilder;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.regex.Pattern;
/**
* Encapsulates the invocation of "hg".
*
* <p>
* This reduces the amount of code in the main logic.
*
* TODO: perhaps a subtype 'Repository' with a repository location field would be good.
*
* @author Kohsuke Kawaguchi
*/
public class HgExe {
private final ArgumentListBuilder base;
/**
* Environment variables to invoke hg with.
*/
private final EnvVars env;
public final Launcher launcher;
public final Node node;
public final TaskListener listener;
private final Capability capability;
public HgExe(MercurialSCM scm, Launcher launcher, AbstractBuild build, TaskListener listener, EnvVars env) throws IOException, InterruptedException {
this(scm,launcher,build.getBuiltOn(),listener,env);
}
public HgExe(MercurialSCM scm, Launcher launcher, Node node, TaskListener listener, EnvVars env) throws IOException, InterruptedException {
base = scm.findHgExe(node, listener, true);
this.node = node;
this.env = env;
this.launcher = launcher;
this.listener = listener;
this.capability = Capability.get(this);
}
private ProcStarter l(ArgumentListBuilder args) {
// set the default stdout
return MercurialSCM.launch(launcher).cmds(args).stdout(listener).envs(env);
}
private ArgumentListBuilder seed() {
return base.clone();
}
public ProcStarter pull() {
return run("pull");
}
public ProcStarter clone(String... args) {
return l(seed().add("clone").add(args));
}
public ProcStarter bundleAll(String file) {
return run("bundle","--all",file);
}
public ProcStarter bundle(Collection<String> bases, String file) {
ArgumentListBuilder args = seed().add("bundle");
for (String head : bases) {
args.add("--base", head);
}
args.add(file);
return l(args);
}
public ProcStarter init(FilePath path) {
return run("init",path.getRemote());
}
public ProcStarter unbundle(String bundleFile) {
return run("unbundle",bundleFile);
}
public ProcStarter cleanAll() {
return run("--config", "extensions.purge=", "clean", "--all");
}
/**
* Runs arbitrary command.
*/
public ProcStarter run(String... args) {
return l(seed().add(args));
}
public ProcStarter run(ArgumentListBuilder args) {
return l(seed().add(args.toCommandArray()));
}
/**
* Obtains the heads of the repository.
*/
public Set<String> heads(FilePath repo, boolean useTimeout) throws IOException, InterruptedException {
if (capability.headsIn15 == null) {
try {
Set<String> output = heads(repo, useTimeout, true);
capability.headsIn15 = true;
return output;
} catch (AbortException x) {
Set<String> output = heads(repo, useTimeout, false);
capability.headsIn15 = false;
return output;
}
} else {
return heads(repo, useTimeout, capability.headsIn15);
}
}
private Set<String> heads(FilePath repo, boolean useTimeout, boolean usingHg15Syntax) throws IOException, InterruptedException {
ArgumentListBuilder args = new ArgumentListBuilder("heads", "--template", "{node}\\n");
if(usingHg15Syntax)
args.add("--topo", "--closed");
String output = popen(repo,listener,useTimeout,args);
Set<String> heads = new LinkedHashSet<String>(Arrays.asList(output.split("\n")));
heads.remove("");
return heads;
}
/**
* Gets the revision ID of the tip of the workspace.
*/
public @CheckForNull String tip(FilePath repository) throws IOException, InterruptedException {
String id = popen(repository, listener, false, new ArgumentListBuilder("log", "--rev", ".", "--template", "{node}"));
if (!REVISIONID_PATTERN.matcher(id).matches()) {
listener.error("Expected to get an id but got '" + id + "' instead.");
return null; // HUDSON-7723
}
return id;
}
/**
* Gets the current value of a specified config item.
*/
public String config(FilePath repository, String name) throws IOException, InterruptedException {
return popen(repository, listener, false, new ArgumentListBuilder("showconfig", name)).trim();
}
public List<String> toArgList() {
return base.toList();
}
/**
* Runs the command and captures the output.
*/
public String popen(FilePath repository, TaskListener listener, boolean useTimeout, ArgumentListBuilder args)
throws IOException, InterruptedException {
args = seed().add(args.toCommandArray());
ByteArrayOutputStream rev = new ByteArrayOutputStream();
if (MercurialSCM.joinWithPossibleTimeout(l(args).pwd(repository).stdout(rev), useTimeout, listener) == 0) {
return rev.toString();
} else {
listener.error("Failed to run " + args.toStringWithQuote());
listener.getLogger().write(rev.toByteArray());
throw new AbortException();
}
}
/**
* Capability of a particular hg invocation configuration (and location) on a specific node. Cached.
*/
private static final class Capability {
/**
* Whether this supports 1.5-style "heads --topo ..." syntax.
*/
volatile Boolean headsIn15;
private static final Map<Node, Map<List<String>, Capability>> MAP = new WeakHashMap<Node,Map<List<String>,Capability>>();
synchronized static Capability get(HgExe hg) {
Map<List<String>,Capability> m = MAP.get(hg.node);
if (m == null) {
m = new HashMap<List<String>,Capability>();
MAP.put(hg.node, m);
}
List<String> hgConfig = hg.toArgList();
Capability cap = m.get(hgConfig);
if (cap==null)
m.put(hgConfig,cap = new Capability());
return cap;
}
}
/**
* Pattern that matches revision ID.
*/
private static final Pattern REVISIONID_PATTERN = Pattern.compile("[0-9a-f]{40}");
}