2007-07-14 23:14:45 +00:00
|
|
|
#include "builtin.h"
|
2005-04-17 19:18:17 +00:00
|
|
|
#include "cache.h"
|
2005-04-18 18:39:48 +00:00
|
|
|
#include "commit.h"
|
2005-04-17 19:18:17 +00:00
|
|
|
|
2007-01-09 08:50:02 +00:00
|
|
|
static int show_merge_base(struct commit *rev1, struct commit *rev2, int show_all)
|
2006-06-29 13:16:46 +00:00
|
|
|
{
|
2006-07-02 09:49:38 +00:00
|
|
|
struct commit_list *result = get_merge_bases(rev1, rev2, 0);
|
2006-06-29 13:16:46 +00:00
|
|
|
|
|
|
|
if (!result)
|
|
|
|
return 1;
|
|
|
|
|
2005-08-24 04:08:59 +00:00
|
|
|
while (result) {
|
2006-06-29 13:16:46 +00:00
|
|
|
printf("%s\n", sha1_to_hex(result->item->object.sha1));
|
2005-08-24 04:08:59 +00:00
|
|
|
if (!show_all)
|
|
|
|
return 0;
|
2006-06-29 13:16:46 +00:00
|
|
|
result = result->next;
|
2005-08-24 04:08:59 +00:00
|
|
|
}
|
2006-06-29 13:16:46 +00:00
|
|
|
|
2005-08-24 04:08:59 +00:00
|
|
|
return 0;
|
2005-04-17 19:18:17 +00:00
|
|
|
}
|
|
|
|
|
2005-08-24 04:08:59 +00:00
|
|
|
static const char merge_base_usage[] =
|
2008-07-13 13:36:15 +00:00
|
|
|
"git merge-base [--all] <commit-id> <commit-id>";
|
2005-08-24 04:08:59 +00:00
|
|
|
|
2008-07-29 05:42:53 +00:00
|
|
|
static struct commit *get_commit_reference(const char *arg)
|
|
|
|
{
|
|
|
|
unsigned char revkey[20];
|
|
|
|
struct commit *r;
|
|
|
|
|
|
|
|
if (get_sha1(arg, revkey))
|
|
|
|
die("Not a valid object name %s", arg);
|
|
|
|
r = lookup_commit_reference(revkey);
|
|
|
|
if (!r)
|
|
|
|
die("Not a valid commit name %s", arg);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2007-01-09 08:50:02 +00:00
|
|
|
int cmd_merge_base(int argc, const char **argv, const char *prefix)
|
2005-04-17 19:18:17 +00:00
|
|
|
{
|
2005-08-24 04:08:59 +00:00
|
|
|
struct commit *rev1, *rev2;
|
2007-01-09 08:50:02 +00:00
|
|
|
int show_all = 0;
|
2005-04-17 19:18:17 +00:00
|
|
|
|
2008-05-14 17:46:53 +00:00
|
|
|
git_config(git_default_config, NULL);
|
2005-11-26 08:50:02 +00:00
|
|
|
|
2005-08-24 04:08:59 +00:00
|
|
|
while (1 < argc && argv[1][0] == '-') {
|
2007-01-09 08:50:02 +00:00
|
|
|
const char *arg = argv[1];
|
2005-08-24 04:08:59 +00:00
|
|
|
if (!strcmp(arg, "-a") || !strcmp(arg, "--all"))
|
|
|
|
show_all = 1;
|
|
|
|
else
|
|
|
|
usage(merge_base_usage);
|
|
|
|
argc--; argv++;
|
|
|
|
}
|
2006-05-08 21:43:38 +00:00
|
|
|
if (argc != 3)
|
2005-08-24 04:08:59 +00:00
|
|
|
usage(merge_base_usage);
|
2008-07-29 05:42:53 +00:00
|
|
|
rev1 = get_commit_reference(argv[1]);
|
|
|
|
rev2 = get_commit_reference(argv[2]);
|
|
|
|
|
2007-01-09 08:50:02 +00:00
|
|
|
return show_merge_base(rev1, rev2, show_all);
|
2005-04-17 19:18:17 +00:00
|
|
|
}
|