1 /******************************************************************************
2 * Copyright (c) 2012, 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 <stephan.hackstedt@googlemail.com - bug 477695
14 *****************************************************************************/
15 package org
.eclipse
.egit
.core
.op
;
17 import org
.eclipse
.core
.resources
.IProject
;
18 import org
.eclipse
.core
.resources
.IWorkspace
;
19 import org
.eclipse
.core
.resources
.IWorkspaceRunnable
;
20 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
21 import org
.eclipse
.core
.runtime
.CoreException
;
22 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
23 import org
.eclipse
.core
.runtime
.SubMonitor
;
24 import org
.eclipse
.core
.runtime
.jobs
.ISchedulingRule
;
25 import org
.eclipse
.egit
.core
.Activator
;
26 import org
.eclipse
.egit
.core
.internal
.job
.RuleUtil
;
27 import org
.eclipse
.egit
.core
.internal
.util
.ProjectUtil
;
28 import org
.eclipse
.jgit
.api
.Git
;
29 import org
.eclipse
.jgit
.api
.StashApplyCommand
;
30 import org
.eclipse
.jgit
.api
.errors
.GitAPIException
;
31 import org
.eclipse
.jgit
.api
.errors
.JGitInternalException
;
32 import org
.eclipse
.jgit
.lib
.Repository
;
33 import org
.eclipse
.jgit
.merge
.MergeStrategy
;
34 import org
.eclipse
.jgit
.revwalk
.RevCommit
;
35 import org
.eclipse
.team
.core
.TeamException
;
38 * Operation that applies a stashed commit in a repository
40 public class StashApplyOperation
implements IEGitOperation
{
42 private final Repository repository
;
44 private final RevCommit commit
;
47 * Create operation for repository
52 public StashApplyOperation(final Repository repository
,
53 final RevCommit commit
) {
54 this.repository
= repository
;
59 public void execute(IProgressMonitor monitor
) throws CoreException
{
60 IWorkspaceRunnable action
= new IWorkspaceRunnable() {
63 public void run(IProgressMonitor pm
) throws CoreException
{
64 SubMonitor progress
= SubMonitor
.convert(pm
, 3);
66 IProject
[] validProjects
= ProjectUtil
67 .getValidOpenProjects(repository
);
69 StashApplyCommand command
= Git
.wrap(repository
)
70 .stashApply().setStashRef(commit
.name());
71 MergeStrategy strategy
= Activator
.getDefault()
72 .getPreferredMergeStrategy();
73 if (strategy
!= null) {
74 command
.setStrategy(strategy
);
78 ProjectUtil
.refreshValidProjects(validProjects
,
79 progress
.newChild(1));
80 } catch (JGitInternalException e
) {
81 throw new TeamException(e
.getLocalizedMessage(),
83 } catch (GitAPIException e
) {
84 throw new TeamException(e
.getLocalizedMessage(),
89 ResourcesPlugin
.getWorkspace().run(action
, getSchedulingRule(),
90 IWorkspace
.AVOID_UPDATE
, monitor
);
94 public ISchedulingRule
getSchedulingRule() {
95 return RuleUtil
.getRule(repository
);