refactor: simplify collection.toArray()
[egit/eclipse.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / op / UntrackOperation.java
blob34bc1d35e287b81b06e756f56139760337e94a07
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;
19 import java.util.Map;
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;
38 /**
39 * Remove one or more existing files/folders from the Git repository.
40 * <p>
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.
45 * </p>
46 * <p>
47 * Resources are only scheduled for removal in the index-
48 * </p>
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;
58 /**
59 * Create a new operation to stop tracking existing files/folders.
61 * @param rsrcs
62 * collection of {@link IResource}s which should be removed from
63 * the relevant Git repositories.
65 public UntrackOperation(final Collection<? extends IResource> rsrcs) {
66 rsrcList = rsrcs;
67 locations = Collections.emptyList();
68 edits = new IdentityHashMap<Repository, DirCacheEditor>();
71 /**
72 * Create a new operation to stop tracking existing files/folders.
74 * @param repository
75 * a Git repository
76 * @param locations
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;
84 this.db = repository;
85 edits = new IdentityHashMap<Repository, DirCacheEditor>();
88 @Override
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);
93 edits.clear();
95 try {
96 for (IResource obj : rsrcList) {
97 remove(obj);
98 progress.worked(1);
100 for (IPath location : locations) {
101 remove(location);
102 progress.worked(1);
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,
110 db.getDirectory()));
111 editor.commit();
112 progress.worked(1);
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));
118 } finally {
119 for (DirCacheEditor editor : edits.values()) {
120 if (editor.getDirCache() != null) {
121 editor.getDirCache().unlock();
124 edits.clear();
128 @Override
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();
135 if (proj == null) {
136 return;
138 final GitProjectData pd = GitProjectData.get(proj);
139 if (pd == null) {
140 return;
142 final RepositoryMapping rm = pd.getRepositoryMapping(resource);
143 if (rm == null) {
144 return;
146 db = rm.getRepository();
148 remove(resource.getLocation());
151 private void remove(final IPath location) throws CoreException {
152 DirCacheEditor e = edits.get(db);
153 if (e == null) {
154 try {
155 e = db.lockDirCache().editor();
156 } catch (IOException err) {
157 throw new CoreException(Activator.error(CoreText.UntrackOperation_failed, err));
159 edits.put(db, e);
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));
166 } else {
167 e.add(new DirCacheEditor.DeletePath(path));