Merge branch 'stable-0.8'
[egit.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / op / UntrackOperation.java
blobba3c28c06a493fb4de122b5549ad797dc0a5680a
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.runtime.CoreException;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.NullProgressMonitor;
24 import org.eclipse.core.runtime.jobs.ISchedulingRule;
25 import org.eclipse.core.runtime.jobs.MultiRule;
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;
33 import org.eclipse.osgi.util.NLS;
35 /**
36 * Remove one or more existing files/folders from the Git repository.
37 * <p>
38 * Accepts a collection of resources (files and/or directories) which should be
39 * removed from the their corresponding Git repositories. Resources in the
40 * collection can be associated with multiple repositories. The operation will
41 * automatically remove each resource from the correct Git repository.
42 * </p>
43 * <p>
44 * Resources are only scheduled for removal in the index-
45 * </p>
47 public class UntrackOperation implements IEGitOperation {
48 private final Collection<? extends IResource> rsrcList;
50 private final IdentityHashMap<Repository, DirCacheEditor> edits;
52 private final IdentityHashMap<RepositoryMapping, Object> mappings;
54 /**
55 * Create a new operation to stop tracking existing files/folders.
57 * @param rsrcs
58 * collection of {@link IResource}s which should be removed from
59 * the relevant Git repositories.
61 public UntrackOperation(final Collection<? extends IResource> rsrcs) {
62 rsrcList = rsrcs;
63 edits = new IdentityHashMap<Repository, DirCacheEditor>();
64 mappings = new IdentityHashMap<RepositoryMapping, Object>();
67 /* (non-Javadoc)
68 * @see org.eclipse.egit.core.op.IEGitOperation#execute(org.eclipse.core.runtime.IProgressMonitor)
70 public void execute(IProgressMonitor m) throws CoreException {
71 if (m == null)
72 m = new NullProgressMonitor();
74 edits.clear();
75 mappings.clear();
77 m.beginTask(CoreText.AddOperation_adding, rsrcList.size() * 200);
78 try {
79 for (IResource obj : rsrcList) {
80 remove(obj);
81 m.worked(200);
84 for (Map.Entry<Repository, DirCacheEditor> e : edits.entrySet()) {
85 final Repository db = e.getKey();
86 final DirCacheEditor editor = e.getValue();
87 m.setTaskName(NLS.bind(CoreText.UntrackOperation_writingIndex, db.getDirectory()));
88 editor.commit();
90 } catch (RuntimeException e) {
91 throw new CoreException(Activator.error(CoreText.UntrackOperation_failed, e));
92 } catch (IOException e) {
93 throw new CoreException(Activator.error(CoreText.UntrackOperation_failed, e));
94 } finally {
95 for (final RepositoryMapping rm : mappings.keySet())
96 rm.fireRepositoryChanged();
97 edits.clear();
98 mappings.clear();
99 m.done();
103 /* (non-Javadoc)
104 * @see org.eclipse.egit.core.op.IEGitOperation#getSchedulingRule()
106 public ISchedulingRule getSchedulingRule() {
107 return new MultiRule(rsrcList.toArray(new IResource[rsrcList.size()]));
110 private void remove(final IResource path) throws CoreException {
111 final IProject proj = path.getProject();
112 final GitProjectData pd = GitProjectData.get(proj);
113 if (pd == null)
114 return;
115 final RepositoryMapping rm = pd.getRepositoryMapping(path);
116 if (rm == null)
117 return;
118 final Repository db = rm.getRepository();
120 DirCacheEditor e = edits.get(db);
121 if (e == null) {
122 try {
123 e = DirCache.lock(db).editor();
124 } catch (IOException err) {
125 throw new CoreException(Activator.error(CoreText.UntrackOperation_failed, err));
127 edits.put(db, e);
128 mappings.put(rm, rm);
131 if (path instanceof IContainer)
132 e.add(new DirCacheEditor.DeleteTree(rm.getRepoRelativePath(path)));
133 else
134 e.add(new DirCacheEditor.DeletePath(rm.getRepoRelativePath(path)));