builtin-merge-base.con commit Merge branch 'maint' (4a09bc9)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "commit.h"
   4
   5static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
   6{
   7        struct commit_list *result;
   8
   9        result = get_merge_bases_many(rev[0], rev_nr - 1, rev + 1, 0);
  10
  11        if (!result)
  12                return 1;
  13
  14        while (result) {
  15                printf("%s\n", sha1_to_hex(result->item->object.sha1));
  16                if (!show_all)
  17                        return 0;
  18                result = result->next;
  19        }
  20
  21        return 0;
  22}
  23
  24static const char merge_base_usage[] =
  25"git merge-base [--all] <commit-id> <commit-id>...";
  26
  27static struct commit *get_commit_reference(const char *arg)
  28{
  29        unsigned char revkey[20];
  30        struct commit *r;
  31
  32        if (get_sha1(arg, revkey))
  33                die("Not a valid object name %s", arg);
  34        r = lookup_commit_reference(revkey);
  35        if (!r)
  36                die("Not a valid commit name %s", arg);
  37
  38        return r;
  39}
  40
  41int cmd_merge_base(int argc, const char **argv, const char *prefix)
  42{
  43        struct commit **rev;
  44        int rev_nr = 0;
  45        int show_all = 0;
  46
  47        git_config(git_default_config, NULL);
  48
  49        while (1 < argc && argv[1][0] == '-') {
  50                const char *arg = argv[1];
  51                if (!strcmp(arg, "-a") || !strcmp(arg, "--all"))
  52                        show_all = 1;
  53                else
  54                        usage(merge_base_usage);
  55                argc--; argv++;
  56        }
  57        if (argc < 3)
  58                usage(merge_base_usage);
  59
  60        rev = xmalloc((argc - 1) * sizeof(*rev));
  61
  62        do {
  63                rev[rev_nr++] = get_commit_reference(argv[1]);
  64                argc--; argv++;
  65        } while (argc > 1);
  66
  67        return show_merge_base(rev, rev_nr, show_all);
  68}