View Javadoc
1   /*
2    * Copyright (C) 2011, 2013 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.File;
46  import java.io.IOException;
47  import java.net.URISyntaxException;
48  import java.text.MessageFormat;
49  import java.util.ArrayList;
50  import java.util.Collection;
51  import java.util.List;
52  
53  import org.eclipse.jgit.api.errors.GitAPIException;
54  import org.eclipse.jgit.api.errors.InvalidRemoteException;
55  import org.eclipse.jgit.api.errors.JGitInternalException;
56  import org.eclipse.jgit.dircache.DirCache;
57  import org.eclipse.jgit.dircache.DirCacheCheckout;
58  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
59  import org.eclipse.jgit.errors.MissingObjectException;
60  import org.eclipse.jgit.internal.JGitText;
61  import org.eclipse.jgit.lib.ConfigConstants;
62  import org.eclipse.jgit.lib.Constants;
63  import org.eclipse.jgit.lib.NullProgressMonitor;
64  import org.eclipse.jgit.lib.ProgressMonitor;
65  import org.eclipse.jgit.lib.Ref;
66  import org.eclipse.jgit.lib.RefUpdate;
67  import org.eclipse.jgit.lib.Repository;
68  import org.eclipse.jgit.revwalk.RevCommit;
69  import org.eclipse.jgit.revwalk.RevWalk;
70  import org.eclipse.jgit.submodule.SubmoduleWalk;
71  import org.eclipse.jgit.transport.FetchResult;
72  import org.eclipse.jgit.transport.RefSpec;
73  import org.eclipse.jgit.transport.RemoteConfig;
74  import org.eclipse.jgit.transport.TagOpt;
75  import org.eclipse.jgit.transport.URIish;
76  
77  /**
78   * Clone a repository into a new working directory
79   *
80   * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-clone.html"
81   *      >Git documentation about Clone</a>
82   */
83  public class CloneCommand extends TransportCommand<CloneCommand, Git> {
84  
85  	private String uri;
86  
87  	private File directory;
88  
89  	private File gitDir;
90  
91  	private boolean bare;
92  
93  	private String remote = Constants.DEFAULT_REMOTE_NAME;
94  
95  	private String branch = Constants.HEAD;
96  
97  	private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
98  
99  	private boolean cloneAllBranches;
100 
101 	private boolean cloneSubmodules;
102 
103 	private boolean noCheckout;
104 
105 	private Collection<String> branchesToClone;
106 
107 	/**
108 	 * Create clone command with no repository set
109 	 */
110 	public CloneCommand() {
111 		super(null);
112 	}
113 
114 	/**
115 	 * Executes the {@code Clone} command.
116 	 *
117 	 * The Git instance returned by this command needs to be closed by the
118 	 * caller to free resources held by the underlying {@link Repository}
119 	 * instance. It is recommended to call this method as soon as you don't need
120 	 * a reference to this {@link Git} instance and the underlying
121 	 * {@link Repository} instance anymore.
122 	 *
123 	 * @return the newly created {@code Git} object with associated repository
124 	 * @throws InvalidRemoteException
125 	 * @throws org.eclipse.jgit.api.errors.TransportException
126 	 * @throws GitAPIException
127 	 */
128 	public Git call() throws GitAPIException, InvalidRemoteException,
129 			org.eclipse.jgit.api.errors.TransportException {
130 		Repository repository = null;
131 		try {
132 			URIish u = new URIish(uri);
133 			repository = init(u);
134 			FetchResult result = fetch(repository, u);
135 			if (!noCheckout)
136 				checkout(repository, result);
137 			return new Git(repository, true);
138 		} catch (IOException ioe) {
139 			if (repository != null) {
140 				repository.close();
141 			}
142 			throw new JGitInternalException(ioe.getMessage(), ioe);
143 		} catch (URISyntaxException e) {
144 			if (repository != null) {
145 				repository.close();
146 			}
147 			throw new InvalidRemoteException(MessageFormat.format(
148 					JGitText.get().invalidRemote, remote));
149 		}
150 	}
151 
152 	private Repository init(URIish u) throws GitAPIException {
153 		InitCommand command = Git.init();
154 		command.setBare(bare);
155 		if (directory == null && gitDir == null)
156 			directory = new File(u.getHumanishName(), Constants.DOT_GIT);
157 		if (directory != null && directory.exists()
158 				&& directory.listFiles().length != 0)
159 			throw new JGitInternalException(MessageFormat.format(
160 					JGitText.get().cloneNonEmptyDirectory, directory.getName()));
161 		if (gitDir != null && gitDir.exists() && gitDir.listFiles().length != 0)
162 			throw new JGitInternalException(MessageFormat.format(
163 					JGitText.get().cloneNonEmptyDirectory, gitDir.getName()));
164 		if (directory != null)
165 			command.setDirectory(directory);
166 		if (gitDir != null)
167 			command.setGitDir(gitDir);
168 		return command.call().getRepository();
169 	}
170 
171 	private FetchResult fetch(Repository clonedRepo, URIish u)
172 			throws URISyntaxException,
173 			org.eclipse.jgit.api.errors.TransportException, IOException,
174 			GitAPIException {
175 		// create the remote config and save it
176 		RemoteConfig config = new RemoteConfig(clonedRepo.getConfig(), remote);
177 		config.addURI(u);
178 
179 		final String dst = (bare ? Constants.R_HEADS : Constants.R_REMOTES
180 				+ config.getName() + "/") + "*"; //$NON-NLS-1$//$NON-NLS-2$
181 		RefSpec refSpec = new RefSpec();
182 		refSpec = refSpec.setForceUpdate(true);
183 		refSpec = refSpec.setSourceDestination(Constants.R_HEADS + "*", dst); //$NON-NLS-1$
184 
185 		config.addFetchRefSpec(refSpec);
186 		config.update(clonedRepo.getConfig());
187 
188 		clonedRepo.getConfig().save();
189 
190 		// run the fetch command
191 		FetchCommand command = new FetchCommand(clonedRepo);
192 		command.setRemote(remote);
193 		command.setProgressMonitor(monitor);
194 		command.setTagOpt(TagOpt.FETCH_TAGS);
195 		configure(command);
196 
197 		List<RefSpec> specs = calculateRefSpecs(dst);
198 		command.setRefSpecs(specs);
199 
200 		return command.call();
201 	}
202 
203 	private List<RefSpec> calculateRefSpecs(final String dst) {
204 		RefSpec wcrs = new RefSpec();
205 		wcrs = wcrs.setForceUpdate(true);
206 		wcrs = wcrs.setSourceDestination(Constants.R_HEADS + "*", dst); //$NON-NLS-1$
207 		List<RefSpec> specs = new ArrayList<RefSpec>();
208 		if (cloneAllBranches)
209 			specs.add(wcrs);
210 		else if (branchesToClone != null
211 				&& branchesToClone.size() > 0) {
212 			for (final String selectedRef : branchesToClone)
213 				if (wcrs.matchSource(selectedRef))
214 					specs.add(wcrs.expandFromSource(selectedRef));
215 		}
216 		return specs;
217 	}
218 
219 	private void checkout(Repository clonedRepo, FetchResult result)
220 			throws MissingObjectException, IncorrectObjectTypeException,
221 			IOException, GitAPIException {
222 
223 		Ref head = null;
224 		if (branch.equals(Constants.HEAD)) {
225 			Ref foundBranch = findBranchToCheckout(result);
226 			if (foundBranch != null)
227 				head = foundBranch;
228 		}
229 		if (head == null) {
230 			head = result.getAdvertisedRef(branch);
231 			if (head == null)
232 				head = result.getAdvertisedRef(Constants.R_HEADS + branch);
233 			if (head == null)
234 				head = result.getAdvertisedRef(Constants.R_TAGS + branch);
235 		}
236 
237 		if (head == null || head.getObjectId() == null)
238 			return; // throw exception?
239 
240 		if (head.getName().startsWith(Constants.R_HEADS)) {
241 			final RefUpdate newHead = clonedRepo.updateRef(Constants.HEAD);
242 			newHead.disableRefLog();
243 			newHead.link(head.getName());
244 			addMergeConfig(clonedRepo, head);
245 		}
246 
247 		final RevCommit commit = parseCommit(clonedRepo, head);
248 
249 		boolean detached = !head.getName().startsWith(Constants.R_HEADS);
250 		RefUpdate u = clonedRepo.updateRef(Constants.HEAD, detached);
251 		u.setNewObjectId(commit.getId());
252 		u.forceUpdate();
253 
254 		if (!bare) {
255 			DirCache dc = clonedRepo.lockDirCache();
256 			DirCacheCheckout co = new DirCacheCheckout(clonedRepo, dc,
257 					commit.getTree());
258 			co.checkout();
259 			if (cloneSubmodules)
260 				cloneSubmodules(clonedRepo);
261 		}
262 	}
263 
264 	private void cloneSubmodules(Repository clonedRepo) throws IOException,
265 			GitAPIException {
266 		SubmoduleInitCommand init = new SubmoduleInitCommand(clonedRepo);
267 		if (init.call().isEmpty())
268 			return;
269 
270 		SubmoduleUpdateCommand update = new SubmoduleUpdateCommand(clonedRepo);
271 		configure(update);
272 		update.setProgressMonitor(monitor);
273 		if (!update.call().isEmpty()) {
274 			SubmoduleWalk walk = SubmoduleWalk.forIndex(clonedRepo);
275 			while (walk.next()) {
276 				Repository subRepo = walk.getRepository();
277 				if (subRepo != null) {
278 					try {
279 						cloneSubmodules(subRepo);
280 					} finally {
281 						subRepo.close();
282 					}
283 				}
284 			}
285 		}
286 	}
287 
288 	private Ref findBranchToCheckout(FetchResult result) {
289 		final Ref idHEAD = result.getAdvertisedRef(Constants.HEAD);
290 		if (idHEAD == null)
291 			return null;
292 
293 		Ref master = result.getAdvertisedRef(Constants.R_HEADS
294 				+ Constants.MASTER);
295 		if (master != null && master.getObjectId().equals(idHEAD.getObjectId()))
296 			return master;
297 
298 		Ref foundBranch = null;
299 		for (final Ref r : result.getAdvertisedRefs()) {
300 			final String n = r.getName();
301 			if (!n.startsWith(Constants.R_HEADS))
302 				continue;
303 			if (r.getObjectId().equals(idHEAD.getObjectId())) {
304 				foundBranch = r;
305 				break;
306 			}
307 		}
308 		return foundBranch;
309 	}
310 
311 	private void addMergeConfig(Repository clonedRepo, Ref head)
312 			throws IOException {
313 		String branchName = Repository.shortenRefName(head.getName());
314 		clonedRepo.getConfig().setString(ConfigConstants.CONFIG_BRANCH_SECTION,
315 				branchName, ConfigConstants.CONFIG_KEY_REMOTE, remote);
316 		clonedRepo.getConfig().setString(ConfigConstants.CONFIG_BRANCH_SECTION,
317 				branchName, ConfigConstants.CONFIG_KEY_MERGE, head.getName());
318 		String autosetupRebase = clonedRepo.getConfig().getString(
319 				ConfigConstants.CONFIG_BRANCH_SECTION, null,
320 				ConfigConstants.CONFIG_KEY_AUTOSETUPREBASE);
321 		if (ConfigConstants.CONFIG_KEY_ALWAYS.equals(autosetupRebase)
322 				|| ConfigConstants.CONFIG_KEY_REMOTE.equals(autosetupRebase))
323 			clonedRepo.getConfig().setBoolean(
324 					ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
325 					ConfigConstants.CONFIG_KEY_REBASE, true);
326 		clonedRepo.getConfig().save();
327 	}
328 
329 	private RevCommit parseCommit(final Repository clonedRepo, final Ref ref)
330 			throws MissingObjectException, IncorrectObjectTypeException,
331 			IOException {
332 		final RevCommit commit;
333 		try (final RevWalk rw = new RevWalk(clonedRepo)) {
334 			commit = rw.parseCommit(ref.getObjectId());
335 		}
336 		return commit;
337 	}
338 
339 	/**
340 	 * @param uri
341 	 *            the URI to clone from, or {@code null} to unset the URI.
342 	 *            The URI must be set before {@link #call} is called.
343 	 * @return this instance
344 	 */
345 	public CloneCommand setURI(String uri) {
346 		this.uri = uri;
347 		return this;
348 	}
349 
350 	/**
351 	 * The optional directory associated with the clone operation. If the
352 	 * directory isn't set, a name associated with the source uri will be used.
353 	 *
354 	 * @see URIish#getHumanishName()
355 	 *
356 	 * @param directory
357 	 *            the directory to clone to, or {@code null} if the directory
358 	 *            name should be taken from the source uri
359 	 * @return this instance
360 	 * @throws IllegalStateException
361 	 *             if the combination of directory, gitDir and bare is illegal.
362 	 *             E.g. if for a non-bare repository directory and gitDir point
363 	 *             to the same directory of if for a bare repository both
364 	 *             directory and gitDir are specified
365 	 */
366 	public CloneCommand setDirectory(File directory) {
367 		validateDirs(directory, gitDir, bare);
368 		this.directory = directory;
369 		return this;
370 	}
371 
372 	/**
373 	 * @param gitDir
374 	 *            the repository meta directory, or {@code null} to choose one
375 	 *            automatically at clone time
376 	 * @return this instance
377 	 * @throws IllegalStateException
378 	 *             if the combination of directory, gitDir and bare is illegal.
379 	 *             E.g. if for a non-bare repository directory and gitDir point
380 	 *             to the same directory of if for a bare repository both
381 	 *             directory and gitDir are specified
382 	 * @since 3.6
383 	 */
384 	public CloneCommand setGitDir(File gitDir) {
385 		validateDirs(directory, gitDir, bare);
386 		this.gitDir = gitDir;
387 		return this;
388 	}
389 
390 	/**
391 	 * @param bare
392 	 *            whether the cloned repository is bare or not
393 	 * @return this instance
394 	 * @throws IllegalStateException
395 	 *             if the combination of directory, gitDir and bare is illegal.
396 	 *             E.g. if for a non-bare repository directory and gitDir point
397 	 *             to the same directory of if for a bare repository both
398 	 *             directory and gitDir are specified
399 	 */
400 	public CloneCommand setBare(boolean bare) throws IllegalStateException {
401 		validateDirs(directory, gitDir, bare);
402 		this.bare = bare;
403 		return this;
404 	}
405 
406 	/**
407 	 * The remote name used to keep track of the upstream repository for the
408 	 * clone operation. If no remote name is set, the default value of
409 	 * <code>Constants.DEFAULT_REMOTE_NAME</code> will be used.
410 	 *
411 	 * @see Constants#DEFAULT_REMOTE_NAME
412 	 * @param remote
413 	 *            name that keeps track of the upstream repository.
414 	 *            {@code null} means to use DEFAULT_REMOTE_NAME.
415 	 * @return this instance
416 	 */
417 	public CloneCommand setRemote(String remote) {
418 		if (remote == null) {
419 			remote = Constants.DEFAULT_REMOTE_NAME;
420 		}
421 		this.remote = remote;
422 		return this;
423 	}
424 
425 	/**
426 	 * @param branch
427 	 *            the initial branch to check out when cloning the repository.
428 	 *            Can be specified as ref name (<code>refs/heads/master</code>),
429 	 *            branch name (<code>master</code>) or tag name (<code>v1.2.3</code>).
430 	 *            The default is to use the branch pointed to by the cloned
431 	 *            repository's HEAD and can be requested by passing {@code null}
432 	 *            or <code>HEAD</code>.
433 	 * @return this instance
434 	 */
435 	public CloneCommand setBranch(String branch) {
436 		if (branch == null) {
437 			branch = Constants.HEAD;
438 		}
439 		this.branch = branch;
440 		return this;
441 	}
442 
443 	/**
444 	 * The progress monitor associated with the clone operation. By default,
445 	 * this is set to <code>NullProgressMonitor</code>
446 	 *
447 	 * @see NullProgressMonitor
448 	 *
449 	 * @param monitor
450 	 * @return {@code this}
451 	 */
452 	public CloneCommand setProgressMonitor(ProgressMonitor monitor) {
453 		if (monitor == null) {
454 			monitor = NullProgressMonitor.INSTANCE;
455 		}
456 		this.monitor = monitor;
457 		return this;
458 	}
459 
460 	/**
461 	 * @param cloneAllBranches
462 	 *            true when all branches have to be fetched (indicates wildcard
463 	 *            in created fetch refspec), false otherwise.
464 	 * @return {@code this}
465 	 */
466 	public CloneCommand setCloneAllBranches(boolean cloneAllBranches) {
467 		this.cloneAllBranches = cloneAllBranches;
468 		return this;
469 	}
470 
471 	/**
472 	 * @param cloneSubmodules
473 	 *            true to initialize and update submodules. Ignored when
474 	 *            {@link #setBare(boolean)} is set to true.
475 	 * @return {@code this}
476 	 */
477 	public CloneCommand setCloneSubmodules(boolean cloneSubmodules) {
478 		this.cloneSubmodules = cloneSubmodules;
479 		return this;
480 	}
481 
482 	/**
483 	 * @param branchesToClone
484 	 *            collection of branches to clone. Ignored when allSelected is
485 	 *            true. Must be specified as full ref names (e.g.
486 	 *            <code>refs/heads/master</code>).
487 	 * @return {@code this}
488 	 */
489 	public CloneCommand setBranchesToClone(Collection<String> branchesToClone) {
490 		this.branchesToClone = branchesToClone;
491 		return this;
492 	}
493 
494 	/**
495 	 * @param noCheckout
496 	 *            if set to <code>true</code> no branch will be checked out
497 	 *            after the clone. This enhances performance of the clone
498 	 *            command when there is no need for a checked out branch.
499 	 * @return {@code this}
500 	 */
501 	public CloneCommand setNoCheckout(boolean noCheckout) {
502 		this.noCheckout = noCheckout;
503 		return this;
504 	}
505 
506 	private static void validateDirs(File directory, File gitDir, boolean bare)
507 			throws IllegalStateException {
508 		if (directory != null) {
509 			if (bare) {
510 				if (gitDir != null && !gitDir.equals(directory))
511 					throw new IllegalStateException(MessageFormat.format(
512 							JGitText.get().initFailedBareRepoDifferentDirs,
513 							gitDir, directory));
514 			} else {
515 				if (gitDir != null && gitDir.equals(directory))
516 					throw new IllegalStateException(MessageFormat.format(
517 							JGitText.get().initFailedNonBareRepoSameDirs,
518 							gitDir, directory));
519 			}
520 		}
521 	}
522 }