Skip to main content
summaryrefslogtreecommitdiffstats
blob: 46e8ab1dec5654f92cd57b40144e733bfab14273 (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
174
175
176
177
178
179
180
181
182
183
184
185
/*******************************************************************************
 * Copyright (C) 2011, Mathias Kinzler <mathias.kinzler@sap.com>
 *
 * 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
 *******************************************************************************/
package org.eclipse.egit.ui.internal.history.command;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

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.egit.core.op.DeleteBranchOperation;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.egit.ui.internal.dialogs.BranchSelectionDialog;
import org.eclipse.egit.ui.internal.dialogs.UnmergedBranchDialog;
import org.eclipse.egit.ui.internal.history.GitHistoryPage;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;

/**
 * Delete a branch pointing to a commit.
 */
public class DeleteBranchOnCommitHandler extends AbstractHistoryCommandHandler {
	public Object execute(ExecutionEvent event) throws ExecutionException {
		final Repository repository = getRepository(event);
		if (repository == null)
			return null;

		IStructuredSelection selection = getSelection(event);

		int totalBranchCount;
		List<Ref> branchesOfCommit;
		try {
			totalBranchCount = getBranchesOfCommit(selection, repository, false)
					.size();
			branchesOfCommit = getBranchesOfCommit(selection, repository, true);
		} catch (IOException e) {
			throw new ExecutionException("Could not obtain current Branch", e); //$NON-NLS-1$
		}
		// this should have been checked by isEnabled()
		if (branchesOfCommit.isEmpty())
			return null;

		final List<Ref> unmergedBranches = new ArrayList<Ref>();
		final Shell shell = getPart(event).getSite().getShell();

		final List<Ref> branchesToDelete;
		// we will show the dialog if there are either multiple branches that might be
		// deleted or if one of the branches is the current head (which we can't delete);
		// in the latter case, we may show the dialog even if there is only one branch to
		// delete instead of quietly deleting an unexpected one, for example a remote
		// tracking branch
		if (totalBranchCount > 1) {
			BranchSelectionDialog<Ref> dlg = new BranchSelectionDialog<Ref>(
					shell,
					branchesOfCommit,
					UIText.DeleteBranchOnCommitHandler_SelectBranchDialogTitle,
					UIText.DeleteBranchOnCommitHandler_SelectBranchDialogMessage,
					SWT.MULTI);
			if (dlg.open() != Window.OK)
				return null;
			branchesToDelete = dlg.getSelectedNodes();
		} else
			branchesToDelete = branchesOfCommit;

		try {
			new ProgressMonitorDialog(shell).run(true, false,
					new IRunnableWithProgress() {
						public void run(final IProgressMonitor monitor)
								throws InvocationTargetException,
								InterruptedException {
							try {
								monitor.beginTask(
										UIText.DeleteBranchCommand_DeletingBranchesProgress,
										branchesToDelete.size());
								for (Ref refNode : branchesToDelete) {
									int result = deleteBranch(repository,
											refNode, false);
									if (result == DeleteBranchOperation.REJECTED_CURRENT) {
										throw new CoreException(
												Activator
														.createErrorStatus(
																UIText.DeleteBranchCommand_CannotDeleteCheckedOutBranch,
																null));
									} else if (result == DeleteBranchOperation.REJECTED_UNMERGED) {
										unmergedBranches.add(refNode);
									} else
										monitor.worked(1);
								}
							} catch (CoreException ex) {
								throw new InvocationTargetException(ex);
							} finally {
								monitor.done();
							}
						}
					});
		} catch (InvocationTargetException e1) {
			Activator.handleError(
					UIText.RepositoriesView_BranchDeletionFailureMessage,
					e1.getCause(), true);
		} catch (InterruptedException e1) {
			// ignore
		}

		if (!unmergedBranches.isEmpty()) {
			MessageDialog messageDialog = new UnmergedBranchDialog<Ref>(shell,
					unmergedBranches);
			if (messageDialog.open() == Window.OK) {
				try {
					new ProgressMonitorDialog(shell).run(true, false,
							new IRunnableWithProgress() {
								public void run(final IProgressMonitor monitor)
										throws InvocationTargetException,
										InterruptedException {
									try {
										monitor.beginTask(
												UIText.DeleteBranchCommand_DeletingBranchesProgress,
												unmergedBranches.size());
										for (Ref node : unmergedBranches) {
											deleteBranch(repository, node, true);
											monitor.worked(1);
										}
									} catch (CoreException ex) {
										throw new InvocationTargetException(ex);
									} finally {
										monitor.done();
									}
								}
							});
				} catch (InvocationTargetException e1) {
					Activator
							.handleError(
									UIText.RepositoriesView_BranchDeletionFailureMessage,
									e1.getCause(), true);
				} catch (InterruptedException e1) {
					// ignore
				}
			}
		}

		return null;
	}

	private int deleteBranch(Repository repo, final Ref ref, boolean force)
			throws CoreException {
		DeleteBranchOperation dbop = new DeleteBranchOperation(repo, ref, force);
		dbop.execute(null);
		return dbop.getStatus();
	}

	@Override
	public boolean isEnabled() {
		GitHistoryPage page = getPage();

		Repository repository = getRepository(page);
		if (repository == null)
			return false;

		List<Ref> branchesOfCommit;
		try {
			branchesOfCommit = getBranchesOfCommit(getSelection(page),
					repository, true);
		} catch (IOException e) {
			Activator.logError("Could not calculate Enablement", e); //$NON-NLS-1$
			return false;
		}
		return !branchesOfCommit.isEmpty();
	}
}

Back to the top