2 * Copyright (C) 2008-2010, Google Inc.
3 * and other copyright owners as documented in the project's IP log.
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
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
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.
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
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.
44 package org
.eclipse
.jgit
.pgm
;
47 import java
.io
.IOException
;
48 import java
.net
.URISyntaxException
;
49 import java
.text
.MessageFormat
;
50 import java
.util
.ArrayList
;
51 import java
.util
.Collections
;
52 import java
.util
.List
;
54 import org
.kohsuke
.args4j
.Argument
;
55 import org
.kohsuke
.args4j
.Option
;
56 import org
.eclipse
.jgit
.errors
.NotSupportedException
;
57 import org
.eclipse
.jgit
.errors
.TransportException
;
58 import org
.eclipse
.jgit
.lib
.Commit
;
59 import org
.eclipse
.jgit
.lib
.Constants
;
60 import org
.eclipse
.jgit
.lib
.GitIndex
;
61 import org
.eclipse
.jgit
.lib
.Ref
;
62 import org
.eclipse
.jgit
.lib
.RefComparator
;
63 import org
.eclipse
.jgit
.lib
.RefUpdate
;
64 import org
.eclipse
.jgit
.lib
.TextProgressMonitor
;
65 import org
.eclipse
.jgit
.lib
.Tree
;
66 import org
.eclipse
.jgit
.lib
.WorkDirCheckout
;
67 import org
.eclipse
.jgit
.storage
.file
.FileRepository
;
68 import org
.eclipse
.jgit
.transport
.FetchResult
;
69 import org
.eclipse
.jgit
.transport
.RefSpec
;
70 import org
.eclipse
.jgit
.transport
.RemoteConfig
;
71 import org
.eclipse
.jgit
.transport
.Transport
;
72 import org
.eclipse
.jgit
.transport
.URIish
;
74 @Command(common
= true, usage
= "usage_cloneRepositoryIntoNewDir")
75 class Clone
extends AbstractFetchCommand
{
76 @Option(name
= "--origin", aliases
= { "-o" }, metaVar
= "metaVar_remoteName", usage
= "usage_useNameInsteadOfOriginToTrackUpstream")
77 private String remoteName
= Constants
.DEFAULT_REMOTE_NAME
;
79 @Argument(index
= 0, required
= true, metaVar
= "metaVar_uriish")
80 private String sourceUri
;
82 @Argument(index
= 1, metaVar
= "metaVar_directory")
83 private String localName
;
85 private FileRepository dst
;
88 protected final boolean requiresRepository() {
93 protected void run() throws Exception
{
94 if (localName
!= null && gitdir
!= null)
95 throw die(CLIText
.get().conflictingUsageOf_git_dir_andArguments
);
97 final URIish uri
= new URIish(sourceUri
);
98 if (localName
== null) {
100 localName
= uri
.getHumanishName();
101 } catch (IllegalArgumentException e
) {
102 throw die(MessageFormat
.format(CLIText
.get().cannotGuessLocalNameFrom
, sourceUri
));
106 gitdir
= new File(localName
, Constants
.DOT_GIT
);
108 dst
= new FileRepository(gitdir
);
110 dst
.getConfig().setBoolean("core", null, "bare", false);
111 dst
.getConfig().save();
114 out
.format(CLIText
.get().initializedEmptyGitRepositoryIn
, gitdir
.getAbsolutePath());
119 final FetchResult r
= runFetch();
120 final Ref branch
= guessHEAD(r
);
124 private void saveRemote(final URIish uri
) throws URISyntaxException
,
126 final RemoteConfig rc
= new RemoteConfig(dst
.getConfig(), remoteName
);
128 rc
.addFetchRefSpec(new RefSpec().setForceUpdate(true)
129 .setSourceDestination(Constants
.R_HEADS
+ "*",
130 Constants
.R_REMOTES
+ remoteName
+ "/*"));
131 rc
.update(dst
.getConfig());
132 dst
.getConfig().save();
135 private FetchResult
runFetch() throws NotSupportedException
,
136 URISyntaxException
, TransportException
{
137 final Transport tn
= Transport
.open(db
, remoteName
);
140 r
= tn
.fetch(new TextProgressMonitor(), null);
144 showFetchResult(tn
, r
);
148 private Ref
guessHEAD(final FetchResult result
) {
149 final Ref idHEAD
= result
.getAdvertisedRef(Constants
.HEAD
);
150 final List
<Ref
> availableRefs
= new ArrayList
<Ref
>();
152 for (final Ref r
: result
.getAdvertisedRefs()) {
153 final String n
= r
.getName();
154 if (!n
.startsWith(Constants
.R_HEADS
))
156 availableRefs
.add(r
);
157 if (idHEAD
== null || head
!= null)
159 if (r
.getObjectId().equals(idHEAD
.getObjectId()))
162 Collections
.sort(availableRefs
, RefComparator
.INSTANCE
);
163 if (idHEAD
!= null && head
== null)
168 private void doCheckout(final Ref branch
) throws IOException
{
170 throw die(CLIText
.get().cannotChekoutNoHeadsAdvertisedByRemote
);
171 if (!Constants
.HEAD
.equals(branch
.getName())) {
172 RefUpdate u
= db
.updateRef(Constants
.HEAD
);
174 u
.link(branch
.getName());
177 final Commit commit
= db
.mapCommit(branch
.getObjectId());
178 final RefUpdate u
= db
.updateRef(Constants
.HEAD
);
179 u
.setNewObjectId(commit
.getCommitId());
182 final GitIndex index
= new GitIndex(db
);
183 final Tree tree
= commit
.getTree();
184 final WorkDirCheckout co
;
186 co
= new WorkDirCheckout(db
, db
.getWorkTree(), index
, tree
);