1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.revwalk;
12
13 import java.io.IOException;
14
15 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
16 import org.eclipse.jgit.errors.MissingObjectException;
17
18
19 class TopoSortGenerator extends Generator {
20 private static final int TOPO_DELAY = RevWalk.TOPO_DELAY;
21
22 private final FIFORevQueue pending;
23
24 private final int outputType;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 TopoSortGenerator(Generator s) throws MissingObjectException,
40 IncorrectObjectTypeException, IOException {
41 super(s.firstParent);
42 pending = new FIFORevQueue(firstParent);
43 outputType = s.outputType() | SORT_TOPO;
44 s.shareFreeList(pending);
45 for (;;) {
46 final RevCommit c = s.next();
47 if (c == null) {
48 break;
49 }
50 for (RevCommit p : c.parents) {
51 p.inDegree++;
52 if (firstParent) {
53 break;
54 }
55 }
56 pending.add(c);
57 }
58 }
59
60 @Override
61 int outputType() {
62 return outputType;
63 }
64
65 @Override
66 void shareFreeList(BlockRevQueue q) {
67 q.shareFreeList(pending);
68 }
69
70 @Override
71 RevCommit next() throws MissingObjectException,
72 IncorrectObjectTypeException, IOException {
73 for (;;) {
74 final RevCommit c = pending.next();
75 if (c == null)
76 return null;
77
78 if (c.inDegree > 0) {
79
80
81
82 c.flags |= TOPO_DELAY;
83 continue;
84 }
85
86
87
88
89 for (RevCommit p : c.parents) {
90 if (--p.inDegree == 0 && (p.flags & TOPO_DELAY) != 0) {
91
92
93
94
95 p.flags &= ~TOPO_DELAY;
96 pending.unpop(p);
97 }
98 if (firstParent) {
99 break;
100 }
101 }
102 return c;
103 }
104 }
105 }