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.net.URISyntaxException;
46  import java.text.MessageFormat;
47  import java.util.ArrayList;
48  import java.util.List;
49  
50  import org.eclipse.jgit.api.errors.GitAPIException;
51  import org.eclipse.jgit.api.errors.InvalidRemoteException;
52  import org.eclipse.jgit.api.errors.JGitInternalException;
53  import org.eclipse.jgit.errors.NoRemoteRepositoryException;
54  import org.eclipse.jgit.errors.NotSupportedException;
55  import org.eclipse.jgit.errors.TransportException;
56  import org.eclipse.jgit.internal.JGitText;
57  import org.eclipse.jgit.lib.ConfigConstants;
58  import org.eclipse.jgit.lib.Constants;
59  import org.eclipse.jgit.lib.NullProgressMonitor;
60  import org.eclipse.jgit.lib.ProgressMonitor;
61  import org.eclipse.jgit.lib.Repository;
62  import org.eclipse.jgit.lib.StoredConfig;
63  import org.eclipse.jgit.transport.FetchResult;
64  import org.eclipse.jgit.transport.RefSpec;
65  import org.eclipse.jgit.transport.TagOpt;
66  import org.eclipse.jgit.transport.Transport;
67  
68  /**
69   * A class used to execute a {@code Fetch} command. It has setters for all
70   * supported options and arguments of this command and a {@link #call()} method
71   * to finally execute the command.
72   *
73   * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-fetch.html"
74   *      >Git documentation about Fetch</a>
75   */
76  public class FetchCommand extends TransportCommand<FetchCommand, FetchResult> {
77  
78  	private String remote = Constants.DEFAULT_REMOTE_NAME;
79  
80  	private List<RefSpec> refSpecs;
81  
82  	private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
83  
84  	private boolean checkFetchedObjects;
85  
86  	private Boolean removeDeletedRefs;
87  
88  	private boolean dryRun;
89  
90  	private boolean thin = Transport.DEFAULT_FETCH_THIN;
91  
92  	private TagOpt tagOption;
93  
94  	/**
95  	 * @param repo
96  	 */
97  	protected FetchCommand(Repository repo) {
98  		super(repo);
99  		refSpecs = new ArrayList<RefSpec>(3);
100 	}
101 
102 	/**
103 	 * Executes the {@code fetch} command with all the options and parameters
104 	 * collected by the setter methods of this class. Each instance of this
105 	 * class should only be used for one invocation of the command (means: one
106 	 * call to {@link #call()})
107 	 *
108 	 * @return a {@link FetchResult} object representing the successful fetch
109 	 *         result
110 	 * @throws InvalidRemoteException
111 	 *             when called with an invalid remote uri
112 	 * @throws org.eclipse.jgit.api.errors.TransportException
113 	 *             when an error occurs during transport
114 	 */
115 	public FetchResult call() throws GitAPIException, InvalidRemoteException,
116 			org.eclipse.jgit.api.errors.TransportException {
117 		checkCallable();
118 
119 		try (Transport transport = Transport.open(repo, remote)) {
120 			transport.setCheckFetchedObjects(checkFetchedObjects);
121 			transport.setRemoveDeletedRefs(isRemoveDeletedRefs());
122 			transport.setDryRun(dryRun);
123 			if (tagOption != null)
124 				transport.setTagOpt(tagOption);
125 			transport.setFetchThin(thin);
126 			configure(transport);
127 
128 			FetchResult result = transport.fetch(monitor, refSpecs);
129 			return result;
130 		} catch (NoRemoteRepositoryException e) {
131 			throw new InvalidRemoteException(MessageFormat.format(
132 					JGitText.get().invalidRemote, remote), e);
133 		} catch (TransportException e) {
134 			throw new org.eclipse.jgit.api.errors.TransportException(
135 					e.getMessage(), e);
136 		} catch (URISyntaxException e) {
137 			throw new InvalidRemoteException(MessageFormat.format(
138 					JGitText.get().invalidRemote, remote));
139 		} catch (NotSupportedException e) {
140 			throw new JGitInternalException(
141 					JGitText.get().exceptionCaughtDuringExecutionOfFetchCommand,
142 					e);
143 		}
144 
145 	}
146 
147 	/**
148 	 * The remote (uri or name) used for the fetch operation. If no remote is
149 	 * set, the default value of <code>Constants.DEFAULT_REMOTE_NAME</code> will
150 	 * be used.
151 	 *
152 	 * @see Constants#DEFAULT_REMOTE_NAME
153 	 * @param remote
154 	 * @return {@code this}
155 	 */
156 	public FetchCommand setRemote(String remote) {
157 		checkCallable();
158 		this.remote = remote;
159 		return this;
160 	}
161 
162 	/**
163 	 * @return the remote used for the remote operation
164 	 */
165 	public String getRemote() {
166 		return remote;
167 	}
168 
169 	/**
170 	 * @return the timeout used for the fetch operation
171 	 */
172 	public int getTimeout() {
173 		return timeout;
174 	}
175 
176 	/**
177 	 * @return whether to check received objects checked for validity
178 	 */
179 	public boolean isCheckFetchedObjects() {
180 		return checkFetchedObjects;
181 	}
182 
183 	/**
184 	 * If set to true, objects received will be checked for validity
185 	 *
186 	 * @param checkFetchedObjects
187 	 * @return {@code this}
188 	 */
189 	public FetchCommand setCheckFetchedObjects(boolean checkFetchedObjects) {
190 		checkCallable();
191 		this.checkFetchedObjects = checkFetchedObjects;
192 		return this;
193 	}
194 
195 	/**
196 	 * @return whether or not to remove refs which no longer exist in the source
197 	 */
198 	public boolean isRemoveDeletedRefs() {
199 		if (removeDeletedRefs != null)
200 			return removeDeletedRefs.booleanValue();
201 		else { // fall back to configuration
202 			boolean result = false;
203 			StoredConfig config = repo.getConfig();
204 			result = config.getBoolean(ConfigConstants.CONFIG_FETCH_SECTION,
205 					null, ConfigConstants.CONFIG_KEY_PRUNE, result);
206 			result = config.getBoolean(ConfigConstants.CONFIG_REMOTE_SECTION,
207 					remote, ConfigConstants.CONFIG_KEY_PRUNE, result);
208 			return result;
209 		}
210 	}
211 
212 	/**
213 	 * If set to true, refs are removed which no longer exist in the source
214 	 *
215 	 * @param removeDeletedRefs
216 	 * @return {@code this}
217 	 */
218 	public FetchCommand setRemoveDeletedRefs(boolean removeDeletedRefs) {
219 		checkCallable();
220 		this.removeDeletedRefs = Boolean.valueOf(removeDeletedRefs);
221 		return this;
222 	}
223 
224 	/**
225 	 * @return the progress monitor for the fetch operation
226 	 */
227 	public ProgressMonitor getProgressMonitor() {
228 		return monitor;
229 	}
230 
231 	/**
232 	 * The progress monitor associated with the fetch operation. By default,
233 	 * this is set to <code>NullProgressMonitor</code>
234 	 *
235 	 * @see NullProgressMonitor
236 	 *
237 	 * @param monitor
238 	 * @return {@code this}
239 	 */
240 	public FetchCommand setProgressMonitor(ProgressMonitor monitor) {
241 		checkCallable();
242 		if (monitor == null) {
243 			monitor = NullProgressMonitor.INSTANCE;
244 		}
245 		this.monitor = monitor;
246 		return this;
247 	}
248 
249 	/**
250 	 * @return the ref specs
251 	 */
252 	public List<RefSpec> getRefSpecs() {
253 		return refSpecs;
254 	}
255 
256 	/**
257 	 * The ref specs to be used in the fetch operation
258 	 *
259 	 * @param specs
260 	 * @return {@code this}
261 	 */
262 	public FetchCommand setRefSpecs(RefSpec... specs) {
263 		checkCallable();
264 		this.refSpecs.clear();
265 		for (RefSpec spec : specs)
266 			refSpecs.add(spec);
267 		return this;
268 	}
269 
270 	/**
271 	 * The ref specs to be used in the fetch operation
272 	 *
273 	 * @param specs
274 	 * @return {@code this}
275 	 */
276 	public FetchCommand setRefSpecs(List<RefSpec> specs) {
277 		checkCallable();
278 		this.refSpecs.clear();
279 		this.refSpecs.addAll(specs);
280 		return this;
281 	}
282 
283 	/**
284 	 * @return the dry run preference for the fetch operation
285 	 */
286 	public boolean isDryRun() {
287 		return dryRun;
288 	}
289 
290 	/**
291 	 * Sets whether the fetch operation should be a dry run
292 	 *
293 	 * @param dryRun
294 	 * @return {@code this}
295 	 */
296 	public FetchCommand setDryRun(boolean dryRun) {
297 		checkCallable();
298 		this.dryRun = dryRun;
299 		return this;
300 	}
301 
302 	/**
303 	 * @return the thin-pack preference for fetch operation
304 	 */
305 	public boolean isThin() {
306 		return thin;
307 	}
308 
309 	/**
310 	 * Sets the thin-pack preference for fetch operation.
311 	 *
312 	 * Default setting is Transport.DEFAULT_FETCH_THIN
313 	 *
314 	 * @param thin
315 	 * @return {@code this}
316 	 */
317 	public FetchCommand setThin(boolean thin) {
318 		checkCallable();
319 		this.thin = thin;
320 		return this;
321 	}
322 
323 	/**
324 	 * Sets the specification of annotated tag behavior during fetch
325 	 *
326 	 * @param tagOpt
327 	 * @return {@code this}
328 	 */
329 	public FetchCommand setTagOpt(TagOpt tagOpt) {
330 		checkCallable();
331 		this.tagOption = tagOpt;
332 		return this;
333 	}
334 }