View Javadoc
1   /*
2    * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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.Arrays;
51  import java.util.Collection;
52  import java.util.Collections;
53  import java.util.HashMap;
54  import java.util.List;
55  import java.util.Map;
56  
57  import org.eclipse.jgit.api.errors.GitAPIException;
58  import org.eclipse.jgit.api.errors.InvalidRemoteException;
59  import org.eclipse.jgit.api.errors.JGitInternalException;
60  import org.eclipse.jgit.errors.NotSupportedException;
61  import org.eclipse.jgit.errors.TooLargeObjectInPackException;
62  import org.eclipse.jgit.errors.TooLargePackException;
63  import org.eclipse.jgit.errors.TransportException;
64  import org.eclipse.jgit.internal.JGitText;
65  import org.eclipse.jgit.lib.Constants;
66  import org.eclipse.jgit.lib.NullProgressMonitor;
67  import org.eclipse.jgit.lib.ProgressMonitor;
68  import org.eclipse.jgit.lib.Ref;
69  import org.eclipse.jgit.lib.Repository;
70  import org.eclipse.jgit.transport.PushResult;
71  import org.eclipse.jgit.transport.RefLeaseSpec;
72  import org.eclipse.jgit.transport.RefSpec;
73  import org.eclipse.jgit.transport.RemoteConfig;
74  import org.eclipse.jgit.transport.RemoteRefUpdate;
75  import org.eclipse.jgit.transport.Transport;
76  
77  /**
78   * A class used to execute a {@code Push} command. It has setters for all
79   * supported options and arguments of this command and a {@link #call()} method
80   * to finally execute the command.
81   *
82   * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-push.html"
83   *      >Git documentation about Push</a>
84   */
85  public class PushCommand extends
86  		TransportCommand<PushCommand, Iterable<PushResult>> {
87  
88  	private String remote = Constants.DEFAULT_REMOTE_NAME;
89  
90  	private final List<RefSpec> refSpecs;
91  
92  	private final Map<String, RefLeaseSpec> refLeaseSpecs;
93  
94  	private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
95  
96  	private String receivePack = RemoteConfig.DEFAULT_RECEIVE_PACK;
97  
98  	private boolean dryRun;
99  	private boolean atomic;
100 	private boolean force;
101 	private boolean thin = Transport.DEFAULT_PUSH_THIN;
102 
103 	private OutputStream out;
104 
105 	private List<String> pushOptions;
106 
107 	/**
108 	 * @param repo
109 	 */
110 	protected PushCommand(Repository repo) {
111 		super(repo);
112 		refSpecs = new ArrayList<>(3);
113 		refLeaseSpecs = new HashMap<>();
114 	}
115 
116 	/**
117 	 * Executes the {@code push} command with all the options and parameters
118 	 * collected by the setter methods of this class. Each instance of this
119 	 * class should only be used for one invocation of the command (means: one
120 	 * call to {@link #call()})
121 	 *
122 	 * @return an iteration over {@link PushResult} objects
123 	 * @throws InvalidRemoteException
124 	 *             when called with an invalid remote uri
125 	 * @throws org.eclipse.jgit.api.errors.TransportException
126 	 *             when an error occurs with the transport
127 	 * @throws GitAPIException
128 	 */
129 	@Override
130 	public Iterable<PushResult> call() throws GitAPIException,
131 			InvalidRemoteException,
132 			org.eclipse.jgit.api.errors.TransportException {
133 		checkCallable();
134 
135 		ArrayList<PushResult> pushResults = new ArrayList<>(3);
136 
137 		try {
138 			if (refSpecs.isEmpty()) {
139 				RemoteConfig config = new RemoteConfig(repo.getConfig(),
140 						getRemote());
141 				refSpecs.addAll(config.getPushRefSpecs());
142 			}
143 			if (refSpecs.isEmpty()) {
144 				Ref head = repo.exactRef(Constants.HEAD);
145 				if (head != null && head.isSymbolic())
146 					refSpecs.add(new RefSpec(head.getLeaf().getName()));
147 			}
148 
149 			if (force) {
150 				for (int i = 0; i < refSpecs.size(); i++)
151 					refSpecs.set(i, refSpecs.get(i).setForceUpdate(true));
152 			}
153 
154 			final List<Transport> transports;
155 			transports = Transport.openAll(repo, remote, Transport.Operation.PUSH);
156 			for (final Transport transport : transports) {
157 				transport.setPushThin(thin);
158 				transport.setPushAtomic(atomic);
159 				if (receivePack != null)
160 					transport.setOptionReceivePack(receivePack);
161 				transport.setDryRun(dryRun);
162 				transport.setPushOptions(pushOptions);
163 				configure(transport);
164 
165 				final Collection<RemoteRefUpdate> toPush = transport
166 						.findRemoteRefUpdatesFor(refSpecs, refLeaseSpecs);
167 
168 				try {
169 					PushResult result = transport.push(monitor, toPush, out);
170 					pushResults.add(result);
171 
172 				} catch (TooLargePackException e) {
173 					throw new org.eclipse.jgit.api.errors.TooLargePackException(
174 							e.getMessage(), e);
175 				} catch (TooLargeObjectInPackException e) {
176 					throw new org.eclipse.jgit.api.errors.TooLargeObjectInPackException(
177 							e.getMessage(), e);
178 				} catch (TransportException e) {
179 					throw new org.eclipse.jgit.api.errors.TransportException(
180 							e.getMessage(), e);
181 				} finally {
182 					transport.close();
183 				}
184 			}
185 
186 		} catch (URISyntaxException e) {
187 			throw new InvalidRemoteException(MessageFormat.format(
188 					JGitText.get().invalidRemote, remote));
189 		} catch (TransportException e) {
190 			throw new org.eclipse.jgit.api.errors.TransportException(
191 					e.getMessage(), e);
192 		} catch (NotSupportedException e) {
193 			throw new JGitInternalException(
194 					JGitText.get().exceptionCaughtDuringExecutionOfPushCommand,
195 					e);
196 		} catch (IOException e) {
197 			throw new JGitInternalException(
198 					JGitText.get().exceptionCaughtDuringExecutionOfPushCommand,
199 					e);
200 		}
201 
202 		return pushResults;
203 	}
204 
205 	/**
206 	 * The remote (uri or name) used for the push operation. If no remote is
207 	 * set, the default value of <code>Constants.DEFAULT_REMOTE_NAME</code> will
208 	 * be used.
209 	 *
210 	 * @see Constants#DEFAULT_REMOTE_NAME
211 	 * @param remote
212 	 * @return {@code this}
213 	 */
214 	public PushCommand setRemote(String remote) {
215 		checkCallable();
216 		this.remote = remote;
217 		return this;
218 	}
219 
220 	/**
221 	 * @return the remote used for the remote operation
222 	 */
223 	public String getRemote() {
224 		return remote;
225 	}
226 
227 	/**
228 	 * The remote executable providing receive-pack service for pack transports.
229 	 * If no receive-pack is set, the default value of
230 	 * <code>RemoteConfig.DEFAULT_RECEIVE_PACK</code> will be used.
231 	 *
232 	 * @see RemoteConfig#DEFAULT_RECEIVE_PACK
233 	 * @param receivePack
234 	 * @return {@code this}
235 	 */
236 	public PushCommand setReceivePack(String receivePack) {
237 		checkCallable();
238 		this.receivePack = receivePack;
239 		return this;
240 	}
241 
242 	/**
243 	 * @return the receive-pack used for the remote operation
244 	 */
245 	public String getReceivePack() {
246 		return receivePack;
247 	}
248 
249 	/**
250 	 * @return the timeout used for the push operation
251 	 */
252 	public int getTimeout() {
253 		return timeout;
254 	}
255 
256 	/**
257 	 * @return the progress monitor for the push operation
258 	 */
259 	public ProgressMonitor getProgressMonitor() {
260 		return monitor;
261 	}
262 
263 	/**
264 	 * The progress monitor associated with the push operation. By default, this
265 	 * is set to <code>NullProgressMonitor</code>
266 	 *
267 	 * @see NullProgressMonitor
268 	 *
269 	 * @param monitor
270 	 * @return {@code this}
271 	 */
272 	public PushCommand setProgressMonitor(ProgressMonitor monitor) {
273 		checkCallable();
274 		if (monitor == null) {
275 			monitor = NullProgressMonitor.INSTANCE;
276 		}
277 		this.monitor = monitor;
278 		return this;
279 	}
280 
281 	/**
282 	 * @return the ref lease specs
283 	 * @since 4.7
284 	 */
285 	public List<RefLeaseSpec> getRefLeaseSpecs() {
286 		return new ArrayList<>(refLeaseSpecs.values());
287 	}
288 
289 	/**
290 	 * The ref lease specs to be used in the push operation,
291 	 * for a force-with-lease push operation.
292 	 *
293 	 * @param specs
294 	 * @return {@code this}
295 	 * @since 4.7
296 	 */
297 	public PushCommand setRefLeaseSpecs(RefLeaseSpec... specs) {
298 		return setRefLeaseSpecs(Arrays.asList(specs));
299 	}
300 
301 	/**
302 	 * The ref lease specs to be used in the push operation,
303 	 * for a force-with-lease push operation.
304 	 *
305 	 * @param specs
306 	 * @return {@code this}
307 	 * @since 4.7
308 	 */
309 	public PushCommand setRefLeaseSpecs(List<RefLeaseSpec> specs) {
310 		checkCallable();
311 		this.refLeaseSpecs.clear();
312 		for (RefLeaseSpec spec : specs) {
313 			refLeaseSpecs.put(spec.getRef(), spec);
314 		}
315 		return this;
316 	}
317 
318 	/**
319 	 * @return the ref specs
320 	 */
321 	public List<RefSpec> getRefSpecs() {
322 		return refSpecs;
323 	}
324 
325 	/**
326 	 * The ref specs to be used in the push operation
327 	 *
328 	 * @param specs
329 	 * @return {@code this}
330 	 */
331 	public PushCommand setRefSpecs(RefSpec... specs) {
332 		checkCallable();
333 		this.refSpecs.clear();
334 		Collections.addAll(refSpecs, specs);
335 		return this;
336 	}
337 
338 	/**
339 	 * The ref specs to be used in the push operation
340 	 *
341 	 * @param specs
342 	 * @return {@code this}
343 	 */
344 	public PushCommand setRefSpecs(List<RefSpec> specs) {
345 		checkCallable();
346 		this.refSpecs.clear();
347 		this.refSpecs.addAll(specs);
348 		return this;
349 	}
350 
351 	/**
352 	 * Push all branches under refs/heads/*.
353 	 *
354 	 * @return {code this}
355 	 */
356 	public PushCommand setPushAll() {
357 		refSpecs.add(Transport.REFSPEC_PUSH_ALL);
358 		return this;
359 	}
360 
361 	/**
362 	 * Push all tags under refs/tags/*.
363 	 *
364 	 * @return {code this}
365 	 */
366 	public PushCommand setPushTags() {
367 		refSpecs.add(Transport.REFSPEC_TAGS);
368 		return this;
369 	}
370 
371 	/**
372 	 * Add a reference to push.
373 	 *
374 	 * @param ref
375 	 *            the source reference. The remote name will match.
376 	 * @return {@code this}.
377 	 */
378 	public PushCommand add(Ref ref) {
379 		refSpecs.add(new RefSpec(ref.getLeaf().getName()));
380 		return this;
381 	}
382 
383 	/**
384 	 * Add a reference to push.
385 	 *
386 	 * @param nameOrSpec
387 	 *            any reference name, or a reference specification.
388 	 * @return {@code this}.
389 	 * @throws JGitInternalException
390 	 *             the reference name cannot be resolved.
391 	 */
392 	public PushCommand add(String nameOrSpec) {
393 		if (0 <= nameOrSpec.indexOf(':')) {
394 			refSpecs.add(new RefSpec(nameOrSpec));
395 		} else {
396 			Ref src;
397 			try {
398 				src = repo.findRef(nameOrSpec);
399 			} catch (IOException e) {
400 				throw new JGitInternalException(
401 						JGitText.get().exceptionCaughtDuringExecutionOfPushCommand,
402 						e);
403 			}
404 			if (src != null)
405 				add(src);
406 		}
407 		return this;
408 	}
409 
410 	/**
411 	 * @return the dry run preference for the push operation
412 	 */
413 	public boolean isDryRun() {
414 		return dryRun;
415 	}
416 
417 	/**
418 	 * Sets whether the push operation should be a dry run
419 	 *
420 	 * @param dryRun
421 	 * @return {@code this}
422 	 */
423 	public PushCommand setDryRun(boolean dryRun) {
424 		checkCallable();
425 		this.dryRun = dryRun;
426 		return this;
427 	}
428 
429 	/**
430 	 * @return the thin-pack preference for push operation
431 	 */
432 	public boolean isThin() {
433 		return thin;
434 	}
435 
436 	/**
437 	 * Sets the thin-pack preference for push operation.
438 	 *
439 	 * Default setting is Transport.DEFAULT_PUSH_THIN
440 	 *
441 	 * @param thin
442 	 * @return {@code this}
443 	 */
444 	public PushCommand setThin(boolean thin) {
445 		checkCallable();
446 		this.thin = thin;
447 		return this;
448 	}
449 
450 	/**
451 	 * @return true if all-or-nothing behavior is requested.
452 	 * @since 4.2
453 	 */
454 	public boolean isAtomic() {
455 		return atomic;
456 	}
457 
458 	/**
459 	 * Requests atomic push (all references updated, or no updates).
460 	 *
461 	 * Default setting is false.
462 	 *
463 	 * @param atomic
464 	 * @return {@code this}
465 	 * @since 4.2
466 	 */
467 	public PushCommand setAtomic(boolean atomic) {
468 		checkCallable();
469 		this.atomic = atomic;
470 		return this;
471 	}
472 
473 	/**
474 	 * @return the force preference for push operation
475 	 */
476 	public boolean isForce() {
477 		return force;
478 	}
479 
480 	/**
481 	 * Sets the force preference for push operation.
482 	 *
483 	 * @param force
484 	 * @return {@code this}
485 	 */
486 	public PushCommand setForce(boolean force) {
487 		checkCallable();
488 		this.force = force;
489 		return this;
490 	}
491 
492 	/**
493 	 * Sets the output stream to write sideband messages to
494 	 *
495 	 * @param out
496 	 * @return {@code this}
497 	 * @since 3.0
498 	 */
499 	public PushCommand setOutputStream(OutputStream out) {
500 		this.out = out;
501 		return this;
502 	}
503 
504 	/**
505 	 * @return the option strings associated with the push operation
506 	 * @since 4.5
507 	 */
508 	public List<String> getPushOptions() {
509 		return pushOptions;
510 	}
511 
512 	/**
513 	 * Sets the option strings associated with the push operation.
514 	 *
515 	 * @param pushOptions
516 	 * @return {@code this}
517 	 * @since 4.5
518 	 */
519 	public PushCommand setPushOptions(List<String> pushOptions) {
520 		this.pushOptions = pushOptions;
521 		return this;
522 	}
523 }