Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 46508b2fed7b59117d568ec17b21be37ca0bed75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*******************************************************************************
 * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com>
 * Copyright (C) 2010, Roland Grunberg <rgrunber@redhat.com>
 * Copyright (C) 2012, 2014 Robin Stocker <robin@nibor.org>
 * Copyright (C) 2015, Stephan Hackstedt <stephan.hackstedt@googlemail.com>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Code extracted from org.eclipse.egit.ui.internal.actions.DiscardChangesAction
 * and reworked.
 *******************************************************************************/
package org.eclipse.egit.core.op;

import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.egit.core.Activator;
import org.eclipse.egit.core.internal.CoreText;
import org.eclipse.egit.core.internal.job.RuleUtil;
import org.eclipse.egit.core.internal.util.ProjectUtil;
import org.eclipse.egit.core.internal.util.ResourceUtil;
import org.eclipse.jgit.api.CheckoutCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;

/**
 * The operation discards changes on a set of resources (checkout with paths).
 * In case of a folder resource all file resources in the sub tree are
 * processed. Untracked files are ignored.
 */
public class DiscardChangesOperation implements IEGitOperation {

	private final Map<Repository, Collection<String>> pathsByRepository;
	private final ISchedulingRule schedulingRule;

	private String revision;

	private Stage stage;

	/**
	 * The index stage to check out for conflicting files.
	 */
	public enum Stage {
		/**
		 * "base" stage
		 */
		BASE(CheckoutCommand.Stage.BASE),
		/**
		 * "ours" stage
		 */
		OURS(CheckoutCommand.Stage.OURS),
		/**
		 * "theirs" stage
		 */
		THEIRS(CheckoutCommand.Stage.THEIRS);

		private CheckoutCommand.Stage checkoutStage;

		private Stage(CheckoutCommand.Stage checkoutStage) {
			this.checkoutStage = checkoutStage;
		}
	}

	/**
	 * Construct a {@link DiscardChangesOperation} object.
	 *
	 * @param files
	 */
	public DiscardChangesOperation(IResource[] files) {
		this(files, null);
	}

	/**
	 * Construct a {@link DiscardChangesOperation} object.
	 *
	 * @param files
	 * @param revision
	 */
	public DiscardChangesOperation(IResource[] files, String revision) {
		this(ResourceUtil.splitResourcesByRepository(files));
		this.revision = revision;
	}

	/**
	 * {@link DiscardChangesOperation} for absolute paths.
	 *
	 * @param paths
	 */
	public DiscardChangesOperation(Collection<IPath> paths) {
		this(ResourceUtil.splitPathsByRepository(paths));
	}

	private DiscardChangesOperation(
			Map<Repository, Collection<String>> pathsByRepository) {
		this.pathsByRepository = pathsByRepository;
		this.schedulingRule = RuleUtil.getRuleForRepositories(pathsByRepository
				.keySet());
	}

	/**
	 * Set the index stage to check out for conflicting files. Not compatible
	 * with revision.
	 *
	 * @param stage
	 */
	public void setStage(Stage stage) {
		if (revision != null)
			throw new IllegalStateException(
					"Either stage or revision can be set, but not both"); //$NON-NLS-1$
		this.stage = stage;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.egit.core.op.IEGitOperation#getSchedulingRule()
	 */
	@Override
	public ISchedulingRule getSchedulingRule() {
		return schedulingRule;
	}

	@Override
	public void execute(IProgressMonitor m) throws CoreException {
		IWorkspaceRunnable action = new IWorkspaceRunnable() {
			@Override
			public void run(IProgressMonitor actMonitor) throws CoreException {
				discardChanges(actMonitor);
			}
		};
		ResourcesPlugin.getWorkspace().run(action, getSchedulingRule(),
				IWorkspace.AVOID_UPDATE, m);
	}

	private void discardChanges(IProgressMonitor monitor) throws CoreException {
		SubMonitor progress = SubMonitor.convert(monitor,
				CoreText.DiscardChangesOperation_discardingChanges,
				pathsByRepository.size() * 2);
		boolean errorOccurred = false;

		for (Entry<Repository, Collection<String>> entry : pathsByRepository
				.entrySet()) {
			Repository repository = entry.getKey();
			Collection<String> paths = entry.getValue();

			try {
				discardChanges(repository, paths);
			} catch (GitAPIException e) {
				errorOccurred = true;
				Activator.logError(
						CoreText.DiscardChangesOperation_discardFailed, e);
			}
			progress.worked(1);

			try {
				ProjectUtil.refreshRepositoryResources(repository, paths,
						progress.newChild(1));
			} catch (CoreException e) {
				errorOccurred = true;
				Activator.logError(
						CoreText.DiscardChangesOperation_refreshFailed, e);
			}
		}

		if (errorOccurred) {
			IStatus status = Activator.error(
					CoreText.DiscardChangesOperation_discardFailedSeeLog, null);
			throw new CoreException(status);
		}
	}

	private void discardChanges(Repository repository, Collection<String> paths)
			throws GitAPIException {
		ResourceUtil.saveLocalHistory(repository);
		try (Git git = new Git(repository)) {
			CheckoutCommand checkoutCommand = git.checkout();
			if (revision != null) {
				checkoutCommand.setStartPoint(revision);
			}
			if (stage != null) {
				checkoutCommand.setStage(stage.checkoutStage);
			}
			if (paths.isEmpty() || paths.contains("")) { //$NON-NLS-1$
				checkoutCommand.setAllPaths(true);
			} else {
				for (String path : paths) {
					checkoutCommand.addPath(path);
				}
			}
			checkoutCommand.call();
		}
	}

}

Back to the top