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>
7 * All rights reserved. This program and the accompanying materials
8 * are made available under the terms of the Eclipse Public License v1.0
9 * which accompanies this distribution, and is available at
10 * http://www.eclipse.org/legal/epl-v10.html
11 *******************************************************************************/
12 package org
.eclipse
.egit
.ui
.internal
.clone
;
15 import java
.lang
.reflect
.InvocationTargetException
;
16 import java
.util
.Collection
;
18 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
19 import org
.eclipse
.core
.runtime
.IStatus
;
20 import org
.eclipse
.core
.runtime
.Status
;
21 import org
.eclipse
.core
.runtime
.jobs
.Job
;
22 import org
.eclipse
.egit
.core
.op
.CloneOperation
;
23 import org
.eclipse
.egit
.ui
.Activator
;
24 import org
.eclipse
.egit
.ui
.UIIcons
;
25 import org
.eclipse
.egit
.ui
.UIText
;
26 import org
.eclipse
.egit
.ui
.internal
.components
.RepositorySelectionPage
;
27 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
28 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
29 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
30 import org
.eclipse
.jface
.wizard
.Wizard
;
31 import org
.eclipse
.jgit
.lib
.Ref
;
32 import org
.eclipse
.jgit
.transport
.URIish
;
33 import org
.eclipse
.osgi
.util
.NLS
;
34 import org
.eclipse
.ui
.IImportWizard
;
35 import org
.eclipse
.ui
.IWorkbench
;
38 * Import Git Repository Wizard. A front end to a git clone operation.
40 public class GitCloneWizard
extends Wizard
implements IImportWizard
{
41 private RepositorySelectionPage cloneSource
;
43 private SourceBranchPage validSource
;
45 private CloneDestinationPage cloneDestination
;
47 private GitProjectsImportPage importProject
;
49 public void init(IWorkbench arg0
, IStructuredSelection arg1
) {
50 setWindowTitle(UIText
.GitCloneWizard_title
);
51 setDefaultPageImageDescriptor(UIIcons
.WIZBAN_IMPORT_REPO
);
52 setNeedsProgressMonitor(true);
53 cloneSource
= new RepositorySelectionPage(true);
54 validSource
= new SourceBranchPage(cloneSource
);
55 cloneDestination
= new CloneDestinationPage(cloneSource
, validSource
);
56 importProject
= new GitProjectsImportPage() {
58 public void setVisible(boolean visible
) {
60 if (cloneDestination
.alreadyClonedInto
== null) {
61 if (performClone(false))
62 cloneDestination
.alreadyClonedInto
= cloneDestination
63 .getDestinationFile().getAbsolutePath();
65 setProjectsList(cloneDestination
.alreadyClonedInto
);
67 super.setVisible(visible
);
73 public boolean performCancel() {
74 if (cloneDestination
.alreadyClonedInto
!= null) {
76 .openQuestion(getShell(), "Aborting clone.",
77 "A complete clone was already made. Do you want to delete it?")) {
78 deleteRecursively(new File(cloneDestination
.alreadyClonedInto
));
84 private void deleteRecursively(File f
) {
85 for (File i
: f
.listFiles()) {
86 if (i
.isDirectory()) {
99 public void addPages() {
100 addPage(cloneSource
);
101 addPage(validSource
);
102 addPage(cloneDestination
);
103 addPage(importProject
);
107 public boolean canFinish() {
108 return cloneDestination
.isPageComplete()
109 && !cloneDestination
.showImportWizard
.getSelection()
110 || importProject
.isPageComplete();
114 public boolean performFinish() {
115 if (!cloneDestination
.showImportWizard
.getSelection())
116 return performClone(true);
117 return importProject
.createProjects();
120 boolean performClone(boolean background
) {
121 final URIish uri
= cloneSource
.getSelection().getURI();
122 final boolean allSelected
= validSource
.isAllSelected();
123 final Collection
<Ref
> selectedBranches
= validSource
124 .getSelectedBranches();
125 final File workdir
= cloneDestination
.getDestinationFile();
126 final String branch
= cloneDestination
.getInitialBranch();
127 final String remoteName
= cloneDestination
.getRemote();
130 if (!workdir
.isDirectory()) {
131 final String errorMessage
= NLS
.bind(
132 UIText
.GitCloneWizard_errorCannotCreate
, workdir
.getPath());
133 ErrorDialog
.openError(getShell(), getWindowTitle(),
134 UIText
.GitCloneWizard_failed
, new Status(IStatus
.ERROR
,
135 Activator
.getPluginId(), 0, errorMessage
, null));
136 // let's give user a chance to fix this minor problem
140 final CloneOperation op
= new CloneOperation(uri
, allSelected
,
141 selectedBranches
, workdir
, branch
, remoteName
);
142 importProject
.setGitDir(op
.getGitDir());
144 final Job job
= new Job(NLS
.bind(UIText
.GitCloneWizard_jobName
, uri
147 protected IStatus
run(final IProgressMonitor monitor
) {
150 return Status
.OK_STATUS
;
151 } catch (InterruptedException e
) {
152 return Status
.CANCEL_STATUS
;
153 } catch (InvocationTargetException e
) {
154 Throwable thr
= e
.getCause();
155 return new Status(IStatus
.ERROR
, Activator
156 .getPluginId(), 0, thr
.getMessage(), thr
);
165 // Perform clone in ModalContext thread with progress
166 // reporting on the wizard.
167 getContainer().run(true, true, op
);
169 } catch (InterruptedException e
) {
170 MessageDialog
.openInformation(getShell(),
171 UIText
.GitCloneWizard_CloneFailedHeading
,
172 UIText
.GitCloneWizard_CloneCanceledMessage
);
174 } catch (Exception e
) {
175 Activator
.logError(UIText
.GitCloneWizard_CloneFailedHeading
, e
);
176 MessageDialog
.openError(getShell(),
177 UIText
.GitCloneWizard_CloneFailedHeading
, e
.toString());