Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 522f153b8763f64c7bf74af9b82d5595e74c19bb (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
/*******************************************************************************
 * Copyright (c) 2010, 2016 SAP AG and others
 * 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:
 *    Christian Halstrick (SAP AG) - initial implementation
 *    Mathias Kinzler (SAP AG) - initial implementation
 *    Robin Rosenberg - Adoption for the history menu
 *    Thomas Wolf <thomas.wolf@paranor.ch> - Bug 495777
 *******************************************************************************/

package org.eclipse.egit.ui.internal.history.command;

import java.io.IOException;
import java.util.List;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.egit.core.op.MergeOperation;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.egit.ui.internal.actions.MergeActionHandler;
import org.eclipse.egit.ui.internal.branch.LaunchFinder;
import org.eclipse.egit.ui.internal.dialogs.BranchSelectionDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RepositoryState;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * Executes the Merge
 */
public class MergeHandler extends AbstractHistoryCommandHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		ObjectId commitId = getSelectedCommitId(event);
		Repository repository = getRepository(event);
		if (repository == null) {
			return null;
		}
		Shell shell = HandlerUtil.getActiveShellChecked(event);

		if (!MergeActionHandler.checkMergeIsPossible(repository, shell)
				|| LaunchFinder.shouldCancelBecauseOfRunningLaunches(repository,
						null)) {
			return null;
		}

		List<Ref> nodes;
		try {
			nodes = getBranchesOfCommit(getSelection(event), repository, true);
		} catch (IOException e) {
			throw new ExecutionException(
					UIText.AbstractHistoryCommitHandler_cantGetBranches,
					e);
		}

		String refName;
		if (nodes.isEmpty()) {
			refName = commitId.getName();
		} else if (nodes.size() == 1) {
			refName = nodes.get(0).getName();
		} else {
			BranchSelectionDialog<Ref> dlg = new BranchSelectionDialog<>(
					shell, nodes,
					UIText.MergeHandler_MergeBranchDialogTitle,
					UIText.MergeHandler_MergeBranchDialogMessage,
					UIText.MergeHandler_MergeBranchDialogButton, SWT.SINGLE);
			if (dlg.open() == Window.OK) {
				refName = dlg.getSelectedNode().getName();
			} else {
				return null;
			}
		}
		MergeOperation op = new MergeOperation(repository, refName);
		MergeActionHandler.doMerge(repository, op, refName);
		return null;
	}

	@Override
	public boolean isEnabled() {
		Repository repository = getRepository(getPage());
		return repository != null
				&& repository.getRepositoryState().equals(RepositoryState.SAFE);
	}

}

Back to the top