blob: 3a64f5d0bdbcc8d7d21edb01f285cc8b41755228 [file] [log] [blame]
Stephen Boydc2e86ad2011-03-22 00:51:05 -07001#include "builtin.h"
Johannes Schindelin6d297f82006-07-08 18:42:41 +02002#include "commit.h"
Johannes Schindelin6d297f82006-07-08 18:42:41 +02003#include "tag.h"
Daniel Barkalowe1b3a2c2008-02-07 11:40:05 -05004#include "merge-recursive.h"
Justin Frankel58a1ece2010-08-26 00:50:45 -05005#include "xdiff-interface.h"
Johannes Schindelin6d297f82006-07-08 18:42:41 +02006
Thiago Farinae78d01b2010-08-30 00:30:22 -03007static const char builtin_merge_recursive_usage[] =
8 "git %s <base>... -- <head> <remote> ...";
9
Shawn O. Pearce7ba3c072006-12-28 02:35:20 -050010static const char *better_branch_name(const char *branch)
11{
12 static char githead_env[8 + 40 + 1];
13 char *name;
14
15 if (strlen(branch) != 40)
16 return branch;
17 sprintf(githead_env, "GITHEAD_%s", branch);
18 name = getenv(githead_env);
19 return name ? name : branch;
20}
21
Daniel Barkalowe1b3a2c2008-02-07 11:40:05 -050022int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
Johannes Schindelin6d297f82006-07-08 18:42:41 +020023{
Miklos Vajna8a2fce12008-08-25 16:25:57 +020024 const unsigned char *bases[21];
Stephan Beyer73118f82008-08-12 22:13:59 +020025 unsigned bases_count = 0;
26 int i, failed;
Stephan Beyer73118f82008-08-12 22:13:59 +020027 unsigned char h1[20], h2[20];
Miklos Vajna8a2fce12008-08-25 16:25:57 +020028 struct merge_options o;
29 struct commit *result;
Johannes Schindelin6d297f82006-07-08 18:42:41 +020030
Miklos Vajna8a2fce12008-08-25 16:25:57 +020031 init_merge_options(&o);
Junio C Hamano85e51b72008-06-30 22:18:57 -070032 if (argv[0] && !suffixcmp(argv[0], "-subtree"))
33 o.subtree_shift = "";
Junio C Hamano68faf682007-02-15 16:32:45 -080034
Johannes Schindelin6d297f82006-07-08 18:42:41 +020035 if (argc < 4)
Thiago Farinae78d01b2010-08-30 00:30:22 -030036 usagef(builtin_merge_recursive_usage, argv[0]);
Johannes Schindelin6d297f82006-07-08 18:42:41 +020037
Johannes Schindelin6d297f82006-07-08 18:42:41 +020038 for (i = 1; i < argc; ++i) {
Avery Pennarun8cc5b292009-11-25 21:23:55 -050039 const char *arg = argv[i];
40
41 if (!prefixcmp(arg, "--")) {
42 if (!arg[2])
43 break;
Jonathan Nieder635a7bb2010-08-26 00:47:58 -050044 if (parse_merge_opt(&o, arg + 2))
Avery Pennarun8cc5b292009-11-25 21:23:55 -050045 die("Unknown option %s", arg);
46 continue;
47 }
Miklos Vajna8a2fce12008-08-25 16:25:57 +020048 if (bases_count < ARRAY_SIZE(bases)-1) {
49 unsigned char *sha = xmalloc(20);
50 if (get_sha1(argv[i], sha))
51 die("Could not parse object '%s'", argv[i]);
52 bases[bases_count++] = sha;
Stephan Beyer73118f82008-08-12 22:13:59 +020053 }
Stephan Beyer73118f82008-08-12 22:13:59 +020054 else
Johannes Schindelinb74d7792009-05-23 10:04:51 +020055 warning("Cannot handle more than %d bases. "
56 "Ignoring %s.",
57 (int)ARRAY_SIZE(bases)-1, argv[i]);
Johannes Schindelin6d297f82006-07-08 18:42:41 +020058 }
59 if (argc - i != 3) /* "--" "<head>" "<remote>" */
60 die("Not handling anything other than two heads merge.");
61
Miklos Vajna8a2fce12008-08-25 16:25:57 +020062 o.branch1 = argv[++i];
63 o.branch2 = argv[++i];
Johannes Schindelin6d297f82006-07-08 18:42:41 +020064
Miklos Vajna8a2fce12008-08-25 16:25:57 +020065 if (get_sha1(o.branch1, h1))
66 die("Could not resolve ref '%s'", o.branch1);
67 if (get_sha1(o.branch2, h2))
68 die("Could not resolve ref '%s'", o.branch2);
Johannes Schindelin6d297f82006-07-08 18:42:41 +020069
Miklos Vajna8a2fce12008-08-25 16:25:57 +020070 o.branch1 = better_branch_name(o.branch1);
71 o.branch2 = better_branch_name(o.branch2);
Shawn O. Pearce3f6ee2d2007-01-14 00:28:58 -050072
Miklos Vajna8a2fce12008-08-25 16:25:57 +020073 if (o.verbosity >= 3)
74 printf("Merging %s with %s\n", o.branch1, o.branch2);
Shawn O. Pearcee0ec1812006-12-23 03:44:47 -050075
Miklos Vajna8a2fce12008-08-25 16:25:57 +020076 failed = merge_recursive_generic(&o, h1, h2, bases_count, bases, &result);
Stephan Beyer73118f82008-08-12 22:13:59 +020077 if (failed < 0)
78 return 128; /* die() error code */
79 return failed;
Johannes Schindelin6d297f82006-07-08 18:42:41 +020080}