Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: da20c9f48cdd52ef981d9adae8c8589e71e5d3e1 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*******************************************************************************
 * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
 * Copyright (C) 2006, Shawn O. Pearce <spearce@spearce.org>
 * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com>
 * Copyright (C) 2010, 2011, Mathias Kinzler <mathias.kinzler@sap.com>
 * 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 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.egit.core.op;

import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Stream;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
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.SubMonitor;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.egit.core.Activator;
import org.eclipse.egit.core.EclipseGitProgressTransformer;
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.jgit.annotations.NonNull;
import org.eclipse.jgit.api.CheckoutCommand;
import org.eclipse.jgit.api.CheckoutResult;
import org.eclipse.jgit.api.CheckoutResult.Status;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.CheckoutConflictException;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.treewalk.AbstractTreeIterator;
import org.eclipse.jgit.treewalk.FileTreeIterator;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
import org.eclipse.jgit.treewalk.filter.PathSuffixFilter;
import org.eclipse.jgit.treewalk.filter.TreeFilter;
import org.eclipse.jgit.util.FileUtils;

/**
 * This class implements checkouts of a specific revision. A check is made that
 * this can be done without data loss.
 */
public class BranchOperation implements IEGitOperation {

	private final String target;

	private Repository[] repositories;

	private @NonNull Map<Repository, CheckoutResult> results = new HashMap<>();

	private boolean delete;

	/**
	 * Construct a {@link BranchOperation} object for a {@link Ref}.
	 *
	 * @param repository
	 * @param target
	 *            a {@link Ref} name or {@link RevCommit} id
	 */
	public BranchOperation(Repository repository, String target) {
		this(repository, target, true);
	}

	/**
	 * Construct a {@link BranchOperation} object for a {@link Ref}.
	 *
	 * @param repository
	 * @param target
	 *            a {@link Ref} name or {@link RevCommit} id
	 * @param delete
	 *            true to delete missing projects on new branch, false to close
	 *            them
	 */
	public BranchOperation(Repository repository, String target, boolean delete) {
		this(new Repository[] { repository }, target, delete);
	}

	/**
	 *
	 * @param repositories
	 * @param target
	 * @param delete
	 */
	public BranchOperation(Repository[] repositories, String target,
			boolean delete) {
		this.repositories = repositories;
		this.target = target;
		this.delete = delete;
	}

	@Override
	public void execute(IProgressMonitor m) throws CoreException {
		IWorkspaceRunnable action = new IWorkspaceRunnable() {

			@Override
			public void run(IProgressMonitor pm) throws CoreException {
				try {
					pm.setTaskName(MessageFormat.format(
							CoreText.BranchOperation_performingBranch, target));
					int numberOfRepositories = repositories.length;
					SubMonitor progress = SubMonitor.convert(pm,
							numberOfRepositories * 2);
					for (Repository repository : repositories) {
						CheckoutResult result = checkoutRepository(repository,
								progress.newChild(1), numberOfRepositories > 1);
						if (result.getStatus() == Status.NONDELETED) {
							retryDelete(repository, result.getUndeletedList());
						}
						results.put(repository, result);
					}
					refreshAffectedProjects(
							progress.newChild(numberOfRepositories));
				} finally {
					pm.done();
				}
			}

			public CheckoutResult checkoutRepository(Repository repo,
					IProgressMonitor monitor, boolean logErrors)
					throws CoreException {
				SubMonitor progress = SubMonitor.convert(monitor, 2);
				closeProjectsMissingAfterCheckout(repo, progress.newChild(1));
				try (Git git = new Git(repo)) {
					CheckoutCommand co = git.checkout().setProgressMonitor(
							new EclipseGitProgressTransformer(
									progress.newChild(1)));
					co.setName(target);
					try {
						co.call();
					} catch (CheckoutConflictException e) {
						// Covered by the status return below.
					} catch (JGitInternalException | GitAPIException e) {
						String msg = MessageFormat.format(
								CoreText.BranchOperation_checkoutError,
								target, repo.getDirectory());
						if (logErrors) {
							Activator.logError(msg, e);
						} else {
							throw new CoreException(Activator.error(msg, e));
						}
					}
					return co.getResult();
				}
			}

			private void closeProjectsMissingAfterCheckout(Repository repo,
					IProgressMonitor monitor) throws CoreException {
				IProject[] missing = getMissingProjects(repo, target);

				if (missing.length > 0) {
					SubMonitor closeMonitor = SubMonitor.convert(monitor,
							missing.length);
					closeMonitor.setWorkRemaining(missing.length);
					for (IProject project : missing) {
						closeMonitor.subTask(MessageFormat.format(
								CoreText.BranchOperation_closingMissingProject,
								project.getName()));
						project.close(closeMonitor.newChild(1));
					}
				}
			}

			private void refreshAffectedProjects(IProgressMonitor monitor)
					throws CoreException {
				IProject[] refreshProjects = results.entrySet().stream()
						.map(this::getAffectedProjects)
						.flatMap(arr -> Stream.of(arr)).distinct()
						.toArray(IProject[]::new);
				ProjectUtil.refreshValidProjects(refreshProjects, delete,
						monitor);
			}

			private IProject[] getAffectedProjects(
					Entry<Repository, CheckoutResult> entry) {
				CheckoutResult result = entry.getValue();

				if (result.getStatus() != Status.OK
						&& result.getStatus() != Status.NONDELETED) {
					// the checkout did not succeed
					return new IProject[0];
				}

				Repository repo = entry.getKey();
				List<String> pathsToHandle = new ArrayList<>();
				pathsToHandle.addAll(result.getModifiedList());
				pathsToHandle.addAll(result.getRemovedList());
				pathsToHandle.addAll(result.getConflictList());
				IProject[] refreshProjects = ProjectUtil
						.getProjectsContaining(repo, pathsToHandle);
				return refreshProjects;
			}
		};
		// lock workspace to protect working tree changes
		ResourcesPlugin.getWorkspace().run(action, getSchedulingRule(),
				IWorkspace.AVOID_UPDATE, m);
	}

