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.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   * A class used to execute a {@code Push} command. It has setters for all
74   * supported options and arguments of this command and a {@link #call()} method
75   * to finally execute the command.
76   *
77   * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-push.html"
78   *      >Git documentation about Push</a>
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 	 * @param repo
101 	 */
102 	protected PushCommand(Repository repo) {
103 		super(repo);
104 		refSpecs = new ArrayList<RefSpec>(3);
105 	}
106 
107 	/**
108 	 * Executes the {@code push} command with all the options and parameters
109 	 * collected by the setter methods of this class. Each instance of this
110 	 * class should only be used for one invocation of the command (means: one
111 	 * call to {@link #call()})
112 	 *
113 	 * @return an iteration over {@link PushResult} objects
114 	 * @throws InvalidRemoteException
115 	 *             when called with an invalid remote uri
116 	 * @throws org.eclipse.jgit.api.errors.TransportException
117 	 *             when an error occurs with the transport
118 	 * @throws GitAPIException
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 	 * The remote (uri or name) used for the push operation. If no remote is
193 	 * set, the default value of <code>Constants.DEFAULT_REMOTE_NAME</code> will
194 	 * be used.
195 	 *
196 	 * @see Constants#DEFAULT_REMOTE_NAME
197 	 * @param remote
198 	 * @return {@code this}
199 	 */
200 	public PushCommand setRemote(String remote) {
201 		checkCallable();
202 		this.remote = remote;
203 		return this;
204 	}
205 
206 	/**
207 	 * @return the remote used for the remote operation
208 	 */
209 	public String getRemote() {
210 		return remote;
211 	}
212 
213 	/**
214 	 * The remote executable providing receive-pack service for pack transports.
215 	 * If no receive-pack is set, the default value of
216 	 * <code>RemoteConfig.DEFAULT_RECEIVE_PACK</code> will be used.
217 	 *
218 	 * @see RemoteConfig#DEFAULT_RECEIVE_PACK
219 	 * @param receivePack
220 	 * @return {@code this}
221 	 */
222 	public PushCommand setReceivePack(String receivePack) {
223 		checkCallable();
224 		this.receivePack = receivePack;
225 		return this;
226 	}
227 
228 	/**
229 	 * @return the receive-pack used for the remote operation
230 	 */
231 	public String getReceivePack() {
232 		return receivePack;
233 	}
234 
235 	/**
236 	 * @return the timeout used for the push operation
237 	 */
238 	public int getTimeout() {
239 		return timeout;
240 	}
241 
242 	/**
243 	 * @return the progress monitor for the push operation
244 	 */
245 	public ProgressMonitor getProgressMonitor() {
246 		return monitor;
247 	}
248 
249 	/**
250 	 * The progress monitor associated with the push operation. By default, this
251 	 * is set to <code>NullProgressMonitor</code>
252 	 *
253 	 * @see NullProgressMonitor
254 	 *
255 	 * @param monitor
256 	 * @return {@code this}
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 	 * @return the ref specs
269 	 */
270 	public List<RefSpec> getRefSpecs() {
271 		return refSpecs;
272 	}
273 
274 	/**
275 	 * The ref specs to be used in the push operation
276 	 *
277 	 * @param specs
278 	 * @return {@code this}
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 	 * The ref specs to be used in the push operation
289 	 *
290 	 * @param specs
291 	 * @return {@code this}
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 	 * Push all branches under refs/heads/*.
302 	 *
303 	 * @return {code this}
304 	 */
305 	public PushCommand setPushAll() {
306 		refSpecs.add(Transport.REFSPEC_PUSH_ALL);
307 		return this;
308 	}
309 
310 	/**
311 	 * Push all tags under refs/tags/*.
312 	 *
313 	 * @return {code this}
314 	 */
315 	public PushCommand setPushTags() {
316 		refSpecs.add(Transport.REFSPEC_TAGS);
317 		return this;
318 	}
319 
320 	/**
321 	 * Add a reference to push.
322 	 *
323 	 * @param ref
324 	 *            the source reference. The remote name will match.
325 	 * @return {@code this}.
326 	 */
327 	public PushCommand add(Ref ref) {
328 		refSpecs.add(new RefSpec(ref.getLeaf().getName()));
329 		return this;
330 	}
331 
332 	/**
333 	 * Add a reference to push.
334 	 *
335 	 * @param nameOrSpec
336 	 *            any reference name, or a reference specification.
337 	 * @return {@code this}.
338 	 * @throws JGitInternalException
339 	 *             the reference name cannot be resolved.
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 	 * @return the dry run preference for the push operation
361 	 */
362 	public boolean isDryRun() {
363 		return dryRun;
364 	}
365 
366 	/**
367 	 * Sets whether the push operation should be a dry run
368 	 *
369 	 * @param dryRun
370 	 * @return {@code this}
371 	 */
372 	public PushCommand setDryRun(boolean dryRun) {
373 		checkCallable();
374 		this.dryRun = dryRun;
375 		return this;
376 	}
377 
378 	/**
379 	 * @return the thin-pack preference for push operation
380 	 */
381 	public boolean isThin() {
382 		return thin;
383 	}
384 
385 	/**
386 	 * Sets the thin-pack preference for push operation.
387 	 *
388 	 * Default setting is Transport.DEFAULT_PUSH_THIN
389 	 *
390 	 * @param thin
391 	 * @return {@code this}
392 	 */
393 	public PushCommand setThin(boolean thin) {
394 		checkCallable();
395 		this.thin = thin;
396 		return this;
397 	}
398 
399 	/**
400 	 * @return the force preference for push operation
401 	 */
402 	public boolean isForce() {
403 		return force;
404 	}
405 
406 	/**
407 	 * Sets the force preference for push operation.
408 	 *
409 	 * @param force
410 	 * @return {@code this}
411 	 */
412 	public PushCommand setForce(boolean force) {
413 		checkCallable();
414 		this.force = force;
415 		return this;
416 	}
417 
418 	/**
419 	 * Sets the output stream to write sideband messages to
420 	 *
421 	 * @param out
422 	 * @return {@code this}
423 	 * @since 3.0
424 	 */
425 	public PushCommand setOutputStream(OutputStream out) {
426 		this.out = out;
427 		return this;
428 	}
429 }