Rewrite egit clone wizard UI
[egit/zawir.git] / org.spearce.egit.ui / src / org / spearce / egit / ui / internal / clone / GitCloneWizard.java
bloba77c2e85c5b18753bbf04899f73b343efb323058
1 /*
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;
19 import java.io.File;
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;
45 /**
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
52 + "/";
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);
67 @Override
68 public void addPages() {
69 addPage(cloneSource);
70 addPage(validSource);
71 addPage(cloneDestination);
74 @Override
75 public boolean performFinish() {
76 final URIish uri;
77 final Repository db;
78 final RemoteConfig origin;
80 try {
81 uri = cloneSource.getURI();
82 } catch (URISyntaxException e) {
83 return false;
86 final File workdir = cloneDestination.getDestinationFile();
87 final String branch = cloneDestination.getInitialBranch();
88 final File gitdir = new File(workdir, ".git");
89 try {
90 db = new Repository(GitProjectData.getWindowCache(), gitdir);
91 db.create();
92 db.writeSymref(Constants.HEAD, branch);
94 final String rn = cloneDestination.getRemote();
95 origin = new RemoteConfig(db.getConfig(), rn);
96 origin.addURI(uri);
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);
105 } else {
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));
118 return false;
119 } catch (URISyntaxException e) {
120 return false;
123 final CloneOperation op = new CloneOperation(db, origin, branch);
124 final Job job = new Job(NLS.bind(UIText.GitCloneWizard_jobName, uri
125 .toString())) {
126 @Override
127 protected IStatus run(final IProgressMonitor monitor) {
128 try {
129 op.run(monitor);
130 if (monitor.isCanceled()) {
131 db.close();
132 delete(workdir);
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);
143 job.setUser(true);
144 job.schedule();
145 return true;
148 private static void delete(final File d) {
149 if (d.isDirectory()) {
150 final File[] items = d.listFiles();
151 if (items != null) {
152 for (final File c : items)
153 delete(c);
156 d.delete();