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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
|
/*
* Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
* Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
* Copyright (C) 2014, Gustaf Lundh <gustaf.lundh@sonymobile.com> and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.revwalk;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;
import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.errors.CorruptObjectException;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.LargeObjectException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.errors.RevWalkException;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.AsyncObjectLoaderQueue;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.MutableObjectId;
import org.eclipse.jgit.lib.NullProgressMonitor;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectIdOwnerMap;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.ProgressMonitor;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.filter.RevFilter;
import org.eclipse.jgit.treewalk.filter.TreeFilter;
import org.eclipse.jgit.util.References;
/**
* Walks a commit graph and produces the matching commits in order.
* <p>
* A RevWalk instance can only be used once to generate results. Running a
* second time requires creating a new RevWalk instance, or invoking
* {@link #reset()} before starting again. Resetting an existing instance may be
* faster for some applications as commit body parsing can be avoided on the
* later invocations.
* <p>
* RevWalk instances are not thread-safe. Applications must either restrict
* usage of a RevWalk instance to a single thread, or implement their own
* synchronization at a higher level.
* <p>
* Multiple simultaneous RevWalk instances per
* {@link org.eclipse.jgit.lib.Repository} are permitted, even from concurrent
* threads. Equality of {@link org.eclipse.jgit.revwalk.RevCommit}s from two
* different RevWalk instances is never true, even if their
* {@link org.eclipse.jgit.lib.ObjectId}s are equal (and thus they describe the
* same commit).
* <p>
* The offered iterator is over the list of RevCommits described by the
* configuration of this instance. Applications should restrict themselves to
* using either the provided Iterator or {@link #next()}, but never use both on
* the same RevWalk at the same time. The Iterator may buffer RevCommits, while
* {@link #next()} does not.
*/
public class RevWalk implements Iterable<RevCommit>, AutoCloseable {
private static final int MB = 1 << 20;
/**
* Set on objects whose important header data has been loaded.
* <p>
* For a RevCommit this indicates we have pulled apart the tree and parent
* references from the raw bytes available in the repository and translated
* those to our own local RevTree and RevCommit instances. The raw buffer is
* also available for message and other header filtering.
* <p>
* For a RevTag this indicates we have pulled part the tag references to
* find out who the tag refers to, and what that object's type is.
*/
static final int PARSED = 1 << 0;
/**
* Set on RevCommit instances added to our {@link #pending} queue.
* <p>
* We use this flag to avoid adding the same commit instance twice to our
* queue, especially if we reached it by more than one path.
*/
static final int SEEN = 1 << 1;
/**
* Set on RevCommit instances the caller does not want output.
* <p>
* We flag commits as uninteresting if the caller does not want commits
* reachable from a commit given to {@link #markUninteresting(RevCommit)}.
* This flag is always carried into the commit's parents and is a key part
* of the "rev-list B --not A" feature; A is marked UNINTERESTING.
*/
static final int UNINTERESTING = 1 << 2;
/**
* Set on a RevCommit that can collapse out of the history.
* <p>
* If the {@link #treeFilter} concluded that this commit matches his
* parents' for all of the paths that the filter is interested in then we
* mark the commit REWRITE. Later we can rewrite the parents of a REWRITE
* child to remove chains of REWRITE commits before we produce the child to
* the application.
*
* @see RewriteGenerator
*/
static final int REWRITE = 1 << 3;
/**
* Temporary mark for use within generators or filters.
* <p>
* This mark is only for local use within a single scope. If someone sets
* the mark they must unset it before any other code can see the mark.
*/
static final int TEMP_MARK = 1 << 4;
/**
* Temporary mark for use within {@link TopoSortGenerator}.
* <p>
* This mark indicates the commit could not produce when it wanted to, as at
* least one child was behind it. Commits with this flag are delayed until
* all children have been output first.
*/
static final int TOPO_DELAY = 1 << 5;
/**
* Temporary mark for use within {@link TopoNonIntermixSortGenerator}.
* <p>
* This mark indicates the commit has been queued for emission in
* {@link TopoSortGenerator} and can be produced. This mark is removed when
* the commit has been produced.
*/
static final int TOPO_QUEUED = 1 << 6;
/**
* Set on a RevCommit when a {@link TreeRevFilter} has been applied.
* <p>
* This flag is processed by the {@link RewriteGenerator} to check if a
* {@link TreeRevFilter} has been applied.
*
* @see TreeRevFilter
* @see RewriteGenerator
*/
static final int TREE_REV_FILTER_APPLIED = 1 << 7;
/** Number of flag bits we keep internal for our own use. See above flags. */
static final int RESERVED_FLAGS = 8;
private static final int APP_FLAGS = -1 & ~((1 << RESERVED_FLAGS) - 1);
final ObjectReader reader;
private final boolean closeReader;
final MutableObjectId idBuffer;
ObjectIdOwnerMap<RevObject> objects;
int freeFlags = APP_FLAGS;
private int delayFreeFlags;
private int retainOnReset;
int carryFlags = UNINTERESTING;
final ArrayList<RevCommit> roots;
AbstractRevQueue queue;
Generator pending;
private final EnumSet<RevSort> sorting;
private RevFilter filter;
private TreeFilter treeFilter;
private boolean retainBody = true;
private boolean rewriteParents = true;
private boolean firstParent;
boolean shallowCommitsInitialized;
private enum GetMergedIntoStrategy {
RETURN_ON_FIRST_FOUND,
RETURN_ON_FIRST_NOT_FOUND,
EVALUATE_ALL
}
/**
* Create a new revision walker for a given repository.
*
* @param repo
* the repository the walker will obtain data from. An
* ObjectReader will be created by the walker, and will be closed
* when the walker is closed.
*/
public RevWalk(Repository repo) {
this(repo.newObjectReader(), true);
}
/**
* Create a new revision walker for a given repository.
* <p>
*
* @param or
* the reader the walker will obtain data from. The reader is not
* closed when the walker is closed (but is closed by {@link
* #dispose()}.
*/
public RevWalk(ObjectReader or) {
this(or, false);
}
RevWalk(ObjectReader or, boolean closeReader) {
reader = or;
idBuffer = new MutableObjectId();
objects = new ObjectIdOwnerMap<>();
roots = new ArrayList<>();
queue = new DateRevQueue(false);
pending = new StartGenerator(this);
sorting = EnumSet.of(RevSort.NONE);
filter = RevFilter.ALL;
treeFilter = TreeFilter.ALL;
this.closeReader = closeReader;
}
/**
* Get the reader this walker is using to load objects.
*
* @return the reader this walker is using to load objects.
*/
public ObjectReader getObjectReader() {
return reader;
}
/**
* Get a reachability checker for commits over this revwalk.
*
* @return the most efficient reachability checker for this repository.
* @throws IOException
* if it cannot open any of the underlying indices.
*
* @since 5.4
* @deprecated use {@code ObjectReader#createReachabilityChecker(RevWalk)}
* instead.
*/
@Deprecated
public final ReachabilityChecker createReachabilityChecker()
throws IOException {
return reader.createReachabilityChecker(this);
}
/**
* {@inheritDoc}
* <p>
* Release any resources used by this walker's reader.
* <p>
* A walker that has been released can be used again, but may need to be
* released after the subsequent usage.
*
* @since 4.0
*/
@Override
public void close() {
if (closeReader) {
reader.close();
}
}
/**
* Mark a commit to start graph traversal from.
* <p>
* Callers are encouraged to use {@link #parseCommit(AnyObjectId)} to obtain
* the commit reference, rather than {@link #lookupCommit(AnyObjectId)}, as
* this method requires the commit to be parsed before it can be added as a
* root for the traversal.
* <p>
* The method will automatically parse an unparsed commit, but error
* handling may be more difficult for the application to explain why a
* RevCommit is not actually a commit. The object pool of this walker would
* also be 'poisoned' by the non-commit RevCommit.
*
* @param c
* the commit to start traversing from. The commit passed must be
* from this same revision walker.
* @throws org.eclipse.jgit.errors.MissingObjectException
* the commit supplied is not available from the object
* database. This usually indicates the supplied commit is
* invalid, but the reference was constructed during an earlier
* invocation to {@link #lookupCommit(AnyObjectId)}.
* @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
* the object was not parsed yet and it was discovered during
* parsing that it is not actually a commit. This usually
* indicates the caller supplied a non-commit SHA-1 to
* {@link #lookupCommit(AnyObjectId)}.
* @throws java.io.IOException
* a pack file or loose object could not be read.
*/
public void markStart(RevCommit c) throws MissingObjectException,
IncorrectObjectTypeException, IOException {
if ((c.flags & SEEN) != 0)
return;
if ((c.flags & PARSED) == 0)
c.parseHeaders(this);
c.flags |= SEEN;
roots.add(c);
queue.add(c);
}
/**
* Mark commits to start graph traversal from.
*
* @param list
* commits to start traversing from. The commits passed must be
* from this same revision walker.
* @throws org.eclipse.jgit.errors.MissingObjectException
* one of the commits supplied is not available from the object
* database. This usually indicates the supplied commit is
* invalid, but the reference was constructed during an earlier
* invocation to {@link #lookupCommit(AnyObjectId)}.
* @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
* the object was not parsed yet and it was discovered during
* parsing that it is not actually a commit. This usually
* indicates the caller supplied a non-commit SHA-1 to
* {@link #lookupCommit(AnyObjectId)}.
* @throws java.io.IOException
* a pack file or loose object could not be read.
*/
public void markStart(Collection<RevCommit> list)
throws MissingObjectException, IncorrectObjectTypeException,
IOException {
for (RevCommit c : list)
markStart(c);
}
/**
* Mark a commit to not produce in the output.
* <p>
* Uninteresting commits denote not just themselves but also their entire
* ancestry chain, back until the merge base of an uninteresting commit and
* an otherwise interesting commit.
* <p>
* Callers are encouraged to use {@link #parseCommit(AnyObjectId)} to obtain
* the commit reference, rather than {@link #lookupCommit(AnyObjectId)}, as
* this method requires the commit to be parsed before it can be added as a
* root for the traversal.
* <p>
* The method will automatically parse an unparsed commit, but error
* handling may be more difficult for the application to explain why a
* RevCommit is not actually a commit. The object pool of this walker would
* also be 'poisoned' by the non-commit RevCommit.
*
* @param c
* the commit to start traversing from. The commit passed must be
* from this same revision walker.
* @throws org.eclipse.jgit.errors.MissingObjectException
* the commit supplied is not available from the object
* database. This usually indicates the supplied commit is
* invalid, but the reference was constructed during an earlier
* invocation to {@link #lookupCommit(AnyObjectId)}.
* @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
* the object was not parsed yet and it was discovered during
* parsing that it is not actually a commit. This usually
* indicates the caller supplied a non-commit SHA-1 to
* {@link #lookupCommit(AnyObjectId)}.
* @throws java.io.IOException
* a pack file or loose object could not be read.
*/
public void markUninteresting(RevCommit c)
throws MissingObjectException, IncorrectObjectTypeException,
IOException {
c.flags |= UNINTERESTING;
carryFlagsImpl(c);
markStart(c);
}
/**
* Determine if a commit is reachable from another commit.
* <p>
* A commit <code>base</code> is an ancestor of <code>tip</code> if we
* can find a path of commits that leads from <code>tip</code> and ends at
* <code>base</code>.
* <p>
* This utility function resets the walker, inserts the two supplied
* commits, and then executes a walk until an answer can be obtained.
* Currently allocated RevFlags that have been added to RevCommit instances
* will be retained through the reset.
*
* @param base
* commit the caller thinks is reachable from <code>tip</code>.
* @param tip
* commit to start iteration from, and which is most likely a
* descendant (child) of <code>base</code>.
* @return true if there is a path directly from <code>tip</code> to
* <code>base</code> (and thus <code>base</code> is fully merged
* into <code>tip</code>); false otherwise.
* @throws org.eclipse.jgit.errors.MissingObjectException
* one or more of the next commit's parents are not available
* from the object database, but were thought to be candidates
* for traversal. This usually indicates a broken link.
* @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
* one or more of the next commit's parents are not actually
* commit objects.
* @throws java.io.IOException
* a pack file or loose object could not be read.
*/
public boolean isMergedInto(RevCommit base, RevCommit tip)
throws MissingObjectException, IncorrectObjectTypeException,
IOException {
final RevFilter oldRF = filter;
final TreeFilter oldTF = treeFilter;
try {
finishDelayedFreeFlags();
reset(~freeFlags & APP_FLAGS);
filter = RevFilter.MERGE_BASE;
treeFilter = TreeFilter.ALL;
markStart(tip);
markStart(base);
RevCommit mergeBase;
while ((mergeBase = next()) != null) {
if (References.isSameObject(mergeBase, base)) {
return true;
}
}
return false;
} finally {
filter = oldRF;
treeFilter = oldTF;
}
}
/**
* Determine the Refs into which a commit is merged.
* <p>
* A commit is merged into a ref if we can find a path of commits that leads
* from that specific ref and ends at <code>commit</code>.
* <p>
*
* @param commit
* commit the caller thinks is reachable from <code>refs</code>.
* @param refs
* refs to start iteration from, and which is most likely a
* descendant (child) of <code>commit</code>.
* @return list of refs that are reachable from <code>commit</code>.
* @throws java.io.IOException
* a pack file or loose object could not be read.
* @since 5.12
*/
public List<Ref> getMergedInto(RevCommit commit, Collection<Ref> refs)
throws IOException{
return getMergedInto(commit, refs, NullProgressMonitor.INSTANCE);
}
/**
* Determine the Refs into which a commit is merged.
* <p>
* A commit is merged into a ref if we can find a path of commits that leads
* from that specific ref and ends at <code>commit</code>.
* <p>
*
* @param commit
* commit the caller thinks is reachable from <code>refs</code>.
* @param refs
* refs to start iteration from, and which is most likely a
* descendant (child) of <code>commit</code>.
* @param monitor
* the callback for progress and cancellation
* @return list of refs that are reachable from <code>commit</code>.
* @throws java.io.IOException
* a pack file or loose object could not be read.
* @since 5.12
*/
public List<Ref> getMergedInto(RevCommit commit, Collection<Ref> refs,
ProgressMonitor monitor) throws IOException{
return getMergedInto(commit, refs,
GetMergedIntoStrategy.EVALUATE_ALL,
monitor);
}
/**
* Determine if a <code>commit</code> is merged into any of the given
* <code>refs</code>.
*
* @param commit
* commit the caller thinks is reachable from <code>refs</code>.
* @param refs
* refs to start iteration from, and which is most likely a
* descendant (child) of <code>commit</code>.
* @return true if commit is merged into any of the refs; false otherwise.
* @throws java.io.IOException
* a pack file or loose object could not be read.
* @since 5.12
*/
public boolean isMergedIntoAny(RevCommit commit, Collection<Ref> refs)
throws IOException {
return getMergedInto(commit, refs,
GetMergedIntoStrategy.RETURN_ON_FIRST_FOUND,
NullProgressMonitor.INSTANCE).size() > 0;
}
/**
* Determine if a <code>commit</code> is merged into all of the given
* <code>refs</code>.
*
* @param commit
* commit the caller thinks is reachable from <code>refs</code>.
* @param refs
* refs to start iteration from, and which is most likely a
* descendant (child) of <code>commit</code>.
* @return true if commit is merged into all of the refs; false otherwise.
* @throws java.io.IOException
* a pack file or loose object could not be read.
* @since 5.12
*/
public boolean isMergedIntoAll(RevCommit commit, Collection<Ref> refs)
throws IOException {
return getMergedInto(commit, refs,
GetMergedIntoStrategy.RETURN_ON_FIRST_NOT_FOUND,
NullProgressMonitor.INSTANCE).size()
== refs.size();
}
private List<Ref> getMergedInto(RevCommit needle, Collection<Ref> haystacks,
Enum returnStrategy, ProgressMonitor monitor) throws IOException {
List<Ref> result = new ArrayList<>();
List<RevCommit> uninteresting = new ArrayList<>();
List<RevCommit> marked = new ArrayList<>();
RevFilter oldRF = filter;
TreeFilter oldTF = treeFilter;
try {
finishDelayedFreeFlags();
reset(~freeFlags & APP_FLAGS);
filter = RevFilter.ALL;
treeFilter = TreeFilter.ALL;
for (Ref r: haystacks) {
if (monitor.isCancelled()) {
return result;
}
monitor.update(1);
RevObject o = peel(parseAny(r.getObjectId()));
if (!(o instanceof RevCommit)) {
continue;
}
RevCommit c = (RevCommit) o;
reset(UNINTERESTING | TEMP_MARK);
markStart(c);
boolean commitFound = false;
RevCommit next;
while ((next = next()) != null) {
if (References.isSameObject(next, needle)
|| (next.flags & TEMP_MARK) != 0) {
result.add(r);
if (returnStrategy == GetMergedIntoStrategy.RETURN_ON_FIRST_FOUND) {
return result;
}
commitFound = true;
c.flags |= TEMP_MARK;
marked.add(c);
break;
}
}
if(!commitFound){
markUninteresting(c);
uninteresting.add(c);
if (returnStrategy == GetMergedIntoStrategy.RETURN_ON_FIRST_NOT_FOUND) {
return result;
}
}
}
} finally {
roots.addAll(uninteresting);
filter = oldRF;
treeFilter = oldTF;
for (RevCommit c : marked) {
c.flags &= ~TEMP_MARK;
}
}
return result;
}
/**
* Pop the next most recent commit.
*
* @return next most recent commit; null if traversal is over.
* @throws org.eclipse.jgit.errors.MissingObjectException
* one or more of the next commit's parents are not available
* from the object database, but were thought to be candidates
* for traversal. This usually indicates a broken link.
* @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
* one or more of the next commit's parents are not actually
* commit objects.
* @throws java.io.IOException
* a pack file or loose object could not be read.
*/
public RevCommit next() throws MissingObjectException,
IncorrectObjectTypeException, IOException {
return pending.next();
}
/**
* Obtain the sort types applied to the commits returned.
*
* @return the sorting strategies employed. At least one strategy is always
* used, but that strategy may be
* {@link org.eclipse.jgit.revwalk.RevSort#NONE}.
*/
public EnumSet<RevSort> getRevSort() {
return sorting.clone();
}
/**
* Check whether the provided sorting strategy is enabled.
*
* @param sort
* a sorting strategy to look for.
* @return true if this strategy is enabled, false otherwise
*/
public boolean hasRevSort(RevSort sort) {
return sorting.contains(sort);
}
/**
* Select a single sorting strategy for the returned commits.
* <p>
* Disables all sorting strategies, then enables only the single strategy
* supplied by the caller.
*
* @param s
* a sorting strategy to enable.
*/
public void sort(RevSort s) {
assertNotStarted();
sorting.clear();
sorting.add(s);
}
/**
* Add or remove a sorting strategy for the returned commits.
* <p>
* Multiple strategies can be applied at once, in which case some strategies
* may take precedence over others. As an example,
* {@link org.eclipse.jgit.revwalk.RevSort#TOPO} must take precedence over
* {@link org.eclipse.jgit.revwalk.RevSort#COMMIT_TIME_DESC}, otherwise it
* cannot enforce its ordering.
*
* @param s
* a sorting strategy to enable or disable.
* @param use
* true if this strategy should be used, false if it should be
* removed.
*/
public void sort(RevSort s, boolean use) {
assertNotStarted();
if (use)
sorting.add(s);
else
sorting.remove(s);
if (sorting.size() > 1)
sorting.remove(RevSort.NONE);
else if (sorting.isEmpty())
sorting.add(RevSort.NONE);
}
/**
* Get the currently configured commit filter.
*
* @return the current filter. Never null as a filter is always needed.
*/
@NonNull
public RevFilter getRevFilter() {
return filter;
}
/**
* Set the commit filter for this walker.
* <p>
* Multiple filters may be combined by constructing an arbitrary tree of
* <code>AndRevFilter</code> or <code>OrRevFilter</code> instances to
* describe the boolean expression required by the application. Custom
* filter implementations may also be constructed by applications.
* <p>
* Note that filters are not thread-safe and may not be shared by concurrent
* RevWalk instances. Every RevWalk must be supplied its own unique filter,
* unless the filter implementation specifically states it is (and always
* will be) thread-safe. Callers may use
* {@link org.eclipse.jgit.revwalk.filter.RevFilter#clone()} to create a
* unique filter tree for this RevWalk instance.
*
* @param newFilter
* the new filter. If null the special
* {@link org.eclipse.jgit.revwalk.filter.RevFilter#ALL} filter
* will be used instead, as it matches every commit.
* @see org.eclipse.jgit.revwalk.filter.AndRevFilter
* @see org.eclipse.jgit.revwalk.filter.OrRevFilter
*/
public void setRevFilter(RevFilter newFilter) {
assertNotStarted();
filter = newFilter != null ? newFilter : RevFilter.ALL;
}
/**
* Get the tree filter used to simplify commits by modified paths.
*
* @return the current filter. Never null as a filter is always needed. If
* no filter is being applied
* {@link org.eclipse.jgit.treewalk.filter.TreeFilter#ALL} is
* returned.
*/
@NonNull
public TreeFilter getTreeFilter() {
return treeFilter;
}
/**
* Set the tree filter used to simplify commits by modified paths.
* <p>
* If null or {@link org.eclipse.jgit.treewalk.filter.TreeFilter#ALL} the
* path limiter is removed. Commits will not be simplified.
* <p>
* If non-null and not
* {@link org.eclipse.jgit.treewalk.filter.TreeFilter#ALL} then the tree
* filter will be installed. Commits will have their ancestry simplified to
* hide commits that do not contain tree entries matched by the filter,
* unless {@code setRewriteParents(false)} is called.
* <p>
* Usually callers should be inserting a filter graph including
* {@link org.eclipse.jgit.treewalk.filter.TreeFilter#ANY_DIFF} along with
* one or more {@link org.eclipse.jgit.treewalk.filter.PathFilter}
* instances.
*
* @param newFilter
* new filter. If null the special
* {@link org.eclipse.jgit.treewalk.filter.TreeFilter#ALL} filter
* will be used instead, as it matches everything.
* @see org.eclipse.jgit.treewalk.filter.PathFilter
*/
public void setTreeFilter(TreeFilter newFilter) {
assertNotStarted();
treeFilter = newFilter != null ? newFilter : TreeFilter.ALL;
}
/**
* Set whether to rewrite parent pointers when filtering by modified paths.
* <p>
* By default, when {@link #setTreeFilter(TreeFilter)} is called with non-
* null and non-{@link org.eclipse.jgit.treewalk.filter.TreeFilter#ALL}
* filter, commits will have their ancestry simplified and parents rewritten
* to hide commits that do not match the filter.
* <p>
* This behavior can be bypassed by passing false to this method.
*
* @param rewrite
* whether to rewrite parents; defaults to true.
* @since 3.4
*/
public void setRewriteParents(boolean rewrite) {
rewriteParents = rewrite;
}
boolean getRewriteParents() {
return rewriteParents;
}
/**
* Should the body of a commit or tag be retained after parsing its headers?
* <p>
* Usually the body is always retained, but some application code might not
* care and would prefer to discard the body of a commit as early as
* possible, to reduce memory usage.
* <p>
* True by default on {@link org.eclipse.jgit.revwalk.RevWalk} and false by
* default for {@link org.eclipse.jgit.revwalk.ObjectWalk}.
*
* @return true if the body should be retained; false it is discarded.
*/
public boolean isRetainBody() {
return retainBody;
}
/**
* Set whether or not the body of a commit or tag is retained.
* <p>
* If a body of a commit or tag is not retained, the application must call
* {@link #parseBody(RevObject)} before the body can be safely accessed
* through the type specific access methods.
* <p>
* True by default on {@link org.eclipse.jgit.revwalk.RevWalk} and false by
* default for {@link org.eclipse.jgit.revwalk.ObjectWalk}.
*
* @param retain
* true to retain bodies; false to discard them early.
*/
public void setRetainBody(boolean retain) {
retainBody = retain;
}
/**
* @return whether only first-parent links should be followed when walking.
*
* @since 5.5
*/
public boolean isFirstParent() {
return firstParent;
}
/**
* Set whether or not only first parent links should be followed.
* <p>
* If set, second- and higher-parent links are not traversed at all.
* <p>
* This must be called prior to {@link #markStart(RevCommit)}.
*
* @param enable
* true to walk only first-parent links.
*
* @since 5.5
*/
public void setFirstParent(boolean enable) {
assertNotStarted();
assertNoCommitsMarkedStart();
firstParent = enable;
queue = new DateRevQueue(firstParent);
pending = new StartGenerator(this);
}
/**
* Locate a reference to a blob without loading it.
* <p>
* The blob may or may not exist in the repository. It is impossible to tell
* from this method's return value.
*
* @param id
* name of the blob object.
* @return reference to the blob object. Never null.
*/
@NonNull
public RevBlob lookupBlob(AnyObjectId id) {
RevBlob c = (RevBlob) objects.get(id);
if (c == null) {
c = new RevBlob(id);
objects.add(c);
}
return c;
}
/**
* Locate a reference to a tree without loading it.
* <p>
* The tree may or may not exist in the repository. It is impossible to tell
* from this method's return value.
*
* @param id
* name of the tree object.
* @return reference to the tree object. Never null.
*/
@NonNull
public RevTree lookupTree(AnyObjectId id) {
RevTree c = (RevTree) objects.get(id);
if (c == null) {
c = new RevTree(id);
objects.add(c);
}
return c;
}
/**
* Locate a reference to a commit without loading it.
* <p>
* The commit may or may not exist in the repository. It is impossible to
* tell from this method's return value.
* <p>
* See {@link #parseHeaders(RevObject)} and {@link #parseBody(RevObject)}
* for loading contents.
*
* @param id
* name of the commit object.
* @return reference to the commit object. Never null.
*/
@NonNull
public RevCommit lookupCommit(AnyObjectId id) {
RevCommit c = (RevCommit) objects.get(id);
if (c == null) {
c = createCommit(id);
objects.add(c);
}
return c;
}
/**
* Locate a reference to a tag without loading it.
* <p>
* The tag may or may not exist in the repository. It is impossible to tell
* from this method's return value.
*
* @param id
* name of the tag object.
* @return reference to the tag object. Never null.
*/
@NonNull
public RevTag lookupTag(AnyObjectId id) {
RevTag c = (RevTag) objects.get(id);
if (c == null) {
c = new RevTag(id);
objects.add(c);
}
return c;
}
/**
* Locate a reference to any object without loading it.
* <p>
* The object may or may not exist in the repository. It is impossible to
* tell from this method's return value.
*
* @param id
* name of the object.
* @param type
* type of the object. Must be a valid Git object type.
* @return reference to the object. Never null.
*/
@NonNull
public RevObject lookupAny(AnyObjectId id, int type) {
RevObject r = objects.get(id);
if (r == null) {
switch (type) {
case Constants.OBJ_COMMIT:
r = createCommit(id);
break;
case Constants.OBJ_TREE:
r = new RevTree(id);
break;
case Constants.OBJ_BLOB:
r = new RevBlob(id);
break;
case Constants.OBJ_TAG:
r = new RevTag(id);
break;
default:
throw new IllegalArgumentException(MessageFormat.format(
JGitText.get().invalidGitType, Integer.valueOf(type)));
}
objects.add(r);
}
return r;
}
/**
* Locate an object that was previously allocated in this walk.
*
* @param id
* name of the object.
* @return reference to the object if it has been previously located;
* otherwise null.
*/
public RevObject lookupOrNull(AnyObjectId id) {
return objects.get(id);
}
/**
* Locate a reference to a commit and immediately parse its content.
* <p>
* Unlike {@link #lookupCommit(AnyObjectId)} this method only returns
* successfully if the commit object exists, is verified to be a commit, and
* was parsed without error.
*
* @param id
* name of the commit object.
* @return reference to the commit object. Never null.
* @throws org.eclipse.jgit.errors.MissingObjectException
* the supplied commit does not exist.
* @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
* the supplied id is not a commit or an annotated tag.
* @throws java.io.IOException
* a pack file or loose object could not be read.
*/
@NonNull
public RevCommit parseCommit(AnyObjectId id)
throws MissingObjectException, IncorrectObjectTypeException,
IOException {
RevObject c = peel(parseAny(id));
if (!(c instanceof RevCommit))
throw new IncorrectObjectTypeException(id.toObjectId(),
Constants.TYPE_COMMIT);
return (RevCommit) c;
}
/**
* Locate a reference to a tree.
* <p>
* This method only returns successfully if the tree object exists, is
* verified to be a tree.
*
* @param id
* name of the tree object, or a commit or annotated tag that may
* reference a tree.
* @return reference to the tree object. Never null.
* @throws org.eclipse.jgit.errors.MissingObjectException
* the supplied tree does not exist.
* @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
* the supplied id is not a tree, a commit or an annotated tag.
* @throws java.io.IOException
* a pack file or loose object could not be read.
*/
@NonNull
public RevTree parseTree(AnyObjectId id)
throws MissingObjectException, IncorrectObjectTypeException,
IOException {
RevObject c = peel(parseAny(id));
final RevTree t;
if (c instanceof RevCommit)
t = ((RevCommit) c).getTree();
else if (!(c instanceof RevTree))
throw new IncorrectObjectTypeException(id.toObjectId(),
Constants.TYPE_TREE);
else
t = (RevTree) c;
parseHeaders(t);
return t;
}
/**
* Locate a reference to an annotated tag and immediately parse its content.
* <p>
* Unlike {@link #lookupTag(AnyObjectId)} this method only returns
* successfully if the tag object exists, is verified to be a tag, and was
* parsed without error.
*
* @param id
* name of the tag object.
* @return reference to the tag object. Never null.
* @throws org.eclipse.jgit.errors.MissingObjectException
* the supplied tag does not exist.
* @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
* the supplied id is not a tag or an annotated tag.
* @throws java.io.IOException
* a pack file or loose object could not be read.
*/
@NonNull
public RevTag parseTag(AnyObjectId id) throws MissingObjectException,
IncorrectObjectTypeException, IOException {
RevObject c = parseAny(id);
if (!(c instanceof RevTag))
throw new IncorrectObjectTypeException(id.toObjectId(),
Constants.TYPE_TAG);
return (RevTag) c;
}
/**
* Locate a reference to any object and immediately parse its headers.
* <p>
* This method only returns successfully if the object exists and was parsed
* without error. Parsing an object can be expensive as the type must be
* determined. For blobs this may mean the blob content was unpacked
* unnecessarily, and thrown away.
*
* @param id
* name of the object.
* @return reference to the object. Never null.
* @throws org.eclipse.jgit.errors.MissingObjectException
* the supplied does not exist.
* @throws java.io.IOException
* a pack file or loose object could not be read.
*/
@NonNull
public RevObject parseAny(AnyObjectId id)
throws MissingObjectException, IOException {
RevObject r = objects.get(id);
if (r == null)
r = parseNew(id, reader.open(id));
else
parseHeaders(r);
return r;
}
private RevObject parseNew(AnyObjectId id, ObjectLoader ldr)
throws LargeObjectException, CorruptObjectException,
MissingObjectException, IOException {
RevObject r;
int type = ldr.getType();
switch (type) {
case Constants.OBJ_COMMIT: {
final RevCommit c = createCommit(id);
c.parseCanonical(this, getCachedBytes(c, ldr));
r = c;
break;
}
case Constants.OBJ_TREE: {
r = new RevTree(id);
r.flags |= PARSED;
break;
}
case Constants.OBJ_BLOB: {
r = new RevBlob(id);
r.flags |= PARSED;
break;
}
case Constants.OBJ_TAG: {
final RevTag t = new RevTag(id);
t.parseCanonical(this, getCachedBytes(t, ldr));
r = t;
break;
}
default:
throw new IllegalArgumentException(MessageFormat.format(
JGitText.get().badObjectType, Integer.valueOf(type)));
}
objects.add(r);
return r;
}
byte[] getCachedBytes(RevObject obj) throws LargeObjectException,
MissingObjectException, IncorrectObjectTypeException, IOException {
return getCachedBytes(obj, reader.open(obj, obj.getType()));
}
byte[] getCachedBytes(RevObject obj, ObjectLoader ldr)
throws LargeObjectException, MissingObjectException, IOException {
try {
return ldr.getCachedBytes(5 * MB);
} catch (LargeObjectException tooBig) {
tooBig.setObjectId(obj);
throw tooBig;
}
}
/**
* Asynchronous object parsing.
*
* @param objectIds
* objects to open from the object store. The supplied collection
* must not be modified until the queue has finished.
* @param reportMissing
* if true missing objects are reported by calling failure with a
* MissingObjectException. This may be more expensive for the
* implementation to guarantee. If false the implementation may
* choose to report MissingObjectException, or silently skip over
* the object with no warning.
* @return queue to read the objects from.
*/
public <T extends ObjectId> AsyncRevObjectQueue parseAny(
Iterable<T> objectIds, boolean reportMissing) {
List<T> need = new ArrayList<>();
List<RevObject> have = new ArrayList<>();
for (T id : objectIds) {
RevObject r = objects.get(id);
if (r != null && (r.flags & PARSED) != 0)
have.add(r);
else
need.add(id);
}
final Iterator<RevObject> objItr = have.iterator();
if (need.isEmpty()) {
return new AsyncRevObjectQueue() {
@Override
public RevObject next() {
return objItr.hasNext() ? objItr.next() : null;
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return true;
}
@Override
public void release() {
// In-memory only, no action required.
}
};
}
final AsyncObjectLoaderQueue<T> lItr = reader.open(need, reportMissing);
return new AsyncRevObjectQueue() {
@Override
public RevObject next() throws MissingObjectException,
IncorrectObjectTypeException, IOException {
if (objItr.hasNext())
return objItr.next();
if (!lItr.next())
return null;
ObjectId id = lItr.getObjectId();
ObjectLoader ldr = lItr.open();
RevObject r = objects.get(id);
if (r == null)
r = parseNew(id, ldr);
else if (r instanceof RevCommit) {
byte[] raw = ldr.getCachedBytes();
((RevCommit) r).parseCanonical(RevWalk.this, raw);
} else if (r instanceof RevTag) {
byte[] raw = ldr.getCachedBytes();
((RevTag) r).parseCanonical(RevWalk.this, raw);
} else
r.flags |= PARSED;
return r;
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return lItr.cancel(mayInterruptIfRunning);
}
@Override
public void release() {
lItr.release();
}
};
}
/**
* Ensure the object's critical headers have been parsed.
* <p>
* This method only returns successfully if the object exists and was parsed
* without error.
*
* @param obj
* the object the caller needs to be parsed.
* @throws org.eclipse.jgit.errors.MissingObjectException
* the supplied does not exist.
* @throws java.io.IOException
* a pack file or loose object could not be read.
*/
public void parseHeaders(RevObject obj)
throws MissingObjectException, IOException {
if ((obj.flags & PARSED) == 0)
obj.parseHeaders(this);
}
/**
* Ensure the object's full body content is available.
* <p>
* This method only returns successfully if the object exists and was parsed
* without error.
*
* @param obj
* the object the caller needs to be parsed.
* @throws org.eclipse.jgit.errors.MissingObjectException
* the supplied does not exist.
* @throws java.io.IOException
* a pack file or loose object could not be read.
*/
public void parseBody(RevObject obj)
throws MissingObjectException, IOException {
obj.parseBody(this);
}
/**
* Peel back annotated tags until a non-tag object is found.
*
* @param obj
* the starting object.
* @return If {@code obj} is not an annotated tag, {@code obj}. Otherwise
* the first non-tag object that {@code obj} references. The
* returned object's headers have been parsed.
* @throws org.eclipse.jgit.errors.MissingObjectException
* a referenced object cannot be found.
* @throws java.io.IOException
* a pack file or loose object could not be read.
*/
public RevObject peel(RevObject obj) throws MissingObjectException,
IOException {
while (obj instanceof RevTag) {
parseHeaders(obj);
obj = ((RevTag) obj).getObject();
}
parseHeaders(obj);
return obj;
}
/**
* Create a new flag for application use during walking.
* <p>
* Applications are only assured to be able to create 24 unique flags on any
* given revision walker instance. Any flags beyond 24 are offered only if
* the implementation has extra free space within its internal storage.
*
* @param name
* description of the flag, primarily useful for debugging.
* @return newly constructed flag instance.
* @throws java.lang.IllegalArgumentException
* too many flags have been reserved on this revision walker.
*/
public RevFlag newFlag(String name) {
final int m = allocFlag();
return new RevFlag(this, name, m);
}
int allocFlag() {
if (freeFlags == 0)
throw new IllegalArgumentException(MessageFormat.format(
JGitText.get().flagsAlreadyCreated,
Integer.valueOf(32 - RESERVED_FLAGS)));
final int m = Integer.lowestOneBit(freeFlags);
freeFlags &= ~m;
return m;
}
/**
* Automatically carry a flag from a child commit to its parents.
* <p>
* A carried flag is copied from the child commit onto its parents when the
* child commit is popped from the lowest level of walk's internal graph.
*
* @param flag
* the flag to carry onto parents, if set on a descendant.
*/
public void carry(RevFlag flag) {
if ((freeFlags & flag.mask) != 0)
throw new IllegalArgumentException(MessageFormat.format(JGitText.get().flagIsDisposed, flag.name));
if (flag.walker != this)
throw new IllegalArgumentException(MessageFormat.format(JGitText.get().flagNotFromThis, flag.name));
carryFlags |= flag.mask;
}
/**
* Automatically carry flags from a child commit to its parents.
* <p>
* A carried flag is copied from the child commit onto its parents when the
* child commit is popped from the lowest level of walk's internal graph.
*
* @param set
* the flags to carry onto parents, if set on a descendant.
*/
public void carry(Collection<RevFlag> set) {
for (RevFlag flag : set)
carry(flag);
}
/**
* Preserve a RevFlag during all {@code reset} methods.
* <p>
* Calling {@code retainOnReset(flag)} avoids needing to pass the flag
* during each {@code resetRetain()} invocation on this instance.
* <p>
* Clearing flags marked retainOnReset requires disposing of the flag with
* {@code #disposeFlag(RevFlag)} or disposing of the entire RevWalk by
* {@code #dispose()}.
*
* @param flag
* the flag to retain during all resets.
* @since 3.6
*/
public final void retainOnReset(RevFlag flag) {
if ((freeFlags & flag.mask) != 0)
throw new IllegalArgumentException(MessageFormat.format(JGitText.get().flagIsDisposed, flag.name));
if (flag.walker != this)
throw new IllegalArgumentException(MessageFormat.format(JGitText.get().flagNotFromThis, flag.name));
retainOnReset |= flag.mask;
}
/**
* Preserve a set of RevFlags during all {@code reset} methods.
* <p>
* Calling {@code retainOnReset(set)} avoids needing to pass the flags
* during each {@code resetRetain()} invocation on this instance.
* <p>
* Clearing flags marked retainOnReset requires disposing of the flag with
* {@code #disposeFlag(RevFlag)} or disposing of the entire RevWalk by
* {@code #dispose()}.
*
* @param flags
* the flags to retain during all resets.
* @since 3.6
*/
public final void retainOnReset(Collection<RevFlag> flags) {
for (RevFlag f : flags)
retainOnReset(f);
}
/**
* Allow a flag to be recycled for a different use.
* <p>
* Recycled flags always come back as a different Java object instance when
* assigned again by {@link #newFlag(String)}.
* <p>
* If the flag was previously being carried, the carrying request is
* removed. Disposing of a carried flag while a traversal is in progress has
* an undefined behavior.
*
* @param flag
* the to recycle.
*/
public void disposeFlag(RevFlag flag) {
freeFlag(flag.mask);
}
void freeFlag(int mask) {
retainOnReset &= ~mask;
if (isNotStarted()) {
freeFlags |= mask;
carryFlags &= ~mask;
} else {
delayFreeFlags |= mask;
}
}
private void finishDelayedFreeFlags() {
if (delayFreeFlags != 0) {
freeFlags |= delayFreeFlags;
carryFlags &= ~delayFreeFlags;
delayFreeFlags = 0;
}
}
/**
* Resets internal state and allows this instance to be used again.
* <p>
* Unlike {@link #dispose()} previously acquired RevObject (and RevCommit)
* instances are not invalidated. RevFlag instances are not invalidated, but
* are removed from all RevObjects.
*/
public final void reset() {
reset(0);
}
/**
* Resets internal state and allows this instance to be used again.
* <p>
* Unlike {@link #dispose()} previously acquired RevObject (and RevCommit)
* instances are not invalidated. RevFlag instances are not invalidated, but
* are removed from all RevObjects.
*
* @param retainFlags
* application flags that should <b>not</b> be cleared from
* existing commit objects.
*/
public final void resetRetain(RevFlagSet retainFlags) {
reset(retainFlags.mask);
}
/**
* Resets internal state and allows this instance to be used again.
* <p>
* Unlike {@link #dispose()} previously acquired RevObject (and RevCommit)
* instances are not invalidated. RevFlag instances are not invalidated, but
* are removed from all RevObjects.
* <p>
* See {@link #retainOnReset(RevFlag)} for an alternative that does not
* require passing the flags during each reset.
*
* @param retainFlags
* application flags that should <b>not</b> be cleared from
* existing commit objects.
*/
public final void resetRetain(RevFlag... retainFlags) {
int mask = 0;
for (RevFlag flag : retainFlags)
mask |= flag.mask;
reset(mask);
}
/**
* Resets internal state and allows this instance to be used again.
* <p>
* Unlike {@link #dispose()} previously acquired RevObject (and RevCommit)
* instances are not invalidated. RevFlag instances are not invalidated, but
* are removed from all RevObjects. The value of {@code firstParent} is
* retained.
*
* @param retainFlags
* application flags that should <b>not</b> be cleared from
* existing commit objects.
*/
protected void reset(int retainFlags) {
finishDelayedFreeFlags();
retainFlags |= PARSED | retainOnReset;
final int clearFlags = ~retainFlags;
final FIFORevQueue q = new FIFORevQueue();
for (RevCommit c : roots) {
if ((c.flags & clearFlags) == 0)
continue;
c.flags &= retainFlags;
c.reset();
q.add(c);
}
for (;;) {
final RevCommit c = q.next();
if (c == null)
break;
if (c.parents == null)
continue;
for (RevCommit p : c.parents) {
if ((p.flags & clearFlags) == 0)
continue;
p.flags &= retainFlags;
p.reset();
q.add(p);
}
}
roots.clear();
queue = new DateRevQueue(firstParent);
pending = new StartGenerator(this);
}
/**
* Dispose all internal state and invalidate all RevObject instances.
* <p>
* All RevObject (and thus RevCommit, etc.) instances previously acquired
* from this RevWalk are invalidated by a dispose call. Applications must
* not retain or use RevObject instances obtained prior to the dispose call.
* All RevFlag instances are also invalidated, and must not be reused.
*/
public void dispose() {
reader.close();
freeFlags = APP_FLAGS;
delayFreeFlags = 0;
retainOnReset = 0;
carryFlags = UNINTERESTING;
firstParent = false;
objects.clear();
roots.clear();
queue = new DateRevQueue(firstParent);
pending = new StartGenerator(this);
shallowCommitsInitialized = false;
}
/**
* Like {@link #next()}, but if a checked exception is thrown during the
* walk it is rethrown as a {@link RevWalkException}.
*
* @throws RevWalkException if an {@link IOException} was thrown.
* @return next most recent commit; null if traversal is over.
*/
@Nullable
private RevCommit nextForIterator() {
try {
return next();
} catch (IOException e) {
throw new RevWalkException(e);
}
}
/**
* {@inheritDoc}
* <p>
* Returns an Iterator over the commits of this walker.
* <p>
* The returned iterator is only useful for one walk. If this RevWalk gets
* reset a new iterator must be obtained to walk over the new results.
* <p>
* Applications must not use both the Iterator and the {@link #next()} API
* at the same time. Pick one API and use that for the entire walk.
* <p>
* If a checked exception is thrown during the walk (see {@link #next()}) it
* is rethrown from the Iterator as a {@link RevWalkException}.
*
* @see RevWalkException
*/
@Override
public Iterator<RevCommit> iterator() {
RevCommit first = nextForIterator();
return new Iterator<>() {
RevCommit next = first;
@Override
public boolean hasNext() {
return next != null;
}
@Override
public RevCommit next() {
RevCommit r = next;
next = nextForIterator();
return r;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
/**
* Throws an exception if we have started producing output.
*/
protected void assertNotStarted() {
if (isNotStarted())
return;
throw new IllegalStateException(JGitText.get().outputHasAlreadyBeenStarted);
}
/**
* Throws an exception if any commits have been marked as start.
* <p>
* If {@link #markStart(RevCommit)} has already been called,
* {@link #reset()} can be called to satisfy this condition.
*
* @since 5.5
*/
protected void assertNoCommitsMarkedStart() {
if (roots.isEmpty())
return;
throw new IllegalStateException(
JGitText.get().commitsHaveAlreadyBeenMarkedAsStart);
}
private boolean isNotStarted() {
return pending instanceof StartGenerator;
}
/**
* Create and return an {@link org.eclipse.jgit.revwalk.ObjectWalk} using
* the same objects.
* <p>
* Prior to using this method, the caller must reset this RevWalk to clean
* any flags that were used during the last traversal.
* <p>
* The returned ObjectWalk uses the same ObjectReader, internal object pool,
* and free RevFlags. Once the ObjectWalk is created, this RevWalk should
* not be used anymore.
*
* @return a new walk, using the exact same object pool.
*/
public ObjectWalk toObjectWalkWithSameObjects() {
ObjectWalk ow = new ObjectWalk(reader);
RevWalk rw = ow;
rw.objects = objects;
rw.freeFlags = freeFlags;
return ow;
}
/**
* Construct a new unparsed commit for the given object.
*
* @param id
* the object this walker requires a commit reference for.
* @return a new unparsed reference for the object.
*/
protected RevCommit createCommit(AnyObjectId id) {
return new RevCommit(id);
}
void carryFlagsImpl(RevCommit c) {
final int carry = c.flags & carryFlags;
if (carry != 0)
RevCommit.carryFlags(c, carry);
}
/**
* Assume additional commits are shallow (have no parents).
* <p>
* This method is a No-op if the collection is empty.
*
* @param ids
* commits that should be treated as shallow commits, in addition
* to any commits already known to be shallow by the repository.
* @since 3.3
*/
public void assumeShallow(Collection<? extends ObjectId> ids) {
for (ObjectId id : ids)
lookupCommit(id).parents = RevCommit.NO_PARENTS;
}
/**
* Reads the "shallow" file and applies it by setting the parents of shallow
* commits to an empty array.
* <p>
* There is a sequencing problem if the first commit being parsed is a
* shallow commit, since {@link RevCommit#parseCanonical(RevWalk, byte[])}
* calls this method before its callers add the new commit to the
* {@link RevWalk#objects} map. That means a call from this method to
* {@link #lookupCommit(AnyObjectId)} fails to find that commit and creates
* a new one, which is promptly discarded.
* <p>
* To avoid that, {@link RevCommit#parseCanonical(RevWalk, byte[])} passes
* its commit to this method, so that this method can apply the shallow
* state to it directly and avoid creating the duplicate commit object.
*
* @param rc
* the initial commit being parsed
* @throws IOException
* if the shallow commits file can't be read
*/
void initializeShallowCommits(RevCommit rc) throws IOException {
if (shallowCommitsInitialized) {
throw new IllegalStateException(
JGitText.get().shallowCommitsAlreadyInitialized);
}
shallowCommitsInitialized = true;
if (reader == null) {
return;
}
for (ObjectId id : reader.getShallowCommits()) {
if (id.equals(rc.getId())) {
rc.parents = RevCommit.NO_PARENTS;
} else {
lookupCommit(id).parents = RevCommit.NO_PARENTS;
}
}
}
}
|