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 static java.util.stream.Collectors.toList;
46  
47  import java.io.IOException;
48  import java.net.URISyntaxException;
49  import java.text.MessageFormat;
50  import java.util.ArrayList;
51  import java.util.Arrays;
52  import java.util.List;
53  
54  import org.eclipse.jgit.annotations.Nullable;
55  import org.eclipse.jgit.api.errors.GitAPIException;
56  import org.eclipse.jgit.api.errors.InvalidConfigurationException;
57  import org.eclipse.jgit.api.errors.InvalidRemoteException;
58  import org.eclipse.jgit.api.errors.JGitInternalException;
59  import org.eclipse.jgit.errors.ConfigInvalidException;
60  import org.eclipse.jgit.errors.NoRemoteRepositoryException;
61  import org.eclipse.jgit.errors.NotSupportedException;
62  import org.eclipse.jgit.errors.TransportException;
63  import org.eclipse.jgit.internal.JGitText;
64  import org.eclipse.jgit.lib.ConfigConstants;
65  import org.eclipse.jgit.lib.Constants;
66  import org.eclipse.jgit.lib.NullProgressMonitor;
67  import org.eclipse.jgit.lib.ObjectId;
68  import org.eclipse.jgit.lib.ProgressMonitor;
69  import org.eclipse.jgit.lib.Repository;
70  import org.eclipse.jgit.lib.StoredConfig;
71  import org.eclipse.jgit.lib.SubmoduleConfig.FetchRecurseSubmodulesMode;
72  import org.eclipse.jgit.revwalk.RevWalk;
73  import org.eclipse.jgit.submodule.SubmoduleWalk;
74  import org.eclipse.jgit.transport.FetchResult;
75  import org.eclipse.jgit.transport.RefSpec;
76  import org.eclipse.jgit.transport.TagOpt;
77  import org.eclipse.jgit.transport.Transport;
78  
79  /**
80   * A class used to execute a {@code Fetch} command. It has setters for all
81   * supported options and arguments of this command and a {@link #call()} method
82   * to finally execute the command.
83   *
84   * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-fetch.html"
85   *      >Git documentation about Fetch</a>
86   */
87  public class FetchCommand extends TransportCommand<FetchCommand, FetchResult> {
88  	private String remote = Constants.DEFAULT_REMOTE_NAME;
89  
90  	private List<RefSpec> refSpecs;
91  
92  	private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
93  
94  	private boolean checkFetchedObjects;
95  
96  	private Boolean removeDeletedRefs;
97  
98  	private boolean dryRun;
99  
100 	private boolean thin = Transport.DEFAULT_FETCH_THIN;
101 
102 	private TagOpt tagOption;
103 
104 	private FetchRecurseSubmodulesMode submoduleRecurseMode = null;
105 
106 	private Callback callback;
107 
108 	/**
109 	 * Callback for status of fetch operation.
110 	 *
111 	 * @since 4.8
112 	 *
113 	 */
114 	public interface Callback {
115 		/**
116 		 * Notify fetching a submodule.
117 		 *
118 		 * @param name
119 		 *            the submodule name.
120 		 */
121 		void fetchingSubmodule(String name);
122 	}
123 
124 	/**
125 	 * Constructor for FetchCommand.
126 	 *
127 	 * @param repo
128 	 *            a {@link org.eclipse.jgit.lib.Repository} object.
129 	 */
130 	protected FetchCommand(Repository repo) {
131 		super(repo);
132 		refSpecs = new ArrayList<>(3);
133 	}
134 
135 	private FetchRecurseSubmodulesMode getRecurseMode(String path) {
136 		// Use the caller-specified mode, if set
137 		if (submoduleRecurseMode != null) {
138 			return submoduleRecurseMode;
139 		}
140 
141 		// Fall back to submodule.name.fetchRecurseSubmodules, if set
142 		FetchRecurseSubmodulesMode mode = repo.getConfig().getEnum(
143 				FetchRecurseSubmodulesMode.values(),
144 				ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
145 				ConfigConstants.CONFIG_KEY_FETCH_RECURSE_SUBMODULES, null);
146 		if (mode != null) {
147 			return mode;
148 		}
149 
150 		// Fall back to fetch.recurseSubmodules, if set
151 		mode = repo.getConfig().getEnum(FetchRecurseSubmodulesMode.values(),
152 				ConfigConstants.CONFIG_FETCH_SECTION, null,
153 				ConfigConstants.CONFIG_KEY_RECURSE_SUBMODULES, null);
154 		if (mode != null) {
155 			return mode;
156 		}
157 
158 		// Default to on-demand mode
159 		return FetchRecurseSubmodulesMode.ON_DEMAND;
160 	}
161 
162 	private void fetchSubmodules(FetchResult results)
163 			throws org.eclipse.jgit.api.errors.TransportException,
164 			GitAPIException, InvalidConfigurationException {
165 		try (SubmoduleWalk walk = new SubmoduleWalk(repo);
166 				RevWalk revWalk = new RevWalk(repo)) {
167 			// Walk over submodules in the parent repository's FETCH_HEAD.
168 			ObjectId fetchHead = repo.resolve(Constants.FETCH_HEAD);
169 			if (fetchHead == null) {
170 				return;
171 			}
172 			walk.setTree(revWalk.parseTree(fetchHead));
173 			while (walk.next()) {
174 				try (Repository submoduleRepo = walk.getRepository()) {
175 
176 					// Skip submodules that don't exist locally (have not been
177 					// cloned), are not registered in the .gitmodules file, or
178 					// not registered in the parent repository's config.
179 					if (submoduleRepo == null || walk.getModulesPath() == null
180 							|| walk.getConfigUrl() == null) {
181 						continue;
182 					}
183 
184 					FetchRecurseSubmodulesMode recurseMode = getRecurseMode(
185 							walk.getPath());
186 
187 					// When the fetch mode is "yes" we always fetch. When the
188 					// mode
189 					// is "on demand", we only fetch if the submodule's revision
190 					// was
191 					// updated to an object that is not currently present in the
192 					// submodule.
193 					if ((recurseMode == FetchRecurseSubmodulesMode.ON_DEMAND
194 							&& !submoduleRepo.hasObject(walk.getObjectId()))
195 							|| recurseMode == FetchRecurseSubmodulesMode.YES) {
196 						FetchCommand f = new FetchCommand(submoduleRepo)
197 								.setProgressMonitor(monitor)
198 								.setTagOpt(tagOption)
199 								.setCheckFetchedObjects(checkFetchedObjects)
200 								.setRemoveDeletedRefs(isRemoveDeletedRefs())
201 								.setThin(thin).setRefSpecs(refSpecs)
202 								.setDryRun(dryRun)
203 								.setRecurseSubmodules(recurseMode);
204 						configure(f);
205 						if (callback != null) {
206 							callback.fetchingSubmodule(walk.getPath());
207 						}
208 						results.addSubmodule(walk.getPath(), f.call());
209 					}
210 				}
211 			}
212 		} catch (IOException e) {
213 			throw new JGitInternalException(e.getMessage(), e);
214 		} catch (ConfigInvalidException e) {
215 			throw new InvalidConfigurationException(e.getMessage(), e);
216 		}
217 	}
218 
219 	/**
220 	 * {@inheritDoc}
221 	 * <p>
222 	 * Execute the {@code fetch} command with all the options and parameters
223 	 * collected by the setter methods of this class. Each instance of this
224 	 * class should only be used for one invocation of the command (means: one
225 	 * call to {@link #call()})
226 	 */
227 	@Override
228 	public FetchResult call() throws GitAPIException, InvalidRemoteException,
229 			org.eclipse.jgit.api.errors.TransportException {
230 		checkCallable();
231 
232 		try (Transport transport = Transport.open(repo, remote)) {
233 			transport.setCheckFetchedObjects(checkFetchedObjects);
234 			transport.setRemoveDeletedRefs(isRemoveDeletedRefs());
235 			transport.setDryRun(dryRun);
236 			if (tagOption != null)
237 				transport.setTagOpt(tagOption);
238 			transport.setFetchThin(thin);
239 			configure(transport);
240 
241 			FetchResult result = transport.fetch(monitor, refSpecs);
242 			if (!repo.isBare()) {
243 				fetchSubmodules(result);
244 			}
245 
246 			return result;
247 		} catch (NoRemoteRepositoryException e) {
248 			throw new InvalidRemoteException(MessageFormat.format(
249 					JGitText.get().invalidRemote, remote), e);
250 		} catch (TransportException e) {
251 			throw new org.eclipse.jgit.api.errors.TransportException(
252 					e.getMessage(), e);
253 		} catch (URISyntaxException e) {
254 			throw new InvalidRemoteException(MessageFormat.format(
255 					JGitText.get().invalidRemote, remote));
256 		} catch (NotSupportedException e) {
257 			throw new JGitInternalException(
258 					JGitText.get().exceptionCaughtDuringExecutionOfFetchCommand,
259 					e);
260 		}
261 
262 	}
263 
264 	/**
265 	 * Set the mode to be used for recursing into submodules.
266 	 *
267 	 * @param recurse
268 	 *            corresponds to the
269 	 *            --recurse-submodules/--no-recurse-submodules options. If
270 	 *            {@code null} use the value of the
271 	 *            {@code submodule.name.fetchRecurseSubmodules} option
272 	 *            configured per submodule. If not specified there, use the
273 	 *            value of the {@code fetch.recurseSubmodules} option configured
274 	 *            in git config. If not configured in either, "on-demand" is the
275 	 *            built-in default.
276 	 * @return {@code this}
277 	 * @since 4.7
278 	 */
279 	public FetchCommand setRecurseSubmodules(
280 			@Nullable FetchRecurseSubmodulesMode recurse) {
281 		checkCallable();
282 		submoduleRecurseMode = recurse;
283 		return this;
284 	}
285 
286 	/**
287 	 * The remote (uri or name) used for the fetch operation. If no remote is
288 	 * set, the default value of <code>Constants.DEFAULT_REMOTE_NAME</code> will
289 	 * be used.
290 	 *
291 	 * @see Constants#DEFAULT_REMOTE_NAME
292 	 * @param remote
293 	 *            name of a remote
294 	 * @return {@code this}
295 	 */
296 	public FetchCommand setRemote(String remote) {
297 		checkCallable();
298 		this.remote = remote;
299 		return this;
300 	}
301 
302 	/**
303 	 * Get the remote
304 	 *
305 	 * @return the remote used for the remote operation
306 	 */
307 	public String getRemote() {
308 		return remote;
309 	}
310 
311 	/**
312 	 * Get timeout
313 	 *
314 	 * @return the timeout used for the fetch operation
315 	 */
316 	public int getTimeout() {
317 		return timeout;
318 	}
319 
320 	/**
321 	 * Whether to check received objects for validity
322 	 *
323 	 * @return whether to check received objects for validity
324 	 */
325 	public boolean isCheckFetchedObjects() {
326 		return checkFetchedObjects;
327 	}
328 
329 	/**
330 	 * If set to {@code true}, objects received will be checked for validity
331 	 *
332 	 * @param checkFetchedObjects
333 	 *            whether to check objects for validity
334 	 * @return {@code this}
335 	 */
336 	public FetchCommand setCheckFetchedObjects(boolean checkFetchedObjects) {
337 		checkCallable();
338 		this.checkFetchedObjects = checkFetchedObjects;
339 		return this;
340 	}
341 
342 	/**
343 	 * Whether to remove refs which no longer exist in the source
344 	 *
345 	 * @return whether to remove refs which no longer exist in the source
346 	 */
347 	public boolean isRemoveDeletedRefs() {
348 		if (removeDeletedRefs != null)
349 			return removeDeletedRefs.booleanValue();
350 		else { // fall back to configuration
351 			boolean result = false;
352 			StoredConfig config = repo.getConfig();
353 			result = config.getBoolean(ConfigConstants.CONFIG_FETCH_SECTION,
354 					null, ConfigConstants.CONFIG_KEY_PRUNE, result);
355 			result = config.getBoolean(ConfigConstants.CONFIG_REMOTE_SECTION,
356 					remote, ConfigConstants.CONFIG_KEY_PRUNE, result);
357 			return result;
358 		}
359 	}
360 
361 	/**
362 	 * If set to {@code true}, refs are removed which no longer exist in the
363 	 * source
364 	 *
365 	 * @param removeDeletedRefs
366 	 *            whether to remove deleted {@code Ref}s
367 	 * @return {@code this}
368 	 */
369 	public FetchCommand setRemoveDeletedRefs(boolean removeDeletedRefs) {
370 		checkCallable();
371 		this.removeDeletedRefs = Boolean.valueOf(removeDeletedRefs);
372 		return this;
373 	}
374 
375 	/**
376 	 * Get progress monitor
377 	 *
378 	 * @return the progress monitor for the fetch operation
379 	 */
380 	public ProgressMonitor getProgressMonitor() {
381 		return monitor;
382 	}
383 
384 	/**
385 	 * The progress monitor associated with the fetch operation. By default,
386 	 * this is set to <code>NullProgressMonitor</code>
387 	 *
388 	 * @see NullProgressMonitor
389 	 * @param monitor
390 	 *            a {@link org.eclipse.jgit.lib.ProgressMonitor}
391 	 * @return {@code this}
392 	 */
393 	public FetchCommand setProgressMonitor(ProgressMonitor monitor) {
394 		checkCallable();
395 		if (monitor == null) {
396 			monitor = NullProgressMonitor.INSTANCE;
397 		}
398 		this.monitor = monitor;
399 		return this;
400 	}
401 
402 	/**
403 	 * Get list of {@code RefSpec}s
404 	 *
405 	 * @return the ref specs
406 	 */
407 	public List<RefSpec> getRefSpecs() {
408 		return refSpecs;
409 	}
410 
411 	/**
412 	 * The ref specs to be used in the fetch operation
413 	 *
414 	 * @param specs
415 	 *            String representation of {@code RefSpec}s
416 	 * @return {@code this}
417 	 * @since 4.9
418 	 */
419 	public FetchCommand setRefSpecs(String... specs) {
420 		return setRefSpecs(
421 				Arrays.stream(specs).map(RefSpec::new).collect(toList()));
422 	}
423 
424 	/**
425 	 * The ref specs to be used in the fetch operation
426 	 *
427 	 * @param specs
428 	 *            one or multiple {@link org.eclipse.jgit.transport.RefSpec}s
429 	 * @return {@code this}
430 	 */
431 	public FetchCommand setRefSpecs(RefSpec... specs) {
432 		return setRefSpecs(Arrays.asList(specs));
433 	}
434 
435 	/**
436 	 * The ref specs to be used in the fetch operation
437 	 *
438 	 * @param specs
439 	 *            list of {@link org.eclipse.jgit.transport.RefSpec}s
440 	 * @return {@code this}
441 	 */
442 	public FetchCommand setRefSpecs(List<RefSpec> specs) {
443 		checkCallable();
444 		this.refSpecs.clear();
445 		this.refSpecs.addAll(specs);
446 		return this;
447 	}
448 
449 	/**
450 	 * Whether to do a dry run
451 	 *
452 	 * @return the dry run preference for the fetch operation
453 	 */
454 	public boolean isDryRun() {
455 		return dryRun;
456 	}
457 
458 	/**
459 	 * Sets whether the fetch operation should be a dry run
460 	 *
461 	 * @param dryRun
462 	 *            whether to do a dry run
463 	 * @return {@code this}
464 	 */
465 	public FetchCommand setDryRun(boolean dryRun) {
466 		checkCallable();
467 		this.dryRun = dryRun;
468 		return this;
469 	}
470 
471 	/**
472 	 * Get thin-pack preference
473 	 *
474 	 * @return the thin-pack preference for fetch operation
475 	 */
476 	public boolean isThin() {
477 		return thin;
478 	}
479 
480 	/**
481 	 * Sets the thin-pack preference for fetch operation.
482 	 *
483 	 * Default setting is Transport.DEFAULT_FETCH_THIN
484 	 *
485 	 * @param thin
486 	 *            the thin-pack preference
487 	 * @return {@code this}
488 	 */
489 	public FetchCommand setThin(boolean thin) {
490 		checkCallable();
491 		this.thin = thin;
492 		return this;
493 	}
494 
495 	/**
496 	 * Sets the specification of annotated tag behavior during fetch
497 	 *
498 	 * @param tagOpt
499 	 *            the {@link org.eclipse.jgit.transport.TagOpt}
500 	 * @return {@code this}
501 	 */
502 	public FetchCommand setTagOpt(TagOpt tagOpt) {
503 		checkCallable();
504 		this.tagOption = tagOpt;
505 		return this;
506 	}
507 
508 	/**
509 	 * Register a progress callback.
510 	 *
511 	 * @param callback
512 	 *            the callback
513 	 * @return {@code this}
514 	 * @since 4.8
515 	 */
516 	public FetchCommand setCallback(Callback callback) {
517 		this.callback = callback;
518 		return this;
519 	}
520 }