2005-04-18 20:39:48 +02:00
|
|
|
#include <stdlib.h>
|
2005-04-17 21:18:17 +02:00
|
|
|
#include "cache.h"
|
2005-04-18 20:39:48 +02:00
|
|
|
#include "commit.h"
|
2005-04-17 21:18:17 +02:00
|
|
|
|
2005-05-20 20:46:10 +02:00
|
|
|
static struct commit *common_ancestor(struct commit *rev1, struct commit *rev2)
|
2005-04-17 21:18:17 +02:00
|
|
|
{
|
2005-07-31 00:10:20 +02:00
|
|
|
struct commit_list *list = NULL;
|
|
|
|
struct commit_list *result = NULL;
|
2005-04-18 20:39:48 +02:00
|
|
|
|
2005-07-31 00:10:20 +02:00
|
|
|
if (rev1 == rev2)
|
|
|
|
return rev1;
|
2005-04-17 21:18:17 +02:00
|
|
|
|
2005-04-24 03:47:23 +02:00
|
|
|
parse_commit(rev1);
|
|
|
|
parse_commit(rev2);
|
2005-04-17 21:18:17 +02:00
|
|
|
|
2005-07-31 00:10:20 +02:00
|
|
|
rev1->object.flags |= 1;
|
|
|
|
rev2->object.flags |= 2;
|
|
|
|
insert_by_date(rev1, &list);
|
|
|
|
insert_by_date(rev2, &list);
|
|
|
|
|
|
|
|
while (list) {
|
|
|
|
struct commit *commit = list->item;
|
|
|
|
struct commit_list *tmp = list, *parents;
|
|
|
|
int flags = commit->object.flags & 3;
|
|
|
|
|
|
|
|
list = list->next;
|
|
|
|
free(tmp);
|
|
|
|
switch (flags) {
|
|
|
|
case 3:
|
|
|
|
insert_by_date(commit, &result);
|
|
|
|
continue;
|
|
|
|
case 0:
|
|
|
|
die("git-merge-base: commit without either parent?");
|
2005-04-18 20:39:48 +02:00
|
|
|
}
|
2005-07-31 00:10:20 +02:00
|
|
|
parents = commit->parents;
|
|
|
|
while (parents) {
|
|
|
|
struct commit *p = parents->item;
|
|
|
|
parents = parents->next;
|
|
|
|
if ((p->object.flags & flags) == flags)
|
|
|
|
continue;
|
|
|
|
parse_commit(p);
|
|
|
|
p->object.flags |= flags;
|
|
|
|
insert_by_date(p, &list);
|
2005-04-17 21:18:17 +02:00
|
|
|
}
|
|
|
|
}
|
2005-07-31 00:10:20 +02:00
|
|
|
if (!result)
|
|
|
|
return NULL;
|
|
|
|
return result->item;
|
2005-04-17 21:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2005-04-18 20:39:48 +02:00
|
|
|
struct commit *rev1, *rev2, *ret;
|
|
|
|
unsigned char rev1key[20], rev2key[20];
|
2005-04-17 21:18:17 +02:00
|
|
|
|
2005-04-18 20:39:48 +02:00
|
|
|
if (argc != 3 ||
|
2005-05-02 01:36:56 +02:00
|
|
|
get_sha1(argv[1], rev1key) ||
|
|
|
|
get_sha1(argv[2], rev2key)) {
|
2005-05-19 13:17:16 +02:00
|
|
|
usage("git-merge-base <commit-id> <commit-id>");
|
2005-04-18 20:39:48 +02:00
|
|
|
}
|
2005-05-19 01:16:51 +02:00
|
|
|
rev1 = lookup_commit_reference(rev1key);
|
|
|
|
rev2 = lookup_commit_reference(rev2key);
|
2005-07-31 00:10:20 +02:00
|
|
|
if (!rev1 || !rev2)
|
|
|
|
return 1;
|
2005-04-18 20:39:48 +02:00
|
|
|
ret = common_ancestor(rev1, rev2);
|
2005-04-18 21:12:00 +02:00
|
|
|
if (!ret)
|
2005-04-18 20:39:48 +02:00
|
|
|
return 1;
|
2005-04-18 21:12:00 +02:00
|
|
|
printf("%s\n", sha1_to_hex(ret->object.sha1));
|
2005-04-17 21:18:17 +02:00
|
|
|
return 0;
|
|
|
|
}
|