Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c605c7ae3bedeb3a9c56281721eaf5d3ce2f778b (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*******************************************************************************
 * Copyright (C) 2010, Dariusz Luksza <dariusz@luksza.org>
 *
 * 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.ui.internal.synchronize.model;

import static org.eclipse.compare.structuremergeviewer.Differencer.RIGHT;
import static org.eclipse.egit.ui.internal.synchronize.compare.GitCompareInput.getFileRevisionLabel;
import static org.eclipse.jgit.lib.ObjectId.zeroId;

import java.io.IOException;

import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.IResourceProvider;
import org.eclipse.compare.ITypedElement;
import org.eclipse.compare.structuremergeviewer.ICompareInput;
import org.eclipse.compare.structuremergeviewer.ICompareInputChangeListener;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.egit.core.Activator;
import org.eclipse.egit.core.synchronize.GitCommitsModelCache.Change;
import org.eclipse.egit.ui.internal.synchronize.compare.ComparisonDataSource;
import org.eclipse.egit.ui.internal.synchronize.compare.GitCompareInput;
import org.eclipse.jgit.lib.AbbreviatedObjectId;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.swt.graphics.Image;
import org.eclipse.team.ui.mapping.ISynchronizationCompareInput;
import org.eclipse.team.ui.mapping.SaveableComparison;

/**
 * Git blob object representation in Git ChangeSet
 */
public class GitModelBlob extends GitModelObject implements
		ISynchronizationCompareInput, IResourceProvider {

	private static final GitModelObject[] empty = new GitModelObject[0];

	private final Change change;

	private ITypedElement ancestorElement;

	private ITypedElement leftElement;

	private ITypedElement rightElement;

	/**
	 * Absolute path to changed object
	 */
	protected final IPath path;

	/**
	 * {@link Repository} associated with this object
	 */
	protected final Repository repo;

	/**
	 * @param parent
	 *            parent object
	 * @param repo
	 *            repository associated with this object
	 * @param change
	 *            change associated with this object
	 * @param path
	 *            absolute path of change
	 */
	public GitModelBlob(GitModelObjectContainer parent, Repository repo,
			Change change, IPath path) {
		super(parent);
		this.repo = repo;
		this.path = path;
		this.change = change;
	}

	@Override
	public GitModelObject[] getChildren() {
		return empty;
	}

	@Override
	public String getName() {
		return path.lastSegment();
	}

	@Override
	public IPath getLocation() {
		return path;
	}

	@Override
	public boolean isContainer() {
		return false;
	}

	@Override
	public int getKind() {
		return change.getKind();
	}

	/**
	 * @return abbreviated object id of base commit
	 */
	public AbbreviatedObjectId getBaseCommitId() {
		return change.getCommitId();
	}

	/**
	 * @return abbreviated object id of remote commit
	 */
	public AbbreviatedObjectId getRemoteCommitId() {
		return change.getRemoteCommitId();
	}

	@Override
	public int repositoryHashCode() {
		return repo.getWorkTree().hashCode();
	}

	@Override
	public void dispose() {
		// there is nothing to dispose
	}

	@Override
	public String toString() {
		return "ModelBlob[objectId=" + change.getObjectId() + ", location=" + getLocation() + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	}

	@Override
	public Image getImage() {
		// not used
		return null;
	}

	@Override
	public ITypedElement getAncestor() {
		prepareTypedElements();
		return ancestorElement;
	}

	@Override
	public ITypedElement getLeft() {
		prepareTypedElements();
		return leftElement;
	}

	@Override
	public ITypedElement getRight() {
		prepareTypedElements();
		return rightElement;
	}

	@Override
	public void addCompareInputChangeListener(
			ICompareInputChangeListener listener) {
		// data in commit will never change, therefore change listeners are
		// useless
	}

	@Override
	public void removeCompareInputChangeListener(
			ICompareInputChangeListener listener) {
		// data in commit will never change, therefore change listeners are
		// useless
	}

	@Override
	public void copy(boolean leftToRight) {
		// do nothing, we should disallow coping content between commits
	}

	@Override
	public SaveableComparison getSaveable() {
		// unused
		return null;
	}

	@Override
	public void prepareInput(CompareConfiguration configuration,
			IProgressMonitor monitor) throws CoreException {
		configuration.setLeftLabel(getFileRevisionLabel(getLeft()));
		configuration.setRightLabel(getFileRevisionLabel(getRight()));
	}

	@Override
	public String getFullPath() {
		return path.toOSString();
	}

	@Override
	public boolean isCompareInputFor(Object object) {
		// not used
		return false;
	}

	@Override
	public int hashCode() {
		int baseHash = 1;
		if (change != null)
			baseHash = change.getObjectId() != null ? change.getObjectId()
				.hashCode() : 31;
		int remoteHash = 11;
		if (change != null)
			remoteHash = change.getRemoteObjectId() != null ? change
				.getRemoteObjectId().hashCode() : 41;

		return baseHash ^ remoteHash ^ path.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		GitModelBlob other = (GitModelBlob) obj;
		if (change == null) {
			if (other.change != null)
				return false;
		} else if (!change.equals(other.change))
			return false;
		if (path == null) {
			if (other.path != null)
				return false;
		} else if (!path.equals(other.path))
			return false;

		return true;
	}

	private void prepareTypedElements() {
		if (ancestorElement != null) // other elements should also not be null
			return;

		ComparisonDataSource baseData;
		ComparisonDataSource remoteData;

		RevCommit baseCommit = null;
		RevCommit remoteCommit = null;
		try (RevWalk rw = new RevWalk(repo)) {
			rw.setRetainBody(true);
			if (change.getCommitId() != null)
				baseCommit = rw.parseCommit(change.getCommitId().toObjectId());
			if (change.getRemoteCommitId() != null)
				remoteCommit = rw.parseCommit(change.getRemoteCommitId()
						.toObjectId());
		} catch (IOException e) {
			Activator.logError(e.getMessage(), e);
		}
		if (baseCommit == null && remoteCommit != null)
			baseCommit = remoteCommit; // prevent from NPE for deleted files

		ObjectId localId = extractObjectId(change.getObjectId());
		ObjectId remoteId = extractObjectId(change.getRemoteObjectId());

		if ((getKind() & RIGHT) == RIGHT) {
			baseData = new ComparisonDataSource(baseCommit, localId);
			remoteData = new ComparisonDataSource(remoteCommit, remoteId);
		} else /* getKind() == LEFT */{
			baseData = new ComparisonDataSource(remoteCommit, remoteId);
			remoteData = new ComparisonDataSource(baseCommit, localId);
		}

		GitCompareInput compareInput = getCompareInput(baseData, remoteData, remoteData);

		ancestorElement = compareInput.getAncestor();
		leftElement = compareInput.getLeft();
		rightElement = compareInput.getRight();
	}

	@Override
	public IResource getResource() {
		IFile file = ResourcesPlugin.getWorkspace().getRoot()
				.getFileForLocation(path);

		return file;
	}

	/**
	 * Returns specific instance of {@link GitCompareInput} for particular
	 * compare input.
	 *
	 * @param baseData
	 * @param remoteData
	 * @param ancestorData
	 * @return Git specific {@link ICompareInput}
	 */
	protected GitCompareInput getCompareInput(ComparisonDataSource baseData,
			ComparisonDataSource remoteData, ComparisonDataSource ancestorData) {
		String gitPath = Repository.stripWorkDir(repo.getWorkTree(), path.toFile());

		return new GitCompareInput(repo, ancestorData, baseData, remoteData,
				gitPath);
	}

	private ObjectId extractObjectId(AbbreviatedObjectId objectId) {
		if (objectId != null)
			return objectId.toObjectId();
		else
			return zeroId();
	}

}

Back to the top