View Javadoc
1   /*
2    * Copyright (C) 2011, 2017 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 java.io.File;
13  import java.io.IOException;
14  import java.net.URISyntaxException;
15  import java.text.MessageFormat;
16  import java.util.ArrayList;
17  import java.util.Collection;
18  import java.util.List;
19  
20  import org.eclipse.jgit.annotations.Nullable;
21  import org.eclipse.jgit.api.errors.GitAPIException;
22  import org.eclipse.jgit.api.errors.InvalidRemoteException;
23  import org.eclipse.jgit.api.errors.JGitInternalException;
24  import org.eclipse.jgit.dircache.DirCache;
25  import org.eclipse.jgit.dircache.DirCacheCheckout;
26  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
27  import org.eclipse.jgit.errors.MissingObjectException;
28  import org.eclipse.jgit.internal.JGitText;
29  import org.eclipse.jgit.lib.AnyObjectId;
30  import org.eclipse.jgit.lib.BranchConfig.BranchRebaseMode;
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.Ref;
37  import org.eclipse.jgit.lib.RefUpdate;
38  import org.eclipse.jgit.lib.Repository;
39  import org.eclipse.jgit.revwalk.RevCommit;
40  import org.eclipse.jgit.revwalk.RevWalk;
41  import org.eclipse.jgit.submodule.SubmoduleWalk;
42  import org.eclipse.jgit.transport.FetchResult;
43  import org.eclipse.jgit.transport.RefSpec;
44  import org.eclipse.jgit.transport.RemoteConfig;
45  import org.eclipse.jgit.transport.TagOpt;
46  import org.eclipse.jgit.transport.URIish;
47  import org.eclipse.jgit.util.FS;
48  import org.eclipse.jgit.util.FileUtils;
49  
50  /**
51   * Clone a repository into a new working directory
52   *
53   * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-clone.html"
54   *      >Git documentation about Clone</a>
55   */
56  public class CloneCommand extends TransportCommand<CloneCommand, Git> {
57  
58  	private String uri;
59  
60  	private File directory;
61  
62  	private File gitDir;
63  
64  	private boolean bare;
65  
66  	private FS fs;
67  
68  	private String remote = Constants.DEFAULT_REMOTE_NAME;
69  
70  	private String branch = Constants.HEAD;
71  
72  	private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
73  
74  	private boolean cloneAllBranches;
75  
76  	private boolean mirror;
77  
78  	private boolean cloneSubmodules;
79  
80  	private boolean noCheckout;
81  
82  	private Collection<String> branchesToClone;
83  
84  	private Callback callback;
85  
86  	private boolean directoryExistsInitially;
87  
88  	private boolean gitDirExistsInitially;
89  
90  	private FETCH_TYPE fetchType;
91  
92  	private TagOpt tagOption;
93  
94  	private enum FETCH_TYPE {
95  		MULTIPLE_BRANCHES, ALL_BRANCHES, MIRROR
96  	}
97  
98  	/**
99  	 * Callback for status of clone operation.
100 	 *
101 	 * @since 4.8
102 	 */
103 	public interface Callback {
104 		/**
105 		 * Notify initialized submodules.
106 		 *
107 		 * @param submodules
108 		 *            the submodules
109 		 *
110 		 */
111 		void initializedSubmodules(Collection<String> submodules);
112 
113 		/**
114 		 * Notify starting to clone a submodule.
115 		 *
116 		 * @param path
117 		 *            the submodule path
118 		 */
119 		void cloningSubmodule(String path);
120 
121 		/**
122 		 * Notify checkout of commit
123 		 *
124 		 * @param commit
125 		 *            the id of the commit being checked out
126 		 * @param path
127 		 *            the submodule path
128 		 */
129 		void checkingOut(AnyObjectId commit, String path);
130 	}
131 
132 	/**
133 	 * Create clone command with no repository set
134 	 */
135 	public CloneCommand() {
136 		super(null);
137 	}
138 
139 	/**
140 	 * Get the git directory. This is primarily used for tests.
141 	 *
142 	 * @return the git directory
143 	 */
144 	@Nullable
145 	File getDirectory() {
146 		return directory;
147 	}
148 
149 	/**
150 	 * {@inheritDoc}
151 	 * <p>
152 	 * Executes the {@code Clone} command.
153 	 *
154 	 * The Git instance returned by this command needs to be closed by the
155 	 * caller to free resources held by the underlying {@link Repository}
156 	 * instance. It is recommended to call this method as soon as you don't need
157 	 * a reference to this {@link Git} instance and the underlying
158 	 * {@link Repository} instance anymore.
159 	 */
160 	@Override
161 	public Git call() throws GitAPIException, InvalidRemoteException,
162 			org.eclipse.jgit.api.errors.TransportException {
163 		URIish u = null;
164 		try {
165 			u = new URIish(uri);
166 			verifyDirectories(u);
167 		} catch (URISyntaxException e) {
168 			throw new InvalidRemoteException(
169 					MessageFormat.format(JGitText.get().invalidURL, uri), e);
170 		}
171 		setFetchType();
172 		@SuppressWarnings("resource") // Closed by caller
173 		Repository repository = init();
174 		FetchResult fetchResult = null;
175 		Thread cleanupHook = new Thread(() -> cleanup());
176 		Runtime.getRuntime().addShutdownHook(cleanupHook);
177 		try {
178 			fetchResult = fetch(repository, u);
179 		} catch (IOException ioe) {
180 			if (repository != null) {
181 				repository.close();
182 			}
183 			cleanup();
184 			throw new JGitInternalException(ioe.getMessage(), ioe);
185 		} catch (URISyntaxException e) {
186 			if (repository != null) {
187 				repository.close();
188 			}
189 			cleanup();
190 			throw new InvalidRemoteException(
191 					MessageFormat.format(JGitText.get().invalidRemote, remote),
192 					e);
193 		} catch (GitAPIException | RuntimeException e) {
194 			if (repository != null) {
195 				repository.close();
196 			}
197 			cleanup();
198 			throw e;
199 		} finally {
200 			Runtime.getRuntime().removeShutdownHook(cleanupHook);
201 		}
202 		if (!noCheckout) {
203 			try {
204 				checkout(repository, fetchResult);
205 			} catch (IOException ioe) {
206 				repository.close();
207 				throw new JGitInternalException(ioe.getMessage(), ioe);
208 			} catch (GitAPIException | RuntimeException e) {
209 				repository.close();
210 				throw e;
211 			}
212 		}
213 		return new Git(repository, true);
214 	}
215 
216 	private void setFetchType() {
217 		if (mirror) {
218 			fetchType = FETCH_TYPE.MIRROR;
219 			setBare(true);
220 		} else if (cloneAllBranches) {
221 			fetchType = FETCH_TYPE.ALL_BRANCHES;
222 		} else if (branchesToClone != null && !branchesToClone.isEmpty()) {
223 			fetchType = FETCH_TYPE.MULTIPLE_BRANCHES;
224 		} else {
225 			// Default: neither mirror nor all nor specific refs given
226 			fetchType = FETCH_TYPE.ALL_BRANCHES;
227 		}
228 	}
229 
230 	private static boolean isNonEmptyDirectory(File dir) {
231 		if (dir != null && dir.exists()) {
232 			File[] files = dir.listFiles();
233 			return files != null && files.length != 0;
234 		}
235 		return false;
236 	}
237 
238 	void verifyDirectories(URIish u) {
239 		if (directory == null && gitDir == null) {
240 			directory = new File(u.getHumanishName() + (bare ? Constants.DOT_GIT_EXT : "")); //$NON-NLS-1$
241 		}
242 		directoryExistsInitially = directory != null && directory.exists();
243 		gitDirExistsInitially = gitDir != null && gitDir.exists();
244 		validateDirs(directory, gitDir, bare);
245 		if (isNonEmptyDirectory(directory)) {
246 			throw new JGitInternalException(MessageFormat.format(
247 					JGitText.get().cloneNonEmptyDirectory, directory.getName()));
248 		}
249 		if (isNonEmptyDirectory(gitDir)) {
250 			throw new JGitInternalException(MessageFormat.format(
251 					JGitText.get().cloneNonEmptyDirectory, gitDir.getName()));
252 		}
253 	}
254 
255 	private Repository init() throws GitAPIException {
256 		InitCommand command = Git.init();
257 		command.setBare(bare);
258 		if (fs != null) {
259 			command.setFs(fs);
260 		}
261 		if (directory != null) {
262 			command.setDirectory(directory);
263 		}
264 		if (gitDir != null) {
265 			command.setGitDir(gitDir);
266 		}
267 		return command.call().getRepository();
268 	}
269 
270 	private FetchResult fetch(Repository clonedRepo, URIish u)
271 			throws URISyntaxException,
272 			org.eclipse.jgit.api.errors.TransportException, IOException,
273 			GitAPIException {
274 		// create the remote config and save it
275 		RemoteConfig config = new RemoteConfig(clonedRepo.getConfig(), remote);
276 		config.addURI(u);
277 
278 		boolean fetchAll = fetchType == FETCH_TYPE.ALL_BRANCHES
279 				|| fetchType == FETCH_TYPE.MIRROR;
280 
281 		config.setFetchRefSpecs(calculateRefSpecs(fetchType, config.getName()));
282 		config.setMirror(fetchType == FETCH_TYPE.MIRROR);
283 		if (tagOption != null) {
284 			config.setTagOpt(tagOption);
285 		}
286 		config.update(clonedRepo.getConfig());
287 
288 		clonedRepo.getConfig().save();
289 
290 		// run the fetch command
291 		FetchCommand command = new FetchCommand(clonedRepo);
292 		command.setRemote(remote);
293 		command.setProgressMonitor(monitor);
294 		if (tagOption != null) {
295 			command.setTagOpt(tagOption);
296 		} else {
297 			command.setTagOpt(
298 					fetchAll ? TagOpt.FETCH_TAGS : TagOpt.AUTO_FOLLOW);
299 		}
300 		configure(command);
301 
302 		return command.call();
303 	}
304 
305 	private List<RefSpec> calculateRefSpecs(FETCH_TYPE type,
306 			String remoteName) {
307 		List<RefSpec> specs = new ArrayList<>();
308 		if (type == FETCH_TYPE.MIRROR) {
309 			specs.add(new RefSpec().setForceUpdate(true).setSourceDestination(
310 					Constants.R_REFS + '*', Constants.R_REFS + '*'));
311 		} else {
312 			RefSpec heads = new RefSpec();
313 			heads = heads.setForceUpdate(true);
314 			final String dst = (bare ? Constants.R_HEADS
315 					: Constants.R_REMOTES + remoteName + '/') + '*';
316 			heads = heads.setSourceDestination(Constants.R_HEADS + '*', dst);
317 			if (type == FETCH_TYPE.MULTIPLE_BRANCHES) {
318 				RefSpec tags = new RefSpec().setForceUpdate(true)
319 						.setSourceDestination(Constants.R_TAGS + '*',
320 								Constants.R_TAGS + '*');
321 				for (String selectedRef : branchesToClone) {
322 					if (heads.matchSource(selectedRef)) {
323 						specs.add(heads.expandFromSource(selectedRef));
324 					} else if (tags.matchSource(selectedRef)) {
325 						specs.add(tags.expandFromSource(selectedRef));
326 					}
327 				}
328 			} else {
329 				// We'll fetch the tags anyway.
330 				specs.add(heads);
331 			}
332 		}
333 		return specs;
334 	}
335 
336 	private void checkout(Repository clonedRepo, FetchResult result)
337 			throws MissingObjectException, IncorrectObjectTypeException,
338 			IOException, GitAPIException {
339 
340 		Ref head = null;
341 		if (branch.equals(Constants.HEAD)) {
342 			Ref foundBranch = findBranchToCheckout(result);
343 			if (foundBranch != null)
344 				head = foundBranch;
345 		}
346 		if (head == null) {
347 			head = result.getAdvertisedRef(branch);
348 			if (head == null)
349 				head = result.getAdvertisedRef(Constants.R_HEADS + branch);
350 			if (head == null)
351 				head = result.getAdvertisedRef(Constants.R_TAGS + branch);
352 		}
353 
354 		if (head == null || head.getObjectId() == null)
355 			return; // TODO throw exception?
356 
357 		if (head.getName().startsWith(Constants.R_HEADS)) {
358 			final RefUpdate newHead = clonedRepo.updateRef(Constants.HEAD);
359 			newHead.disableRefLog();
360 			newHead.link(head.getName());
361 			addMergeConfig(clonedRepo, head);
362 		}
363 
364 		final RevCommit commit = parseCommit(clonedRepo, head);
365 
366 		boolean detached = !head.getName().startsWith(Constants.R_HEADS);
367 		RefUpdate u = clonedRepo.updateRef(Constants.HEAD, detached);
368 		u.setNewObjectId(commit.getId());
369 		u.forceUpdate();
370 
371 		if (!bare) {
372 			DirCache dc = clonedRepo.lockDirCache();
373 			DirCacheCheckout co = new DirCacheCheckout(clonedRepo, dc,
374 					commit.getTree());
375 			co.setProgressMonitor(monitor);
376 			co.checkout();
377 			if (cloneSubmodules)
378 				cloneSubmodules(clonedRepo);
379 		}
380 	}
381 
382 	private void cloneSubmodules(Repository clonedRepo) throws IOException,
383 			GitAPIException {
384 		SubmoduleInitCommand init = new SubmoduleInitCommand(clonedRepo);
385 		Collection<String> submodules = init.call();
386 		if (submodules.isEmpty()) {
387 			return;
388 		}
389 		if (callback != null) {
390 			callback.initializedSubmodules(submodules);
391 		}
392 
393 		SubmoduleUpdateCommand update = new SubmoduleUpdateCommand(clonedRepo);
394 		configure(update);
395 		update.setProgressMonitor(monitor);
396 		update.setCallback(callback);
397 		if (!update.call().isEmpty()) {
398 			SubmoduleWalk walk = SubmoduleWalk.forIndex(clonedRepo);
399 			while (walk.next()) {
400 				try (Repository subRepo = walk.getRepository()) {
401 					if (subRepo != null) {
402 						cloneSubmodules(subRepo);
403 					}
404 				}
405 			}
406 		}
407 	}
408 
409 	private Ref findBranchToCheckout(FetchResult result) {
410 		final Ref idHEAD = result.getAdvertisedRef(Constants.HEAD);
411 		ObjectId headId = idHEAD != null ? idHEAD.getObjectId() : null;
412 		if (headId == null) {
413 			return null;
414 		}
415 
416 		if (idHEAD != null && idHEAD.isSymbolic()) {
417 			return idHEAD.getTarget();
418 		}
419 
420 		Ref master = result.getAdvertisedRef(Constants.R_HEADS
421 				+ Constants.MASTER);
422 		ObjectId objectId = master != null ? master.getObjectId() : null;
423 		if (headId.equals(objectId)) {
424 			return master;
425 		}
426 
427 		Ref foundBranch = null;
428 		for (Ref r : result.getAdvertisedRefs()) {
429 			final String n = r.getName();
430 			if (!n.startsWith(Constants.R_HEADS))
431 				continue;
432 			if (headId.equals(r.getObjectId())) {
433 				foundBranch = r;
434 				break;
435 			}
436 		}
437 		return foundBranch;
438 	}
439 
440 	private void addMergeConfig(Repository clonedRepo, Ref head)
441 			throws IOException {
442 		String branchName = Repository.shortenRefName(head.getName());
443 		clonedRepo.getConfig().setString(ConfigConstants.CONFIG_BRANCH_SECTION,
444 				branchName, ConfigConstants.CONFIG_KEY_REMOTE, remote);
445 		clonedRepo.getConfig().setString(ConfigConstants.CONFIG_BRANCH_SECTION,
446 				branchName, ConfigConstants.CONFIG_KEY_MERGE, head.getName());
447 		String autosetupRebase = clonedRepo.getConfig().getString(
448 				ConfigConstants.CONFIG_BRANCH_SECTION, null,
449 				ConfigConstants.CONFIG_KEY_AUTOSETUPREBASE);
450 		if (ConfigConstants.CONFIG_KEY_ALWAYS.equals(autosetupRebase)
451 				|| ConfigConstants.CONFIG_KEY_REMOTE.equals(autosetupRebase))
452 			clonedRepo.getConfig().setEnum(
453 					ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
454 					ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
455 		clonedRepo.getConfig().save();
456 	}
457 
458 	private RevCommit parseCommit(Repository clonedRepo, Ref ref)
459 			throws MissingObjectException, IncorrectObjectTypeException,
460 			IOException {
461 		final RevCommit commit;
462 		try (RevWalkRevWalk.html#RevWalk">RevWalk rw = new RevWalk(clonedRepo)) {
463 			commit = rw.parseCommit(ref.getObjectId());
464 		}
465 		return commit;
466 	}
467 
468 	/**
469 	 * Set the URI to clone from
470 	 *
471 	 * @param uri
472 	 *            the URI to clone from, or {@code null} to unset the URI. The
473 	 *            URI must be set before {@link #call} is called.
474 	 * @return this instance
475 	 */
476 	public CloneCommand setURI(String uri) {
477 		this.uri = uri;
478 		return this;
479 	}
480 
481 	/**
482 	 * The optional directory associated with the clone operation. If the
483 	 * directory isn't set, a name associated with the source uri will be used.
484 	 *
485 	 * @see URIish#getHumanishName()
486 	 * @param directory
487 	 *            the directory to clone to, or {@code null} if the directory
488 	 *            name should be taken from the source uri
489 	 * @return this instance
490 	 * @throws java.lang.IllegalStateException
491 	 *             if the combination of directory, gitDir and bare is illegal.
492 	 *             E.g. if for a non-bare repository directory and gitDir point
493 	 *             to the same directory of if for a bare repository both
494 	 *             directory and gitDir are specified
495 	 */
496 	public CloneCommand setDirectory(File directory) {
497 		validateDirs(directory, gitDir, bare);
498 		this.directory = directory;
499 		return this;
500 	}
501 
502 	/**
503 	 * Set the repository meta directory (.git)
504 	 *
505 	 * @param gitDir
506 	 *            the repository meta directory, or {@code null} to choose one
507 	 *            automatically at clone time
508 	 * @return this instance
509 	 * @throws java.lang.IllegalStateException
510 	 *             if the combination of directory, gitDir and bare is illegal.
511 	 *             E.g. if for a non-bare repository directory and gitDir point
512 	 *             to the same directory of if for a bare repository both
513 	 *             directory and gitDir are specified
514 	 * @since 3.6
515 	 */
516 	public CloneCommand setGitDir(File gitDir) {
517 		validateDirs(directory, gitDir, bare);
518 		this.gitDir = gitDir;
519 		return this;
520 	}
521 
522 	/**
523 	 * Set whether the cloned repository shall be bare
524 	 *
525 	 * @param bare
526 	 *            whether the cloned repository is bare or not
527 	 * @return this instance
528 	 * @throws java.lang.IllegalStateException
529 	 *             if the combination of directory, gitDir and bare is illegal.
530 	 *             E.g. if for a non-bare repository directory and gitDir point
531 	 *             to the same directory of if for a bare repository both
532 	 *             directory and gitDir are specified
533 	 */
534 	public CloneCommand setBare(boolean bare) throws IllegalStateException {
535 		validateDirs(directory, gitDir, bare);
536 		this.bare = bare;
537 		return this;
538 	}
539 
540 	/**
541 	 * Set the file system abstraction to be used for repositories created by
542 	 * this command.
543 	 *
544 	 * @param fs
545 	 *            the abstraction.
546 	 * @return {@code this} (for chaining calls).
547 	 * @since 4.10
548 	 */
549 	public CloneCommand setFs(FS fs) {
550 		this.fs = fs;
551 		return this;
552 	}
553 
554 	/**
555 	 * The remote name used to keep track of the upstream repository for the
556 	 * clone operation. If no remote name is set, the default value of
557 	 * <code>Constants.DEFAULT_REMOTE_NAME</code> will be used.
558 	 *
559 	 * @see Constants#DEFAULT_REMOTE_NAME
560 	 * @param remote
561 	 *            name that keeps track of the upstream repository.
562 	 *            {@code null} means to use DEFAULT_REMOTE_NAME.
563 	 * @return this instance
564 	 */
565 	public CloneCommand setRemote(String remote) {
566 		if (remote == null) {
567 			remote = Constants.DEFAULT_REMOTE_NAME;
568 		}
569 		this.remote = remote;
570 		return this;
571 	}
572 
573 	/**
574 	 * Set the initial branch
575 	 *
576 	 * @param branch
577 	 *            the initial branch to check out when cloning the repository.
578 	 *            Can be specified as ref name (<code>refs/heads/master</code>),
579 	 *            branch name (<code>master</code>) or tag name
580 	 *            (<code>v1.2.3</code>). The default is to use the branch
581 	 *            pointed to by the cloned repository's HEAD and can be
582 	 *            requested by passing {@code null} or <code>HEAD</code>.
583 	 * @return this instance
584 	 */
585 	public CloneCommand setBranch(String branch) {
586 		if (branch == null) {
587 			branch = Constants.HEAD;
588 		}
589 		this.branch = branch;
590 		return this;
591 	}
592 
593 	/**
594 	 * The progress monitor associated with the clone operation. By default,
595 	 * this is set to <code>NullProgressMonitor</code>
596 	 *
597 	 * @see NullProgressMonitor
598 	 * @param monitor
599 	 *            a {@link org.eclipse.jgit.lib.ProgressMonitor}
600 	 * @return {@code this}
601 	 */
602 	public CloneCommand setProgressMonitor(ProgressMonitor monitor) {
603 		if (monitor == null) {
604 			monitor = NullProgressMonitor.INSTANCE;
605 		}
606 		this.monitor = monitor;
607 		return this;
608 	}
609 
610 	/**
611 	 * Set whether all branches have to be fetched.
612 	 * <p>
613 	 * If {@code false}, use {@link #setBranchesToClone(Collection)} to define
614 	 * what will be cloned. If neither are set, all branches will be cloned.
615 	 * </p>
616 	 *
617 	 * @param cloneAllBranches
618 	 *            {@code true} when all branches have to be fetched (indicates
619 	 *            wildcard in created fetch refspec), {@code false} otherwise.
620 	 * @return {@code this}
621 	 */
622 	public CloneCommand setCloneAllBranches(boolean cloneAllBranches) {
623 		this.cloneAllBranches = cloneAllBranches;
624 		return this;
625 	}
626 
627 	/**
628 	 * Set up a mirror of the source repository. This implies that a bare
629 	 * repository will be created. Compared to {@link #setBare},
630 	 * {@code #setMirror} not only maps local branches of the source to local
631 	 * branches of the target, it maps all refs (including remote-tracking
632 	 * branches, notes etc.) and sets up a refspec configuration such that all
633 	 * these refs are overwritten by a git remote update in the target
634 	 * repository.
635 	 *
636 	 * @param mirror
637 	 *            whether to mirror all refs from the source repository
638 	 *
639 	 * @return {@code this}
640 	 * @since 5.6
641 	 */
642 	public CloneCommand setMirror(boolean mirror) {
643 		this.mirror = mirror;
644 		return this;
645 	}
646 
647 	/**
648 	 * Set whether to clone submodules
649 	 *
650 	 * @param cloneSubmodules
651 	 *            true to initialize and update submodules. Ignored when
652 	 *            {@link #setBare(boolean)} is set to true.
653 	 * @return {@code this}
654 	 */
655 	public CloneCommand setCloneSubmodules(boolean cloneSubmodules) {
656 		this.cloneSubmodules = cloneSubmodules;
657 		return this;
658 	}
659 
660 	/**
661 	 * Set the branches or tags to clone.
662 	 * <p>
663 	 * This is ignored if {@link #setCloneAllBranches(boolean)
664 	 * setCloneAllBranches(true)} or {@link #setMirror(boolean) setMirror(true)}
665 	 * is used. If {@code branchesToClone} is {@code null} or empty, it's also
666 	 * ignored.
667 	 * </p>
668 	 *
669 	 * @param branchesToClone
670 	 *            collection of branches to clone. Must be specified as full ref
671 	 *            names (e.g. {@code refs/heads/master} or
672 	 *            {@code refs/tags/v1.0.0}).
673 	 * @return {@code this}
674 	 */
675 	public CloneCommand setBranchesToClone(Collection<String> branchesToClone) {
676 		this.branchesToClone = branchesToClone;
677 		return this;
678 	}
679 
680 	/**
681 	 * Set the tag option used for the remote configuration explicitly.
682 	 *
683 	 * @param tagOption
684 	 *            tag option to be used for the remote config
685 	 * @return {@code this}
686 	 * @since 5.8
687 	 */
688 	public CloneCommand setTagOption(TagOpt tagOption) {
689 		this.tagOption = tagOption;
690 		return this;
691 	}
692 
693 	/**
694 	 * Set the --no-tags option. Tags are not cloned now and the remote
695 	 * configuration is initialized with the --no-tags option as well.
696 	 *
697 	 * @return {@code this}
698 	 * @since 5.8
699 	 */
700 	public CloneCommand setNoTags() {
701 		return setTagOption(TagOpt.NO_TAGS);
702 	}
703 
704 	/**
705 	 * Set whether to skip checking out a branch
706 	 *
707 	 * @param noCheckout
708 	 *            if set to <code>true</code> no branch will be checked out
709 	 *            after the clone. This enhances performance of the clone
710 	 *            command when there is no need for a checked out branch.
711 	 * @return {@code this}
712 	 */
713 	public CloneCommand setNoCheckout(boolean noCheckout) {
714 		this.noCheckout = noCheckout;
715 		return this;
716 	}
717 
718 	/**
719 	 * Register a progress callback.
720 	 *
721 	 * @param callback
722 	 *            the callback
723 	 * @return {@code this}
724 	 * @since 4.8
725 	 */
726 	public CloneCommand setCallback(Callback callback) {
727 		this.callback = callback;
728 		return this;
729 	}
730 
731 	private static void validateDirs(File directory, File gitDir, boolean bare)
732 			throws IllegalStateException {
733 		if (directory != null) {
734 			if (directory.exists() && !directory.isDirectory()) {
735 				throw new IllegalStateException(MessageFormat.format(
736 						JGitText.get().initFailedDirIsNoDirectory, directory));
737 			}
738 			if (gitDir != null && gitDir.exists() && !gitDir.isDirectory()) {
739 				throw new IllegalStateException(MessageFormat.format(
740 						JGitText.get().initFailedGitDirIsNoDirectory,
741 						gitDir));
742 			}
743 			if (bare) {
744 				if (gitDir != null && !gitDir.equals(directory))
745 					throw new IllegalStateException(MessageFormat.format(
746 							JGitText.get().initFailedBareRepoDifferentDirs,
747 							gitDir, directory));
748 			} else {
749 				if (gitDir != null && gitDir.equals(directory))
750 					throw new IllegalStateException(MessageFormat.format(
751 							JGitText.get().initFailedNonBareRepoSameDirs,
752 							gitDir, directory));
753 			}
754 		}
755 	}
756 
757 	private void cleanup() {
758 		try {
759 			if (directory != null) {
760 				if (!directoryExistsInitially) {
761 					FileUtils.delete(directory, FileUtils.RECURSIVE
762 							| FileUtils.SKIP_MISSING | FileUtils.IGNORE_ERRORS);
763 				} else {
764 					deleteChildren(directory);
765 				}
766 			}
767 			if (gitDir != null) {
768 				if (!gitDirExistsInitially) {
769 					FileUtils.delete(gitDir, FileUtils.RECURSIVE
770 							| FileUtils.SKIP_MISSING | FileUtils.IGNORE_ERRORS);
771 				} else {
772 					deleteChildren(gitDir);
773 				}
774 			}
775 		} catch (IOException e) {
776 			// Ignore; this is a best-effort cleanup in error cases, and
777 			// IOException should not be raised anyway
778 		}
779 	}
780 
781 	private void deleteChildren(File file) throws IOException {
782 		File[] files = file.listFiles();
783 		if (files == null) {
784 			return;
785 		}
786 		for (File child : files) {
787 			FileUtils.delete(child, FileUtils.RECURSIVE | FileUtils.SKIP_MISSING
788 					| FileUtils.IGNORE_ERRORS);
789 		}
790 	}
791 }