Code

merge-recursive.c: Add more generic merge_recursive_generic()
[git.git] / builtin-merge-recursive.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "merge-recursive.h"
6 static const char *better_branch_name(const char *branch)
7 {
8         static char githead_env[8 + 40 + 1];
9         char *name;
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 }
18 int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
19 {
20         const char *bases[21];
21         unsigned bases_count = 0;
22         int i, failed;
23         const char *branch1, *branch2;
24         unsigned char h1[20], h2[20];
25         int subtree_merge = 0;
27         if (argv[0]) {
28                 int namelen = strlen(argv[0]);
29                 if (8 < namelen &&
30                     !strcmp(argv[0] + namelen - 8, "-subtree"))
31                         subtree_merge = 1;
32         }
34         git_config(merge_recursive_config, NULL);
35         merge_recursive_setup(subtree_merge);
36         if (argc < 4)
37                 die("Usage: %s <base>... -- <head> <remote> ...\n", argv[0]);
39         for (i = 1; i < argc; ++i) {
40                 if (!strcmp(argv[i], "--")) {
41                         bases[bases_count] = NULL;
42                         break;
43                 }
44                 if (bases_count < ARRAY_SIZE(bases)-1)
45                         bases[bases_count++] = argv[i];
46                 else
47                         warning("Cannot handle more than %zu bases. "
48                                 "Ignoring %s.", ARRAY_SIZE(bases)-1, argv[i]);
49         }
50         if (argc - i != 3) /* "--" "<head>" "<remote>" */
51                 die("Not handling anything other than two heads merge.");
53         branch1 = argv[++i];
54         branch2 = argv[++i];
56         if (get_sha1(branch1, h1))
57                 die("Could not resolve ref '%s'", branch1);
58         if (get_sha1(branch2, h2))
59                 die("Could not resolve ref '%s'", branch2);
61         branch1 = better_branch_name(branch1);
62         branch2 = better_branch_name(branch2);
64         if (merge_recursive_verbosity >= 3)
65                 printf("Merging %s with %s\n", branch1, branch2);
67         failed = merge_recursive_generic(bases, h1, branch1, h2, branch2);
68         if (failed < 0)
69                 return 128; /* die() error code */
70         return failed;
71 }