1 /******************************************************************************
2 * Copyright (c) 2011, 2015 GitHub Inc and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License 2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-2.0/
8 * SPDX-License-Identifier: EPL-2.0
11 * Kevin Sawicki (GitHub Inc.) - initial API and implementation
12 * Laurent Delaigue (Obeo) - use of preferred merge strategy
13 * Stephan Hackstedt - Bug 477695
14 *****************************************************************************/
15 package org
.eclipse
.egit
.core
.op
;
17 import java
.text
.MessageFormat
;
19 import org
.eclipse
.core
.resources
.IWorkspace
;
20 import org
.eclipse
.core
.resources
.IWorkspaceRunnable
;
21 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
22 import org
.eclipse
.core
.runtime
.CoreException
;
23 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
24 import org
.eclipse
.core
.runtime
.SubMonitor
;
25 import org
.eclipse
.core
.runtime
.jobs
.ISchedulingRule
;
26 import org
.eclipse
.egit
.core
.Activator
;
27 import org
.eclipse
.egit
.core
.internal
.CoreText
;
28 import org
.eclipse
.egit
.core
.internal
.job
.RuleUtil
;
29 import org
.eclipse
.egit
.core
.internal
.util
.ProjectUtil
;
30 import org
.eclipse
.jgit
.api
.CherryPickCommand
;
31 import org
.eclipse
.jgit
.api
.CherryPickResult
;
32 import org
.eclipse
.jgit
.api
.Git
;
33 import org
.eclipse
.jgit
.api
.errors
.GitAPIException
;
34 import org
.eclipse
.jgit
.lib
.Repository
;
35 import org
.eclipse
.jgit
.merge
.MergeStrategy
;
36 import org
.eclipse
.jgit
.revwalk
.RevCommit
;
37 import org
.eclipse
.team
.core
.TeamException
;
40 * Cherry pick operation
42 public class CherryPickOperation
implements IEGitOperation
{
44 private final Repository repo
;
46 private final RevCommit commit
;
48 private int parentIndex
= -1;
50 private CherryPickResult result
;
53 * Create cherry pick operation
58 public CherryPickOperation(Repository repository
, RevCommit commit
) {
59 this.repo
= repository
;
64 * Defines the parent to diff against if the commit is a merge commit.
65 * Ignored if the commit has only one parent.
68 * defining the diff, zero-based
70 public void setMainlineIndex(int parentIndex
) {
71 if (parentIndex
>= 0 && parentIndex
< commit
.getParentCount()) {
72 this.parentIndex
= parentIndex
;
77 * @return cherry pick result
79 public CherryPickResult
getResult() {
84 public void execute(IProgressMonitor m
) throws CoreException
{
85 IWorkspaceRunnable action
= new IWorkspaceRunnable() {
88 public void run(IProgressMonitor pm
) throws CoreException
{
89 SubMonitor progress
= SubMonitor
.convert(pm
, 2);
91 progress
.subTask(MessageFormat
.format(
92 CoreText
.CherryPickOperation_cherryPicking
,
94 try (Git git
= new Git(repo
)) {
95 CherryPickCommand command
= git
.cherryPick()
96 .include(commit
.getId());
97 MergeStrategy strategy
= Activator
.getDefault()
98 .getPreferredMergeStrategy();
99 if (strategy
!= null) {
100 command
.setStrategy(strategy
);
103 && parentIndex
< commit
.getParentCount()) {
104 command
.setMainlineParentNumber(parentIndex
+ 1);
106 result
= command
.call();
107 } catch (GitAPIException e
) {
108 throw new TeamException(e
.getLocalizedMessage(),
113 ProjectUtil
.refreshValidProjects(
114 ProjectUtil
.getValidOpenProjects(repo
),
115 progress
.newChild(1));
118 ResourcesPlugin
.getWorkspace().run(action
, getSchedulingRule(),
119 IWorkspace
.AVOID_UPDATE
, m
);
123 public ISchedulingRule
getSchedulingRule() {
124 return RuleUtil
.getRule(repo
);