2007-07-15 03:14:45 +04:00
|
|
|
#include "builtin.h"
|
2005-04-17 23:18:17 +04:00
|
|
|
#include "cache.h"
|
2005-04-18 22:39:48 +04:00
|
|
|
#include "commit.h"
|
2008-10-02 16:59:19 +04:00
|
|
|
#include "parse-options.h"
|
2005-04-17 23:18:17 +04:00
|
|
|
|
2008-07-30 09:04:14 +04:00
|
|
|
static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
|
2006-06-29 17:16:46 +04:00
|
|
|
{
|
2008-07-30 09:04:14 +04:00
|
|
|
struct commit_list *result;
|
|
|
|
|
|
|
|
result = get_merge_bases_many(rev[0], rev_nr - 1, rev + 1, 0);
|
2006-06-29 17:16:46 +04:00
|
|
|
|
|
|
|
if (!result)
|
|
|
|
return 1;
|
|
|
|
|
2005-08-24 08:08:59 +04:00
|
|
|
while (result) {
|
2006-06-29 17:16:46 +04:00
|
|
|
printf("%s\n", sha1_to_hex(result->item->object.sha1));
|
2005-08-24 08:08:59 +04:00
|
|
|
if (!show_all)
|
|
|
|
return 0;
|
2006-06-29 17:16:46 +04:00
|
|
|
result = result->next;
|
2005-08-24 08:08:59 +04:00
|
|
|
}
|
2006-06-29 17:16:46 +04:00
|
|
|
|
2005-08-24 08:08:59 +04:00
|
|
|
return 0;
|
2005-04-17 23:18:17 +04:00
|
|
|
}
|
|
|
|
|
2008-10-02 16:59:19 +04:00
|
|
|
static const char * const merge_base_usage[] = {
|
2011-04-15 12:34:03 +04:00
|
|
|
"git merge-base [-a|--all] <commit> <commit>...",
|
|
|
|
"git merge-base [-a|--all] --octopus <commit>...",
|
2010-08-17 11:01:54 +04:00
|
|
|
"git merge-base --independent <commit>...",
|
2008-10-02 16:59:19 +04:00
|
|
|
NULL
|
|
|
|
};
|
2005-08-24 08:08:59 +04:00
|
|
|
|
2008-07-29 09:42:53 +04: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;
|
|
|
|
}
|
|
|
|
|
2010-08-17 11:01:54 +04:00
|
|
|
static int handle_octopus(int count, const char **args, int reduce, int show_all)
|
2010-08-17 11:01:15 +04:00
|
|
|
{
|
|
|
|
struct commit_list *revs = NULL;
|
|
|
|
struct commit_list *result;
|
|
|
|
int i;
|
|
|
|
|
2010-08-17 11:01:54 +04:00
|
|
|
if (reduce)
|
|
|
|
show_all = 1;
|
|
|
|
|
|
|
|
for (i = count - 1; i >= 0; i--)
|
2010-08-17 11:01:15 +04:00
|
|
|
commit_list_insert(get_commit_reference(args[i]), &revs);
|
2010-08-17 11:01:54 +04:00
|
|
|
|
|
|
|
result = reduce ? reduce_heads(revs) : get_octopus_merge_bases(revs);
|
2010-08-17 11:01:15 +04:00
|
|
|
|
|
|
|
if (!result)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
while (result) {
|
|
|
|
printf("%s\n", sha1_to_hex(result->item->object.sha1));
|
|
|
|
if (!show_all)
|
|
|
|
return 0;
|
|
|
|
result = result->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-01-09 11:50:02 +03:00
|
|
|
int cmd_merge_base(int argc, const char **argv, const char *prefix)
|
2005-04-17 23:18:17 +04:00
|
|
|
{
|
2008-07-30 09:04:14 +04:00
|
|
|
struct commit **rev;
|
|
|
|
int rev_nr = 0;
|
2007-01-09 11:50:02 +03:00
|
|
|
int show_all = 0;
|
2010-08-17 11:01:15 +04:00
|
|
|
int octopus = 0;
|
2010-08-17 11:01:54 +04:00
|
|
|
int reduce = 0;
|
2005-04-17 23:18:17 +04:00
|
|
|
|
2008-10-02 16:59:19 +04:00
|
|
|
struct option options[] = {
|
2010-08-17 11:01:15 +04:00
|
|
|
OPT_BOOLEAN('a', "all", &show_all, "output all common ancestors"),
|
|
|
|
OPT_BOOLEAN(0, "octopus", &octopus, "find ancestors for a single n-way merge"),
|
2010-08-17 11:01:54 +04:00
|
|
|
OPT_BOOLEAN(0, "independent", &reduce, "list revs not reachable from others"),
|
2008-10-02 16:59:19 +04:00
|
|
|
OPT_END()
|
|
|
|
};
|
2008-07-30 09:04:14 +04:00
|
|
|
|
2008-10-02 16:59:19 +04:00
|
|
|
git_config(git_default_config, NULL);
|
2009-05-23 22:53:12 +04:00
|
|
|
argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0);
|
2010-08-17 11:01:54 +04:00
|
|
|
if (!octopus && !reduce && argc < 2)
|
2008-10-02 16:59:19 +04:00
|
|
|
usage_with_options(merge_base_usage, options);
|
2010-08-17 11:01:54 +04:00
|
|
|
if (reduce && (show_all || octopus))
|
|
|
|
die("--independent cannot be used with other options");
|
2010-08-17 11:01:15 +04:00
|
|
|
|
2010-08-17 11:01:54 +04:00
|
|
|
if (octopus || reduce)
|
|
|
|
return handle_octopus(argc, argv, reduce, show_all);
|
2010-08-17 11:01:15 +04:00
|
|
|
|
2008-10-02 16:59:19 +04:00
|
|
|
rev = xmalloc(argc * sizeof(*rev));
|
|
|
|
while (argc-- > 0)
|
|
|
|
rev[rev_nr++] = get_commit_reference(*argv++);
|
2008-07-30 09:04:14 +04:00
|
|
|
return show_merge_base(rev, rev_nr, show_all);
|
2005-04-17 23:18:17 +04:00
|
|
|
}
|