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
;
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
;
42 * Add one or more new files/folders to the Git repository.
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
51 * Resources are only scheduled for addition in the index.
54 public class TrackOperation
implements IWorkspaceRunnable
{
55 private final Collection rsrcList
;
58 * Create a new operation to track additional files/folders.
61 * collection of {@link IResource}s which should be added to the
62 * relevant Git repositories.
64 public TrackOperation(final Collection rsrcs
) {
68 public void run(IProgressMonitor m
) throws CoreException
{
70 m
= new NullProgressMonitor();
73 final IdentityHashMap
<RepositoryMapping
, Boolean
> tomerge
= new IdentityHashMap
<RepositoryMapping
, Boolean
>();
74 m
.beginTask(CoreText
.AddOperation_adding
, rsrcList
.size() * 200);
76 final Iterator i
= rsrcList
.iterator();
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();
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
{
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
) {
102 throw Activator
.error(CoreText
.AddOperation_failed
, e
);
106 },IResource
.DEPTH_INFINITE
, IContainer
.EXCLUDE_DERIVED
);
108 index
.add(rm
.getWorkDir(), new File(rm
.getWorkDir(),prefix
+ toAdd
.getProjectRelativePath().toFile()));
114 for (RepositoryMapping rm
: tomerge
.keySet()) {
115 m
.setTaskName("Writing index for "+rm
.getRepository().getDirectory());
116 rm
.getRepository().getIndex().write();
118 } catch (RuntimeException e
) {
120 throw Activator
.error(CoreText
.AddOperation_failed
, e
);
121 } catch (IOException e
) {
123 throw Activator
.error(CoreText
.AddOperation_failed
, e
);
126 final Iterator i
= tomerge
.keySet().iterator();
127 while (i
.hasNext()) {
128 final RepositoryMapping r
= (RepositoryMapping
) i
.next();
129 r
.getRepository().getIndex().read();
132 } catch (IOException e
) {