Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f9c7db901130ef97bbc603c2260fa6a70cdc71fa (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*******************************************************************************
 * Copyright (C) 2011, 2013 Dariusz Luksza <dariusz@luksza.org> and others.
 *
 * 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.synchronize;

import static org.eclipse.jgit.lib.ObjectId.zeroId;
import static org.eclipse.jgit.treewalk.filter.TreeFilter.ANY_DIFF;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jgit.lib.AbbreviatedObjectId;
import org.eclipse.jgit.lib.MutableObjectId;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevFlag;
import org.eclipse.jgit.revwalk.RevFlagSet;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.EmptyTreeIterator;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
import org.eclipse.jgit.treewalk.filter.TreeFilter;

/**
 * Retrieves list of commits and the changes associated with each commit
 */
public class GitCommitsModelCache {

	/**
	 * Constant copied from org.eclipse.compare.structuremergeviewer.Differencer.ADDITION
	 * in order to avoid UI dependencies introduced by the org.eclipse.compare bundle
	 */
	public static final int ADDITION = 1;

	/**
	 * Constant copied from org.eclipse.compare.structuremergeviewer.Differencer.DELETION
	 * in order to avoid UI dependencies introduced by the org.eclipse.compare bundle
	 */
	public static final int DELETION = 2;

	/**
	 * Constant copied from org.eclipse.compare.structuremergeviewer.Differencer.CHANGE
	 * in order to avoid UI dependencies introduced by the org.eclipse.compare bundle
	 */
	public static final int CHANGE = 3;

	/**
	 * Bit mask (value 3) for extracting the kind of difference.
	 */
	private static final int CHANGE_TYPE_MASK = 3;

	/**
	 * Constant copied from org.eclipse.compare.structuremergeviewer.Differencer.LEFT
	 * in order to avoid UI dependencies introduced by the org.eclipse.compare bundle
	 * <p>
	 * Note that in the context of synchronization, this means "INCOMING", shown
	 * by an arrow pointing from right to left.
	 */
	public static final int LEFT = 4;

	/**
	 * Constant copied from org.eclipse.compare.structuremergeviewer.Differencer.RIGHT
	 * in order to avoid UI dependencies introduced by the org.eclipse.compare bundle
	 * <p>
	 * Note that in the context of synchronization, this means "OUTGOING", shown
	 * by an arrow pointing from left to right.
	 */
	public static final int RIGHT = 8;

	/**
	 * Corresponds to {@link RevCommit} object, but contains only those data
	 * that are required by Synchronize view Change Set
	 */
	public static class Commit {
		private int direction;

		private String shortMessage;

		private AbbreviatedObjectId commitId;

		private Date commitDate;

		private String authorName;

		private String committerName;

		private Map<String, Change> children;

		private Commit() {
			// reduce the visibility of the default constructor
		}

		/**
		 * Indicates if this commit is incoming or outgoing. Returned value
		 * corresponds to org.eclipse.compare.structuremergeviewer.Differencer#LEFT for incoming and
		 * org.eclipse.compare.structuremergeviewer.Differencer#RIGHT for outgoing changes
		 *
		 * @return change direction
		 */
		public int getDirection() {
			return direction;
		}

		/**
		 * @return commit id
		 */
		public AbbreviatedObjectId getId() {
			return commitId;
		}

		/**
		 * @return commit author
		 */
		public String getAuthorName() {
			return authorName;
		}

		/**
		 * @return the committer name
		 */
		public String getCommitterName() {
			return committerName;
		}

		/**
		 * @return commit date
		 */
		public Date getCommitDate() {
			return commitDate;
		}

		/**
		 * @return commit short message
		 */
		public String getShortMessage() {
			return shortMessage;
		}

		/**
		 * @return list of changes made by this commit or {@code null} when
		 *         commit doesn't have any changes
		 */
		public Map<String, Change> getChildren() {
			return children;
		}

		/**
		 * Disposes nested resources
		 */
		public void dispose() {
			children.clear();
		}

	}

	/**
	 * Describes single tree or blob change in commit.
	 */
	public static class Change {
		int kind;

		String name;

		AbbreviatedObjectId objectId;

		AbbreviatedObjectId commitId;

		AbbreviatedObjectId remoteCommitId;

		AbbreviatedObjectId remoteObjectId;

		Change() {
			// reduce the visibility of the default constructor
		}

		/**
		 * Describes if this change is incoming/outgoing addition, deletion or
		 * change.
		 *
		 * It uses static values of LEFT, RIGHT, ADDITION, DELETION, CHANGE from
		 * org.eclipse.compare.structuremergeviewer.Differencer class.
		 *
		 * @return kind
		 */
		public int getKind() {
			return kind;
		}

		/**
		 * @return object name
		 */
		public String getName() {
			return name;
		}

		/**
		 * @return id of commit containing this change
		 */
		public AbbreviatedObjectId getCommitId() {
			return commitId;
		}

		/**
		 * @return id of parent commit
		 */
		public AbbreviatedObjectId getRemoteCommitId() {
			return remoteCommitId;
		}

		/**
		 * @return object id
		 */
		public AbbreviatedObjectId getObjectId() {
			return objectId;
		}

		/**
		 * @return remote object id
		 */
		public AbbreviatedObjectId getRemoteObjectId() {
			return remoteObjectId;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result
					+ ((objectId == null) ? 0 : objectId.hashCode());
			result = prime
					* result
					+ ((remoteObjectId == null) ? 0 : remoteObjectId.hashCode());
			return result;
		}

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

		@Override
		public String toString() {
			StringBuilder change = new StringBuilder("Change("); //$NON-NLS-1$
			if ((kind & LEFT) != 0)
				change.append("INCOMING "); //$NON-NLS-1$
			else
				// should be RIGHT
				change.append("OUTGOING "); //$NON-NLS-1$
			int changeType = kind & CHANGE_TYPE_MASK;
			if (changeType == CHANGE)
				change.append("CHANGE "); //$NON-NLS-1$
			else if (changeType == ADDITION)
				change.append("ADDITION "); //$NON-NLS-1$
			else if (changeType == DELETION)
				change.append("DELETION "); //$NON-NLS-1$

			change.append(name);
			change.append(";\n\tcurrent objectId: "); //$NON-NLS-1$
			change.append(getObjectId(objectId));
			change.append(";\n\tparent objectId: "); //$NON-NLS-1$
			change.append(getObjectId(remoteObjectId));
			change.append(";\n\tcurrent commit: "); //$NON-NLS-1$
			change.append(getObjectId(commitId));
			change.append(";\n\tparent commit: "); //$NON-NLS-1$
			change.append(getObjectId(remoteCommitId));
			change.append("\n)"); //$NON-NLS-1$

			return change.toString();
		}

		private String getObjectId(AbbreviatedObjectId object) {
			if (object != null)
				return object.toObjectId().getName();
			else
				return ObjectId.zeroId().getName();
		}

	}

	static final AbbreviatedObjectId ZERO_ID = AbbreviatedObjectId
			.fromObjectId(zeroId());

	/**
	 * Scans given {@code repo} and build list of commits between two given
	 * RevCommit objectId's. Each commit contains list of changed resources
	 *
	 * @param repo
	 *            repository that should be scanned
	 * @param srcId
	 *            commit id that is considered the "local" version (e.g. from
	 *            master)
	 * @param dstId
	 *            commit id that is considered the "remote" version (e.g. from
	 *            origin/master)
	 * @param pathFilter
	 *            path filter definition or {@code null} when all paths should
	 *            be included
	 * @return list of {@link Commit} object's between {@code srcId} and
	 *         {@code dstId}
	 * @throws IOException
	 */
	public static List<Commit> build(Repository repo, ObjectId srcId,
			ObjectId dstId, TreeFilter pathFilter) throws IOException {
		if (dstId.equals(srcId))
			return new ArrayList<Commit>(0);

		try (RevWalk rw = new RevWalk(repo)) {

			final RevFlag localFlag = rw.newFlag("local"); //$NON-NLS-1$
			final RevFlag remoteFlag = rw.newFlag("remote"); //$NON-NLS-1$
			final RevFlagSet allFlags = new RevFlagSet();
			allFlags.add(localFlag);
			allFlags.add(remoteFlag);
			rw.carry(allFlags);

			RevCommit srcCommit = rw.parseCommit(srcId);
			srcCommit.add(localFlag);
			rw.markStart(srcCommit);
			srcCommit = null; // free not needed resources

			RevCommit dstCommit = rw.parseCommit(dstId);
			dstCommit.add(remoteFlag);
			rw.markStart(dstCommit);
			dstCommit = null; // free not needed resources

			if (pathFilter != null)
				rw.setTreeFilter(pathFilter);

			List<Commit> result = new ArrayList<Commit>();
			for (RevCommit revCommit : rw) {
				if (revCommit.hasAll(allFlags))
					break;

				Commit commit = new Commit();
				commit.shortMessage = revCommit.getShortMessage();
				commit.commitId = AbbreviatedObjectId.fromObjectId(revCommit);
				commit.authorName = revCommit.getAuthorIdent().getName();
				commit.committerName = revCommit.getCommitterIdent().getName();
				commit.commitDate = revCommit.getAuthorIdent().getWhen();

				RevCommit parentCommit = getParentCommit(revCommit);
				if (revCommit.has(localFlag))
					// Outgoing
					commit.direction = RIGHT;
				else if (revCommit.has(remoteFlag))
					// Incoming
					commit.direction = LEFT;
				else
					throw new GitCommitsModelDirectionException();

				commit.children = getChangedObjects(repo, revCommit,
						parentCommit, pathFilter, commit.direction);

				if (commit.children != null)
					result.add(commit);
			}
			rw.dispose();
			return result;
		}
	}

	private static RevCommit getParentCommit(RevCommit commit) {
		if (commit.getParents().length > 0)
			return commit.getParents()[0];
		else
			return null;
	}

	private static Map<String, Change> getChangedObjects(Repository repo,
			RevCommit commit, RevCommit parentCommit,
			TreeFilter pathFilter, final int direction) throws IOException {
		final Map<String, Change> result = new HashMap<String, GitCommitsModelCache.Change>();
		try (final TreeWalk tw = new TreeWalk(repo)) {
			int commitIndex = addTree(tw, commit);
			int parentCommitIndex = addTree(tw, parentCommit);

			tw.setRecursive(true);
			if (pathFilter == null)
				tw.setFilter(ANY_DIFF);
			else
				tw.setFilter(AndTreeFilter.create(ANY_DIFF, pathFilter));

			final AbbreviatedObjectId commitId = getAbbreviatedObjectId(commit);
			final AbbreviatedObjectId parentCommitId = getAbbreviatedObjectId(parentCommit);

			MutableObjectId idBuf = new MutableObjectId();
			while (tw.next()) {
				Change change = new Change();
				change.commitId = commitId;
				change.remoteCommitId = parentCommitId;
				change.name = tw.getNameString();
				tw.getObjectId(idBuf, commitIndex);
				change.objectId = AbbreviatedObjectId.fromObjectId(idBuf);
				tw.getObjectId(idBuf, parentCommitIndex);
				change.remoteObjectId = AbbreviatedObjectId.fromObjectId(idBuf);

				calculateAndSetChangeKind(direction, change);

				result.put(tw.getPathString(), change);
			}
		}

		return result.size() > 0 ? result : null;
	}

	private static int addTree(TreeWalk tw, RevCommit commit)
			throws IOException {
		if (commit != null)
			return tw.addTree(commit.getTree());
		else
			return tw.addTree(new EmptyTreeIterator());
	}

	private static AbbreviatedObjectId getAbbreviatedObjectId(RevCommit commit) {
		if (commit != null)
			return AbbreviatedObjectId.fromObjectId(commit);
		else
			return ZERO_ID;
	}

	static void calculateAndSetChangeKind(final int direction, Change change) {
		if (ZERO_ID.equals(change.objectId)) { // removed in commit
			change.objectId = null; // clear zero id;
			change.kind = direction | DELETION;
		} else if (ZERO_ID.equals(change.remoteObjectId)) { // added in commit
			change.remoteObjectId = null; // clear zero id;
			change.kind = direction | ADDITION;
		} else
			change.kind = direction | CHANGE;
	}

}

Back to the top