Make attempt to refresh when merge conflicts exists more user friendly
[egit.git] / org.spearce.egit.core / src / org / spearce / egit / core / internal / UpdateJob.java
blob72b419081b5c556abeaac55b4a6966a4ed3eb4a6
1 /*
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;
19 import java.io.File;
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) {
54 if (m == null) {
55 m = new NullProgressMonitor();
58 trace("running");
59 try {
60 final IdentityHashMap<RepositoryMapping, Boolean> tomerge = new IdentityHashMap<RepositoryMapping, Boolean>();
61 try {
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) {
69 count[0]++;
71 return true;
73 }, IContainer.EXCLUDE_DERIVED);
74 } else if (obj instanceof IResource) {
75 count[0]++;
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 {
91 try {
92 if (resource.getType() == IResource.FILE) {
93 String path = rm.getRepoRelativePath(resource);
94 Entry entry = index.getEntry(path);
95 if (entry != null) {
96 entry.update(new File(rm.getWorkDir(),path));
98 fm.worked(1);
100 } catch (IOException e) {
101 e.printStackTrace();
102 throw Activator.error(CoreText.UpdateOperation_failed, e);
104 return true;
106 },IResource.DEPTH_INFINITE, IContainer.EXCLUDE_DERIVED);
107 } else {
108 String path = rm.getRepoRelativePath(r);
109 Entry entry = index.getEntry(path);
110 if (entry != null) {
111 entry.update(new File(rm.getWorkDir(),path));
113 m.worked(1);
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) {
124 e.printStackTrace();
125 return Activator.error(CoreText.UpdateOperation_failed, e).getStatus();
126 } catch (IOException e) {
127 e.printStackTrace();
128 return Activator.error(CoreText.UpdateOperation_failed, e).getStatus();
129 } catch (CoreException e) {
130 e.printStackTrace();
131 return Activator.error(CoreText.UpdateOperation_failed, e).getStatus();
132 } finally {
133 try {
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.recomputeMerge();
140 } catch (IOException e) {
141 e.printStackTrace();
142 } finally {
143 m.done();
146 } finally {
147 trace("done");
148 m.done();
151 return Status.OK_STATUS;
154 private void trace(final String m) {
155 Activator.trace("(UpdateJob)"+m);