aboutsummaryrefslogtreecommitdiffstats
blob: d46a143910e7b1e456d99e1071301b2b86537a3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*******************************************************************************
 * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.egit.core.op;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.IdentityHashMap;
import java.util.Iterator;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.egit.core.Activator;
import org.eclipse.egit.core.CoreText;
import org.eclipse.egit.core.internal.trace.GitTraceLocation;
import org.eclipse.egit.core.project.RepositoryMapping;
import org.eclipse.osgi.util.NLS;
import org.eclipse.team.core.Team;
import org.eclipse.jgit.lib.GitIndex;
import org.eclipse.jgit.lib.GitIndex.Entry;

/**
 * Add one or more new files/folders to the Git repository.
 * <p>
 * Accepts a collection of resources (files and/or directories) which should be
 * added to the their corresponding Git repositories. Resources in the
 * collection can be associated with multiple repositories. The operation will
 * automatically associate each resource with the nearest containing Git
 * repository.
 * </p>
 * <p>
 * Resources are only scheduled for addition in the index.
 * </p>
 */
public class TrackOperation implements IWorkspaceRunnable {
	private final Collection rsrcList;

	/**
	 * Create a new operation to track additional files/folders.
	 *
	 * @param rsrcs
	 *            collection of {@link IResource}s which should be added to the
	 *            relevant Git repositories.
	 */
	public TrackOperation(final Collection rsrcs) {
		rsrcList = rsrcs;
	}

	public void run(IProgressMonitor m) throws CoreException {
		if (m == null) {
			m = new NullProgressMonitor();
		}

		final IdentityHashMap<RepositoryMapping, Boolean> tomerge = new IdentityHashMap<RepositoryMapping, Boolean>();
		m.beginTask(CoreText.AddOperation_adding, rsrcList.size() * 200);
		try {
			for (Object obj : rsrcList) {
				obj = ((IAdaptable)obj).getAdapter(IResource.class);
				if (obj instanceof IResource) {
					final IResource toAdd = (IResource)obj;
					final RepositoryMapping rm = RepositoryMapping.getMapping(toAdd);
					final GitIndex index = rm.getRepository().getIndex();

					if (obj instanceof IFile) {
						String repoPath = rm.getRepoRelativePath((IResource) obj);
						Entry entry = index.getEntry(repoPath);
						if (entry != null) {
							if (!entry.isAssumedValid()) {
								// TODO is this the right location?
								if (GitTraceLocation.CORE.isActive())
									GitTraceLocation
											.getTrace()
											.trace(
													GitTraceLocation.CORE
															.getLocation(),
													"Already tracked - skipping"); //$NON-NLS-1$
								continue;
							}
						}
					}

					tomerge.put(rm, Boolean.TRUE);
					if (toAdd instanceof IContainer) {
						((IContainer)toAdd).accept(new IResourceVisitor() {
							public boolean visit(IResource resource) throws CoreException {
								try {
									String repoPath = rm.getRepoRelativePath(resource);
									// We use add to reset the assume valid bit, so we check the bit
									// first. If a resource within a ignored folder is marked
									// we ignore it here, i.e. there is no way to unmark it expect
									// by explicitly selecting and invoking track on it.
									boolean isIgnored = Team.isIgnoredHint(resource);
									if (resource.getType() == IResource.FILE) {
										Entry entry = index.getEntry(repoPath);
										if (!isIgnored || entry != null && entry.isAssumedValid()) {
											entry = index.add(rm.getWorkDir(), new File(rm.getWorkDir(), repoPath));
											entry.setAssumeValid(false);
										}
									}
									if (isIgnored)
										return false;

								} catch (IOException e) {
									if (GitTraceLocation.CORE.isActive())
										GitTraceLocation.getTrace().trace(GitTraceLocation.CORE.getLocation(), e.getMessage(), e);
									throw Activator.error(CoreText.AddOperation_failed, e);
								}
								return true;
							}
						},IResource.DEPTH_INFINITE, IContainer.EXCLUDE_DERIVED);
					} else {
						Entry entry = index.add(rm.getWorkDir(), new File(rm.getWorkDir(),rm.getRepoRelativePath(toAdd)));
						entry.setAssumeValid(false);

					}
				}
				m.worked(200);
			}
			for (RepositoryMapping rm : tomerge.keySet()) {
				m.setTaskName(NLS.bind(CoreText.TrackOperation_writingIndex, rm.getRepository().getDirectory()));
				rm.getRepository().getIndex().write();
			}
		} catch (RuntimeException e) {
			if (GitTraceLocation.CORE.isActive())
				GitTraceLocation.getTrace().trace(GitTraceLocation.CORE.getLocation(), e.getMessage(), e);
			throw Activator.error(CoreText.AddOperation_failed, e);
		} catch (IOException e) {
			if (GitTraceLocation.CORE.isActive())
				GitTraceLocation.getTrace().trace(GitTraceLocation.CORE.getLocation(), e.getMessage(), e);
			throw Activator.error(CoreText.AddOperation_failed, e);
		} finally {
			try {
				final Iterator i = tomerge.keySet().iterator();
				while (i.hasNext()) {
					final RepositoryMapping r = (RepositoryMapping) i.next();
					r.getRepository().getIndex().read();
					r.fireRepositoryChanged();
				}
			} catch (IOException e) {
				if (GitTraceLocation.CORE.isActive())
					GitTraceLocation.getTrace().trace(GitTraceLocation.CORE.getLocation(), e.getMessage(), e);
			} finally {
				m.done();
			}
		}
	}
}