2 * Copyrighy (C) 2007 Robin Rosenberg
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
.internal
;
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
.IProject
;
27 import org
.eclipse
.core
.resources
.IResource
;
28 import org
.eclipse
.core
.resources
.IResourceProxy
;
29 import org
.eclipse
.core
.resources
.IResourceProxyVisitor
;
30 import org
.eclipse
.core
.resources
.IResourceVisitor
;
31 import org
.eclipse
.core
.runtime
.CoreException
;
32 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
33 import org
.eclipse
.core
.runtime
.IStatus
;
34 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
35 import org
.eclipse
.core
.runtime
.Status
;
36 import org
.eclipse
.core
.runtime
.jobs
.Job
;
37 import org
.spearce
.egit
.core
.Activator
;
38 import org
.spearce
.egit
.core
.CoreText
;
39 import org
.spearce
.egit
.core
.project
.GitProjectData
;
40 import org
.spearce
.egit
.core
.project
.RepositoryMapping
;
41 import org
.spearce
.jgit
.lib
.GitIndex
;
42 import org
.spearce
.jgit
.lib
.GitIndex
.Entry
;
44 public class UpdateJob
extends Job
{
46 private final Collection rsrcList
;
48 public UpdateJob(Collection rsrcList
) {
49 super("Update index");
50 this.rsrcList
= rsrcList
;
51 setPriority(Job
.LONG
);
54 protected IStatus
run(IProgressMonitor m
) {
56 m
= new NullProgressMonitor();
61 final IdentityHashMap
<RepositoryMapping
, Boolean
> tomerge
= new IdentityHashMap
<RepositoryMapping
, Boolean
>();
63 final int[] count
=new int[1];
64 long t0
=System
.currentTimeMillis();
65 for (Object obj
: rsrcList
) {
66 if (obj
instanceof IContainer
) {
67 ((IContainer
)obj
).accept(new IResourceProxyVisitor() {
68 public boolean visit(IResourceProxy rp
) throws CoreException
{
69 if (rp
.getType() == IResource
.FILE
) {
74 }, IContainer
.EXCLUDE_DERIVED
);
75 } else if (obj
instanceof IResource
) {
79 long t1
=System
.currentTimeMillis();
80 System
.out
.println("Counted "+count
[0]+" items to update in "+(t1
-t0
)/1000.0+"s");
81 m
.beginTask(CoreText
.UpdateOperation_updating
, count
[0]);
82 final IProgressMonitor fm
= m
;
83 for (Object obj
: rsrcList
) {
84 if (obj
instanceof IResource
) {
85 final IResource r
= (IResource
)obj
;
86 final IProject p
= r
.getProject();
87 final GitProjectData pd
= GitProjectData
.get(r
.getProject());
88 final RepositoryMapping rm
= pd
.getRepositoryMapping(p
);
89 final GitIndex index
= rm
.getRepository().getIndex();
90 tomerge
.put(rm
, Boolean
.TRUE
);
91 String prefix
= rm
.getSubset();
95 prefix
= prefix
+ "/";
96 final String fprefix
= prefix
;
97 if (r
instanceof IContainer
) {
98 ((IContainer
)r
).accept(new IResourceVisitor() {
99 public boolean visit(IResource resource
) throws CoreException
{
101 if (resource
.getType() == IResource
.FILE
) {
102 String path
= fprefix
+ resource
.getProjectRelativePath();
103 Entry entry
= index
.getEntry(path
);
105 entry
.update(new File(rm
.getWorkDir(),path
), rm
.getRepository());
109 } catch (IOException e
) {
111 throw Activator
.error(CoreText
.UpdateOperation_failed
, e
);
115 },IResource
.DEPTH_INFINITE
, IContainer
.EXCLUDE_DERIVED
);
117 String path
= fprefix
+ r
.getProjectRelativePath();
118 Entry entry
= index
.getEntry(path
);
120 entry
.update(new File(rm
.getWorkDir(),path
), rm
.getRepository());
126 for (RepositoryMapping rm
: tomerge
.keySet()) {
127 m
.setTaskName("Writing index for "+rm
.getRepository().getDirectory());
128 rm
.getRepository().getIndex().write();
130 } catch (RuntimeException e
) {
132 return Activator
.error(CoreText
.UpdateOperation_failed
, e
).getStatus();
133 } catch (IOException e
) {
135 return Activator
.error(CoreText
.UpdateOperation_failed
, e
).getStatus();
136 } catch (CoreException e
) {
138 return Activator
.error(CoreText
.UpdateOperation_failed
, e
).getStatus();
141 final Iterator i
= tomerge
.keySet().iterator();
142 while (i
.hasNext()) {
143 final RepositoryMapping r
= (RepositoryMapping
) i
.next();
144 r
.getRepository().getIndex().read();
147 } catch (IOException e
) {
158 return Status
.OK_STATUS
;
161 private void trace(final String m
) {
162 Activator
.trace("(UpdateJob)"+m
);