Fix missing support for monitor == null
[egit.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / op / MergeOperation.java
blob0b9dfda4cc14aa8000c5a342c01af400df48430e
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 *******************************************************************************/
11 package org.eclipse.egit.core.op;
13 import java.io.IOException;
15 import org.eclipse.core.resources.IWorkspaceRunnable;
16 import org.eclipse.core.resources.ResourcesPlugin;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.NullProgressMonitor;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.core.runtime.SubProgressMonitor;
23 import org.eclipse.core.runtime.jobs.ISchedulingRule;
24 import org.eclipse.egit.core.Activator;
25 import org.eclipse.egit.core.CoreText;
26 import org.eclipse.egit.core.internal.util.ProjectUtil;
27 import org.eclipse.jgit.api.CheckoutConflictException;
28 import org.eclipse.jgit.api.ConcurrentRefUpdateException;
29 import org.eclipse.jgit.api.Git;
30 import org.eclipse.jgit.api.InvalidMergeHeadsException;
31 import org.eclipse.jgit.api.MergeCommand;
32 import org.eclipse.jgit.api.MergeResult;
33 import org.eclipse.jgit.api.NoHeadException;
34 import org.eclipse.jgit.lib.Repository;
35 import org.eclipse.jgit.merge.MergeStrategy;
36 import org.eclipse.osgi.util.NLS;
37 import org.eclipse.team.core.TeamException;
39 /**
40 * This class implements the merge of a ref with the current head
43 public class MergeOperation implements IEGitOperation {
45 private final Repository repository;
47 private final String refName;
49 private MergeStrategy mergeStrategy;
51 private MergeResult mergeResult;
53 /**
54 * @param repository
55 * @param refName name of a commit which should be merged
57 public MergeOperation(Repository repository, String refName) {
58 this.repository = repository;
59 this.refName = refName;
62 /**
63 * Create a MergeOperation object
64 * @param repository
65 * @param refName name of a commit which should be merged
66 * @param mergeStrategy the strategy to use for merge
68 public MergeOperation(Repository repository, String refName,
69 String mergeStrategy) {
70 this.repository = repository;
71 this.refName = refName;
72 if (mergeStrategy != null)
73 this.mergeStrategy = MergeStrategy.get(mergeStrategy);
76 public void execute(IProgressMonitor m) throws CoreException {
77 IProgressMonitor monitor;
78 if (m == null)
79 monitor = new NullProgressMonitor();
80 else
81 monitor = m;
82 IWorkspaceRunnable action = new IWorkspaceRunnable() {
84 public void run(IProgressMonitor mymonitor) throws CoreException {
85 mymonitor.beginTask(NLS.bind(CoreText.MergeOperation_ProgressMerge, refName), 3);
86 Git git = new Git(repository);
87 mymonitor.worked(1);
88 MergeCommand merge;
89 try {
90 merge = git.merge().include(repository.getRef(refName));
91 } catch (IOException e) {
92 throw new TeamException(CoreText.MergeOperation_InternalError, e);
94 if (mergeStrategy != null) {
95 merge.setStrategy(mergeStrategy);
97 try {
98 mergeResult = merge.call();
99 mymonitor.worked(1);
100 if (MergeResult.MergeStatus.FAILED.equals(mergeResult.getMergeStatus()))
101 throw new TeamException(mergeResult.toString());
102 else if (MergeResult.MergeStatus.NOT_SUPPORTED.equals(mergeResult.getMergeStatus()))
103 throw new TeamException(new Status(IStatus.INFO, Activator.getPluginId(), mergeResult.toString()));
104 } catch (NoHeadException e) {
105 throw new TeamException(CoreText.MergeOperation_MergeFailedNoHead, e);
106 } catch (ConcurrentRefUpdateException e) {
107 throw new TeamException(CoreText.MergeOperation_MergeFailedRefUpdate, e);
108 } catch (CheckoutConflictException e) {
109 throw new TeamException(e.getLocalizedMessage(), e.getCause());
110 } catch (InvalidMergeHeadsException e) {
111 throw new TeamException(e.getLocalizedMessage(), e.getCause());
113 ProjectUtil.refreshProjects(repository, new SubProgressMonitor(
114 mymonitor, 1));
115 mymonitor.done();
118 // lock workspace to protect working tree changes
119 ResourcesPlugin.getWorkspace().run(action, monitor);
122 public ISchedulingRule getSchedulingRule() {
123 return ResourcesPlugin.getWorkspace().getRoot();