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
.ui
.Activator
;
36 import org
.spearce
.egit
.ui
.UIText
;
37 import org
.spearce
.jgit
.lib
.Constants
;
38 import org
.spearce
.jgit
.lib
.Ref
;
39 import org
.spearce
.jgit
.lib
.Repository
;
40 import org
.spearce
.jgit
.transport
.RefSpec
;
41 import org
.spearce
.jgit
.transport
.RemoteConfig
;
42 import org
.spearce
.jgit
.transport
.URIish
;
45 * Import Git Repository Wizard. A front end to a git clone operation.
47 public class GitCloneWizard
extends Wizard
implements IImportWizard
{
48 private static final String HEADS_PREFIX
= Constants
.HEADS_PREFIX
;
50 private static final String REMOTES_PREFIX_S
= Constants
.REMOTES_PREFIX
53 private CloneSourcePage cloneSource
;
55 private SourceBranchPage validSource
;
57 private CloneDestinationPage cloneDestination
;
59 public void init(IWorkbench arg0
, IStructuredSelection arg1
) {
60 setWindowTitle(UIText
.GitCloneWizard_title
);
61 cloneSource
= new CloneSourcePage();
62 validSource
= new SourceBranchPage(cloneSource
);
63 cloneDestination
= new CloneDestinationPage(cloneSource
, validSource
);
67 public void addPages() {
70 addPage(cloneDestination
);
74 public boolean performFinish() {
77 final RemoteConfig origin
;
80 uri
= cloneSource
.getURI();
81 } catch (URISyntaxException e
) {
85 final File workdir
= cloneDestination
.getDestinationFile();
86 final String branch
= cloneDestination
.getInitialBranch();
87 final File gitdir
= new File(workdir
, ".git");
89 db
= new Repository(gitdir
);
91 db
.writeSymref(Constants
.HEAD
, branch
);
93 final String rn
= cloneDestination
.getRemote();
94 origin
= new RemoteConfig(db
.getConfig(), rn
);
97 final String dst
= REMOTES_PREFIX_S
+ origin
.getName();
98 RefSpec wcrs
= new RefSpec();
99 wcrs
= wcrs
.setForceUpdate(true);
100 wcrs
= wcrs
.setSourceDestination(HEADS_PREFIX
+ "/*", dst
+ "/*");
102 if (validSource
.isAllSelected()) {
103 origin
.addFetchRefSpec(wcrs
);
105 for (final Ref ref
: validSource
.getSelectedBranches())
106 if (wcrs
.matchSource(ref
))
107 origin
.addFetchRefSpec(wcrs
.expandFromSource(ref
));
110 origin
.update(db
.getConfig());
111 db
.getConfig().save();
112 } catch (IOException err
) {
113 Activator
.logError(UIText
.GitCloneWizard_failed
, err
);
114 ErrorDialog
.openError(getShell(), getWindowTitle(),
115 UIText
.GitCloneWizard_failed
, new Status(IStatus
.ERROR
,
116 Activator
.getPluginId(), 0, err
.getMessage(), err
));
118 } catch (URISyntaxException e
) {
122 final CloneOperation op
= new CloneOperation(db
, origin
, branch
);
123 final Job job
= new Job(NLS
.bind(UIText
.GitCloneWizard_jobName
, uri
126 protected IStatus
run(final IProgressMonitor monitor
) {
129 if (monitor
.isCanceled()) {
132 return Status
.CANCEL_STATUS
;
134 return Status
.OK_STATUS
;
135 } catch (InvocationTargetException e
) {
136 Throwable thr
= e
.getCause();
137 return new Status(IStatus
.ERROR
, Activator
.getPluginId(),
138 0, thr
.getMessage(), thr
);
147 private static void delete(final File d
) {
148 if (d
.isDirectory()) {
149 final File
[] items
= d
.listFiles();
151 for (final File c
: items
)