Git Repositories View: Simple fetch and push
[egit/spearce.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / fetch / FetchConfiguredRemoteAction.java
blob84328ac51fe8690703dbb5dbb58dd8c9da83fdbe
1 /*******************************************************************************
2 * Copyright (c) 2010 SAP AG.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
8 * Contributors:
9 * Mathias Kinzler (SAP AG) - initial implementation
10 *******************************************************************************/
11 package org.eclipse.egit.ui.internal.fetch;
13 import java.io.IOException;
14 import java.net.URISyntaxException;
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.EclipseGitProgressTransformer;
21 import org.eclipse.egit.ui.Activator;
22 import org.eclipse.egit.ui.UIText;
23 import org.eclipse.jface.dialogs.Dialog;
24 import org.eclipse.jgit.errors.NotSupportedException;
25 import org.eclipse.jgit.errors.TransportException;
26 import org.eclipse.jgit.lib.Repository;
27 import org.eclipse.jgit.transport.FetchResult;
28 import org.eclipse.jgit.transport.RemoteConfig;
29 import org.eclipse.jgit.transport.Transport;
30 import org.eclipse.osgi.util.NLS;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.ui.PlatformUI;
34 /**
35 * Fetches from a remote as configured
37 public class FetchConfiguredRemoteAction {
39 private final Repository repository;
41 private final String remoteName;
43 /**
44 * The default constructor
46 * @param repository
47 * a {@link Repository}
48 * @param remoteName
49 * the name of a remote as configured for fetching
51 public FetchConfiguredRemoteAction(Repository repository, String remoteName) {
52 this.repository = repository;
53 this.remoteName = remoteName;
56 /**
57 * Runs this action
58 * <p>
60 * @param shell
61 * a shell may be null; if provided, a pop up will be displayed
62 * indicating the fetch result; if Exceptions occur, these will
63 * be displayed
66 public void run(final Shell shell) {
67 final RemoteConfig config;
68 Exception prepareException = null;
69 final Transport transport;
70 try {
71 config = new RemoteConfig(repository.getConfig(), remoteName);
72 if (config.getURIs().isEmpty()) {
73 throw new IOException(
74 NLS.bind(
75 UIText.FetchConfiguredRemoteAction_NoUrisDefinedMessage,
76 remoteName));
78 if (config.getFetchRefSpecs().isEmpty()) {
79 throw new IOException(
80 NLS.bind(
81 UIText.FetchConfiguredRemoteAction_NoSpecsDefinedMessage,
82 remoteName));
84 transport = Transport.open(repository, config);
85 } catch (URISyntaxException e) {
86 prepareException = e;
87 return;
88 } catch (IOException e) {
89 prepareException = e;
90 return;
91 } finally {
92 if (prepareException != null)
93 Activator.handleError(prepareException.getMessage(),
94 prepareException, shell != null);
97 Job job = new Job(
98 "Fetch from " + repository.getDirectory().getParentFile().getName() + " - " + remoteName) { //$NON-NLS-1$ //$NON-NLS-2$
100 @Override
101 protected IStatus run(IProgressMonitor monitor) {
102 final FetchResult result;
103 Exception fetchException = null;
104 try {
105 result = transport.fetch(new EclipseGitProgressTransformer(
106 monitor), config.getFetchRefSpecs());
107 } catch (final NotSupportedException e) {
108 fetchException = e;
109 return new Status(IStatus.ERROR, Activator.getPluginId(),
110 UIText.FetchWizard_fetchNotSupported, e);
111 } catch (final TransportException e) {
112 if (monitor.isCanceled())
113 return Status.CANCEL_STATUS;
114 fetchException = e;
115 return new Status(IStatus.ERROR, Activator.getPluginId(),
116 UIText.FetchWizard_transportError, e);
117 } finally {
118 if (fetchException != null)
119 Activator.handleError(fetchException.getMessage(),
120 fetchException, shell != null);
122 if (shell != null) {
123 PlatformUI.getWorkbench().getDisplay().asyncExec(
124 new Runnable() {
125 public void run() {
126 Dialog dialog = new FetchResultDialog(
127 shell, repository, result,
128 repository.getDirectory()
129 .getParentFile().getName()
130 + " - " + remoteName); //$NON-NLS-1$
131 dialog.open();
135 return Status.OK_STATUS;
140 job.setUser(true);
141 job.schedule();