2 * Copyright (C) 2008 Roger C. Soares
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License, version 2.1, 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 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser 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
.ui
.internal
.clone
;
20 import java
.io
.IOException
;
21 import java
.lang
.reflect
.InvocationTargetException
;
22 import java
.net
.URISyntaxException
;
24 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
25 import org
.eclipse
.core
.runtime
.IStatus
;
26 import org
.eclipse
.core
.runtime
.Status
;
27 import org
.eclipse
.core
.runtime
.jobs
.Job
;
28 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
29 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
30 import org
.eclipse
.jface
.wizard
.Wizard
;
31 import org
.eclipse
.osgi
.util
.NLS
;
32 import org
.eclipse
.ui
.IImportWizard
;
33 import org
.eclipse
.ui
.IWorkbench
;
34 import org
.spearce
.egit
.core
.op
.CloneOperation
;
35 import org
.spearce
.egit
.core
.project
.GitProjectData
;
36 import org
.spearce
.egit
.ui
.Activator
;
37 import org
.spearce
.egit
.ui
.UIText
;
38 import org
.spearce
.jgit
.lib
.Constants
;
39 import org
.spearce
.jgit
.lib
.Ref
;
40 import org
.spearce
.jgit
.lib
.Repository
;
41 import org
.spearce
.jgit
.transport
.RefSpec
;
42 import org
.spearce
.jgit
.transport
.RemoteConfig
;
43 import org
.spearce
.jgit
.transport
.URIish
;
46 * Import Git Repository Wizard. A front end to a git clone operation.
48 public class GitCloneWizard
extends Wizard
implements IImportWizard
{
49 private static final String HEADS_PREFIX
= Constants
.HEADS_PREFIX
;
51 private static final String REMOTES_PREFIX_S
= Constants
.REMOTES_PREFIX
54 private CloneSourcePage cloneSource
;
56 private SourceBranchPage validSource
;
58 private CloneDestinationPage cloneDestination
;
60 public void init(IWorkbench arg0
, IStructuredSelection arg1
) {
61 setWindowTitle(UIText
.GitCloneWizard_title
);
62 cloneSource
= new CloneSourcePage();
63 validSource
= new SourceBranchPage(cloneSource
);
64 cloneDestination
= new CloneDestinationPage(cloneSource
, validSource
);
68 public void addPages() {
71 addPage(cloneDestination
);
75 public boolean performFinish() {
78 final RemoteConfig origin
;
81 uri
= cloneSource
.getURI();
82 } catch (URISyntaxException e
) {
86 final File workdir
= cloneDestination
.getDestinationFile();
87 final String branch
= cloneDestination
.getInitialBranch();
88 final File gitdir
= new File(workdir
, ".git");
90 db
= new Repository(GitProjectData
.getWindowCache(), gitdir
);
92 db
.writeSymref(Constants
.HEAD
, branch
);
94 final String rn
= cloneDestination
.getRemote();
95 origin
= new RemoteConfig(db
.getConfig(), rn
);
98 final String dst
= REMOTES_PREFIX_S
+ origin
.getName();
99 RefSpec wcrs
= new RefSpec();
100 wcrs
= wcrs
.setForceUpdate(true);
101 wcrs
= wcrs
.setSourceDestination(HEADS_PREFIX
+ "/*", dst
+ "/*");
103 if (validSource
.isAllSelected()) {
104 origin
.addFetchRefSpec(wcrs
);
106 for (final Ref ref
: validSource
.getSelectedBranches())
107 if (wcrs
.matchSource(ref
))
108 origin
.addFetchRefSpec(wcrs
.expandFromSource(ref
));
111 origin
.update(db
.getConfig());
112 db
.getConfig().save();
113 } catch (IOException err
) {
114 Activator
.logError(UIText
.GitCloneWizard_failed
, err
);
115 ErrorDialog
.openError(getShell(), getWindowTitle(),
116 UIText
.GitCloneWizard_failed
, new Status(IStatus
.ERROR
,
117 Activator
.getPluginId(), 0, err
.getMessage(), err
));
119 } catch (URISyntaxException e
) {
123 final CloneOperation op
= new CloneOperation(db
, origin
, branch
);
124 final Job job
= new Job(NLS
.bind(UIText
.GitCloneWizard_jobName
, uri
127 protected IStatus
run(final IProgressMonitor monitor
) {
130 if (monitor
.isCanceled()) {
133 return Status
.CANCEL_STATUS
;
135 return Status
.OK_STATUS
;
136 } catch (InvocationTargetException e
) {
137 Throwable thr
= e
.getCause();
138 return new Status(IStatus
.ERROR
, Activator
.getPluginId(),
139 0, thr
.getMessage(), thr
);
148 private static void delete(final File d
) {
149 if (d
.isDirectory()) {
150 final File
[] items
= d
.listFiles();
152 for (final File c
: items
)