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