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
.IResource
;
27 import org
.eclipse
.core
.resources
.IResourceProxy
;
28 import org
.eclipse
.core
.resources
.IResourceProxyVisitor
;
29 import org
.eclipse
.core
.resources
.IResourceVisitor
;
30 import org
.eclipse
.core
.runtime
.CoreException
;
31 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
32 import org
.eclipse
.core
.runtime
.IStatus
;
33 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
34 import org
.eclipse
.core
.runtime
.Status
;
35 import org
.eclipse
.core
.runtime
.jobs
.Job
;
36 import org
.spearce
.egit
.core
.Activator
;
37 import org
.spearce
.egit
.core
.CoreText
;
38 import org
.spearce
.egit
.core
.project
.RepositoryMapping
;
39 import org
.spearce
.jgit
.errors
.NotSupportedException
;
40 import org
.spearce
.jgit
.lib
.GitIndex
;
41 import org
.spearce
.jgit
.lib
.GitIndex
.Entry
;
43 public class UpdateJob
extends Job
{
45 private final Collection rsrcList
;
47 public UpdateJob(Collection rsrcList
) {
48 super("Update index");
49 this.rsrcList
= rsrcList
;
50 setPriority(Job
.LONG
);
53 protected IStatus
run(IProgressMonitor m
) {
55 m
= new NullProgressMonitor();
60 final IdentityHashMap
<RepositoryMapping
, Boolean
> tomerge
= new IdentityHashMap
<RepositoryMapping
, Boolean
>();
62 final int[] count
=new int[1];
63 long t0
=System
.currentTimeMillis();
64 for (Object obj
: rsrcList
) {
65 if (obj
instanceof IContainer
) {
66 ((IContainer
)obj
).accept(new IResourceProxyVisitor() {
67 public boolean visit(IResourceProxy rp
) throws CoreException
{
68 if (rp
.getType() == IResource
.FILE
) {
73 }, IContainer
.EXCLUDE_DERIVED
);
74 } else if (obj
instanceof IResource
) {
78 long t1
=System
.currentTimeMillis();
79 System
.out
.println("Counted "+count
[0]+" items to update in "+(t1
-t0
)/1000.0+"s");
80 m
.beginTask(CoreText
.UpdateOperation_updating
, count
[0]);
81 final IProgressMonitor fm
= m
;
82 for (Object obj
: rsrcList
) {
83 if (obj
instanceof IResource
) {
84 final IResource r
= (IResource
)obj
;
85 final RepositoryMapping rm
= RepositoryMapping
.getMapping(r
);
86 final GitIndex index
= rm
.getRepository().getIndex();
87 tomerge
.put(rm
, Boolean
.TRUE
);
88 if (r
instanceof IContainer
) {
89 ((IContainer
)r
).accept(new IResourceVisitor() {
90 public boolean visit(IResource resource
) throws CoreException
{
92 if (resource
.getType() == IResource
.FILE
) {
93 String path
= rm
.getRepoRelativePath(resource
);
94 Entry entry
= index
.getEntry(path
);
96 entry
.update(new File(rm
.getWorkDir(),path
));
100 } catch (IOException e
) {
102 throw Activator
.error(CoreText
.UpdateOperation_failed
, e
);
106 },IResource
.DEPTH_INFINITE
, IContainer
.EXCLUDE_DERIVED
);
108 String path
= rm
.getRepoRelativePath(r
);
109 Entry entry
= index
.getEntry(path
);
111 entry
.update(new File(rm
.getWorkDir(),path
));
117 for (RepositoryMapping rm
: tomerge
.keySet()) {
118 m
.setTaskName("Writing index for "+rm
.getRepository().getDirectory());
119 rm
.getRepository().getIndex().write();
121 } catch (NotSupportedException e
) {
122 return Activator
.error(e
.getMessage(),e
).getStatus();
123 } catch (RuntimeException e
) {
125 return Activator
.error(CoreText
.UpdateOperation_failed
, e
).getStatus();
126 } catch (IOException e
) {
128 return Activator
.error(CoreText
.UpdateOperation_failed
, e
).getStatus();
129 } catch (CoreException e
) {
131 return Activator
.error(CoreText
.UpdateOperation_failed
, e
).getStatus();
134 final Iterator i
= tomerge
.keySet().iterator();
135 while (i
.hasNext()) {
136 final RepositoryMapping r
= (RepositoryMapping
) i
.next();
137 r
.getRepository().getIndex().read();
140 } catch (IOException e
) {
151 return Status
.OK_STATUS
;
154 private void trace(final String m
) {
155 Activator
.trace("(UpdateJob)"+m
);