Use getRepostoryRelativeName everywhere
[egit.git] / org.spearce.egit.core / src / org / spearce / egit / core / op / TrackOperation.java
blob7c71ba9c5d6e7a2581948eaae0a42b82372f6b18
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();
86 if (obj instanceof IFile) {
87 String repoPath = rm.getRepoRelativePath((IResource) obj);
88 if (index.getEntry(repoPath) != null) {
89 System.out.println("Already tracked - skipping");
90 continue;
94 tomerge.put(rm, Boolean.TRUE);
95 if (toAdd instanceof IContainer) {
96 ((IContainer)toAdd).accept(new IResourceVisitor() {
97 public boolean visit(IResource resource) throws CoreException {
98 try {
99 if (resource.getType() == IResource.FILE) {
100 if (!Team.isIgnored((IFile)resource))
101 index.add(rm.getWorkDir(), new File(rm.getWorkDir(),rm.getRepoRelativePath(resource)));
103 } catch (IOException e) {
104 e.printStackTrace();
105 throw Activator.error(CoreText.AddOperation_failed, e);
107 return true;
109 },IResource.DEPTH_INFINITE, IContainer.EXCLUDE_DERIVED);
110 } else {
111 index.add(rm.getWorkDir(), new File(rm.getWorkDir(),rm.getRepoRelativePath(toAdd)));
115 m.worked(200);
117 for (RepositoryMapping rm : tomerge.keySet()) {
118 m.setTaskName("Writing index for "+rm.getRepository().getDirectory());
119 rm.getRepository().getIndex().write();
121 } catch (RuntimeException e) {
122 e.printStackTrace();
123 throw Activator.error(CoreText.AddOperation_failed, e);
124 } catch (IOException e) {
125 e.printStackTrace();
126 throw Activator.error(CoreText.AddOperation_failed, e);
127 } finally {
128 try {
129 final Iterator i = tomerge.keySet().iterator();
130 while (i.hasNext()) {
131 final RepositoryMapping r = (RepositoryMapping) i.next();
132 r.getRepository().getIndex().read();
133 r.recomputeMerge();
135 } catch (IOException e) {
136 e.printStackTrace();
137 } finally {
138 m.done();