builtin / merge-base.con commit fetch: handle overlaping refspecs on --prune (e6f6371)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "commit.h"
   4#include "parse-options.h"
   5
   6static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
   7{
   8        struct commit_list *result;
   9
  10        result = get_merge_bases_many(rev[0], rev_nr - 1, rev + 1, 0);
  11
  12        if (!result)
  13                return 1;
  14
  15        while (result) {
  16                printf("%s\n", sha1_to_hex(result->item->object.sha1));
  17                if (!show_all)
  18                        return 0;
  19                result = result->next;
  20        }
  21
  22        return 0;
  23}
  24
  25static const char * const merge_base_usage[] = {
  26        N_("git merge-base [-a|--all] <commit> <commit>..."),
  27        N_("git merge-base [-a|--all] --octopus <commit>..."),
  28        N_("git merge-base --independent <commit>..."),
  29        N_("git merge-base --is-ancestor <commit> <commit>"),
  30        NULL
  31};
  32
  33static struct commit *get_commit_reference(const char *arg)
  34{
  35        unsigned char revkey[20];
  36        struct commit *r;
  37
  38        if (get_sha1(arg, revkey))
  39                die("Not a valid object name %s", arg);
  40        r = lookup_commit_reference(revkey);
  41        if (!r)
  42                die("Not a valid commit name %s", arg);
  43
  44        return r;
  45}
  46
  47static int handle_independent(int count, const char **args)
  48{
  49        struct commit_list *revs = NULL;
  50        struct commit_list *result;
  51        int i;
  52
  53        for (i = count - 1; i >= 0; i--)
  54                commit_list_insert(get_commit_reference(args[i]), &revs);
  55
  56        result = reduce_heads(revs);
  57        if (!result)
  58                return 1;
  59
  60        while (result) {
  61                printf("%s\n", sha1_to_hex(result->item->object.sha1));
  62                result = result->next;
  63        }
  64        return 0;
  65}
  66
  67static int handle_octopus(int count, const char **args, int show_all)
  68{
  69        struct commit_list *revs = NULL;
  70        struct commit_list *result;
  71        int i;
  72
  73        for (i = count - 1; i >= 0; i--)
  74                commit_list_insert(get_commit_reference(args[i]), &revs);
  75
  76        result = reduce_heads(get_octopus_merge_bases(revs));
  77
  78        if (!result)
  79                return 1;
  80
  81        while (result) {
  82                printf("%s\n", sha1_to_hex(result->item->object.sha1));
  83                if (!show_all)
  84                        return 0;
  85                result = result->next;
  86        }
  87
  88        return 0;
  89}
  90
  91static int handle_is_ancestor(int argc, const char **argv)
  92{
  93        struct commit *one, *two;
  94
  95        if (argc != 2)
  96                die("--is-ancestor takes exactly two commits");
  97        one = get_commit_reference(argv[0]);
  98        two = get_commit_reference(argv[1]);
  99        if (in_merge_bases(one, two))
 100                return 0;
 101        else
 102                return 1;
 103}
 104
 105int cmd_merge_base(int argc, const char **argv, const char *prefix)
 106{
 107        struct commit **rev;
 108        int rev_nr = 0;
 109        int show_all = 0;
 110        int octopus = 0;
 111        int reduce = 0;
 112        int is_ancestor = 0;
 113
 114        struct option options[] = {
 115                OPT_BOOL('a', "all", &show_all, N_("output all common ancestors")),
 116                OPT_BOOL(0, "octopus", &octopus, N_("find ancestors for a single n-way merge")),
 117                OPT_BOOL(0, "independent", &reduce, N_("list revs not reachable from others")),
 118                OPT_BOOL(0, "is-ancestor", &is_ancestor,
 119                         N_("is the first one ancestor of the other?")),
 120                OPT_END()
 121        };
 122
 123        git_config(git_default_config, NULL);
 124        argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0);
 125        if (!octopus && !reduce && argc < 2)
 126                usage_with_options(merge_base_usage, options);
 127        if (is_ancestor && (show_all || octopus || reduce))
 128                die("--is-ancestor cannot be used with other options");
 129        if (is_ancestor)
 130                return handle_is_ancestor(argc, argv);
 131        if (reduce && (show_all || octopus))
 132                die("--independent cannot be used with other options");
 133
 134        if (octopus)
 135                return handle_octopus(argc, argv, show_all);
 136        else if (reduce)
 137                return handle_independent(argc, argv);
 138
 139        rev = xmalloc(argc * sizeof(*rev));
 140        while (argc-- > 0)
 141                rev[rev_nr++] = get_commit_reference(*argv++);
 142        return show_merge_base(rev, rev_nr, show_all);
 143}