builtin-merge-base.con commit hash-object: cleanup handling of command line options (8a2f5e5)
   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
  25int cmd_merge_base(int argc, const char **argv, const char *prefix)
  26{
  27        struct commit *rev1, *rev2;
  28        unsigned char rev1key[20], rev2key[20];
  29        int show_all = 0;
  30
  31        git_config(git_default_config);
  32
  33        while (1 < argc && argv[1][0] == '-') {
  34                const char *arg = argv[1];
  35                if (!strcmp(arg, "-a") || !strcmp(arg, "--all"))
  36                        show_all = 1;
  37                else
  38                        usage(merge_base_usage);
  39                argc--; argv++;
  40        }
  41        if (argc != 3)
  42                usage(merge_base_usage);
  43        if (get_sha1(argv[1], rev1key))
  44                die("Not a valid object name %s", argv[1]);
  45        if (get_sha1(argv[2], rev2key))
  46                die("Not a valid object name %s", argv[2]);
  47        rev1 = lookup_commit_reference(rev1key);
  48        rev2 = lookup_commit_reference(rev2key);
  49        if (!rev1 || !rev2)
  50                return 1;
  51        return show_merge_base(rev1, rev2, show_all);
  52}