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
;
37 import org
.eclipse
.ui
.IViewPart
;
38 import org
.eclipse
.ui
.PlatformUI
;
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
;
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
);
65 public boolean performCancel() {
66 if (alreadyClonedInto
!= null) {
67 File test
= new File(alreadyClonedInto
);
69 && MessageDialog
.openQuestion(getShell(),
70 UIText
.GitCloneWizard_abortingCloneTitle
,
71 UIText
.GitCloneWizard_abortingCloneMsg
)) {
72 deleteRecursively(new File(alreadyClonedInto
));
78 private void deleteRecursively(File f
) {
79 File
[] children
= f
.listFiles();
81 for (File i
: children
) {
82 if (i
.isDirectory()) {
95 public void addPages() {
98 addPage(cloneDestination
);
102 public boolean canFinish() {
103 return cloneDestination
.isPageComplete();
107 public boolean performFinish() {
109 return performClone(false);
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
123 selectedBranches
= Collections
.emptyList();
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();
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
144 final RepositoriesView view
;
145 IViewPart vp
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow()
146 .getActivePage().findView(RepositoriesView
.VIEW_ID
);
148 view
= (RepositoriesView
) vp
;
153 final CloneOperation op
= new CloneOperation(uri
, allSelected
,
154 selectedBranches
, workdir
, branch
, remoteName
);
156 alreadyClonedInto
= workdir
.getPath();
159 final Job job
= new Job(NLS
.bind(UIText
.GitCloneWizard_jobName
, uri
162 protected IStatus
run(final IProgressMonitor monitor
) {
165 RepositorySelectionPage
.saveUriInPrefs(uri
.toString());
166 RepositoriesView
.addDir(op
.getGitDir());
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
);
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
{
193 if (monitor
.isCanceled())
194 throw new InterruptedException();
198 RepositorySelectionPage
.saveUriInPrefs(uri
.toString());
199 RepositoriesView
.addDir(op
.getGitDir());
201 view
.scheduleRefresh();
203 } catch (InterruptedException e
) {
204 MessageDialog
.openInformation(getShell(),
205 UIText
.GitCloneWizard_CloneFailedHeading
,
206 UIText
.GitCloneWizard_CloneCanceledMessage
);
208 } catch (Exception e
) {
209 Activator
.handleError(UIText
.GitCloneWizard_CloneFailedHeading
,