	@Override
	public ISchedulingRule getSchedulingRule() {
		return RuleUtil.getRuleForRepositories(Arrays.asList(repositories));
	}

	/**
	 * @return the result of the operation
	 */
	@NonNull
	public Map<Repository, CheckoutResult> getResults() {
		return results;
	}

	/**
	 * @param repo
	 * @return return the result specific to a repository
	 */
	public CheckoutResult getResult(Repository repo) {
		return results.get(repo);
	}

	void retryDelete(Repository repo, List<String> pathList) {
		// try to delete, but for a short time only
		long startTime = System.currentTimeMillis();
		for (String path : pathList) {
			if (System.currentTimeMillis() - startTime > 1000) {
				break;
			}
			File fileToDelete = new File(repo.getWorkTree(), path);
			if (fileToDelete.exists()) {
				try {
					// Only files should be passed here, thus
					// we ignore attempt to delete submodules when
					// we switch to a branch without a submodule
					if (!fileToDelete.isFile()) {
						FileUtils.delete(fileToDelete, FileUtils.RETRY);
					}
				} catch (IOException e) {
					// ignore here
				}
			}
		}
	}

	/**
	 * Compute the current projects that will be missing after the given branch
	 * is checked out
	 *
	 * @param repository
	 * @param branch
	 * @return non-null but possibly empty array of missing projects
	 * @throws CoreException
	 */
	private IProject[] getMissingProjects(Repository repository,
			String branch) throws CoreException {
		IProject[] openProjects = ProjectUtil.getValidOpenProjects(repository);
		if (delete || openProjects.length == 0) {
			return new IProject[0];
		}
		ObjectId targetTreeId;
		ObjectId currentTreeId;
		try {
			targetTreeId = repository.resolve(branch + "^{tree}"); //$NON-NLS-1$
			currentTreeId = repository.resolve(Constants.HEAD + "^{tree}"); //$NON-NLS-1$
		} catch (IOException e) {
			return new IProject[0];
		}
		if (targetTreeId == null || currentTreeId == null) {
			return new IProject[0];
		}
		Map<File, IProject> locations = new HashMap<>();
		for (IProject project : openProjects) {
			IPath location = project.getLocation();
			if (location == null) {
				continue;
			}
			location = location
					.append(IProjectDescription.DESCRIPTION_FILE_NAME);
			locations.put(location.toFile(), project);
		}

		List<IProject> toBeClosed = new ArrayList<>();
		File root = repository.getWorkTree();
		try (TreeWalk walk = new TreeWalk(repository)) {
			walk.addTree(targetTreeId);
			walk.addTree(currentTreeId);
			walk.addTree(new FileTreeIterator(repository));
			walk.setRecursive(true);
			walk.setFilter(AndTreeFilter.create(PathSuffixFilter
					.create(IProjectDescription.DESCRIPTION_FILE_NAME),
					TreeFilter.ANY_DIFF));
			while (walk.next()) {
				AbstractTreeIterator targetIter = walk.getTree(0,
						AbstractTreeIterator.class);
				if (targetIter != null) {
					continue;
				}
				AbstractTreeIterator currentIter = walk.getTree(1,
						AbstractTreeIterator.class);
				AbstractTreeIterator workingIter = walk.getTree(2,
						AbstractTreeIterator.class);
				if (currentIter == null || workingIter == null) {
					continue;
				}
				IProject project = locations.get(new File(root, walk
						.getPathString()));
				if (project != null) {
					toBeClosed.add(project);
				}
			}
		} catch (IOException e) {
			return new IProject[0];
		}
		return toBeClosed.toArray(new IProject[0]);
	}
}

Back to the top