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 * See LICENSE for the full license text, also available.
9 *******************************************************************************/
10 package org
.spearce
.egit
.core
.op
;
13 import java
.io
.IOException
;
14 import java
.util
.Collection
;
15 import java
.util
.IdentityHashMap
;
16 import java
.util
.Iterator
;
18 import org
.eclipse
.core
.resources
.IContainer
;
19 import org
.eclipse
.core
.resources
.IFile
;
20 import org
.eclipse
.core
.resources
.IResource
;
21 import org
.eclipse
.core
.resources
.IResourceVisitor
;
22 import org
.eclipse
.core
.resources
.IWorkspaceRunnable
;
23 import org
.eclipse
.core
.runtime
.CoreException
;
24 import org
.eclipse
.core
.runtime
.IAdaptable
;
25 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
26 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
27 import org
.eclipse
.team
.core
.Team
;
28 import org
.spearce
.egit
.core
.Activator
;
29 import org
.spearce
.egit
.core
.CoreText
;
30 import org
.spearce
.egit
.core
.project
.RepositoryMapping
;
31 import org
.spearce
.jgit
.lib
.GitIndex
;
32 import org
.spearce
.jgit
.lib
.GitIndex
.Entry
;
35 * Add one or more new files/folders to the Git repository.
37 * Accepts a collection of resources (files and/or directories) which should be
38 * added to the their corresponding Git repositories. Resources in the
39 * collection can be associated with multiple repositories. The operation will
40 * automatically associate each resource with the nearest containing Git
44 * Resources are only scheduled for addition in the index.
47 public class TrackOperation
implements IWorkspaceRunnable
{
48 private final Collection rsrcList
;
51 * Create a new operation to track additional files/folders.
54 * collection of {@link IResource}s which should be added to the
55 * relevant Git repositories.
57 public TrackOperation(final Collection rsrcs
) {
61 public void run(IProgressMonitor m
) throws CoreException
{
63 m
= new NullProgressMonitor();
66 final IdentityHashMap
<RepositoryMapping
, Boolean
> tomerge
= new IdentityHashMap
<RepositoryMapping
, Boolean
>();
67 m
.beginTask(CoreText
.AddOperation_adding
, rsrcList
.size() * 200);
69 for (Object obj
: rsrcList
) {
70 obj
= ((IAdaptable
)obj
).getAdapter(IResource
.class);
71 if (obj
instanceof IResource
) {
72 final IResource toAdd
= (IResource
)obj
;
73 final RepositoryMapping rm
= RepositoryMapping
.getMapping(toAdd
);
74 final GitIndex index
= rm
.getRepository().getIndex();
76 if (obj
instanceof IFile
) {
77 String repoPath
= rm
.getRepoRelativePath((IResource
) obj
);
78 Entry entry
= index
.getEntry(repoPath
);
80 if (!entry
.isAssumedValid()) {
81 System
.out
.println("Already tracked - skipping");
87 tomerge
.put(rm
, Boolean
.TRUE
);
88 if (toAdd
instanceof IContainer
) {
89 ((IContainer
)toAdd
).accept(new IResourceVisitor() {
90 public boolean visit(IResource resource
) throws CoreException
{
92 String repoPath
= rm
.getRepoRelativePath(resource
);
93 // We use add to reset the assume valid bit, so we check the bit
94 // first. If a resource within a ignored folder is marked
95 // we ignore it here, i.e. there is no way to unmark it expect
96 // by explicitly selecting and invoking track on it.
97 if (resource
.getType() == IResource
.FILE
) {
98 Entry entry
= index
.getEntry(repoPath
);
99 if (!Team
.isIgnoredHint(resource
) || entry
!= null && entry
.isAssumedValid()) {
100 entry
= index
.add(rm
.getWorkDir(), new File(rm
.getWorkDir(), repoPath
));
101 entry
.setAssumeValid(false);
104 if (Team
.isIgnoredHint(resource
))
107 } catch (IOException e
) {
109 throw Activator
.error(CoreText
.AddOperation_failed
, e
);
113 },IResource
.DEPTH_INFINITE
, IContainer
.EXCLUDE_DERIVED
);
115 Entry entry
= index
.add(rm
.getWorkDir(), new File(rm
.getWorkDir(),rm
.getRepoRelativePath(toAdd
)));
116 entry
.setAssumeValid(false);
122 for (RepositoryMapping rm
: tomerge
.keySet()) {
123 m
.setTaskName("Writing index for "+rm
.getRepository().getDirectory());
124 rm
.getRepository().getIndex().write();
126 } catch (RuntimeException e
) {
128 throw Activator
.error(CoreText
.AddOperation_failed
, e
);
129 } catch (IOException e
) {
131 throw Activator
.error(CoreText
.AddOperation_failed
, e
);
134 final Iterator i
= tomerge
.keySet().iterator();
135 while (i
.hasNext()) {
136 final RepositoryMapping r
= (RepositoryMapping
) i
.next();
137 r
.getRepository().getIndex().read();
138 r
.fireRepositoryChanged();
140 } catch (IOException e
) {