|
| 1 | +#include <stdlib.h> |
1 | 2 | #include "cache.h"
|
2 |
| -#include "revision.h" |
| 3 | +#include "commit.h" |
3 | 4 |
|
4 |
| -/* |
5 |
| - * This is stupid. We could have much better heurstics, I bet. |
6 |
| - */ |
7 |
| -static int better(struct revision *new, struct revision *old) |
| 5 | +static struct commit *process_list(struct commit_list **list_p, int this_mark, |
| 6 | + int other_mark) |
8 | 7 | {
|
9 |
| - return new->date > old->date; |
| 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) { |
| 14 | + /* |
| 15 | + printf("%d already seen %s %x\n", |
| 16 | + this_mark |
| 17 | + sha1_to_hex(posn->parent->sha1), |
| 18 | + posn->parent->flags); |
| 19 | + */ |
| 20 | + /* do nothing; this indicates that this side |
| 21 | + * split and reformed, and we only need to |
| 22 | + * mark it once. |
| 23 | + */ |
| 24 | + } else if (posn->item->object.flags & other_mark) { |
| 25 | + return posn->item; |
| 26 | + } else { |
| 27 | + /* |
| 28 | + printf("%d based on %s\n", |
| 29 | + this_mark, |
| 30 | + sha1_to_hex(posn->parent->sha1)); |
| 31 | + */ |
| 32 | + posn->item->object.flags |= this_mark; |
| 33 | + |
| 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; |
| 41 | + } |
| 42 | + } |
| 43 | + posn = posn->next; |
| 44 | + } |
| 45 | + return NULL; |
10 | 46 | }
|
11 | 47 |
|
12 |
| -static struct revision *common_parent(struct revision *rev1, struct revision *rev2) |
| 48 | +struct commit *common_ancestor(struct commit *rev1, struct commit *rev2) |
13 | 49 | {
|
14 |
| - int i; |
15 |
| - struct revision *best = NULL; |
| 50 | + struct commit_list *rev1list = malloc(sizeof(struct commit_list)); |
| 51 | + struct commit_list *rev2list = malloc(sizeof(struct commit_list)); |
| 52 | + |
| 53 | + rev1list->item = rev1; |
| 54 | + rev1list->next = NULL; |
16 | 55 |
|
17 |
| - mark_reachable(rev1, 1); |
18 |
| - mark_reachable(rev2, 2); |
19 |
| - for (i = 0; i < nr_revs ;i++) { |
20 |
| - struct revision *rev = revs[i]; |
21 |
| - if ((rev->flags & 3) != 3) |
22 |
| - continue; |
23 |
| - if (!best) { |
24 |
| - best = rev; |
25 |
| - continue; |
| 56 | + rev2list->item = rev2; |
| 57 | + rev2list->next = NULL; |
| 58 | + |
| 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; |
| 65 | + } |
| 66 | + ret = process_list(&rev2list, 0x2, 0x1); |
| 67 | + if (ret) { |
| 68 | + /* XXXX free lists */ |
| 69 | + return ret; |
26 | 70 | }
|
27 |
| - if (better(rev, best)) |
28 |
| - best = rev; |
29 | 71 | }
|
30 |
| - return best; |
| 72 | + return NULL; |
31 | 73 | }
|
32 | 74 |
|
33 | 75 | int main(int argc, char **argv)
|
34 | 76 | {
|
35 |
| - unsigned char rev1[20], rev2[20]; |
36 |
| - struct revision *common; |
| 77 | + struct commit *rev1, *rev2, *ret; |
| 78 | + unsigned char rev1key[20], rev2key[20]; |
37 | 79 |
|
38 |
| - if (argc != 3 || get_sha1_hex(argv[1], rev1) || get_sha1_hex(argv[2], rev2)) |
39 |
| - usage("merge-base <commit1> <commit2>"); |
40 |
| - |
41 |
| - /* |
42 |
| - * We will eventually want to include a revision cache file |
43 |
| - * that "rev-tree.c" has generated, since this is going to |
44 |
| - * otherwise be quite expensive for big trees.. |
45 |
| - * |
46 |
| - * That's some time off, though, and in the meantime we know |
47 |
| - * that we have a solution to the eventual expense. |
48 |
| - */ |
49 |
| - parse_commit(rev1); |
50 |
| - parse_commit(rev2); |
51 |
| - |
52 |
| - common = common_parent(lookup_rev(rev1), lookup_rev(rev2)); |
53 |
| - if (!common) |
54 |
| - die("no common parent found"); |
55 |
| - printf("%s\n", sha1_to_hex(common->sha1)); |
56 |
| - return 0; |
| 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>"); |
| 84 | + } |
| 85 | + rev1 = lookup_commit(rev1key); |
| 86 | + rev2 = lookup_commit(rev2key); |
| 87 | + ret = common_ancestor(rev1, rev2); |
| 88 | + if (ret) { |
| 89 | + printf("%s\n", sha1_to_hex(ret->object.sha1)); |
| 90 | + return 0; |
| 91 | + } else { |
| 92 | + return 1; |
| 93 | + } |
| 94 | + |
57 | 95 | }
|
0 commit comments