5 static struct commit
*process_list(struct commit_list
**list_p
, int this_mark
,
8 struct commit_list
*parent
, *temp
;
9 struct commit_list
*posn
= *list_p
;
12 parse_commit(posn
->item
);
13 if (posn
->item
->object
.flags
& this_mark
) {
15 printf("%d already seen %s %x\n",
17 sha1_to_hex(posn->parent->sha1),
20 /* do nothing; this indicates that this side
21 * split and reformed, and we only need to
24 } else if (posn
->item
->object
.flags
& other_mark
) {
28 printf("%d based on %s\n",
30 sha1_to_hex(posn->parent->sha1));
32 posn
->item
->object
.flags
|= this_mark
;
34 parent
= posn
->item
->parents
;
36 temp
= malloc(sizeof(struct commit_list
));
38 temp
->item
= parent
->item
;
40 parent
= parent
->next
;
48 struct commit
*common_ancestor(struct commit
*rev1
, struct commit
*rev2
)
50 struct commit_list
*rev1list
= malloc(sizeof(struct commit_list
));
51 struct commit_list
*rev2list
= malloc(sizeof(struct commit_list
));
53 rev1list
->item
= rev1
;
54 rev1list
->next
= NULL
;
56 rev2list
->item
= rev2
;
57 rev2list
->next
= NULL
;
59 while (rev1list
|| rev2list
) {
61 ret
= process_list(&rev1list
, 0x1, 0x2);
66 ret
= process_list(&rev2list
, 0x2, 0x1);
75 int main(int argc
, char **argv
)
77 struct commit
*rev1
, *rev2
, *ret
;
78 unsigned char rev1key
[20], rev2key
[20];
81 get_sha1_hex(argv
[1], rev1key
) ||
82 get_sha1_hex(argv
[2], rev2key
)) {
83 usage("merge-base <commit-id> <commit-id>");
85 rev1
= lookup_commit(rev1key
);
86 rev2
= lookup_commit(rev2key
);
87 ret
= common_ancestor(rev1
, rev2
);
90 printf("%s\n", sha1_to_hex(ret
->object
.sha1
));