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() {
108 return performClone(false);
111 boolean performClone(boolean background
) {
112 final URIish uri
= cloneSource
.getSelection().getURI();
113 final boolean allSelected
;
114 final Collection
<Ref
> selectedBranches
;
115 if (validSource
.isSourceRepoEmpty()) {
116 // fetch all branches of empty repo
118 selectedBranches
= Collections
.emptyList();
120 allSelected
= validSource
.isAllSelected();
121 selectedBranches
= validSource
.getSelectedBranches();
123 final File workdir
= cloneDestination
.getDestinationFile();
124 final String branch
= cloneDestination
.getInitialBranch();
125 final String remoteName
= cloneDestination
.getRemote();
129 if (!workdir
.isDirectory()) {
130 final String errorMessage
= NLS
.bind(
131 UIText
.GitCloneWizard_errorCannotCreate
, workdir
.getPath());
132 ErrorDialog
.openError(getShell(), getWindowTitle(),
133 UIText
.GitCloneWizard_failed
, new Status(IStatus
.ERROR
,
134 Activator
.getPluginId(), 0, errorMessage
, null));
135 // let's give user a chance to fix this minor problem
139 final RepositoriesView view
;
140 IViewPart vp
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow()
141 .getActivePage().findView(RepositoriesView
.VIEW_ID
);
143 view
= (RepositoriesView
) vp
;
148 final CloneOperation op
= new CloneOperation(uri
, allSelected
,
149 selectedBranches
, workdir
, branch
, remoteName
);
151 alreadyClonedInto
= workdir
.getPath();
154 final Job job
= new Job(NLS
.bind(UIText
.GitCloneWizard_jobName
, uri
157 protected IStatus
run(final IProgressMonitor monitor
) {
160 RepositorySelectionPage
.saveUriInPrefs(uri
.toString());
161 RepositoriesView
.addDir(op
.getGitDir());
163 view
.scheduleRefresh();
165 return Status
.OK_STATUS
;
166 } catch (InterruptedException e
) {
167 return Status
.CANCEL_STATUS
;
168 } catch (InvocationTargetException e
) {
169 Throwable thr
= e
.getCause();
170 return new Status(IStatus
.ERROR
, Activator
171 .getPluginId(), 0, thr
.getMessage(), thr
);
181 // Perform clone in ModalContext thread with progress
182 // reporting on the wizard.
183 getContainer().run(true, true, new IRunnableWithProgress() {
184 public void run(IProgressMonitor monitor
)
185 throws InvocationTargetException
,
186 InterruptedException
{
188 if (monitor
.isCanceled())
189 throw new InterruptedException();
193 RepositorySelectionPage
.saveUriInPrefs(uri
.toString());
194 RepositoriesView
.addDir(op
.getGitDir());
196 view
.scheduleRefresh();
198 } catch (InterruptedException e
) {
199 MessageDialog
.openInformation(getShell(),
200 UIText
.GitCloneWizard_CloneFailedHeading
,
201 UIText
.GitCloneWizard_CloneCanceledMessage
);
203 } catch (Exception e
) {
204 Activator
.handleError(UIText
.GitCloneWizard_CloneFailedHeading
,