1 /*******************************************************************************
2 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * Copyright (C) 2008, Google Inc.
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the Eclipse Public License 2.0
8 * which accompanies this distribution, and is available at
9 * https://www.eclipse.org/legal/epl-2.0/
11 * SPDX-License-Identifier: EPL-2.0
12 *******************************************************************************/
13 package org
.eclipse
.egit
.core
.op
;
15 import java
.io
.IOException
;
16 import java
.util
.Collection
;
17 import java
.util
.Collections
;
18 import java
.util
.IdentityHashMap
;
21 import org
.eclipse
.core
.resources
.IProject
;
22 import org
.eclipse
.core
.resources
.IResource
;
23 import org
.eclipse
.core
.runtime
.CoreException
;
24 import org
.eclipse
.core
.runtime
.IPath
;
25 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
26 import org
.eclipse
.core
.runtime
.Path
;
27 import org
.eclipse
.core
.runtime
.SubMonitor
;
28 import org
.eclipse
.core
.runtime
.jobs
.ISchedulingRule
;
29 import org
.eclipse
.egit
.core
.Activator
;
30 import org
.eclipse
.egit
.core
.internal
.CoreText
;
31 import org
.eclipse
.egit
.core
.internal
.job
.RuleUtil
;
32 import org
.eclipse
.egit
.core
.project
.GitProjectData
;
33 import org
.eclipse
.egit
.core
.project
.RepositoryMapping
;
34 import org
.eclipse
.jgit
.dircache
.DirCacheEditor
;
35 import org
.eclipse
.jgit
.lib
.Repository
;
36 import org
.eclipse
.osgi
.util
.NLS
;
39 * Remove one or more existing files/folders from the Git repository.
41 * Accepts a collection of resources (files and/or directories) which should be
42 * removed from the their corresponding Git repositories. Resources in the
43 * collection can be associated with multiple repositories. The operation will
44 * automatically remove each resource from the correct Git repository.
47 * Resources are only scheduled for removal in the index-
50 public class UntrackOperation
implements IEGitOperation
{
51 private final Collection
<?
extends IResource
> rsrcList
;
52 private final Collection
<IPath
> locations
;
54 private Repository db
;
56 private final IdentityHashMap
<Repository
, DirCacheEditor
> edits
;
59 * Create a new operation to stop tracking existing files/folders.
62 * collection of {@link IResource}s which should be removed from
63 * the relevant Git repositories.
65 public UntrackOperation(final Collection
<?
extends IResource
> rsrcs
) {
67 locations
= Collections
.emptyList();
68 edits
= new IdentityHashMap
<Repository
, DirCacheEditor
>();
72 * Create a new operation to stop tracking existing files/folders.
77 * collection of {@link IPath}s which should be removed from the
78 * relevant Git repositories.
80 public UntrackOperation(final Repository repository
,
81 final Collection
<IPath
> locations
) {
82 rsrcList
= Collections
.emptyList();
83 this.locations
= locations
;
85 edits
= new IdentityHashMap
<Repository
, DirCacheEditor
>();
89 public void execute(IProgressMonitor monitor
) throws CoreException
{
90 SubMonitor progress
= SubMonitor
.convert(monitor
, (rsrcList
.size() + locations
.size()) * 2);
91 progress
.setTaskName(CoreText
.UntrackOperation_adding
);
96 for (IResource obj
: rsrcList
) {
100 for (IPath location
: locations
) {
105 progress
.setWorkRemaining(edits
.size());
106 for (Map
.Entry
<Repository
, DirCacheEditor
> e
: edits
.entrySet()) {
107 final DirCacheEditor editor
= e
.getValue();
108 progress
.setTaskName(
109 NLS
.bind(CoreText
.UntrackOperation_writingIndex
,
114 } catch (RuntimeException e
) {
115 throw new CoreException(Activator
.error(CoreText
.UntrackOperation_failed
, e
));
116 } catch (IOException e
) {
117 throw new CoreException(Activator
.error(CoreText
.UntrackOperation_failed
, e
));
119 for (DirCacheEditor editor
: edits
.values()) {
120 if (editor
.getDirCache() != null) {
121 editor
.getDirCache().unlock();
129 public ISchedulingRule
getSchedulingRule() {
130 return RuleUtil
.getRuleForRepositories(rsrcList
.toArray(new IResource
[0]));
133 private void remove(final IResource resource
) throws CoreException
{
134 final IProject proj
= resource
.getProject();
138 final GitProjectData pd
= GitProjectData
.get(proj
);
142 final RepositoryMapping rm
= pd
.getRepositoryMapping(resource
);
146 db
= rm
.getRepository();
148 remove(resource
.getLocation());
151 private void remove(final IPath location
) throws CoreException
{
152 DirCacheEditor e
= edits
.get(db
);
155 e
= db
.lockDirCache().editor();
156 } catch (IOException err
) {
157 throw new CoreException(Activator
.error(CoreText
.UntrackOperation_failed
, err
));
162 IPath dbDir
= new Path(db
.getWorkTree().getAbsolutePath());
163 String path
= location
.makeRelativeTo(dbDir
).toString();
164 if (location
.toFile().isDirectory()) {
165 e
.add(new DirCacheEditor
.DeleteTree(path
));
167 e
.add(new DirCacheEditor
.DeletePath(path
));