Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3969f815c07fb3773d97b50b7aea08523ecb6071 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/******************************************************************************
 *  Copyright (C) 2012, 2013 GitHub Inc. and others.
 *  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
 *
 *  Contributors:
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
 *****************************************************************************/
package org.eclipse.egit.ui.internal.repository.tree.command;

import java.text.MessageFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.CoreException;
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.StashDropOperation;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.JobFamilies;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.egit.ui.internal.commit.CommitEditorInput;
import org.eclipse.egit.ui.internal.repository.tree.StashedCommitNode;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

/**
 * Command to drop one or all stashed commits
 */
public class StashDropCommand extends
		RepositoriesViewCommandHandler<StashedCommitNode> {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		final List<StashedCommitNode> nodes = getSelectedNodes(event);
		if (nodes.isEmpty())
			return null;

		final Repository repo = nodes.get(0).getRepository();
		if (repo == null)
			return null;

		// Confirm deletion of selected nodes
		final AtomicBoolean confirmed = new AtomicBoolean();
		final Shell shell = getActiveShell(event);
		shell.getDisplay().syncExec(new Runnable() {

			@Override
			public void run() {
				String message;
				if (nodes.size() > 1)
					message = MessageFormat.format(
							UIText.StashDropCommand_confirmMultiple,
							Integer.toString(nodes.size()));
				else
					message = MessageFormat.format(
							UIText.StashDropCommand_confirmSingle,
							Integer.toString(nodes.get(0).getIndex()));

				confirmed.set(MessageDialog.openConfirm(shell,
						UIText.StashDropCommand_confirmTitle, message));
			}
		});
		if (!confirmed.get())
			return null;

		Job job = new Job(UIText.StashDropCommand_jobTitle) {

			@Override
			protected IStatus run(IProgressMonitor monitor) {
				monitor.beginTask(UIText.StashDropCommand_jobTitle,
						nodes.size());

				// Sort by highest to lowest stash commit index.
				// This avoids shifting problems that cause the indices of the
				// selected nodes not match the indices in the repository
				Collections.sort(nodes, new Comparator<StashedCommitNode>() {

					@Override
					public int compare(StashedCommitNode n1,
							StashedCommitNode n2) {
						return n1.getIndex() < n2.getIndex() ? 1 : -1;
					}
				});

				for (StashedCommitNode node : nodes) {
					final int index = node.getIndex();
					if (index < 0)
						return null;
					final RevCommit commit = node.getObject();
					if (commit == null)
						return null;
					final String stashName = node.getObject().getName();
					final StashDropOperation op = new StashDropOperation(repo,
							node.getIndex());
					monitor.subTask(stashName);
					try {
						op.execute(monitor);
					} catch (CoreException e) {
						Activator.logError(MessageFormat.format(
								UIText.StashDropCommand_dropFailed,
								node.getObject().name()), e);
					}
					tryToCloseEditor(node);
					monitor.worked(1);
				}
				monitor.done();
				return Status.OK_STATUS;
			}

			private void tryToCloseEditor(final StashedCommitNode node) {
				Display.getDefault().asyncExec(new Runnable() {

					@Override
					public void run() {
						IWorkbenchPage activePage = PlatformUI.getWorkbench()
								.getActiveWorkbenchWindow().getActivePage();
						IEditorReference[] editorReferences = activePage
								.getEditorReferences();
						for (IEditorReference editorReference : editorReferences) {
							IEditorInput editorInput = null;
							try {
								editorInput = editorReference.getEditorInput();
							} catch (PartInitException e) {
								Activator.handleError(e.getMessage(), e, true);
							}
							if (editorInput instanceof CommitEditorInput) {
								CommitEditorInput comEditorInput = (CommitEditorInput) editorInput;
								if (comEditorInput.getCommit().getRevCommit()
										.equals(node.getObject())) {
									activePage.closeEditor(
											editorReference.getEditor(false),
											false);
								}
							}
						}
					}
				});

			}

			@Override
			public boolean belongsTo(Object family) {
				if (JobFamilies.STASH.equals(family))
					return true;
				return super.belongsTo(family);
			}
		};
		job.setUser(true);
		job.setRule((new StashDropOperation(repo, nodes.get(0).getIndex()))
				.getSchedulingRule());
		job.schedule();
		return null;
	}
}

Back to the top