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