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
;
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
;
39 * Import Git Repository Wizard. A front end to a git clone operation.
41 public class GitCloneWizard
extends Wizard
{
42 private RepositorySelectionPage cloneSource
;
44 private SourceBranchPage validSource
;
46 private CloneDestinationPage cloneDestination
;
48 String alreadyClonedInto
;
51 * The default constructor
53 public GitCloneWizard() {
54 setWindowTitle(UIText
.GitCloneWizard_title
);
55 setDefaultPageImageDescriptor(UIIcons
.WIZBAN_IMPORT_REPO
);
56 setNeedsProgressMonitor(true);
57 cloneSource
= new RepositorySelectionPage(true, null);
58 validSource
= new SourceBranchPage(cloneSource
);
59 cloneDestination
= new CloneDestinationPage(cloneSource
, validSource
);
63 public boolean performCancel() {
64 if (alreadyClonedInto
!= null) {
65 File test
= new File(alreadyClonedInto
);
67 && MessageDialog
.openQuestion(getShell(),
68 UIText
.GitCloneWizard_abortingCloneTitle
,
69 UIText
.GitCloneWizard_abortingCloneMsg
)) {
70 deleteRecursively(new File(alreadyClonedInto
));
76 private void deleteRecursively(File f
) {
77 File
[] children
= f
.listFiles();
79 for (File i
: children
) {
80 if (i
.isDirectory()) {
93 public void addPages() {
96 addPage(cloneDestination
);
100 public boolean canFinish() {
101 return cloneDestination
.isPageComplete();
105 public boolean performFinish() {
107 return performClone(false);
109 setWindowTitle(UIText
.GitCloneWizard_title
);
113 boolean performClone(boolean background
) {
114 final URIish uri
= cloneSource
.getSelection().getURI();
115 setWindowTitle(NLS
.bind(UIText
.GitCloneWizard_jobName
, uri
.toString()));
116 final boolean allSelected
;
117 final Collection
<Ref
> selectedBranches
;
118 if (validSource
.isSourceRepoEmpty()) {
119 // fetch all branches of empty repo
121 selectedBranches
= Collections
.emptyList();
123 allSelected
= validSource
.isAllSelected();
124 selectedBranches
= validSource
.getSelectedBranches();
126 final File workdir
= cloneDestination
.getDestinationFile();
127 final String branch
= cloneDestination
.getInitialBranch();
128 final String remoteName
= cloneDestination
.getRemote();
132 if (!workdir
.isDirectory()) {
133 final String errorMessage
= NLS
.bind(
134 UIText
.GitCloneWizard_errorCannotCreate
, workdir
.getPath());
135 ErrorDialog
.openError(getShell(), getWindowTitle(),
136 UIText
.GitCloneWizard_failed
, new Status(IStatus
.ERROR
,
137 Activator
.getPluginId(), 0, errorMessage
, null));
138 // let's give user a chance to fix this minor problem
142 final CloneOperation op
= new CloneOperation(uri
, allSelected
,
143 selectedBranches
, workdir
, branch
, remoteName
);
145 alreadyClonedInto
= workdir
.getPath();
148 final Job job
= new Job(NLS
.bind(UIText
.GitCloneWizard_jobName
, uri
151 protected IStatus
run(final IProgressMonitor monitor
) {
154 RepositorySelectionPage
.saveUriInPrefs(uri
.toString());
155 RepositoriesView
.addDir(op
.getGitDir());
156 return Status
.OK_STATUS
;
157 } catch (InterruptedException e
) {
158 return Status
.CANCEL_STATUS
;
159 } catch (InvocationTargetException e
) {
160 Throwable thr
= e
.getCause();
161 return new Status(IStatus
.ERROR
, Activator
162 .getPluginId(), 0, thr
.getMessage(), thr
);
172 // Perform clone in ModalContext thread with progress
173 // reporting on the wizard.
174 getContainer().run(true, true, new IRunnableWithProgress() {
175 public void run(IProgressMonitor monitor
)
176 throws InvocationTargetException
,
177 InterruptedException
{
179 if (monitor
.isCanceled())
180 throw new InterruptedException();
184 RepositorySelectionPage
.saveUriInPrefs(uri
.toString());
185 RepositoriesView
.addDir(op
.getGitDir());
187 } catch (InterruptedException e
) {
188 MessageDialog
.openInformation(getShell(),
189 UIText
.GitCloneWizard_CloneFailedHeading
,
190 UIText
.GitCloneWizard_CloneCanceledMessage
);
192 } catch (Exception e
) {
193 Activator
.handleError(UIText
.GitCloneWizard_CloneFailedHeading
,