1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.diff;
12
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.List;
17
18 import org.eclipse.jgit.attributes.Attribute;
19 import org.eclipse.jgit.internal.JGitText;
20 import org.eclipse.jgit.lib.AbbreviatedObjectId;
21 import org.eclipse.jgit.lib.AnyObjectId;
22 import org.eclipse.jgit.lib.Constants;
23 import org.eclipse.jgit.lib.FileMode;
24 import org.eclipse.jgit.lib.MutableObjectId;
25 import org.eclipse.jgit.lib.ObjectId;
26 import org.eclipse.jgit.treewalk.TreeWalk;
27 import org.eclipse.jgit.treewalk.filter.TreeFilter;
28 import org.eclipse.jgit.treewalk.filter.TreeFilterMarker;
29
30
31
32
33 public class DiffEntry {
34
35 static final AbbreviatedObjectId A_ZERO = AbbreviatedObjectId
36 .fromObjectId(ObjectId.zeroId());
37
38
39 public static final String DEV_NULL = "/dev/null";
40
41
42 public enum ChangeType {
43
44 ADD,
45
46
47 MODIFY,
48
49
50 DELETE,
51
52
53 RENAME,
54
55
56 COPY;
57 }
58
59
60 public enum Side {
61
62 OLD,
63
64
65 NEW;
66 }
67
68
69
70
71 protected DiffEntry(){
72
73 }
74
75
76
77
78
79
80
81
82
83
84
85
86 public static List<DiffEntry> scan(TreeWalk walk) throws IOException {
87 return scan(walk, false);
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 public static List<DiffEntry> scan(TreeWalk walk, boolean includeTrees)
109 throws IOException {
110 return scan(walk, includeTrees, null);
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 public static List<DiffEntry> scan(TreeWalk walk, boolean includeTrees,
138 TreeFilter[] markTreeFilters)
139 throws IOException {
140 if (walk.getTreeCount() != 2)
141 throw new IllegalArgumentException(
142 JGitText.get().treeWalkMustHaveExactlyTwoTrees);
143 if (includeTrees && walk.isRecursive())
144 throw new IllegalArgumentException(
145 JGitText.get().cannotBeRecursiveWhenTreesAreIncluded);
146
147 TreeFilterMarker treeFilterMarker;
148 if (markTreeFilters != null && markTreeFilters.length > 0)
149 treeFilterMarker = new TreeFilterMarker(markTreeFilters);
150 else
151 treeFilterMarker = null;
152
153 List<DiffEntry> r = new ArrayList<>();
154 MutableObjectId idBuf = new MutableObjectId();
155 while (walk.next()) {
156 DiffEntry entry = new DiffEntry();
157
158 walk.getObjectId(idBuf, 0);
159 entry.oldId = AbbreviatedObjectId.fromObjectId(idBuf);
160
161 walk.getObjectId(idBuf, 1);
162 entry.newId = AbbreviatedObjectId.fromObjectId(idBuf);
163
164 entry.oldMode = walk.getFileMode(0);
165 entry.newMode = walk.getFileMode(1);
166 entry.newPath = entry.oldPath = walk.getPathString();
167
168 if (walk.getAttributesNodeProvider() != null) {
169 entry.diffAttribute = walk.getAttributes()
170 .get(Constants.ATTR_DIFF);
171 }
172
173 if (treeFilterMarker != null)
174 entry.treeFilterMarks = treeFilterMarker.getMarks(walk);
175
176 if (entry.oldMode == FileMode.MISSING) {
177 entry.oldPath = DiffEntry.DEV_NULL;
178 entry.changeType = ChangeType.ADD;
179 r.add(entry);
180
181 } else if (entry.newMode == FileMode.MISSING) {
182 entry.newPath = DiffEntry.DEV_NULL;
183 entry.changeType = ChangeType.DELETE;
184 r.add(entry);
185
186 } else if (!entry.oldId.equals(entry.newId)) {
187 entry.changeType = ChangeType.MODIFY;
188 if (RenameDetector.sameType(entry.oldMode, entry.newMode))
189 r.add(entry);
190 else
191 r.addAll(breakModify(entry));
192 } else if (entry.oldMode != entry.newMode) {
193 entry.changeType = ChangeType.MODIFY;
194 r.add(entry);
195 }
196
197 if (includeTrees && walk.isSubtree())
198 walk.enterSubtree();
199 }
200 return r;
201 }
202
203 static DiffEntry add(String path, AnyObjectId id) {
204 DiffEntry e = new DiffEntry();
205 e.oldId = A_ZERO;
206 e.oldMode = FileMode.MISSING;
207 e.oldPath = DEV_NULL;
208
209 e.newId = AbbreviatedObjectId.fromObjectId(id);
210 e.newMode = FileMode.REGULAR_FILE;
211 e.newPath = path;
212 e.changeType = ChangeType.ADD;
213 return e;
214 }
215
216 static DiffEntry delete(String path, AnyObjectId id) {
217 DiffEntry e = new DiffEntry();
218 e.oldId = AbbreviatedObjectId.fromObjectId(id);
219 e.oldMode = FileMode.REGULAR_FILE;
220 e.oldPath = path;
221
222 e.newId = A_ZERO;
223 e.newMode = FileMode.MISSING;
224 e.newPath = DEV_NULL;
225 e.changeType = ChangeType.DELETE;
226 return e;
227 }
228
229 static DiffEntry modify(String path) {
230 DiffEntry e = new DiffEntry();
231 e.oldMode = FileMode.REGULAR_FILE;
232 e.oldPath = path;
233
234 e.newMode = FileMode.REGULAR_FILE;
235 e.newPath = path;
236 e.changeType = ChangeType.MODIFY;
237 return e;
238 }
239
240
241
242
243
244
245
246
247
248
249 static List<DiffEntry> breakModify(DiffEntry entry) {
250 DiffEntry del = new DiffEntry();
251 del.oldId = entry.getOldId();
252 del.oldMode = entry.getOldMode();
253 del.oldPath = entry.getOldPath();
254
255 del.newId = A_ZERO;
256 del.newMode = FileMode.MISSING;
257 del.newPath = DiffEntry.DEV_NULL;
258 del.changeType = ChangeType.DELETE;
259 del.diffAttribute = entry.diffAttribute;
260
261 DiffEntry add = new DiffEntry();
262 add.oldId = A_ZERO;
263 add.oldMode = FileMode.MISSING;
264 add.oldPath = DiffEntry.DEV_NULL;
265
266 add.newId = entry.getNewId();
267 add.newMode = entry.getNewMode();
268 add.newPath = entry.getNewPath();
269 add.changeType = ChangeType.ADD;
270 add.diffAttribute = entry.diffAttribute;
271 return Arrays.asList(del, add);
272 }
273
274 static DiffEntry pair(ChangeType changeType, DiffEntry src, DiffEntry dst,
275 int score) {
276 DiffEntry r = new DiffEntry();
277
278 r.oldId = src.oldId;
279 r.oldMode = src.oldMode;
280 r.oldPath = src.oldPath;
281
282 r.newId = dst.newId;
283 r.newMode = dst.newMode;
284 r.newPath = dst.newPath;
285 r.diffAttribute = dst.diffAttribute;
286
287 r.changeType = changeType;
288 r.score = score;
289
290 r.treeFilterMarks = src.treeFilterMarks | dst.treeFilterMarks;
291
292 return r;
293 }
294
295
296 protected String oldPath;
297
298
299 protected String newPath;
300
301
302
303
304
305
306 protected Attribute diffAttribute;
307
308
309 protected FileMode oldMode;
310
311
312 protected FileMode newMode;
313
314
315 protected ChangeType changeType;
316
317
318 protected int score;
319
320
321 protected AbbreviatedObjectId oldId;
322
323
324 protected AbbreviatedObjectId newId;
325
326
327
328
329
330 private int treeFilterMarks = 0;
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347 public String getOldPath() {
348 return oldPath;
349 }
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366 public String getNewPath() {
367 return newPath;
368 }
369
370
371
372
373
374
375
376
377 public String getPath(Side side) {
378 return side == Side.OLD ? getOldPath() : getNewPath();
379 }
380
381
382
383
384
385 public Attribute getDiffAttribute() {
386 return diffAttribute;
387 }
388
389
390
391
392
393
394 public FileMode getOldMode() {
395 return oldMode;
396 }
397
398
399
400
401
402
403 public FileMode getNewMode() {
404 return newMode;
405 }
406
407
408
409
410
411
412
413
414 public FileMode getMode(Side side) {
415 return side == Side.OLD ? getOldMode() : getNewMode();
416 }
417
418
419
420
421
422
423 public ChangeType getChangeType() {
424 return changeType;
425 }
426
427
428
429
430
431
432
433
434
435 public int getScore() {
436 return score;
437 }
438
439
440
441
442
443
444 public AbbreviatedObjectId getOldId() {
445 return oldId;
446 }
447
448
449
450
451
452
453 public AbbreviatedObjectId getNewId() {
454 return newId;
455 }
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482 public boolean isMarked(int index) {
483 return (treeFilterMarks & (1L << index)) != 0;
484 }
485
486
487
488
489
490
491
492
493
494 public int getTreeFilterMarks() {
495 return treeFilterMarks;
496 }
497
498
499
500
501
502
503
504
505 public AbbreviatedObjectId getId(Side side) {
506 return side == Side.OLD ? getOldId() : getNewId();
507 }
508
509
510 @SuppressWarnings("nls")
511 @Override
512 public String toString() {
513 StringBuilder buf = new StringBuilder();
514 buf.append("DiffEntry[");
515 buf.append(changeType);
516 buf.append(" ");
517 switch (changeType) {
518 case ADD:
519 buf.append(newPath);
520 break;
521 case COPY:
522 buf.append(oldPath + "->" + newPath);
523 break;
524 case DELETE:
525 buf.append(oldPath);
526 break;
527 case MODIFY:
528 buf.append(oldPath);
529 break;
530 case RENAME:
531 buf.append(oldPath + "->" + newPath);
532 break;
533 }
534 buf.append("]");
535 return buf.toString();
536 }
537 }