Remove trailing whitespace across EGit
[egit.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / op / UntrackOperation.java
blobff6863bd00ee70a6750756083465d31e2059477c
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 * which accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 *******************************************************************************/
11 package org.eclipse.egit.core.op;
13 import java.io.IOException;
14 import java.util.Collection;
15 import java.util.IdentityHashMap;
16 import java.util.Map;
18 import org.eclipse.core.resources.IContainer;
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.resources.IWorkspaceRunnable;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IAdaptable;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.core.runtime.NullProgressMonitor;
26 import org.eclipse.egit.core.Activator;
27 import org.eclipse.egit.core.CoreText;
28 import org.eclipse.egit.core.project.GitProjectData;
29 import org.eclipse.egit.core.project.RepositoryMapping;
30 import org.eclipse.jgit.dircache.DirCache;
31 import org.eclipse.jgit.dircache.DirCacheEditor;
32 import org.eclipse.jgit.lib.Repository;
34 /**
35 * Remove one or more existing files/folders from the Git repository.
36 * <p>
37 * Accepts a collection of resources (files and/or directories) which should be
38 * removed from the their corresponding Git repositories. Resources in the
39 * collection can be associated with multiple repositories. The operation will
40 * automatically remove each resource from the correct Git repository.
41 * </p>
42 * <p>
43 * Resources are only scheduled for removal in the index-
44 * </p>
46 public class UntrackOperation implements IWorkspaceRunnable {
47 private final Collection rsrcList;
49 private final IdentityHashMap<Repository, DirCacheEditor> edits;
51 private final IdentityHashMap<RepositoryMapping, Object> mappings;
53 /**
54 * Create a new operation to stop tracking existing files/folders.
56 * @param rsrcs
57 * collection of {@link IResource}s which should be removed from
58 * the relevant Git repositories.
60 public UntrackOperation(final Collection rsrcs) {
61 rsrcList = rsrcs;
62 edits = new IdentityHashMap<Repository, DirCacheEditor>();
63 mappings = new IdentityHashMap<RepositoryMapping, Object>();
66 public void run(IProgressMonitor m) throws CoreException {
67 if (m == null)
68 m = new NullProgressMonitor();
70 edits.clear();
71 mappings.clear();
73 m.beginTask(CoreText.AddOperation_adding, rsrcList.size() * 200);
74 try {
75 for (Object obj : rsrcList) {
76 obj = ((IAdaptable) obj).getAdapter(IResource.class);
77 if (obj instanceof IResource)
78 remove((IResource) obj);
79 m.worked(200);
82 for (Map.Entry<Repository, DirCacheEditor> e : edits.entrySet()) {
83 final Repository db = e.getKey();
84 final DirCacheEditor editor = e.getValue();
85 m.setTaskName("Writing index for " + db.getDirectory());
86 editor.commit();
88 } catch (RuntimeException e) {
89 throw Activator.error(CoreText.UntrackOperation_failed, e);
90 } catch (IOException e) {
91 throw Activator.error(CoreText.UntrackOperation_failed, e);
92 } finally {
93 for (final RepositoryMapping rm : mappings.keySet())
94 rm.fireRepositoryChanged();
95 edits.clear();
96 mappings.clear();
97 m.done();
101 private void remove(final IResource path) throws CoreException {
102 final IProject proj = path.getProject();
103 final GitProjectData pd = GitProjectData.get(proj);
104 if (pd == null)
105 return;
106 final RepositoryMapping rm = pd.getRepositoryMapping(path);
107 if (rm == null)
108 return;
109 final Repository db = rm.getRepository();
111 DirCacheEditor e = edits.get(db);
112 if (e == null) {
113 try {
114 e = DirCache.lock(db).editor();
115 } catch (IOException err) {
116 throw Activator.error(CoreText.UntrackOperation_failed, err);
118 edits.put(db, e);
119 mappings.put(rm, rm);
122 if (path instanceof IContainer)
123 e.add(new DirCacheEditor.DeleteTree(rm.getRepoRelativePath(path)));
124 else
125 e.add(new DirCacheEditor.DeletePath(rm.getRepoRelativePath(path)));