Replace org.apache.log4j package import by bundle dependency
[egit.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / internal / UpdateJob.java
blobdd3deeaee93d9f8442fc9eef58b03e29967b78b8
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 * which accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 *******************************************************************************/
11 package org.eclipse.egit.core.internal;
13 import java.io.File;
14 import java.io.IOException;
15 import java.util.Collection;
16 import java.util.IdentityHashMap;
17 import java.util.Iterator;
19 import org.eclipse.core.resources.IContainer;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.resources.IResourceProxy;
22 import org.eclipse.core.resources.IResourceProxyVisitor;
23 import org.eclipse.core.resources.IResourceVisitor;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IAdaptable;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.NullProgressMonitor;
29 import org.eclipse.core.runtime.Status;
30 import org.eclipse.core.runtime.jobs.Job;
31 import org.eclipse.egit.core.Activator;
32 import org.eclipse.egit.core.CoreText;
33 import org.eclipse.egit.core.internal.trace.GitTraceLocation;
34 import org.eclipse.egit.core.project.RepositoryMapping;
35 import org.eclipse.jgit.errors.NotSupportedException;
36 import org.eclipse.jgit.lib.GitIndex;
37 import org.eclipse.jgit.lib.GitIndex.Entry;
38 import org.eclipse.osgi.util.NLS;
40 /**
41 * This job updates the index with the content of all specified
42 * and tracked resources. If a project is selected all tracked
43 * resources withing that container are updated.
45 public class UpdateJob extends Job {
47 private final Collection rsrcList;
49 /**
50 * Construct an UpdateJob for the specified resources.
52 * @param rsrcList
54 public UpdateJob(Collection rsrcList) {
55 super(CoreText.UpdateJob_updatingIndex);
56 this.rsrcList = rsrcList;
57 setPriority(Job.LONG);
60 protected IStatus run(IProgressMonitor m) {
61 if (m == null) {
62 m = new NullProgressMonitor();
65 trace("running"); //$NON-NLS-1$
66 try {
67 final IdentityHashMap<RepositoryMapping, Boolean> tomerge = new IdentityHashMap<RepositoryMapping, Boolean>();
68 try {
69 final int[] count=new int[1];
70 long t0=System.currentTimeMillis();
71 for (Object obj : rsrcList) {
72 obj = ((IAdaptable)obj).getAdapter(IResource.class);
73 if (obj instanceof IContainer) {
74 ((IContainer)obj).accept(new IResourceProxyVisitor() {
75 public boolean visit(IResourceProxy rp) throws CoreException {
76 if (rp.getType() == IResource.FILE) {
77 count[0]++;
79 return true;
81 }, IContainer.EXCLUDE_DERIVED);
82 } else if (obj instanceof IResource) {
83 count[0]++;
86 long t1=System.currentTimeMillis();
87 // TODO is this the right location?
88 if (GitTraceLocation.CORE.isActive())
89 GitTraceLocation.getTrace().trace(
90 GitTraceLocation.CORE.getLocation(),
91 "Counted " + count[0] //$NON-NLS-1$
92 + " items to update in " //$NON-NLS-1$
93 + (t1 - t0) / 1000.0 + "s"); //$NON-NLS-1$
94 m.beginTask(CoreText.UpdateOperation_updating, count[0]);
95 final IProgressMonitor fm = m;
96 for (Object obj : rsrcList) {
97 if (obj instanceof IResource) {
98 final IResource r = (IResource)obj;
99 final RepositoryMapping rm = RepositoryMapping.getMapping(r);
100 final GitIndex index = rm.getRepository().getIndex();
101 tomerge.put(rm, Boolean.TRUE);
102 if (r instanceof IContainer) {
103 ((IContainer)r).accept(new IResourceVisitor() {
104 public boolean visit(IResource resource) throws CoreException {
105 try {
106 if (resource.getType() == IResource.FILE) {
107 String path = rm.getRepoRelativePath(resource);
108 Entry entry = index.getEntry(path);
109 if (entry != null) {
110 entry.update(new File(rm.getWorkDir(),path));
112 fm.worked(1);
114 } catch (IOException e) {
115 if (GitTraceLocation.CORE.isActive())
116 GitTraceLocation.getTrace().trace(GitTraceLocation.CORE.getLocation(), e.getMessage(), e);
117 throw new CoreException(Activator.error(CoreText.UpdateOperation_failed, e));
119 return true;
121 },IResource.DEPTH_INFINITE, IContainer.EXCLUDE_DERIVED);
122 } else {
123 String path = rm.getRepoRelativePath(r);
124 Entry entry = index.getEntry(path);
125 if (entry != null) {
126 entry.update(new File(rm.getWorkDir(),path));
128 m.worked(1);
132 for (RepositoryMapping rm : tomerge.keySet()) {
133 m.setTaskName(NLS.bind(CoreText.UpdateJob_writingIndex, rm
134 .getRepository().getDirectory()));
135 rm.getRepository().getIndex().write();
137 } catch (NotSupportedException e) {
138 return Activator.error(e.getMessage(),e);
139 } catch (RuntimeException e) {
140 if (GitTraceLocation.CORE.isActive())
141 GitTraceLocation.getTrace().trace(GitTraceLocation.CORE.getLocation(), e.getMessage(), e);
142 return Activator.error(CoreText.UpdateOperation_failed, e);
143 } catch (IOException e) {
144 if (GitTraceLocation.CORE.isActive())
145 GitTraceLocation.getTrace().trace(GitTraceLocation.CORE.getLocation(), e.getMessage(), e);
146 return Activator.error(CoreText.UpdateOperation_failed, e);
147 } catch (CoreException e) {
148 if (GitTraceLocation.CORE.isActive())
149 GitTraceLocation.getTrace().trace(GitTraceLocation.CORE.getLocation(), e.getMessage(), e);
150 return Activator.error(CoreText.UpdateOperation_failed, e);
151 } finally {
152 try {
153 final Iterator i = tomerge.keySet().iterator();
154 while (i.hasNext()) {
155 final RepositoryMapping r = (RepositoryMapping) i.next();
156 r.getRepository().getIndex().read();
157 r.fireRepositoryChanged();
159 } catch (IOException e) {
160 if (GitTraceLocation.CORE.isActive())
161 GitTraceLocation.getTrace().trace(GitTraceLocation.CORE.getLocation(), e.getMessage(), e);
162 } finally {
163 m.done();
166 } finally {
167 trace("done"); //$NON-NLS-1$
168 m.done();
171 return Status.OK_STATUS;
174 private void trace(final String m) {
175 // TODO is this the right location?
176 if (GitTraceLocation.CORE.isActive())
177 GitTraceLocation.getTrace().trace(
178 GitTraceLocation.CORE.getLocation(), "(UpdateJob)" + m); //$NON-NLS-1$