From 48f56df8ecd4f9dd9974c04ef845b271cf5d6560 Mon Sep 17 00:00:00 2001 From: Mathias Kinzler Date: Wed, 19 May 2010 11:41:19 +0200 Subject: [PATCH] Git Repositories View: Simple fetch and push This allows to execute fetch and push by a simple context menu on the fetch/push specification in the Git Repositories View. The actions provided here might also be helpful for Bug: 312586 Change-Id: I275a0c01c0fcec3e7e9a9d4e5c1b3d3a03df940d Signed-off-by: Mathias Kinzler Signed-off-by: Matthias Sohn --- .../src/org/eclipse/egit/ui/UIText.java | 21 +++ .../fetch/FetchConfiguredRemoteAction.java | 143 ++++++++++++++++++++ .../internal/push/PushConfiguredRemoteAction.java | 150 +++++++++++++++++++++ .../ui/internal/repository/RepositoriesView.java | 26 +++- .../src/org/eclipse/egit/ui/uitext.properties | 7 + 5 files changed, 346 insertions(+), 1 deletion(-) create mode 100644 org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/fetch/FetchConfiguredRemoteAction.java create mode 100644 org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/push/PushConfiguredRemoteAction.java diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java index f12dc6a1..45f5690f 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java @@ -859,6 +859,15 @@ public class UIText extends NLS { public static String PushAction_wrongURITitle; /** */ + public static String PushConfiguredRemoteAction_NoSpecDefined; + + /** */ + public static String PushConfiguredRemoteAction_NoUpdatesFoundMessage; + + /** */ + public static String PushConfiguredRemoteAction_NoUrisMessage; + + /** */ public static String PushWizard_cantConnectToAny; /** */ @@ -1255,6 +1264,12 @@ public class UIText extends NLS { public static String FetchAction_wrongURIMessage; /** */ + public static String FetchConfiguredRemoteAction_NoSpecsDefinedMessage; + + /** */ + public static String FetchConfiguredRemoteAction_NoUrisDefinedMessage; + + /** */ public static String FetchResultDialog_labelEmptyResult; /** */ @@ -1663,6 +1678,9 @@ public class UIText extends NLS { public static String RepositoriesView_DeleteRepoDeterminProjectsMessage; /** */ + public static String RepositoriesView_DoPushMenuItem; + + /** */ public static String RepositoriesView_Error_WindowTitle; /** */ @@ -1672,6 +1690,9 @@ public class UIText extends NLS { public static String RepositoriesView_ExistingProjects_Nodetext; /** */ + public static String RepositoriesView_FetchMenu; + + /** */ public static String RepositoriesView_Import_Button; /** */ diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/fetch/FetchConfiguredRemoteAction.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/fetch/FetchConfiguredRemoteAction.java new file mode 100644 index 00000000..84328ac5 --- /dev/null +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/fetch/FetchConfiguredRemoteAction.java @@ -0,0 +1,143 @@ +/******************************************************************************* + * Copyright (c) 2010 SAP AG. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathias Kinzler (SAP AG) - initial implementation + *******************************************************************************/ +package org.eclipse.egit.ui.internal.fetch; + +import java.io.IOException; +import java.net.URISyntaxException; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.egit.core.EclipseGitProgressTransformer; +import org.eclipse.egit.ui.Activator; +import org.eclipse.egit.ui.UIText; +import org.eclipse.jface.dialogs.Dialog; +import org.eclipse.jgit.errors.NotSupportedException; +import org.eclipse.jgit.errors.TransportException; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.transport.FetchResult; +import org.eclipse.jgit.transport.RemoteConfig; +import org.eclipse.jgit.transport.Transport; +import org.eclipse.osgi.util.NLS; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.PlatformUI; + +/** + * Fetches from a remote as configured + */ +public class FetchConfiguredRemoteAction { + + private final Repository repository; + + private final String remoteName; + + /** + * The default constructor + * + * @param repository + * a {@link Repository} + * @param remoteName + * the name of a remote as configured for fetching + */ + public FetchConfiguredRemoteAction(Repository repository, String remoteName) { + this.repository = repository; + this.remoteName = remoteName; + } + + /** + * Runs this action + *

+ * + * @param shell + * a shell may be null; if provided, a pop up will be displayed + * indicating the fetch result; if Exceptions occur, these will + * be displayed + * + */ + public void run(final Shell shell) { + final RemoteConfig config; + Exception prepareException = null; + final Transport transport; + try { + config = new RemoteConfig(repository.getConfig(), remoteName); + if (config.getURIs().isEmpty()) { + throw new IOException( + NLS.bind( + UIText.FetchConfiguredRemoteAction_NoUrisDefinedMessage, + remoteName)); + } + if (config.getFetchRefSpecs().isEmpty()) { + throw new IOException( + NLS.bind( + UIText.FetchConfiguredRemoteAction_NoSpecsDefinedMessage, + remoteName)); + } + transport = Transport.open(repository, config); + } catch (URISyntaxException e) { + prepareException = e; + return; + } catch (IOException e) { + prepareException = e; + return; + } finally { + if (prepareException != null) + Activator.handleError(prepareException.getMessage(), + prepareException, shell != null); + } + + Job job = new Job( + "Fetch from " + repository.getDirectory().getParentFile().getName() + " - " + remoteName) { //$NON-NLS-1$ //$NON-NLS-2$ + + @Override + protected IStatus run(IProgressMonitor monitor) { + final FetchResult result; + Exception fetchException = null; + try { + result = transport.fetch(new EclipseGitProgressTransformer( + monitor), config.getFetchRefSpecs()); + } catch (final NotSupportedException e) { + fetchException = e; + return new Status(IStatus.ERROR, Activator.getPluginId(), + UIText.FetchWizard_fetchNotSupported, e); + } catch (final TransportException e) { + if (monitor.isCanceled()) + return Status.CANCEL_STATUS; + fetchException = e; + return new Status(IStatus.ERROR, Activator.getPluginId(), + UIText.FetchWizard_transportError, e); + } finally { + if (fetchException != null) + Activator.handleError(fetchException.getMessage(), + fetchException, shell != null); + } + if (shell != null) { + PlatformUI.getWorkbench().getDisplay().asyncExec( + new Runnable() { + public void run() { + Dialog dialog = new FetchResultDialog( + shell, repository, result, + repository.getDirectory() + .getParentFile().getName() + + " - " + remoteName); //$NON-NLS-1$ + dialog.open(); + } + }); + } + return Status.OK_STATUS; + } + + }; + + job.setUser(true); + job.schedule(); + } +} diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/push/PushConfiguredRemoteAction.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/push/PushConfiguredRemoteAction.java new file mode 100644 index 00000000..05ce69b8 --- /dev/null +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/push/PushConfiguredRemoteAction.java @@ -0,0 +1,150 @@ +/******************************************************************************* + * Copyright (c) 2010 SAP AG. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathias Kinzler (SAP AG) - initial implementation + *******************************************************************************/ +package org.eclipse.egit.ui.internal.push; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.net.URISyntaxException; +import java.util.Collection; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.egit.core.op.PushOperation; +import org.eclipse.egit.core.op.PushOperationResult; +import org.eclipse.egit.core.op.PushOperationSpecification; +import org.eclipse.egit.ui.Activator; +import org.eclipse.egit.ui.UIText; +import org.eclipse.jface.dialogs.Dialog; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.transport.RefSpec; +import org.eclipse.jgit.transport.RemoteConfig; +import org.eclipse.jgit.transport.RemoteRefUpdate; +import org.eclipse.jgit.transport.Transport; +import org.eclipse.jgit.transport.URIish; +import org.eclipse.osgi.util.NLS; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.PlatformUI; + +/** + * Push to a remote as configured + */ +public class PushConfiguredRemoteAction { + + private final Repository repository; + + private final String remoteName; + + /** + * The default constructor + * + * @param repository + * a {@link Repository} + * @param remoteName + * the name of a remote as configured for fetching + */ + public PushConfiguredRemoteAction(Repository repository, String remoteName) { + this.repository = repository; + this.remoteName = remoteName; + } + + /** + * Runs this action + *

+ * + * @param shell + * a shell may be null; if provided, a pop up will be displayed + * indicating the fetch result + * @param dryRun + * + */ + public void run(final Shell shell, boolean dryRun) { + RemoteConfig config; + PushOperationSpecification spec; + Exception pushException = null; + final PushOperation op; + try { + config = new RemoteConfig(repository.getConfig(), remoteName); + if (config.getPushURIs().isEmpty()) { + throw new IOException(NLS.bind( + UIText.PushConfiguredRemoteAction_NoUrisMessage, + remoteName)); + } + final Collection pushSpecs = config.getPushRefSpecs(); + if (pushSpecs.isEmpty()) { + throw new IOException(NLS.bind( + UIText.PushConfiguredRemoteAction_NoSpecDefined, + remoteName)); + } + final Collection updates = Transport + .findRemoteRefUpdatesFor(repository, pushSpecs, null); + if (updates.isEmpty()) { + throw new IOException( + NLS.bind( + UIText.PushConfiguredRemoteAction_NoUpdatesFoundMessage, + remoteName)); + } + + spec = new PushOperationSpecification(); + for (final URIish uri : config.getPushURIs()) + spec.addURIRefUpdates(uri, + ConfirmationPage.copyUpdates(updates)); + + op = new PushOperation(repository, spec, dryRun, config); + + } catch (URISyntaxException e) { + pushException = e; + return; + } catch (IOException e) { + pushException = e; + return; + } finally { + if (pushException != null) + Activator.handleError(pushException.getMessage(), + pushException, shell != null); + } + + final Job job = new Job( + "Push to " + repository.getDirectory().getParentFile().getName() + " - " + remoteName) { //$NON-NLS-1$ //$NON-NLS-2$ + + @Override + protected IStatus run(IProgressMonitor monitor) { + try { + op.run(monitor); + final PushOperationResult res = op.getOperationResult(); + if (shell != null) { + PlatformUI.getWorkbench().getDisplay().asyncExec( + new Runnable() { + public void run() { + final Dialog dialog = new PushResultDialog( + shell, repository, res, + repository.getDirectory() + .getParentFile() + .getName() + + " - " + remoteName); //$NON-NLS-1$ + dialog.open(); + } + }); + } + return Status.OK_STATUS; + } catch (InvocationTargetException e) { + return new Status(IStatus.ERROR, Activator.getPluginId(), e + .getCause().getMessage(), e); + } + } + + }; + + job.setUser(true); + job.schedule(); + } +} diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoriesView.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoriesView.java index 703421c0..947d0d3e 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoriesView.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoriesView.java @@ -50,6 +50,8 @@ import org.eclipse.egit.ui.UIIcons; import org.eclipse.egit.ui.UIText; import org.eclipse.egit.ui.internal.clone.GitCloneWizard; import org.eclipse.egit.ui.internal.clone.GitCreateProjectViaWizardWizard; +import org.eclipse.egit.ui.internal.fetch.FetchConfiguredRemoteAction; +import org.eclipse.egit.ui.internal.push.PushConfiguredRemoteAction; import org.eclipse.egit.ui.internal.repository.RepositoryTreeNode.RepositoryTreeNodeType; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IAction; @@ -866,6 +868,18 @@ public class RepositoriesView extends ViewPart implements ISelectionProvider, final String configName = (String) node.getParent().getObject(); + MenuItem doFetch = new MenuItem(men, SWT.PUSH); + doFetch.setText(UIText.RepositoriesView_FetchMenu); + doFetch.addSelectionListener(new SelectionAdapter() { + + @Override + public void widgetSelected(SelectionEvent evt) { + new FetchConfiguredRemoteAction(node.getRepository(), + configName).run(getSite().getShell()); + } + + }); + MenuItem configureUrlFetch = new MenuItem(men, SWT.PUSH); configureUrlFetch .setText(UIText.RepositoriesView_ConfigureFetchMenu); @@ -912,8 +926,18 @@ public class RepositoriesView extends ViewPart implements ISelectionProvider, final String configName = (String) node.getParent().getObject(); - MenuItem configureUrlPush = new MenuItem(men, SWT.PUSH); + MenuItem doPush = new MenuItem(men, SWT.PUSH); + doPush.setText(UIText.RepositoriesView_DoPushMenuItem); + doPush.addSelectionListener(new SelectionAdapter() { + + @Override + public void widgetSelected(SelectionEvent evt) { + new PushConfiguredRemoteAction(node.getRepository(), + configName).run(getSite().getShell(), false); + } + }); + MenuItem configureUrlPush = new MenuItem(men, SWT.PUSH); configureUrlPush.setText(UIText.RepositoriesView_ConfigurePushMenu); configureUrlPush.addSelectionListener(new SelectionAdapter() { diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties index 3cd5337a..260c070e 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties @@ -311,6 +311,9 @@ HistoryPreferencePage_title=Git PushAction_wrongURIDescription=Remote repositories URIs configuration is corrupted. PushAction_wrongURITitle=Corrupted Configuration +PushConfiguredRemoteAction_NoSpecDefined=No Push Specs defined for remote {0} +PushConfiguredRemoteAction_NoUpdatesFoundMessage=No matching updates found for Push Specs defined for remote {0} +PushConfiguredRemoteAction_NoUrisMessage=No Push URIs defined for remote {0} PushWizard_cantConnectToAny=Can't connect to any repository: {0} PushWizard_cantPrepareUpdatesMessage=Can't resolve ref specifications locally (local refs changed?) or create tracking ref update. @@ -450,6 +453,8 @@ ResultDialog_label=Pushed to {0}. FetchAction_wrongURITitle=Corrupted Configuration FetchAction_wrongURIMessage=Remote repositories URIs configuration is corrupted. +FetchConfiguredRemoteAction_NoSpecsDefinedMessage=No Fetch specs defined for remote {0} +FetchConfiguredRemoteAction_NoUrisDefinedMessage=No Fetch URIs defined for remote {0} FetchResultDialog_labelEmptyResult=No ref to fetch from {0} - everything up to date. FetchResultDialog_labelNonEmptyResult=Fetched from {0}. @@ -597,9 +602,11 @@ RepositoriesView_CreateFetch_menu=Create Fetch... RepositoriesView_CreatePush_menu=Create Push... RepositoriesView_DeleteBranchMenu=Delete Branch... RepositoriesView_DeleteRepoDeterminProjectsMessage=Determining projects that must be deleted +RepositoriesView_DoPushMenuItem=Push... RepositoriesView_Error_WindowTitle=Error RepositoriesView_ErrorHeader=Error RepositoriesView_ExistingProjects_Nodetext=Existing Projects +RepositoriesView_FetchMenu=Fetch... RepositoriesView_Import_Button=Import... RepositoriesView_ImportExistingProjects_MenuItem=Import Existing projects... RepositoriesView_ImportProject_MenuItem=Import -- 2.11.4.GIT