Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3d9103e72ce52c3a18146522ed2fb525af19b0e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*******************************************************************************
 * Copyright (C) 2008, Roger C. Soares <rogersoares@intelinet.com.br>
 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
 * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.egit.ui.internal.clone;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.egit.core.op.CloneOperation;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.UIIcons;
import org.eclipse.egit.ui.UIText;
import org.eclipse.egit.ui.internal.components.RepositorySelectionPage;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.osgi.util.NLS;
import org.eclipse.ui.IImportWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.transport.URIish;

/**
 * Import Git Repository Wizard. A front end to a git clone operation.
 */
public class GitCloneWizard extends Wizard implements IImportWizard {
	private RepositorySelectionPage cloneSource;

	private SourceBranchPage validSource;

	private CloneDestinationPage cloneDestination;

	private GitProjectsImportPage importProject;

	public void init(IWorkbench arg0, IStructuredSelection arg1) {
		setWindowTitle(UIText.GitCloneWizard_title);
		setDefaultPageImageDescriptor(UIIcons.WIZBAN_IMPORT_REPO);
		setNeedsProgressMonitor(true);
		cloneSource = new RepositorySelectionPage(true);
		validSource = new SourceBranchPage(cloneSource);
		cloneDestination = new CloneDestinationPage(cloneSource, validSource);
		importProject = new GitProjectsImportPage() {
			@Override
			public void setVisible(boolean visible) {
				if (visible) {
					if (cloneDestination.alreadyClonedInto == null) {
						performClone(false);
						cloneDestination.alreadyClonedInto = cloneDestination
								.getDestinationFile().getAbsolutePath();
					}
					setProjectsList(cloneDestination.alreadyClonedInto);
				}
				super.setVisible(visible);
			}
		};
	}

	@Override
	public boolean performCancel() {
		if (cloneDestination.alreadyClonedInto != null) {
			if (MessageDialog
					.openQuestion(getShell(), "Aborting clone.",
							"A complete clone was already made. Do you want to delete it?")) {
				deleteRecursively(new File(cloneDestination.alreadyClonedInto));
			}
		}
		return true;
	}

	private void deleteRecursively(File f) {
		for (File i : f.listFiles()) {
			if (i.isDirectory()) {
				deleteRecursively(i);
			} else {
				if (!i.delete()) {
					i.deleteOnExit();
				}
			}
		}
		if (!f.delete())
			f.deleteOnExit();
	}

	@Override
	public void addPages() {
		addPage(cloneSource);
		addPage(validSource);
		addPage(cloneDestination);
		addPage(importProject);
	}

	@Override
	public boolean canFinish() {
		return cloneDestination.isPageComplete()
				&& !cloneDestination.showImportWizard.getSelection()
				|| importProject.isPageComplete();
	}

	@Override
	public boolean performFinish() {
		if (!cloneDestination.showImportWizard.getSelection())
			return performClone(true);
		return importProject.createProjects();
	}

	boolean performClone(boolean background) {
		final URIish uri = cloneSource.getSelection().getURI();
		final boolean allSelected = validSource.isAllSelected();
		final Collection<Ref> selectedBranches = validSource
				.getSelectedBranches();
		final File workdir = cloneDestination.getDestinationFile();
		final String branch = cloneDestination.getInitialBranch();
		final String remoteName = cloneDestination.getRemote();

		workdir.mkdirs();
		if (!workdir.isDirectory()) {
			final String errorMessage = NLS.bind(
					UIText.GitCloneWizard_errorCannotCreate, workdir.getPath());
			ErrorDialog.openError(getShell(), getWindowTitle(),
					UIText.GitCloneWizard_failed, new Status(IStatus.ERROR,
							Activator.getPluginId(), 0, errorMessage, null));
			// let's give user a chance to fix this minor problem
			return false;
		}

		final CloneOperation op = new CloneOperation(uri, allSelected,
				selectedBranches, workdir, branch, remoteName);
		importProject.setGitDir(op.getGitDir());
		if (background) {
			final Job job = new Job(NLS.bind(UIText.GitCloneWizard_jobName, uri
					.toString())) {
				@Override
				protected IStatus run(final IProgressMonitor monitor) {
					try {
						op.run(monitor);
						return Status.OK_STATUS;
					} catch (InterruptedException e) {
						return Status.CANCEL_STATUS;
					} catch (InvocationTargetException e) {
						Throwable thr = e.getCause();
						return new Status(IStatus.ERROR, Activator
								.getPluginId(), 0, thr.getMessage(), thr);
					}
				}
			};
			job.setUser(true);
			job.schedule();
			return true;
		} else {
			try {
				PlatformUI.getWorkbench().getProgressService().run(false, true,
						op);
				return true;
			} catch (Exception e) {
				Activator.logError("Failed to clone", e);
				MessageDialog.openError(getShell(), "Failed clone", e
						.toString());
				return false;
			}
		}
	}
}

Back to the top