Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 36c8a202f171bfaabf812ea0153c8e79a11009d5 (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
/*******************************************************************************
 * Copyright (C) 2012, 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 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.Collection;
import java.util.List;
import java.util.Map;

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.indexdiff.IndexDiffCache;
import org.eclipse.egit.core.internal.indexdiff.IndexDiffCacheEntry;
import org.eclipse.egit.core.internal.job.RuleUtil;
import org.eclipse.egit.core.internal.util.ResourceUtil;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.util.FileUtils;

/**
 * Operation to delete a collection of (untracked) paths, even it they are
 * non-workspace resources.
 */
public class DeletePathsOperation implements IEGitOperation {

	private final Collection<IPath> paths;

	private final ISchedulingRule schedulingRule;

	/**
	 * @param paths
	 *            the files to delete
	 */
	public DeletePathsOperation(final Collection<IPath> paths) {
		this.paths = paths;
		schedulingRule = calculateSchedulingRule();
	}

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

	@Override
	public ISchedulingRule getSchedulingRule() {
		return schedulingRule;
	}

	private void deletePaths(IProgressMonitor monitor) throws CoreException {
		SubMonitor progress = SubMonitor.convert(monitor,
				CoreText.DeleteResourcesOperation_deletingResources,
				paths.size() + 1);
		boolean errorOccurred = false;

		boolean refreshAll = false;
		List<IPath> refreshCachePaths = new ArrayList<IPath>();

		for (IPath path : paths) {
			IResource resource = ResourceUtil.getResourceForLocation(path, false);
			if (resource != null && resource.exists())
				resource.delete(false, progress.newChild(1));
			else {
				File file = path.toFile();
				if (file.exists()) {
					try {
						FileUtils.delete(file, FileUtils.RECURSIVE);
					} catch (IOException e) {
						errorOccurred = true;
						String message = MessageFormat
								.format(CoreText.DeleteResourcesOperation_deleteFailed,
										file.getPath());
						Activator.logError(message, e);
					}
					refreshCachePaths.add(path);
					// Selectively refreshing an IndexDiffCacheEntry only works for files,
					// so refresh all in case of a directory
					if (file.isDirectory())
						refreshAll = true;
				}
				progress.worked(1);
			}
		}

		if (!refreshCachePaths.isEmpty())
			refreshIndexDiffCache(refreshCachePaths, refreshAll);
		progress.worked(1);

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

	private ISchedulingRule calculateSchedulingRule() {
		return RuleUtil.getRuleForContainers(paths);
	}

	private void refreshIndexDiffCache(List<IPath> refreshCachePaths, boolean refreshAll) {
		Map<Repository, Collection<String>> resourcesByRepository = ResourceUtil.splitPathsByRepository(refreshCachePaths);
		for (Map.Entry<Repository, Collection<String>> entry : resourcesByRepository.entrySet()) {
			Repository repository = entry.getKey();
			Collection<String> files = entry.getValue();

			IndexDiffCache cache = Activator.getDefault().getIndexDiffCache();
			IndexDiffCacheEntry cacheEntry = cache.getIndexDiffCacheEntry(repository);
			if (cacheEntry != null)
				if (refreshAll)
					cacheEntry.refresh();
				else
					cacheEntry.refreshFiles(files);
		}
	}
}

Back to the top