Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 56833344ec01324b070554e7d578fd2e59b190da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*******************************************************************************
 * Copyright (C) 2016, 2017 Thomas Wolf <thomas.wolf@paranor.ch>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.egit.ui.internal.push;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.egit.core.op.PushOperation;
import org.eclipse.egit.core.op.PushOperationResult;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.JobFamilies;
import org.eclipse.egit.ui.UIPreferences;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.egit.ui.internal.jobs.RepositoryJob;
import org.eclipse.jface.action.IAction;
import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.PushResult;
import org.eclipse.jgit.transport.RemoteRefUpdate;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.osgi.util.NLS;

/**
 * Background job for pushing to repositories.
 */
public class PushJob extends RepositoryJob {

	private final PushOperation operation;

	private final PushOperationResult resultToCompare;

	private final String destinationString;

	private final Repository localDb;

	private final boolean showConfigureButton;

	private final @NonNull PushMode pushMode;

	private PushOperationResult operationResult;

	/**
	 * Creates a new {@link PushJob} that performs the given
	 * {@link PushOperation} on the given {@link Repository}.
	 *
	 * @param name
	 *            of the job
	 * @param repository
	 *            to push to
	 * @param operation
	 *            to perform
	 * @param expectedResult
	 *            of the operation
	 * @param destinationString
	 *            describing where to push to
	 * @param showConfigureButton
	 *            whether the result dialog should have a configuration button
	 * @param pushMode
	 *            this push is for
	 */
	public PushJob(String name, Repository repository, PushOperation operation,
			PushOperationResult expectedResult, String destinationString,
			boolean showConfigureButton, @NonNull PushMode pushMode) {
		super(name, UIPreferences.SHOW_PUSH_POPUP_SUCCESS);
		this.operation = operation;
		this.resultToCompare = expectedResult;
		this.destinationString = destinationString;
		this.localDb = repository;
		this.showConfigureButton = showConfigureButton;
		this.pushMode = pushMode;
	}

	@Override
	protected IStatus performJob(final IProgressMonitor monitor) {
		try {
			operation.run(monitor);
		} catch (final InvocationTargetException e) {
			return new Status(IStatus.ERROR, Activator.getPluginId(),
					UIText.PushJob_unexpectedError, e.getCause());
		}

		operationResult = operation.getOperationResult();
		if (!operationResult.isSuccessfulConnectionForAnyURI()) {
			return new Status(IStatus.ERROR, Activator.getPluginId(),
					NLS.bind(UIText.PushJob_cantConnectToAny,
							operationResult.getErrorStringForAllURis()));
		}

		return Status.OK_STATUS;
	}

	@Override
	protected IAction getAction() {
		Repository repo = localDb;
		if (repo != null && (resultToCompare == null
				|| !resultToCompare.equals(operationResult))) {
			return new ShowPushResultAction(repo, operationResult,
					destinationString, showConfigureButton, pushMode);
		}
		return null;
	}

	@Override
	protected IStatus getDeferredStatus() {
		for (URIish uri : operationResult.getURIs()) {
			PushResult outcome = operationResult.getPushResult(uri);
			for (RemoteRefUpdate update : outcome.getRemoteUpdates()) {
				switch (update.getStatus()) {
				case NOT_ATTEMPTED:
				case UP_TO_DATE:
				case OK:
					continue;
				default:
					return new Status(IStatus.ERROR, Activator.getPluginId(),
							IStatus.ERROR, update.getMessage(), null);
				}
			}
		}
		return super.getDeferredStatus();
	}

	@Override
	public boolean belongsTo(Object family) {
		if (JobFamilies.PUSH.equals(family)) {
			return true;
		}
		return super.belongsTo(family);
	}

}

Back to the top