Use the new git index
[egit.git] / org.spearce.egit.core / src / org / spearce / egit / core / op / TrackOperation.java
blob165b34f39e93fe4a7cdb4b474181337eb3852b8b
1 /*
2 * Copyright (C) 2006 Shawn Pearce <spearce@spearce.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License, version 2.1, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org.spearce.egit.core.op;
19 import java.io.File;
20 import java.io.IOException;
21 import java.util.Collection;
22 import java.util.IdentityHashMap;
23 import java.util.Iterator;
25 import org.eclipse.core.resources.IContainer;
26 import org.eclipse.core.resources.IFile;
27 import org.eclipse.core.resources.IProject;
28 import org.eclipse.core.resources.IResource;
29 import org.eclipse.core.resources.IResourceVisitor;
30 import org.eclipse.core.resources.IWorkspaceRunnable;
31 import org.eclipse.core.runtime.CoreException;
32 import org.eclipse.core.runtime.IProgressMonitor;
33 import org.eclipse.core.runtime.NullProgressMonitor;
34 import org.eclipse.team.core.Team;
35 import org.spearce.egit.core.Activator;
36 import org.spearce.egit.core.CoreText;
37 import org.spearce.egit.core.project.GitProjectData;
38 import org.spearce.egit.core.project.RepositoryMapping;
39 import org.spearce.jgit.lib.GitIndex;
41 /**
42 * Add one or more new files/folders to the Git repository.
43 * <p>
44 * Accepts a collection of resources (files and/or directories) which should be
45 * added to the their corresponding Git repositories. Resources in the
46 * collection can be associated with multiple repositories. The operation will
47 * automatically associate each resource with the nearest containing Git
48 * repository.
49 * </p>
50 * <p>
51 * Resources are only scheduled for addition in the index.
52 * </p>
54 public class TrackOperation implements IWorkspaceRunnable {
55 private final Collection rsrcList;
57 /**
58 * Create a new operation to track additional files/folders.
60 * @param rsrcs
61 * collection of {@link IResource}s which should be added to the
62 * relevant Git repositories.
64 public TrackOperation(final Collection rsrcs) {
65 rsrcList = rsrcs;
68 public void run(IProgressMonitor m) throws CoreException {
69 if (m == null) {
70 m = new NullProgressMonitor();
73 final IdentityHashMap<RepositoryMapping, Boolean> tomerge = new IdentityHashMap<RepositoryMapping, Boolean>();
74 m.beginTask(CoreText.AddOperation_adding, rsrcList.size() * 200);
75 try {
76 final Iterator i = rsrcList.iterator();
77 while (i.hasNext()) {
78 final Object obj = i.next();
79 if (obj instanceof IResource) {
80 final IResource toAdd = (IResource)obj;
81 final IProject p = toAdd.getProject();
82 final GitProjectData pd = GitProjectData.get(toAdd.getProject());
83 final RepositoryMapping rm = pd.getRepositoryMapping(p);
84 final GitIndex index = rm.getRepository().getIndex();
85 tomerge.put(rm, Boolean.TRUE);
86 String prefix = rm.getSubset();
87 if (prefix == null)
88 prefix = "";
89 else
90 prefix = prefix + "/";
91 final String fprefix = prefix;
92 if (toAdd instanceof IContainer) {
93 ((IContainer)toAdd).accept(new IResourceVisitor() {
94 public boolean visit(IResource resource) throws CoreException {
95 try {
96 if (resource.getType() == IResource.FILE) {
97 if (!Team.isIgnored((IFile)resource))
98 index.add(rm.getWorkDir(), new File(rm.getWorkDir(),fprefix + resource.getProjectRelativePath().toFile()));
100 } catch (IOException e) {
101 e.printStackTrace();
102 throw Activator.error(CoreText.AddOperation_failed, e);
104 return true;
106 },IResource.DEPTH_INFINITE, IContainer.EXCLUDE_DERIVED);
107 } else {
108 index.add(rm.getWorkDir(), new File(rm.getWorkDir(),prefix + toAdd.getProjectRelativePath().toFile()));
112 m.worked(200);
114 for (RepositoryMapping rm : tomerge.keySet()) {
115 m.setTaskName("Writing index for "+rm.getRepository().getDirectory());
116 rm.getRepository().getIndex().write();
118 } catch (RuntimeException e) {
119 e.printStackTrace();
120 throw Activator.error(CoreText.AddOperation_failed, e);
121 } catch (IOException e) {
122 e.printStackTrace();
123 throw Activator.error(CoreText.AddOperation_failed, e);
124 } finally {
125 try {
126 final Iterator i = tomerge.keySet().iterator();
127 while (i.hasNext()) {
128 final RepositoryMapping r = (RepositoryMapping) i.next();
129 r.getRepository().getIndex().read();
130 r.recomputeMerge();
132 } catch (IOException e) {
133 e.printStackTrace();
134 } finally {
135 m.done();