Rewrite UntrackOperation to use DirCacheBuilder
[egit.git] / org.spearce.egit.core / src / org / spearce / egit / core / op / UntrackOperation.java
blob46565523328bb30cfd3e1c1923da7616ad117325
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 v1.0
8 * See LICENSE for the full license text, also available.
9 *******************************************************************************/
10 package org.spearce.egit.core.op;
12 import java.io.IOException;
13 import java.util.Collection;
14 import java.util.IdentityHashMap;
15 import java.util.Map;
17 import org.eclipse.core.resources.IContainer;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.IWorkspaceRunnable;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IAdaptable;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.NullProgressMonitor;
25 import org.spearce.egit.core.Activator;
26 import org.spearce.egit.core.CoreText;
27 import org.spearce.egit.core.project.GitProjectData;
28 import org.spearce.egit.core.project.RepositoryMapping;
29 import org.spearce.jgit.dircache.DirCache;
30 import org.spearce.jgit.dircache.DirCacheEditor;
31 import org.spearce.jgit.lib.Repository;
33 /**
34 * Remove one or more existing files/folders from the Git repository.
35 * <p>
36 * Accepts a collection of resources (files and/or directories) which should be
37 * removed from the their corresponding Git repositories. Resources in the
38 * collection can be associated with multiple repositories. The operation will
39 * automatically remove each resource from the correct Git repository.
40 * </p>
41 * <p>
42 * Resources are only scheduled for removal in the index-
43 * </p>
45 public class UntrackOperation implements IWorkspaceRunnable {
46 private final Collection rsrcList;
48 private final IdentityHashMap<Repository, DirCacheEditor> edits;
50 private final IdentityHashMap<RepositoryMapping, Object> mappings;
52 /**
53 * Create a new operation to stop tracking existing files/folders.
55 * @param rsrcs
56 * collection of {@link IResource}s which should be removed from
57 * the relevant Git repositories.
59 public UntrackOperation(final Collection rsrcs) {
60 rsrcList = rsrcs;
61 edits = new IdentityHashMap<Repository, DirCacheEditor>();
62 mappings = new IdentityHashMap<RepositoryMapping, Object>();
65 public void run(IProgressMonitor m) throws CoreException {
66 if (m == null)
67 m = new NullProgressMonitor();
69 edits.clear();
70 mappings.clear();
72 m.beginTask(CoreText.AddOperation_adding, rsrcList.size() * 200);
73 try {
74 for (Object obj : rsrcList) {
75 obj = ((IAdaptable) obj).getAdapter(IResource.class);
76 if (obj instanceof IResource)
77 remove((IResource) obj);
78 m.worked(200);
81 for (Map.Entry<Repository, DirCacheEditor> e : edits.entrySet()) {
82 final Repository db = e.getKey();
83 final DirCacheEditor editor = e.getValue();
84 m.setTaskName("Writing index for " + db.getDirectory());
85 editor.commit();
87 } catch (RuntimeException e) {
88 throw Activator.error(CoreText.UntrackOperation_failed, e);
89 } catch (IOException e) {
90 throw Activator.error(CoreText.UntrackOperation_failed, e);
91 } finally {
92 for (final RepositoryMapping rm : mappings.keySet())
93 rm.fireRepositoryChanged();
94 edits.clear();
95 mappings.clear();
96 m.done();
100 private void remove(final IResource path) throws CoreException {
101 final IProject proj = path.getProject();
102 final GitProjectData pd = GitProjectData.get(proj);
103 if (pd == null)
104 return;
105 final RepositoryMapping rm = pd.getRepositoryMapping(path);
106 if (rm == null)
107 return;
108 final Repository db = rm.getRepository();
110 DirCacheEditor e = edits.get(db);
111 if (e == null) {
112 try {
113 e = DirCache.lock(db).editor();
114 } catch (IOException err) {
115 throw Activator.error(CoreText.UntrackOperation_failed, err);
117 edits.put(db, e);
118 mappings.put(rm, rm);
121 if (path instanceof IContainer)
122 e.add(new DirCacheEditor.DeleteTree(rm.getRepoRelativePath(path)));
123 else
124 e.add(new DirCacheEditor.DeletePath(rm.getRepoRelativePath(path)));