]> git.ipfire.org Git - thirdparty/git.git/blame - merge-base.c
[PATCH] Making it easier to find which change introduced a bug
[thirdparty/git.git] / merge-base.c
CommitLineData
b5039db6 1#include <stdlib.h>
6683463e 2#include "cache.h"
b5039db6 3#include "commit.h"
6683463e 4
b5039db6
DB
5static struct commit *process_list(struct commit_list **list_p, int this_mark,
6 int other_mark)
6683463e 7{
b6b15db3 8 struct commit *item = (*list_p)->item;
58e28af6
DB
9
10 if (item->object.flags & other_mark) {
b6b15db3
DB
11 return item;
12 } else {
58e28af6 13 pop_most_recent_commit(list_p, this_mark);
b5039db6
DB
14 }
15 return NULL;
6683463e
LT
16}
17
e99d59ff 18static struct commit *common_ancestor(struct commit *rev1, struct commit *rev2)
6683463e 19{
b6b15db3
DB
20 struct commit_list *rev1list = NULL;
21 struct commit_list *rev2list = NULL;
b5039db6 22
b6b15db3 23 commit_list_insert(rev1, &rev1list);
58e28af6 24 rev1->object.flags |= 0x1;
b6b15db3 25 commit_list_insert(rev2, &rev2list);
58e28af6 26 rev2->object.flags |= 0x2;
6683463e 27
b6b15db3
DB
28 parse_commit(rev1);
29 parse_commit(rev2);
6683463e 30
b5039db6
DB
31 while (rev1list || rev2list) {
32 struct commit *ret;
b6b15db3
DB
33 if (!rev1list) {
34 // process 2
35 ret = process_list(&rev2list, 0x2, 0x1);
36 } else if (!rev2list) {
37 // process 1
38 ret = process_list(&rev1list, 0x1, 0x2);
39 } else if (rev1list->item->date < rev2list->item->date) {
40 // process 2
41 ret = process_list(&rev2list, 0x2, 0x1);
42 } else {
43 // process 1
44 ret = process_list(&rev1list, 0x1, 0x2);
b5039db6 45 }
b5039db6 46 if (ret) {
b6b15db3
DB
47 free_commit_list(rev1list);
48 free_commit_list(rev2list);
b5039db6 49 return ret;
6683463e 50 }
6683463e 51 }
b5039db6 52 return NULL;
6683463e
LT
53}
54
55int main(int argc, char **argv)
56{
b5039db6
DB
57 struct commit *rev1, *rev2, *ret;
58 unsigned char rev1key[20], rev2key[20];
6683463e 59
b5039db6 60 if (argc != 3 ||
3c249c95
LT
61 get_sha1(argv[1], rev1key) ||
62 get_sha1(argv[2], rev2key)) {
667bb59b 63 usage("git-merge-base <commit-id> <commit-id>");
b5039db6 64 }
9b632be3
LT
65 rev1 = lookup_commit_reference(rev1key);
66 rev2 = lookup_commit_reference(rev2key);
b5039db6 67 ret = common_ancestor(rev1, rev2);
b51ad431 68 if (!ret)
b5039db6 69 return 1;
b51ad431 70 printf("%s\n", sha1_to_hex(ret->object.sha1));
6683463e
LT
71 return 0;
72}