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