Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a71ee53b5ee7e0331c7cd49be0179a98cb4b020b (plain) (blame)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
/*******************************************************************************
 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
 * Copyright (C) 2008, Google Inc.
 * Copyright (C) 2010, Matthias Sohn <matthias.sohn@sap.com>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.egit.core.op;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.egit.core.Activator;
import org.eclipse.egit.core.internal.CoreText;
import org.eclipse.egit.core.internal.job.RuleUtil;
import org.eclipse.egit.core.project.GitProjectData;
import org.eclipse.egit.core.project.RepositoryMapping;
import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.dircache.DirCacheEntry;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.osgi.util.NLS;

/**
 * Tell JGit to ignore changes in selected files
 */
public class AssumeUnchangedOperation implements IEGitOperation {
	private final Collection<? extends IResource> rsrcList;
	private final Collection<IPath> locations;

	private Repository db;

	private final IdentityHashMap<Repository, DirCache> caches;

	private final boolean assumeUnchanged;

	/**
	 * Create a new operation to ignore changes in tracked files
	 *
	 * @param rsrcs
	 *            collection of {@link IResource}s which should be ignored when
	 *            looking for changes or committing.
	 * @param assumeUnchanged
	 *            {@code true} to set the assume-valid flag in the git index
	 *            entry, {@code false} to unset the assume-valid flag in the git
	 *            index entry
	 */
	public AssumeUnchangedOperation(
			final Collection<? extends IResource> rsrcs, boolean assumeUnchanged) {
		rsrcList = rsrcs;
		locations = Collections.emptyList();
		caches = new IdentityHashMap<Repository, DirCache>();
		this.assumeUnchanged = assumeUnchanged;
	}

	/**
	 * Create a new operation to ignore changes in tracked files
	 *
	 * @param repository
	 *            a Git repository
	 * @param locations
	 *            collection of {@link IPath}s which should be ignored when
	 *            looking for changes or committing.
	 * @param assumeUnchanged
	 *            {@code true} to set the assume-valid flag in the git index
	 *            entry, {@code false} to unset the assume-valid flag in the git
	 *            index entry
	 */
	public AssumeUnchangedOperation(final Repository repository,
			final Collection<IPath> locations,
			boolean assumeUnchanged) {
		this.db = repository;
		this.locations = locations;
		this.rsrcList = Collections.emptyList();
		caches = new IdentityHashMap<Repository, DirCache>();
		this.assumeUnchanged = assumeUnchanged;
	}

	@Override
	public void execute(IProgressMonitor monitor) throws CoreException {
		SubMonitor progress = SubMonitor.convert(monitor, (rsrcList.size() + locations.size()) * 2);
		progress.setTaskName(CoreText.AssumeUnchangedOperation_adding);

		caches.clear();

		try {
			for (IResource resource : rsrcList) {
				assumeValid(resource);
				progress.worked(1);
			}
			for (IPath location : locations) {
				assumeValid(location);
				progress.worked(1);
			}

			progress.setWorkRemaining(caches.size());
			for (Map.Entry<Repository, DirCache> e : caches.entrySet()) {
				final Repository db = e.getKey();
				final DirCache editor = e.getValue();
				progress.setTaskName(NLS.bind(
						CoreText.AssumeUnchangedOperation_writingIndex, db
								.getDirectory()));
				editor.write();
				editor.commit();
				progress.worked(1);
			}
		} catch (RuntimeException e) {
			throw new CoreException(Activator.error(CoreText.UntrackOperation_failed, e));
		} catch (IOException e) {
			throw new CoreException(Activator.error(CoreText.UntrackOperation_failed, e));
		} finally {
			for (DirCache cache : caches.values()) {
				cache.unlock();
			}
			caches.clear();
		}
	}

	@Override
	public ISchedulingRule getSchedulingRule() {
		return RuleUtil.getRuleForRepositories(rsrcList.toArray(new IResource[rsrcList.size()]));
	}

	private void assumeValid(final IResource resource) throws CoreException {
		final IProject proj = resource.getProject();
		if (proj == null) {
			return;
		}
		final GitProjectData pd = GitProjectData.get(proj);
		if (pd == null) {
			return;
		}
		final RepositoryMapping rm = pd.getRepositoryMapping(resource);
		if (rm == null) {
			return;
		}
		this.db = rm.getRepository();
		assumeValid(resource.getLocation());
	}

	private void assumeValid(final IPath location) throws CoreException {
		DirCache cache = caches.get(db);
		if (cache == null) {
			try {
				cache = db.lockDirCache();
			} catch (IOException err) {
				throw new CoreException(Activator.error(CoreText.UntrackOperation_failed, err));
			}
			caches.put(db, cache);
		}

		IPath dbDir = new Path(db.getWorkTree().getAbsolutePath());
		final String path = location.makeRelativeTo(dbDir).toString();
		final DirCacheEntry ent = cache.getEntry(path);
		if (ent != null) {
			ent.setAssumeValid(assumeUnchanged);
		}
	}
}

Back to the top