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
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
;
36 * Action for selecting a commit and merging it with the current branch.
38 public class MergeAction
extends RepositoryAction
{
41 public void execute(IAction action
) {
42 final Repository repository
= getRepository(true);
43 if (repository
== null)
46 if (!canMerge(repository
))
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
) {
58 protected IStatus
run(IProgressMonitor monitor
) {
60 new MergeOperation(repository
, refName
).execute(monitor
);
61 } catch (final CoreException e
) {
62 getShell().getDisplay().asyncExec(new Runnable(){
64 Utils
.handleError(getShell(), e
, "Merge impossible", "Unsupported Operation"); //$NON-NLS-1$ //$NON-NLS-2$
67 return Status
.OK_STATUS
;
77 private boolean canMerge(final Repository repository
) {
78 String message
= null;
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
))
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);
101 public boolean isEnabled() {
102 boolean enabled
= true;
103 Repository repository
= getRepository(false);
104 if (repository
== null)