5 static struct commit *process_list(struct commit_list **list_p, int this_mark,
8 struct commit *item = (*list_p)->item;
10 if (item->object.flags & this_mark) {
12 printf("%d already seen %s %x\n",
14 sha1_to_hex(posn->parent->sha1),
17 /* do nothing; this indicates that this side
18 * split and reformed, and we only need to
21 *list_p = (*list_p)->next;
22 } else if (item->object.flags & other_mark) {
26 printf("%d based on %s\n",
28 sha1_to_hex(posn->parent->sha1));
30 pop_most_recent_commit(list_p);
31 item->object.flags |= this_mark;
36 struct commit *common_ancestor(struct commit *rev1, struct commit *rev2)
38 struct commit_list *rev1list = NULL;
39 struct commit_list *rev2list = NULL;
41 commit_list_insert(rev1, &rev1list);
42 commit_list_insert(rev2, &rev2list);
47 while (rev1list || rev2list) {
51 ret = process_list(&rev2list, 0x2, 0x1);
52 } else if (!rev2list) {
54 ret = process_list(&rev1list, 0x1, 0x2);
55 } else if (rev1list->item->date < rev2list->item->date) {
57 ret = process_list(&rev2list, 0x2, 0x1);
60 ret = process_list(&rev1list, 0x1, 0x2);
63 free_commit_list(rev1list);
64 free_commit_list(rev2list);
71 int main(int argc, char **argv)
73 struct commit *rev1, *rev2, *ret;
74 unsigned char rev1key[20], rev2key[20];
77 get_sha1_hex(argv[1], rev1key) ||
78 get_sha1_hex(argv[2], rev2key)) {
79 usage("merge-base <commit-id> <commit-id>");
81 rev1 = lookup_commit(rev1key);
82 rev2 = lookup_commit(rev2key);
83 ret = common_ancestor(rev1, rev2);
86 printf("%s\n", sha1_to_hex(ret->object.sha1));