Fix UI Tests for Git Clone
[egit/spearce.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / clone / GitCloneWizard.java
blob163561b0264138a83634f7604209844d993889b7
1 /*******************************************************************************
2 * Copyright (C) 2008, Roger C. Soares <rogersoares@intelinet.com.br>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
5 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
6 * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
8 * All rights reserved. This program and the accompanying materials
9 * are made available under the terms of the Eclipse Public License v1.0
10 * which accompanies this distribution, and is available at
11 * http://www.eclipse.org/legal/epl-v10.html
12 *******************************************************************************/
13 package org.eclipse.egit.ui.internal.clone;
15 import java.io.File;
16 import java.lang.reflect.InvocationTargetException;
17 import java.util.Collection;
18 import java.util.Collections;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.runtime.jobs.Job;
24 import org.eclipse.egit.core.op.CloneOperation;
25 import org.eclipse.egit.ui.Activator;
26 import org.eclipse.egit.ui.UIIcons;
27 import org.eclipse.egit.ui.UIText;
28 import org.eclipse.egit.ui.internal.components.RepositorySelectionPage;
29 import org.eclipse.egit.ui.internal.repository.RepositoriesView;
30 import org.eclipse.jface.dialogs.ErrorDialog;
31 import org.eclipse.jface.dialogs.MessageDialog;
32 import org.eclipse.jface.operation.IRunnableWithProgress;
33 import org.eclipse.jface.wizard.Wizard;
34 import org.eclipse.jgit.lib.Ref;
35 import org.eclipse.jgit.transport.URIish;
36 import org.eclipse.osgi.util.NLS;
37 import org.eclipse.ui.IViewPart;
38 import org.eclipse.ui.PlatformUI;
40 /**
41 * Import Git Repository Wizard. A front end to a git clone operation.
43 public class GitCloneWizard extends Wizard {
44 private RepositorySelectionPage cloneSource;
46 private SourceBranchPage validSource;
48 private CloneDestinationPage cloneDestination;
50 String alreadyClonedInto;
52 /**
53 * The default constructor
55 public GitCloneWizard() {
56 setWindowTitle(UIText.GitCloneWizard_title);
57 setDefaultPageImageDescriptor(UIIcons.WIZBAN_IMPORT_REPO);
58 setNeedsProgressMonitor(true);
59 cloneSource = new RepositorySelectionPage(true, null);
60 validSource = new SourceBranchPage(cloneSource);
61 cloneDestination = new CloneDestinationPage(cloneSource, validSource);
64 @Override
65 public boolean performCancel() {
66 if (alreadyClonedInto != null) {
67 File test = new File(alreadyClonedInto);
68 if (test.exists()
69 && MessageDialog.openQuestion(getShell(),
70 UIText.GitCloneWizard_abortingCloneTitle,
71 UIText.GitCloneWizard_abortingCloneMsg)) {
72 deleteRecursively(new File(alreadyClonedInto));
75 return true;
78 private void deleteRecursively(File f) {
79 File[] children = f.listFiles();
80 if (children != null)
81 for (File i : children) {
82 if (i.isDirectory()) {
83 deleteRecursively(i);
84 } else {
85 if (!i.delete()) {
86 i.deleteOnExit();
90 if (!f.delete())
91 f.deleteOnExit();
94 @Override
95 public void addPages() {
96 addPage(cloneSource);
97 addPage(validSource);
98 addPage(cloneDestination);
101 @Override
102 public boolean canFinish() {
103 return cloneDestination.isPageComplete();
106 @Override
107 public boolean performFinish() {
108 try {
109 return performClone(false);
110 } finally {
111 setWindowTitle(UIText.GitCloneWizard_title);
115 boolean performClone(boolean background) {
116 final URIish uri = cloneSource.getSelection().getURI();
117 setWindowTitle(NLS.bind(UIText.GitCloneWizard_jobName, uri.toString()));
118 final boolean allSelected;
119 final Collection<Ref> selectedBranches;
120 if (validSource.isSourceRepoEmpty()) {
121 // fetch all branches of empty repo
122 allSelected = true;
123 selectedBranches = Collections.emptyList();
124 } else {
125 allSelected = validSource.isAllSelected();
126 selectedBranches = validSource.getSelectedBranches();
128 final File workdir = cloneDestination.getDestinationFile();
129 final String branch = cloneDestination.getInitialBranch();
130 final String remoteName = cloneDestination.getRemote();
132 workdir.mkdirs();
134 if (!workdir.isDirectory()) {
135 final String errorMessage = NLS.bind(
136 UIText.GitCloneWizard_errorCannotCreate, workdir.getPath());
137 ErrorDialog.openError(getShell(), getWindowTitle(),
138 UIText.GitCloneWizard_failed, new Status(IStatus.ERROR,
139 Activator.getPluginId(), 0, errorMessage, null));
140 // let's give user a chance to fix this minor problem
141 return false;
144 final RepositoriesView view;
145 IViewPart vp = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
146 .getActivePage().findView(RepositoriesView.VIEW_ID);
147 if (vp != null) {
148 view = (RepositoriesView) vp;
149 } else {
150 view = null;
153 final CloneOperation op = new CloneOperation(uri, allSelected,
154 selectedBranches, workdir, branch, remoteName);
156 alreadyClonedInto = workdir.getPath();
158 if (background) {
159 final Job job = new Job(NLS.bind(UIText.GitCloneWizard_jobName, uri
160 .toString())) {
161 @Override
162 protected IStatus run(final IProgressMonitor monitor) {
163 try {
164 op.run(monitor);
165 RepositorySelectionPage.saveUriInPrefs(uri.toString());
166 RepositoriesView.addDir(op.getGitDir());
167 if (view != null)
168 view.scheduleRefresh();
170 return Status.OK_STATUS;
171 } catch (InterruptedException e) {
172 return Status.CANCEL_STATUS;
173 } catch (InvocationTargetException e) {
174 Throwable thr = e.getCause();
175 return new Status(IStatus.ERROR, Activator
176 .getPluginId(), 0, thr.getMessage(), thr);
181 job.setUser(true);
182 job.schedule();
183 return true;
184 } else {
185 try {
186 // Perform clone in ModalContext thread with progress
187 // reporting on the wizard.
188 getContainer().run(true, true, new IRunnableWithProgress() {
189 public void run(IProgressMonitor monitor)
190 throws InvocationTargetException,
191 InterruptedException {
192 op.run(monitor);
193 if (monitor.isCanceled())
194 throw new InterruptedException();
198 RepositorySelectionPage.saveUriInPrefs(uri.toString());
199 RepositoriesView.addDir(op.getGitDir());
200 if (view != null)
201 view.scheduleRefresh();
202 return true;
203 } catch (InterruptedException e) {
204 MessageDialog.openInformation(getShell(),
205 UIText.GitCloneWizard_CloneFailedHeading,
206 UIText.GitCloneWizard_CloneCanceledMessage);
207 return false;
208 } catch (Exception e) {
209 Activator.handleError(UIText.GitCloneWizard_CloneFailedHeading,
210 e, true);
211 return false;