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 this.monitor = monitor;
261 return this;
262 }
263
264
265
266
267 public List<RefSpec> getRefSpecs() {
268 return refSpecs;
269 }
270
271
272
273
274
275
276
277 public PushCommand setRefSpecs(RefSpec... specs) {
278 checkCallable();
279 this.refSpecs.clear();
280 Collections.addAll(refSpecs, specs);
281 return this;
282 }
283
284
285
286
287
288
289
290 public PushCommand setRefSpecs(List<RefSpec> specs) {
291 checkCallable();
292 this.refSpecs.clear();
293 this.refSpecs.addAll(specs);
294 return this;
295 }
296
297
298
299
300
301
302 public PushCommand setPushAll() {
303 refSpecs.add(Transport.REFSPEC_PUSH_ALL);
304 return this;
305 }
306
307
308
309
310
311
312 public PushCommand setPushTags() {
313 refSpecs.add(Transport.REFSPEC_TAGS);
314 return this;
315 }
316
317
318
319
320
321
322
323
324 public PushCommand add(Ref ref) {
325 refSpecs.add(new RefSpec(ref.getLeaf().getName()));
326 return this;
327 }
328
329
330
331
332
333
334
335
336
337
338 public PushCommand add(String nameOrSpec) {
339 if (0 <= nameOrSpec.indexOf(':')) {
340 refSpecs.add(new RefSpec(nameOrSpec));
341 } else {
342 Ref src;
343 try {
344 src = repo.getRef(nameOrSpec);
345 } catch (IOException e) {
346 throw new JGitInternalException(
347 JGitText.get().exceptionCaughtDuringExecutionOfPushCommand,
348 e);
349 }
350 if (src != null)
351 add(src);
352 }
353 return this;
354 }
355
356
357
358
359 public boolean isDryRun() {
360 return dryRun;
361 }
362
363
364
365
366
367
368
369 public PushCommand setDryRun(boolean dryRun) {
370 checkCallable();
371 this.dryRun = dryRun;
372 return this;
373 }
374
375
376
377
378 public boolean isThin() {
379 return thin;
380 }
381
382
383
384
385
386
387
388
389
390 public PushCommand setThin(boolean thin) {
391 checkCallable();
392 this.thin = thin;
393 return this;
394 }
395
396
397
398
399 public boolean isForce() {
400 return force;
401 }
402
403
404
405
406
407
408
409 public PushCommand setForce(boolean force) {
410 checkCallable();
411 this.force = force;
412 return this;
413 }
414
415
416
417
418
419
420
421
422 public PushCommand setOutputStream(OutputStream out) {
423 this.out = out;
424 return this;
425 }
426 }