5a52f3d0a23d12ed64fd49134cf94c8b0045eeb2
   1#include "cache.h"
   2#include "commit.h"
   3#include "tag.h"
   4#include "merge-recursive.h"
   5
   6static const char *better_branch_name(const char *branch)
   7{
   8        static char githead_env[8 + 40 + 1];
   9        char *name;
  10
  11        if (strlen(branch) != 40)
  12                return branch;
  13        sprintf(githead_env, "GITHEAD_%s", branch);
  14        name = getenv(githead_env);
  15        return name ? name : branch;
  16}
  17
  18int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
  19{
  20        const unsigned char *bases[21];
  21        unsigned bases_count = 0;
  22        int i, failed;
  23        unsigned char h1[20], h2[20];
  24        struct merge_options o;
  25        struct commit *result;
  26
  27        init_merge_options(&o);
  28        if (argv[0] && !suffixcmp(argv[0], "-subtree"))
  29                o.subtree_shift = "";
  30
  31        if (argc < 4)
  32                usagef("%s <base>... -- <head> <remote> ...", argv[0]);
  33
  34        for (i = 1; i < argc; ++i) {
  35                const char *arg = argv[i];
  36
  37                if (!prefixcmp(arg, "--")) {
  38                        if (!arg[2])
  39                                break;
  40                        if (parse_merge_opt(&o, arg + 2))
  41                                die("Unknown option %s", arg);
  42                        continue;
  43                }
  44                if (bases_count < ARRAY_SIZE(bases)-1) {
  45                        unsigned char *sha = xmalloc(20);
  46                        if (get_sha1(argv[i], sha))
  47                                die("Could not parse object '%s'", argv[i]);
  48                        bases[bases_count++] = sha;
  49                }
  50                else
  51                        warning("Cannot handle more than %d bases. "
  52                                "Ignoring %s.",
  53                                (int)ARRAY_SIZE(bases)-1, argv[i]);
  54        }
  55        if (argc - i != 3) /* "--" "<head>" "<remote>" */
  56                die("Not handling anything other than two heads merge.");
  57
  58        o.branch1 = argv[++i];
  59        o.branch2 = argv[++i];
  60
  61        if (get_sha1(o.branch1, h1))
  62                die("Could not resolve ref '%s'", o.branch1);
  63        if (get_sha1(o.branch2, h2))
  64                die("Could not resolve ref '%s'", o.branch2);
  65
  66        o.branch1 = better_branch_name(o.branch1);
  67        o.branch2 = better_branch_name(o.branch2);
  68
  69        if (o.verbosity >= 3)
  70                printf("Merging %s with %s\n", o.branch1, o.branch2);
  71
  72        failed = merge_recursive_generic(&o, h1, h2, bases_count, bases, &result);
  73        if (failed < 0)
  74                return 128; /* die() error code */
  75        return failed;
  76}