Add action to trigger (fast-forward) merge
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / actions / MergeAction.java
blob9a90c125eb092c3e9fa5855391d7785ec0576934
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 * Stefan Lay (SAP AG) - initial implementation
10 *******************************************************************************/
12 package org.eclipse.egit.ui.internal.actions;
14 import java.io.IOException;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.core.runtime.jobs.Job;
21 import org.eclipse.egit.core.op.MergeOperation;
22 import org.eclipse.egit.ui.Activator;
23 import org.eclipse.egit.ui.UIText;
24 import org.eclipse.egit.ui.internal.dialogs.MergeTargetSelectionDialog;
25 import org.eclipse.jface.action.IAction;
26 import org.eclipse.jface.dialogs.IDialogConstants;
27 import org.eclipse.jface.dialogs.MessageDialog;
28 import org.eclipse.jgit.lib.Constants;
29 import org.eclipse.jgit.lib.Ref;
30 import org.eclipse.jgit.lib.Repository;
31 import org.eclipse.jgit.lib.RepositoryState;
32 import org.eclipse.osgi.util.NLS;
33 import org.eclipse.team.internal.ui.Utils;
35 /**
36 * Action for selecting a commit and merging it with the current branch.
38 public class MergeAction extends RepositoryAction {
40 @Override
41 public void execute(IAction action) {
42 final Repository repository = getRepository(true);
43 if (repository == null)
44 return;
46 if (!canMerge(repository))
47 return;
49 MergeTargetSelectionDialog mergeTargetSelectionDialog = new MergeTargetSelectionDialog(
50 getShell(), repository);
51 if (mergeTargetSelectionDialog.open() == IDialogConstants.OK_ID) {
53 final String refName = mergeTargetSelectionDialog.getRefName();
55 String jobname = NLS.bind(UIText.MergeAction_JobNameMerge, refName);
56 Job job = new Job(jobname) {
57 @Override
58 protected IStatus run(IProgressMonitor monitor) {
59 try {
60 new MergeOperation(repository, refName).execute(monitor);
61 } catch (final CoreException e) {
62 getShell().getDisplay().asyncExec(new Runnable(){
63 public void run() {
64 Utils.handleError(getShell(), e, "Merge impossible", "Unsupported Operation"); //$NON-NLS-1$ //$NON-NLS-2$
65 }});
67 return Status.OK_STATUS;
70 job.setUser(true);
71 job.schedule();
77 private boolean canMerge(final Repository repository) {
78 String message = null;
79 try {
80 Ref head = repository.getRef(Constants.HEAD);
81 if (head == null || !head.isSymbolic())
82 message = UIText.MergeAction_HeadIsNoBranch;
83 else if (!repository.getRepositoryState().equals(
84 RepositoryState.SAFE))
85 message = NLS.bind(
86 UIText.MergeAction_WrongRepositoryState,
87 repository.getRepositoryState());
88 } catch (IOException e) {
89 Activator.logError(e.getMessage(), e);
90 message = e.getMessage();
93 if (message != null) {
94 MessageDialog.openError(getShell(),
95 UIText.MergeAction_CannotMerge, message);
97 return (message == null);
100 @Override
101 public boolean isEnabled() {
102 boolean enabled = true;
103 Repository repository = getRepository(false);
104 if (repository == null)
105 enabled = false;
106 return enabled;