1 /*******************************************************************************
2 * Copyright (C) 2007, Robin Rosenberg <me@lathund.dewire.com>
3 * Copyright (C) 2008, 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
.internal
;
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
.IResource
;
20 import org
.eclipse
.core
.resources
.IResourceProxy
;
21 import org
.eclipse
.core
.resources
.IResourceProxyVisitor
;
22 import org
.eclipse
.core
.resources
.IResourceVisitor
;
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
.IStatus
;
27 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
28 import org
.eclipse
.core
.runtime
.Status
;
29 import org
.eclipse
.core
.runtime
.jobs
.Job
;
30 import org
.spearce
.egit
.core
.Activator
;
31 import org
.spearce
.egit
.core
.CoreText
;
32 import org
.spearce
.egit
.core
.project
.RepositoryMapping
;
33 import org
.spearce
.jgit
.errors
.NotSupportedException
;
34 import org
.spearce
.jgit
.lib
.GitIndex
;
35 import org
.spearce
.jgit
.lib
.GitIndex
.Entry
;
38 * This job updates the index with the content of all specified
39 * and tracked resources. If a project is selected all tracked
40 * resources withing that container are updated.
42 public class UpdateJob
extends Job
{
44 private final Collection rsrcList
;
47 * Construct an UpdateJob for the specified resources.
51 public UpdateJob(Collection rsrcList
) {
52 super("Update index");
53 this.rsrcList
= rsrcList
;
54 setPriority(Job
.LONG
);
57 protected IStatus
run(IProgressMonitor m
) {
59 m
= new NullProgressMonitor();
64 final IdentityHashMap
<RepositoryMapping
, Boolean
> tomerge
= new IdentityHashMap
<RepositoryMapping
, Boolean
>();
66 final int[] count
=new int[1];
67 long t0
=System
.currentTimeMillis();
68 for (Object obj
: rsrcList
) {
69 obj
= ((IAdaptable
)obj
).getAdapter(IResource
.class);
70 if (obj
instanceof IContainer
) {
71 ((IContainer
)obj
).accept(new IResourceProxyVisitor() {
72 public boolean visit(IResourceProxy rp
) throws CoreException
{
73 if (rp
.getType() == IResource
.FILE
) {
78 }, IContainer
.EXCLUDE_DERIVED
);
79 } else if (obj
instanceof IResource
) {
83 long t1
=System
.currentTimeMillis();
84 System
.out
.println("Counted "+count
[0]+" items to update in "+(t1
-t0
)/1000.0+"s");
85 m
.beginTask(CoreText
.UpdateOperation_updating
, count
[0]);
86 final IProgressMonitor fm
= m
;
87 for (Object obj
: rsrcList
) {
88 if (obj
instanceof IResource
) {
89 final IResource r
= (IResource
)obj
;
90 final RepositoryMapping rm
= RepositoryMapping
.getMapping(r
);
91 final GitIndex index
= rm
.getRepository().getIndex();
92 tomerge
.put(rm
, Boolean
.TRUE
);
93 if (r
instanceof IContainer
) {
94 ((IContainer
)r
).accept(new IResourceVisitor() {
95 public boolean visit(IResource resource
) throws CoreException
{
97 if (resource
.getType() == IResource
.FILE
) {
98 String path
= rm
.getRepoRelativePath(resource
);
99 Entry entry
= index
.getEntry(path
);
101 entry
.update(new File(rm
.getWorkDir(),path
));
105 } catch (IOException e
) {
107 throw Activator
.error(CoreText
.UpdateOperation_failed
, e
);
111 },IResource
.DEPTH_INFINITE
, IContainer
.EXCLUDE_DERIVED
);
113 String path
= rm
.getRepoRelativePath(r
);
114 Entry entry
= index
.getEntry(path
);
116 entry
.update(new File(rm
.getWorkDir(),path
));
122 for (RepositoryMapping rm
: tomerge
.keySet()) {
123 m
.setTaskName("Writing index for "+rm
.getRepository().getDirectory());
124 rm
.getRepository().getIndex().write();
126 } catch (NotSupportedException e
) {
127 return Activator
.error(e
.getMessage(),e
).getStatus();
128 } catch (RuntimeException e
) {
130 return Activator
.error(CoreText
.UpdateOperation_failed
, e
).getStatus();
131 } catch (IOException e
) {
133 return Activator
.error(CoreText
.UpdateOperation_failed
, e
).getStatus();
134 } catch (CoreException e
) {
136 return Activator
.error(CoreText
.UpdateOperation_failed
, e
).getStatus();
139 final Iterator i
= tomerge
.keySet().iterator();
140 while (i
.hasNext()) {
141 final RepositoryMapping r
= (RepositoryMapping
) i
.next();
142 r
.getRepository().getIndex().read();
143 r
.fireRepositoryChanged();
145 } catch (IOException e
) {
156 return Status
.OK_STATUS
;
159 private void trace(final String m
) {
160 Activator
.trace("(UpdateJob)"+m
);