2 * Copyright (C) 2008 Shawn Pearce <spearce@spearce.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License, version 2, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org
.spearce
.egit
.core
.op
;
19 import java
.io
.IOException
;
20 import java
.lang
.reflect
.InvocationTargetException
;
22 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
23 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
24 import org
.eclipse
.core
.runtime
.SubProgressMonitor
;
25 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
26 import org
.eclipse
.osgi
.util
.NLS
;
27 import org
.spearce
.egit
.core
.CoreText
;
28 import org
.spearce
.egit
.core
.EclipseGitProgressTransformer
;
29 import org
.spearce
.jgit
.errors
.NotSupportedException
;
30 import org
.spearce
.jgit
.errors
.TransportException
;
31 import org
.spearce
.jgit
.lib
.Commit
;
32 import org
.spearce
.jgit
.lib
.Constants
;
33 import org
.spearce
.jgit
.lib
.GitIndex
;
34 import org
.spearce
.jgit
.lib
.Ref
;
35 import org
.spearce
.jgit
.lib
.RefUpdate
;
36 import org
.spearce
.jgit
.lib
.Repository
;
37 import org
.spearce
.jgit
.lib
.Tree
;
38 import org
.spearce
.jgit
.lib
.WorkDirCheckout
;
39 import org
.spearce
.jgit
.transport
.FetchResult
;
40 import org
.spearce
.jgit
.transport
.RemoteConfig
;
41 import org
.spearce
.jgit
.transport
.Transport
;
44 * Clones a repository from a remote location to a local location.
46 public class CloneOperation
implements IRunnableWithProgress
{
47 private final Repository local
;
49 private final RemoteConfig remote
;
51 private final String branch
;
53 private FetchResult fetchResult
;
56 * Create a new clone operation.
59 * repository the checkout will happen within.
61 * remote we should fetch from.
63 * branch to initially clone from.
65 public CloneOperation(final Repository r
, final RemoteConfig t
,
72 public void run(final IProgressMonitor pm
) throws InvocationTargetException
{
73 final IProgressMonitor monitor
;
75 monitor
= new NullProgressMonitor();
80 monitor
.beginTask(NLS
.bind(CoreText
.CloneOperation_title
, remote
81 .getURIs().get(0).toString()), 5000);
82 doFetch(new SubProgressMonitor(monitor
, 4000));
83 doCheckout(new SubProgressMonitor(monitor
, 1000));
84 } catch (IOException e
) {
85 if (!monitor
.isCanceled())
86 throw new InvocationTargetException(e
);
92 private void doFetch(final IProgressMonitor monitor
)
93 throws NotSupportedException
, TransportException
{
94 fetchResult
= Transport
.open(local
, remote
).fetch(
95 new EclipseGitProgressTransformer(monitor
), null);
98 private void doCheckout(final IProgressMonitor monitor
) throws IOException
{
99 final Ref head
= fetchResult
.getAdvertisedRef(branch
);
100 if (head
== null || head
.getObjectId() == null)
103 final GitIndex index
= new GitIndex(local
);
104 final Commit mapCommit
= local
.mapCommit(head
.getObjectId());
105 final Tree tree
= mapCommit
.getTree();
107 final WorkDirCheckout co
;
109 u
= local
.updateRef(Constants
.HEAD
);
110 u
.setNewObjectId(mapCommit
.getCommitId());
113 monitor
.setTaskName("Checking out files");
114 co
= new WorkDirCheckout(local
, local
.getWorkDir(), index
, tree
);
116 monitor
.setTaskName("Writing index");