7 #define UNINTERESTING 4
9 static struct commit
*interesting(struct commit_list
*list
)
12 struct commit
*commit
= list
->item
;
14 if (commit
->object
.flags
& UNINTERESTING
)
22 * A pathological example of how this thing works.
24 * Suppose we had this commit graph, where chronologically
25 * the timestamp on the commit are A <= B <= C <= D <= E <= F
26 * and we are trying to figure out the merge base for E and F
37 * First we push E and F to list to be processed. E gets bit 1
38 * and F gets bit 2. The list becomes:
40 * list=F(2) E(1), result=empty
42 * Then we pop F, the newest commit, from the list. Its flag is 2.
43 * We scan its parents, mark them reachable from the side that F is
44 * reachable from, and push them to the list:
46 * list=E(1) D(2) A(2), result=empty
48 * Next pop E and do the same.
50 * list=D(2) B(1) A(2), result=empty
52 * Next pop D and do the same.
54 * list=C(2) B(1) A(2), result=empty
56 * Next pop C and do the same.
58 * list=B(1) A(2), result=empty
60 * Now it is B's turn. We mark its parent, C, reachable from B's side,
61 * and push it to the list:
63 * list=C(3) A(2), result=empty
65 * Now pop C and notice it has flags==3. It is placed on the result list,
66 * and the list now contains:
68 * list=A(2), result=C(3)
70 * We pop A and do the same.
72 * list=B(3), result=C(3)
74 * Next, we pop B and something very interesting happens. It has flags==3
75 * so it is also placed on the result list, and its parents are marked
76 * uninteresting, retroactively, and placed back on the list:
78 * list=C(7), result=C(7) B(3)
80 * Now, list does not have any interesting commit. So we find the newest
81 * commit from the result list that is not marked uninteresting. Which is
85 * Another pathological example how this thing used to fail to mark an
86 * ancestor of a merge base as UNINTERESTING before we introduced the
87 * postprocessing phase (mark_reachable_commits).
103 * list A B C D E F G H
104 * G1 H2 - - - - - - 1 2
105 * H2 E1 B1 - 1 - - 1 - 1 2
106 * F2 E1 B1 A2 2 1 - - 1 2 1 2
107 * E3 B1 A2 2 1 - - 3 2 1 2
108 * B1 A2 2 1 - - 3 2 1 2
109 * C1 A2 2 1 1 - 3 2 1 2
110 * D1 A2 2 1 1 1 3 2 1 2
115 * At this point, unfortunately, everybody in the list is
116 * uninteresting, so we fail to complete the following two
117 * steps to fully marking uninteresting commits.
122 * and we ended up showing E as an interesting merge base.
123 * The postprocessing phase re-injects C and continues traversal
124 * to contaminate D and E.
127 static void mark_reachable_commits(struct commit_list
*result
,
128 struct commit_list
*list
)
130 struct commit_list
*tmp
;
133 * Postprocess to fully contaminate the well.
135 for (tmp
= result
; tmp
; tmp
= tmp
->next
) {
136 struct commit
*c
= tmp
->item
;
137 /* Reinject uninteresting ones to list,
138 * so we can scan their parents.
140 if (c
->object
.flags
& UNINTERESTING
)
141 commit_list_insert(c
, &list
);
144 struct commit
*c
= list
->item
;
145 struct commit_list
*parents
;
151 /* Anything taken out of the list is uninteresting, so
152 * mark all its parents uninteresting. We do not
153 * parse new ones (we already parsed all the relevant
156 parents
= c
->parents
;
158 struct commit
*p
= parents
->item
;
159 parents
= parents
->next
;
160 if (!(p
->object
.flags
& UNINTERESTING
)) {
161 p
->object
.flags
|= UNINTERESTING
;
162 commit_list_insert(p
, &list
);
168 struct commit_list
*get_merge_bases(struct commit
*rev1
, struct commit
*rev2
)
170 struct commit_list
*list
= NULL
;
171 struct commit_list
*result
= NULL
;
172 struct commit_list
*tmp
= NULL
;
175 return commit_list_insert(rev1
, &result
);
180 rev1
->object
.flags
|= PARENT1
;
181 rev2
->object
.flags
|= PARENT2
;
182 insert_by_date(rev1
, &list
);
183 insert_by_date(rev2
, &list
);
185 while (interesting(list
)) {
186 struct commit
*commit
= list
->item
;
187 struct commit_list
*parents
;
188 int flags
= commit
->object
.flags
189 & (PARENT1
| PARENT2
| UNINTERESTING
);
194 if (flags
== (PARENT1
| PARENT2
)) {
195 insert_by_date(commit
, &result
);
197 /* Mark parents of a found merge uninteresting */
198 flags
|= UNINTERESTING
;
200 parents
= commit
->parents
;
202 struct commit
*p
= parents
->item
;
203 parents
= parents
->next
;
204 if ((p
->object
.flags
& flags
) == flags
)
207 p
->object
.flags
|= flags
;
208 insert_by_date(p
, &list
);
215 if (result
->next
&& list
)
216 mark_reachable_commits(result
, list
);
218 /* cull duplicates */
219 for (tmp
= result
, list
= NULL
; tmp
; ) {
220 struct commit
*commit
= tmp
->item
;
221 struct commit_list
*next
= tmp
->next
;
222 if (commit
->object
.flags
& UNINTERESTING
) {
230 commit
->object
.flags
|= UNINTERESTING
;
237 clear_commit_marks(rev1
, PARENT1
| PARENT2
| UNINTERESTING
);
238 clear_commit_marks(rev2
, PARENT1
| PARENT2
| UNINTERESTING
);
243 static int show_all
= 0;
245 static int merge_base(struct commit
*rev1
, struct commit
*rev2
)
247 struct commit_list
*result
= get_merge_bases(rev1
, rev2
);
253 printf("%s\n", sha1_to_hex(result
->item
->object
.sha1
));
256 result
= result
->next
;
262 static const char merge_base_usage
[] =
263 "git-merge-base [--all] <commit-id> <commit-id>";
265 int main(int argc
, char **argv
)
267 struct commit
*rev1
, *rev2
;
268 unsigned char rev1key
[20], rev2key
[20];
270 setup_git_directory();
271 git_config(git_default_config
);
273 while (1 < argc
&& argv
[1][0] == '-') {
275 if (!strcmp(arg
, "-a") || !strcmp(arg
, "--all"))
278 usage(merge_base_usage
);
282 usage(merge_base_usage
);
283 if (get_sha1(argv
[1], rev1key
))
284 die("Not a valid object name %s", argv
[1]);
285 if (get_sha1(argv
[2], rev2key
))
286 die("Not a valid object name %s", argv
[2]);
287 rev1
= lookup_commit_reference(rev1key
);
288 rev2
= lookup_commit_reference(rev2key
);
291 return merge_base(rev1
, rev2
);