Always use a single WindowCache for the entire JVM
[egit/zawir.git] / org.spearce.egit.ui / src / org / spearce / egit / ui / internal / clone / GitCloneWizard.java
blob247342f4b35e13916a405d64b15f447d0f8e3343
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.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;
44 /**
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
51 + "/";
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);
66 @Override
67 public void addPages() {
68 addPage(cloneSource);
69 addPage(validSource);
70 addPage(cloneDestination);
73 @Override
74 public boolean performFinish() {
75 final URIish uri;
76 final Repository db;
77 final RemoteConfig origin;
79 try {
80 uri = cloneSource.getURI();
81 } catch (URISyntaxException e) {
82 return false;
85 final File workdir = cloneDestination.getDestinationFile();
86 final String branch = cloneDestination.getInitialBranch();
87 final File gitdir = new File(workdir, ".git");
88 try {
89 db = new Repository(gitdir);
90 db.create();
91 db.writeSymref(Constants.HEAD, branch);
93 final String rn = cloneDestination.getRemote();
94 origin = new RemoteConfig(db.getConfig(), rn);
95 origin.addURI(uri);
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);
104 } else {
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));
117 return false;
118 } catch (URISyntaxException e) {
119 return false;
122 final CloneOperation op = new CloneOperation(db, origin, branch);
123 final Job job = new Job(NLS.bind(UIText.GitCloneWizard_jobName, uri
124 .toString())) {
125 @Override
126 protected IStatus run(final IProgressMonitor monitor) {
127 try {
128 op.run(monitor);
129 if (monitor.isCanceled()) {
130 db.close();
131 delete(workdir);
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);
142 job.setUser(true);
143 job.schedule();
144 return true;
147 private static void delete(final File d) {
148 if (d.isDirectory()) {
149 final File[] items = d.listFiles();
150 if (items != null) {
151 for (final File c : items)
152 delete(c);
155 d.delete();