Initial EGit contribution to eclipse.org
[egit.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / op / TrackOperation.java
blobcd431a4847d017c245bd3519726e3cadad01389a
1 /*******************************************************************************
2 * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
3 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
4 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
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.File;
14 import java.io.IOException;
15 import java.util.Collection;
16 import java.util.IdentityHashMap;
17 import java.util.Iterator;
19 import org.eclipse.core.resources.IContainer;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.resources.IResourceVisitor;
23 import org.eclipse.core.resources.IWorkspaceRunnable;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IAdaptable;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.NullProgressMonitor;
28 import org.eclipse.egit.core.Activator;
29 import org.eclipse.egit.core.CoreText;
30 import org.eclipse.egit.core.project.RepositoryMapping;
31 import org.eclipse.team.core.Team;
32 import org.eclipse.jgit.lib.GitIndex;
33 import org.eclipse.jgit.lib.GitIndex.Entry;
35 /**
36 * Add one or more new files/folders to the Git repository.
37 * <p>
38 * Accepts a collection of resources (files and/or directories) which should be
39 * added to the their corresponding Git repositories. Resources in the
40 * collection can be associated with multiple repositories. The operation will
41 * automatically associate each resource with the nearest containing Git
42 * repository.
43 * </p>
44 * <p>
45 * Resources are only scheduled for addition in the index.
46 * </p>
48 public class TrackOperation implements IWorkspaceRunnable {
49 private final Collection rsrcList;
51 /**
52 * Create a new operation to track additional files/folders.
54 * @param rsrcs
55 * collection of {@link IResource}s which should be added to the
56 * relevant Git repositories.
58 public TrackOperation(final Collection rsrcs) {
59 rsrcList = rsrcs;
62 public void run(IProgressMonitor m) throws CoreException {
63 if (m == null) {
64 m = new NullProgressMonitor();
67 final IdentityHashMap<RepositoryMapping, Boolean> tomerge = new IdentityHashMap<RepositoryMapping, Boolean>();
68 m.beginTask(CoreText.AddOperation_adding, rsrcList.size() * 200);
69 try {
70 for (Object obj : rsrcList) {
71 obj = ((IAdaptable)obj).getAdapter(IResource.class);
72 if (obj instanceof IResource) {
73 final IResource toAdd = (IResource)obj;
74 final RepositoryMapping rm = RepositoryMapping.getMapping(toAdd);
75 final GitIndex index = rm.getRepository().getIndex();
77 if (obj instanceof IFile) {
78 String repoPath = rm.getRepoRelativePath((IResource) obj);
79 Entry entry = index.getEntry(repoPath);
80 if (entry != null) {
81 if (!entry.isAssumedValid()) {
82 System.out.println("Already tracked - skipping");
83 continue;
88 tomerge.put(rm, Boolean.TRUE);
89 if (toAdd instanceof IContainer) {
90 ((IContainer)toAdd).accept(new IResourceVisitor() {
91 public boolean visit(IResource resource) throws CoreException {
92 try {
93 String repoPath = rm.getRepoRelativePath(resource);
94 // We use add to reset the assume valid bit, so we check the bit
95 // first. If a resource within a ignored folder is marked
96 // we ignore it here, i.e. there is no way to unmark it expect
97 // by explicitly selecting and invoking track on it.
98 boolean isIgnored = Team.isIgnoredHint(resource);
99 if (resource.getType() == IResource.FILE) {
100 Entry entry = index.getEntry(repoPath);
101 if (!isIgnored || entry != null && entry.isAssumedValid()) {
102 entry = index.add(rm.getWorkDir(), new File(rm.getWorkDir(), repoPath));
103 entry.setAssumeValid(false);
106 if (isIgnored)
107 return false;
109 } catch (IOException e) {
110 e.printStackTrace();
111 throw Activator.error(CoreText.AddOperation_failed, e);
113 return true;
115 },IResource.DEPTH_INFINITE, IContainer.EXCLUDE_DERIVED);
116 } else {
117 Entry entry = index.add(rm.getWorkDir(), new File(rm.getWorkDir(),rm.getRepoRelativePath(toAdd)));
118 entry.setAssumeValid(false);
122 m.worked(200);
124 for (RepositoryMapping rm : tomerge.keySet()) {
125 m.setTaskName("Writing index for "+rm.getRepository().getDirectory());
126 rm.getRepository().getIndex().write();
128 } catch (RuntimeException e) {
129 e.printStackTrace();
130 throw Activator.error(CoreText.AddOperation_failed, e);
131 } catch (IOException e) {
132 e.printStackTrace();
133 throw Activator.error(CoreText.AddOperation_failed, e);
134 } finally {
135 try {
136 final Iterator i = tomerge.keySet().iterator();
137 while (i.hasNext()) {
138 final RepositoryMapping r = (RepositoryMapping) i.next();
139 r.getRepository().getIndex().read();
140 r.fireRepositoryChanged();
142 } catch (IOException e) {
143 e.printStackTrace();
144 } finally {
145 m.done();