1 /*******************************************************************************
2 * Copyright (C) 2012, Robin Stocker <robin@nibor.org>
3 * Copyright (C) 2015, Stephan Hackstedt <stephan.hackstedt@googlemail.com>
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License 2.0
7 * which accompanies this distribution, and is available at
8 * https://www.eclipse.org/legal/epl-2.0/
10 * SPDX-License-Identifier: EPL-2.0
11 *******************************************************************************/
12 package org
.eclipse
.egit
.core
.op
;
15 import java
.io
.IOException
;
16 import java
.text
.MessageFormat
;
17 import java
.util
.ArrayList
;
18 import java
.util
.Collection
;
19 import java
.util
.List
;
22 import org
.eclipse
.core
.resources
.IResource
;
23 import org
.eclipse
.core
.resources
.IWorkspace
;
24 import org
.eclipse
.core
.resources
.IWorkspaceRunnable
;
25 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
26 import org
.eclipse
.core
.runtime
.CoreException
;
27 import org
.eclipse
.core
.runtime
.IPath
;
28 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
29 import org
.eclipse
.core
.runtime
.IStatus
;
30 import org
.eclipse
.core
.runtime
.SubMonitor
;
31 import org
.eclipse
.core
.runtime
.jobs
.ISchedulingRule
;
32 import org
.eclipse
.egit
.core
.Activator
;
33 import org
.eclipse
.egit
.core
.internal
.CoreText
;
34 import org
.eclipse
.egit
.core
.internal
.indexdiff
.IndexDiffCache
;
35 import org
.eclipse
.egit
.core
.internal
.indexdiff
.IndexDiffCacheEntry
;
36 import org
.eclipse
.egit
.core
.internal
.job
.RuleUtil
;
37 import org
.eclipse
.egit
.core
.internal
.util
.ResourceUtil
;
38 import org
.eclipse
.jgit
.lib
.Repository
;
39 import org
.eclipse
.jgit
.util
.FileUtils
;
42 * Operation to delete a collection of (untracked) paths, even it they are
43 * non-workspace resources.
45 public class DeletePathsOperation
implements IEGitOperation
{
47 private final Collection
<IPath
> paths
;
49 private final ISchedulingRule schedulingRule
;
55 public DeletePathsOperation(final Collection
<IPath
> paths
) {
57 schedulingRule
= calculateSchedulingRule();
61 public void execute(IProgressMonitor m
) throws CoreException
{
62 IWorkspaceRunnable action
= new IWorkspaceRunnable() {
64 public void run(IProgressMonitor actMonitor
) throws CoreException
{
65 deletePaths(actMonitor
);
68 ResourcesPlugin
.getWorkspace().run(action
, getSchedulingRule(),
69 IWorkspace
.AVOID_UPDATE
, m
);
73 public ISchedulingRule
getSchedulingRule() {
74 return schedulingRule
;
77 private void deletePaths(IProgressMonitor monitor
) throws CoreException
{
78 SubMonitor progress
= SubMonitor
.convert(monitor
,
79 CoreText
.DeleteResourcesOperation_deletingResources
,
81 boolean errorOccurred
= false;
83 boolean refreshAll
= false;
84 List
<IPath
> refreshCachePaths
= new ArrayList
<IPath
>();
86 for (IPath path
: paths
) {
87 IResource resource
= ResourceUtil
.getResourceForLocation(path
, false);
88 if (resource
!= null && resource
.exists())
89 resource
.delete(false, progress
.newChild(1));
91 File file
= path
.toFile();
94 FileUtils
.delete(file
, FileUtils
.RECURSIVE
);
95 } catch (IOException e
) {
97 String message
= MessageFormat
98 .format(CoreText
.DeleteResourcesOperation_deleteFailed
,
100 Activator
.logError(message
, e
);
102 refreshCachePaths
.add(path
);
103 // Selectively refreshing an IndexDiffCacheEntry only works for files,
104 // so refresh all in case of a directory
105 if (file
.isDirectory())
112 if (!refreshCachePaths
.isEmpty())
113 refreshIndexDiffCache(refreshCachePaths
, refreshAll
);
117 IStatus status
= Activator
.error(
118 CoreText
.DeleteResourcesOperation_deleteFailedSeeLog
, null);
119 throw new CoreException(status
);
123 private ISchedulingRule
calculateSchedulingRule() {
124 return RuleUtil
.getRuleForContainers(paths
);
127 private void refreshIndexDiffCache(List
<IPath
> refreshCachePaths
, boolean refreshAll
) {
128 Map
<Repository
, Collection
<String
>> resourcesByRepository
= ResourceUtil
.splitPathsByRepository(refreshCachePaths
);
129 for (Map
.Entry
<Repository
, Collection
<String
>> entry
: resourcesByRepository
.entrySet()) {
130 Repository repository
= entry
.getKey();
131 Collection
<String
> files
= entry
.getValue();
133 IndexDiffCache cache
= Activator
.getDefault().getIndexDiffCache();
134 IndexDiffCacheEntry cacheEntry
= cache
.getIndexDiffCacheEntry(repository
);
135 if (cacheEntry
!= null)
137 cacheEntry
.refresh();
139 cacheEntry
.refreshFiles(files
);