[PATCH] Add some functions for commit lists
[git/mingw.git] / merge-base.c
blobac1153bc5646cb2d515ff206b759f4a79e90273a
1 #include <stdlib.h>
2 #include "cache.h"
3 #include "commit.h"
5 static struct commit *process_list(struct commit_list **list_p, int this_mark,
6 int other_mark)
8 struct commit_list *parent, *temp;
9 struct commit_list *posn = *list_p;
10 *list_p = NULL;
11 while (posn) {
12 parse_commit(posn->item);
13 if (posn->item->object.flags & this_mark) {
15 printf("%d already seen %s %x\n",
16 this_mark
17 sha1_to_hex(posn->parent->sha1),
18 posn->parent->flags);
20 /* do nothing; this indicates that this side
21 * split and reformed, and we only need to
22 * mark it once.
24 } else if (posn->item->object.flags & other_mark) {
25 return posn->item;
26 } else {
28 printf("%d based on %s\n",
29 this_mark,
30 sha1_to_hex(posn->parent->sha1));
32 posn->item->object.flags |= this_mark;
34 parent = posn->item->parents;
35 while (parent) {
36 temp = malloc(sizeof(struct commit_list));
37 temp->next = *list_p;
38 temp->item = parent->item;
39 *list_p = temp;
40 parent = parent->next;
43 posn = posn->next;
45 return NULL;
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) {
60 struct commit *ret;
61 ret = process_list(&rev1list, 0x1, 0x2);
62 if (ret) {
63 /* XXXX free lists */
64 return ret;
66 ret = process_list(&rev2list, 0x2, 0x1);
67 if (ret) {
68 /* XXXX free lists */
69 return ret;
72 return NULL;
75 int main(int argc, char **argv)
77 struct commit *rev1, *rev2, *ret;
78 unsigned char rev1key[20], rev2key[20];
80 if (argc != 3 ||
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);
88 if (!ret)
89 return 1;
90 printf("%s\n", sha1_to_hex(ret->object.sha1));
91 return 0;