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
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 class RewriteGenerator extends Generator {
39 private static final int REWRITE = RevWalk.REWRITE;
40
41
42 private static final int DUPLICATE = RevWalk.TEMP_MARK;
43
44 private final Generator source;
45
46 RewriteGenerator(Generator s) {
47 super(s.firstParent);
48 source = s;
49 }
50
51 @Override
52 void shareFreeList(BlockRevQueue q) {
53 source.shareFreeList(q);
54 }
55
56 @Override
57 int outputType() {
58 return source.outputType() & ~NEEDS_REWRITE;
59 }
60
61 @Override
62 RevCommit next() throws MissingObjectException,
63 IncorrectObjectTypeException, IOException {
64 final RevCommit c = source.next();
65 if (c == null) {
66 return null;
67 }
68 boolean rewrote = false;
69 final RevCommit[] pList = c.parents;
70 final int nParents = pList.length;
71 for (int i = 0; i < nParents; i++) {
72 final RevCommit oldp = pList[i];
73 final RevCommit newp = rewrite(oldp);
74 if (firstParent) {
75 if (newp == null) {
76 c.parents = RevCommit.NO_PARENTS;
77 } else {
78 c.parents = new RevCommit[] { newp };
79 }
80 return c;
81 }
82 if (oldp != newp) {
83 pList[i] = newp;
84 rewrote = true;
85 }
86 }
87 if (rewrote) {
88 c.parents = cleanup(pList);
89 }
90 return c;
91 }
92
93 private RevCommit./../../../org/eclipse/jgit/revwalk/RevCommit.html#RevCommit">RevCommit rewrite(RevCommit p) {
94 for (;;) {
95 final RevCommit[] pList = p.parents;
96 if (pList.length > 1) {
97
98
99 return p;
100 }
101
102 if ((p.flags & RevWalk.UNINTERESTING) != 0) {
103
104
105
106 return p;
107 }
108
109 if ((p.flags & REWRITE) == 0) {
110
111
112
113 return p;
114 }
115
116 if (pList.length == 0) {
117
118
119
120 return null;
121 }
122
123 p = pList[0];
124 }
125 }
126
127 private RevCommit../../../org/eclipse/jgit/revwalk/RevCommit.html#RevCommit">RevCommit[] cleanup(RevCommit[] oldList) {
128
129
130
131
132 int newCnt = 0;
133 for (int o = 0; o < oldList.length; o++) {
134 final RevCommit p = oldList[o];
135 if (p == null)
136 continue;
137 if ((p.flags & DUPLICATE) != 0) {
138 oldList[o] = null;
139 continue;
140 }
141 p.flags |= DUPLICATE;
142 newCnt++;
143 }
144
145 if (newCnt == oldList.length) {
146 for (RevCommit p : oldList)
147 p.flags &= ~DUPLICATE;
148 return oldList;
149 }
150
151 final RevCommit.html#RevCommit">RevCommit[] newList = new RevCommit[newCnt];
152 newCnt = 0;
153 for (RevCommit p : oldList) {
154 if (p != null) {
155 newList[newCnt++] = p;
156 p.flags &= ~DUPLICATE;
157 }
158 }
159
160 return newList;
161 }
162 }