Avoid refresh on up-to-date pull operation
[egit/eclipse.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / op / CherryPickOperation.java
blob957691b820d57e7fb91b129daff6ce474d7e3be3
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
10 * Contributors:
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;
39 /**
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;
52 /**
53 * Create cherry pick operation
55 * @param repository
56 * @param commit
58 public CherryPickOperation(Repository repository, RevCommit commit) {
59 this.repo = repository;
60 this.commit = commit;
63 /**
64 * Defines the parent to diff against if the commit is a merge commit.
65 * Ignored if the commit has only one parent.
67 * @param parentIndex
68 * defining the diff, zero-based
70 public void setMainlineIndex(int parentIndex) {
71 if (parentIndex >= 0 && parentIndex < commit.getParentCount()) {
72 this.parentIndex = parentIndex;
76 /**
77 * @return cherry pick result
79 public CherryPickResult getResult() {
80 return result;
83 @Override
84 public void execute(IProgressMonitor m) throws CoreException {
85 IWorkspaceRunnable action = new IWorkspaceRunnable() {
87 @Override
88 public void run(IProgressMonitor pm) throws CoreException {
89 SubMonitor progress = SubMonitor.convert(pm, 2);
91 progress.subTask(MessageFormat.format(
92 CoreText.CherryPickOperation_cherryPicking,
93 commit.name()));
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);
102 if (parentIndex >= 0
103 && parentIndex < commit.getParentCount()) {
104 command.setMainlineParentNumber(parentIndex + 1);
106 result = command.call();
107 } catch (GitAPIException e) {
108 throw new TeamException(e.getLocalizedMessage(),
109 e.getCause());
111 progress.worked(1);
113 ProjectUtil.refreshValidProjects(
114 ProjectUtil.getValidOpenProjects(repo),
115 progress.newChild(1));
118 ResourcesPlugin.getWorkspace().run(action, getSchedulingRule(),
119 IWorkspace.AVOID_UPDATE, m);
122 @Override
123 public ISchedulingRule getSchedulingRule() {
124 return RuleUtil.getRule(repo);