1 /*******************************************************************************
2 * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * See LICENSE for the full license text, also available.
7 *******************************************************************************/
8 package org
.eclipse
.egit
.ui
.internal
.push
;
10 import java
.io
.IOException
;
11 import java
.lang
.reflect
.InvocationTargetException
;
12 import java
.net
.URISyntaxException
;
13 import java
.util
.Collection
;
14 import java
.util
.List
;
16 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
17 import org
.eclipse
.core
.runtime
.IStatus
;
18 import org
.eclipse
.core
.runtime
.Status
;
19 import org
.eclipse
.core
.runtime
.jobs
.Job
;
20 import org
.eclipse
.egit
.core
.op
.PushOperation
;
21 import org
.eclipse
.egit
.core
.op
.PushOperationResult
;
22 import org
.eclipse
.egit
.core
.op
.PushOperationSpecification
;
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
.RefSpecPage
;
27 import org
.eclipse
.egit
.ui
.internal
.components
.RepositorySelection
;
28 import org
.eclipse
.egit
.ui
.internal
.components
.RepositorySelectionPage
;
29 import org
.eclipse
.jface
.dialogs
.Dialog
;
30 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
31 import org
.eclipse
.jface
.wizard
.IWizardPage
;
32 import org
.eclipse
.jface
.wizard
.Wizard
;
33 import org
.eclipse
.osgi
.util
.NLS
;
34 import org
.eclipse
.swt
.widgets
.Shell
;
35 import org
.eclipse
.ui
.PlatformUI
;
36 import org
.spearce
.jgit
.lib
.Repository
;
37 import org
.spearce
.jgit
.lib
.RepositoryConfig
;
38 import org
.spearce
.jgit
.transport
.RefSpec
;
39 import org
.spearce
.jgit
.transport
.RemoteConfig
;
40 import org
.spearce
.jgit
.transport
.RemoteRefUpdate
;
41 import org
.spearce
.jgit
.transport
.Transport
;
42 import org
.spearce
.jgit
.transport
.URIish
;
45 * Wizard allowing user to specify all needed data to push to another repository
46 * - including selection of remote repository and refs specifications.
48 * Push operation is performed upon successful completion of this wizard.
50 public class PushWizard
extends Wizard
{
51 private static String
getURIsString(final Collection
<URIish
> uris
) {
52 final StringBuilder sb
= new StringBuilder();
54 for (final URIish uri
: uris
) {
58 sb
.append(", "); //$NON-NLS-1$
64 private Repository localDb
;
66 private final RepositorySelectionPage repoPage
;
68 private final RefSpecPage refSpecPage
;
70 private ConfirmationPage confirmPage
;
73 * Create push wizard for specified local repository.
76 * repository to push from.
77 * @throws URISyntaxException
78 * when configuration of this repository contains illegal URIs.
80 public PushWizard(final Repository localDb
) throws URISyntaxException
{
81 this.localDb
= localDb
;
82 final List
<RemoteConfig
> remotes
= RemoteConfig
83 .getAllRemoteConfigs(localDb
.getConfig());
84 repoPage
= new RepositorySelectionPage(false, remotes
);
85 refSpecPage
= new RefSpecPage(localDb
, true, repoPage
);
86 confirmPage
= new ConfirmationPage(localDb
, repoPage
, refSpecPage
);
87 // TODO use/create another cool icon
88 setDefaultPageImageDescriptor(UIIcons
.WIZBAN_IMPORT_REPO
);
89 setNeedsProgressMonitor(true);
93 public void addPages() {
100 public boolean performFinish() {
101 if (repoPage
.getSelection().isConfigSelected()
102 && refSpecPage
.isSaveRequested()) {
106 final PushOperation operation
= createPushOperation();
107 if (operation
== null)
109 final PushOperationResult resultToCompare
;
110 if (confirmPage
.isShowOnlyIfChangedSelected())
111 resultToCompare
= confirmPage
.getConfirmedResult();
113 resultToCompare
= null;
114 final Job job
= new PushJob(operation
, resultToCompare
,
115 getDestinationString());
124 public String
getWindowTitle() {
125 final IWizardPage currentPage
= getContainer().getCurrentPage();
126 if (currentPage
== repoPage
|| currentPage
== null)
127 return UIText
.PushWizard_windowTitleDefault
;
128 final String destination
= getDestinationString();
129 return NLS
.bind(UIText
.PushWizard_windowTitleWithDestination
,
133 private void saveRefSpecs() {
134 final RemoteConfig rc
= repoPage
.getSelection().getConfig();
135 rc
.setPushRefSpecs(refSpecPage
.getRefSpecs());
136 final RepositoryConfig config
= localDb
.getConfig();
140 } catch (final IOException e
) {
141 ErrorDialog
.openError(getShell(), UIText
.PushWizard_cantSaveTitle
,
142 UIText
.PushWizard_cantSaveMessage
, new Status(
143 IStatus
.WARNING
, Activator
.getPluginId(), e
145 // Continue, it's not critical.
149 private PushOperation
createPushOperation() {
151 final PushOperationSpecification spec
;
152 final RemoteConfig config
= repoPage
.getSelection().getConfig();
153 if (confirmPage
.isConfirmed()) {
154 final PushOperationResult confirmedResult
= confirmPage
155 .getConfirmedResult();
156 spec
= confirmedResult
.deriveSpecification(confirmPage
157 .isRequireUnchangedSelected());
159 final Collection
<RefSpec
> fetchSpecs
;
161 fetchSpecs
= config
.getFetchRefSpecs();
165 final Collection
<RemoteRefUpdate
> updates
= Transport
166 .findRemoteRefUpdatesFor(localDb
, refSpecPage
167 .getRefSpecs(), fetchSpecs
);
168 if (updates
.isEmpty()) {
169 ErrorDialog
.openError(getShell(),
170 UIText
.PushWizard_missingRefsTitle
, null,
171 new Status(IStatus
.ERROR
, Activator
.getPluginId(),
172 UIText
.PushWizard_missingRefsMessage
));
176 spec
= new PushOperationSpecification();
177 for (final URIish uri
: repoPage
.getSelection().getAllURIs())
178 spec
.addURIRefUpdates(uri
, ConfirmationPage
179 .copyUpdates(updates
));
181 return new PushOperation(localDb
, spec
, false, config
);
182 } catch (final IOException e
) {
183 ErrorDialog
.openError(getShell(),
184 UIText
.PushWizard_cantPrepareUpdatesTitle
,
185 UIText
.PushWizard_cantPrepareUpdatesMessage
, new Status(
186 IStatus
.ERROR
, Activator
.getPluginId(), e
192 private String
getDestinationString() {
193 final RepositorySelection repoSelection
= repoPage
.getSelection();
194 final String destination
;
195 if (repoSelection
.isConfigSelected())
196 destination
= repoSelection
.getConfigName();
198 destination
= repoSelection
.getURI().toString();
202 private class PushJob
extends Job
{
203 private final PushOperation operation
;
205 private final PushOperationResult resultToCompare
;
207 private final String destinationString
;
209 public PushJob(final PushOperation operation
,
210 final PushOperationResult resultToCompare
,
211 final String destinationString
) {
212 super(NLS
.bind(UIText
.PushWizard_jobName
, getURIsString(operation
213 .getSpecification().getURIs())));
214 this.operation
= operation
;
215 this.resultToCompare
= resultToCompare
;
216 this.destinationString
= destinationString
;
220 protected IStatus
run(final IProgressMonitor monitor
) {
222 operation
.run(monitor
);
223 } catch (final InvocationTargetException e
) {
224 return new Status(IStatus
.ERROR
, Activator
.getPluginId(),
225 UIText
.PushWizard_unexpectedError
, e
.getCause());
228 final PushOperationResult result
= operation
.getOperationResult();
229 if (!result
.isSuccessfulConnectionForAnyURI()) {
230 return new Status(IStatus
.ERROR
, Activator
.getPluginId(), NLS
231 .bind(UIText
.PushWizard_cantConnectToAny
, result
232 .getErrorStringForAllURis()));
235 if (resultToCompare
== null || !result
.equals(resultToCompare
)) {
236 PlatformUI
.getWorkbench().getDisplay().asyncExec(
239 final Shell shell
= PlatformUI
.getWorkbench()
240 .getActiveWorkbenchWindow().getShell();
241 final Dialog dialog
= new PushResultDialog(
242 shell
, localDb
, result
,
248 return Status
.OK_STATUS
;