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 package org.eclipse.jgit.api;
44
45 import java.io.IOException;
46 import java.io.OutputStream;
47 import java.net.URISyntaxException;
48 import java.text.MessageFormat;
49 import java.util.ArrayList;
50 import java.util.Collection;
51 import java.util.Collections;
52 import java.util.List;
53
54 import org.eclipse.jgit.api.errors.GitAPIException;
55 import org.eclipse.jgit.api.errors.InvalidRemoteException;
56 import org.eclipse.jgit.api.errors.JGitInternalException;
57 import org.eclipse.jgit.errors.NotSupportedException;
58 import org.eclipse.jgit.errors.TooLargePackException;
59 import org.eclipse.jgit.errors.TransportException;
60 import org.eclipse.jgit.internal.JGitText;
61 import org.eclipse.jgit.lib.Constants;
62 import org.eclipse.jgit.lib.NullProgressMonitor;
63 import org.eclipse.jgit.lib.ProgressMonitor;
64 import org.eclipse.jgit.lib.Ref;
65 import org.eclipse.jgit.lib.Repository;
66 import org.eclipse.jgit.transport.PushResult;
67 import org.eclipse.jgit.transport.RefSpec;
68 import org.eclipse.jgit.transport.RemoteConfig;
69 import org.eclipse.jgit.transport.RemoteRefUpdate;
70 import org.eclipse.jgit.transport.Transport;
71
72
73
74
75
76
77
78
79
80 public class PushCommand extends
81 TransportCommand<PushCommand, Iterable<PushResult>> {
82
83 private String remote = Constants.DEFAULT_REMOTE_NAME;
84
85 private final List<RefSpec> refSpecs;
86
87 private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
88
89 private String receivePack = RemoteConfig.DEFAULT_RECEIVE_PACK;
90
91 private boolean dryRun;
92
93 private boolean force;
94
95 private boolean thin = Transport.DEFAULT_PUSH_THIN;
96
97 private OutputStream out;
98
99
100
101
102 protected PushCommand(Repository repo) {
103 super(repo);
104 refSpecs = new ArrayList<RefSpec>(3);
105 }
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120 public Iterable<PushResult> call() throws GitAPIException,
121 InvalidRemoteException,
122 org.eclipse.jgit.api.errors.TransportException {
123 checkCallable();
124
125 ArrayList<PushResult> pushResults = new ArrayList<PushResult>(3);
126
127 try {
128 if (refSpecs.isEmpty()) {
129 RemoteConfig config = new RemoteConfig(repo.getConfig(),
130 getRemote());
131 refSpecs.addAll(config.getPushRefSpecs());
132 }
133 if (refSpecs.isEmpty()) {
134 Ref head = repo.getRef(Constants.HEAD);
135 if (head != null && head.isSymbolic())
136 refSpecs.add(new RefSpec(head.getLeaf().getName()));
137 }
138
139 if (force) {
140 for (int i = 0; i < refSpecs.size(); i++)
141 refSpecs.set(i, refSpecs.get(i).setForceUpdate(true));
142 }
143
144 final List<Transport> transports;
145 transports = Transport.openAll(repo, remote, Transport.Operation.PUSH);
146 for (final Transport transport : transports) {
147 transport.setPushThin(thin);
148 if (receivePack != null)
149 transport.setOptionReceivePack(receivePack);
150 transport.setDryRun(dryRun);
151 configure(transport);
152
153 final Collection<RemoteRefUpdate> toPush = transport
154 .findRemoteRefUpdatesFor(refSpecs);
155
156 try {
157 PushResult result = transport.push(monitor, toPush, out);
158 pushResults.add(result);
159
160 } catch (TooLargePackException e) {
161 throw new org.eclipse.jgit.api.errors.TooLargePackException(
162 e.getMessage(), e);
163 } catch (TransportException e) {
164 throw new org.eclipse.jgit.api.errors.TransportException(
165 e.getMessage(), e);
166 } finally {
167 transport.close();
168 }
169 }
170
171 } catch (URISyntaxException e) {
172 throw new InvalidRemoteException(MessageFormat.format(
173 JGitText.get().invalidRemote, remote));
174 } catch (TransportException e) {
175 throw new org.eclipse.jgit.api.errors.TransportException(
176 e.getMessage(), e);
177 } catch (NotSupportedException e) {
178 throw new JGitInternalException(
179 JGitText.get().exceptionCaughtDuringExecutionOfPushCommand,
180 e);
181 } catch (IOException e) {
182 throw new JGitInternalException(
183 JGitText.get().exceptionCaughtDuringExecutionOfPushCommand,
184 e);
185 }
186
187 return pushResults;
188
189 }
190
191
192
193
194
195
196
197
198
199
200 public PushCommand setRemote(String remote) {
201 checkCallable();
202 this.remote = remote;
203 return this;
204 }
205
206
207
208
209 public String getRemote() {
210 return remote;
211 }
212
213
214
215
216
217
218
219
220
221
222 public PushCommand setReceivePack(String receivePack) {
223 checkCallable();
224 this.receivePack = receivePack;
225 return this;
226 }
227
228
229
230
231 public String getReceivePack() {
232 return receivePack;
233 }
234
235
236
237
238 public int getTimeout() {
239 return timeout;
240 }
241
242
243
244
245 public ProgressMonitor getProgressMonitor() {
246 return monitor;
247 }
248
249
250
251
252
253
254
255
256
257
258 public PushCommand setProgressMonitor(ProgressMonitor monitor) {
259 checkCallable();
260 if (monitor == null) {
261 monitor = NullProgressMonitor.INSTANCE;
262 }
263 this.monitor = monitor;
264 return this;
265 }
266
267
268
269
270 public List<RefSpec> getRefSpecs() {
271 return refSpecs;
272 }
273
274
275
276
277
278
279
280 public PushCommand setRefSpecs(RefSpec... specs) {
281 checkCallable();
282 this.refSpecs.clear();
283 Collections.addAll(refSpecs, specs);
284 return this;
285 }
286
287
288
289
290
291
292
293 public PushCommand setRefSpecs(List<RefSpec> specs) {
294 checkCallable();
295 this.refSpecs.clear();
296 this.refSpecs.addAll(specs);
297 return this;
298 }
299
300
301
302
303
304
305 public PushCommand setPushAll() {
306 refSpecs.add(Transport.REFSPEC_PUSH_ALL);
307 return this;
308 }
309
310
311
312
313
314
315 public PushCommand setPushTags() {
316 refSpecs.add(Transport.REFSPEC_TAGS);
317 return this;
318 }
319
320
321
322
323
324
325
326
327 public PushCommand add(Ref ref) {
328 refSpecs.add(new RefSpec(ref.getLeaf().getName()));
329 return this;
330 }
331
332
333
334
335
336
337
338
339
340
341 public PushCommand add(String nameOrSpec) {
342 if (0 <= nameOrSpec.indexOf(':')) {
343 refSpecs.add(new RefSpec(nameOrSpec));
344 } else {
345 Ref src;
346 try {
347 src = repo.getRef(nameOrSpec);
348 } catch (IOException e) {
349 throw new JGitInternalException(
350 JGitText.get().exceptionCaughtDuringExecutionOfPushCommand,
351 e);
352 }
353 if (src != null)
354 add(src);
355 }
356 return this;
357 }
358
359
360
361
362 public boolean isDryRun() {
363 return dryRun;
364 }
365
366
367
368
369
370
371
372 public PushCommand setDryRun(boolean dryRun) {
373 checkCallable();
374 this.dryRun = dryRun;
375 return this;
376 }
377
378
379
380
381 public boolean isThin() {
382 return thin;
383 }
384
385
386
387
388
389
390
391
392
393 public PushCommand setThin(boolean thin) {
394 checkCallable();
395 this.thin = thin;
396 return this;
397 }
398
399
400
401
402 public boolean isForce() {
403 return force;
404 }
405
406
407
408
409
410
411
412 public PushCommand setForce(boolean force) {
413 checkCallable();
414 this.force = force;
415 return this;
416 }
417
418
419
420
421
422
423
424
425 public PushCommand setOutputStream(OutputStream out) {
426 this.out = out;
427 return this;
428 }
429 }