blob: 710674c6b2cccec4f1e0baa477a11d78cda72833 [file] [log] [blame]
Johannes Schindelin6d297f82006-07-08 18:42:41 +02001#include "cache.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"
Johannes Schindelin6d297f82006-07-08 18:42:41 +02005
Shawn O. Pearce7ba3c072006-12-28 02:35:20 -05006static 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
Daniel Barkalowe1b3a2c2008-02-07 11:40:05 -050018int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
Johannes Schindelin6d297f82006-07-08 18:42:41 +020019{
Miklos Vajna8a2fce12008-08-25 16:25:57 +020020 const unsigned char *bases[21];
Stephan Beyer73118f82008-08-12 22:13:59 +020021 unsigned bases_count = 0;
22 int i, failed;
Stephan Beyer73118f82008-08-12 22:13:59 +020023 unsigned char h1[20], h2[20];
Miklos Vajna8a2fce12008-08-25 16:25:57 +020024 struct merge_options o;
25 struct commit *result;
Johannes Schindelin6d297f82006-07-08 18:42:41 +020026
Miklos Vajna8a2fce12008-08-25 16:25:57 +020027 init_merge_options(&o);
Junio C Hamano68faf682007-02-15 16:32:45 -080028 if (argv[0]) {
29 int namelen = strlen(argv[0]);
30 if (8 < namelen &&
31 !strcmp(argv[0] + namelen - 8, "-subtree"))
Miklos Vajna8a2fce12008-08-25 16:25:57 +020032 o.subtree_merge = 1;
Junio C Hamano68faf682007-02-15 16:32:45 -080033 }
34
Johannes Schindelin6d297f82006-07-08 18:42:41 +020035 if (argc < 4)
Jonathan Nieder0874f462009-11-09 09:05:03 -060036 usagef("%s <base>... -- <head> <remote> ...", argv[0]);
Johannes Schindelin6d297f82006-07-08 18:42:41 +020037
Johannes Schindelin6d297f82006-07-08 18:42:41 +020038 for (i = 1; i < argc; ++i) {
Miklos Vajna8a2fce12008-08-25 16:25:57 +020039 if (!strcmp(argv[i], "--"))
Johannes Schindelin6d297f82006-07-08 18:42:41 +020040 break;
Miklos Vajna8a2fce12008-08-25 16:25:57 +020041 if (bases_count < ARRAY_SIZE(bases)-1) {
42 unsigned char *sha = xmalloc(20);
43 if (get_sha1(argv[i], sha))
44 die("Could not parse object '%s'", argv[i]);
45 bases[bases_count++] = sha;
Stephan Beyer73118f82008-08-12 22:13:59 +020046 }
Stephan Beyer73118f82008-08-12 22:13:59 +020047 else
Johannes Schindelinb74d7792009-05-23 10:04:51 +020048 warning("Cannot handle more than %d bases. "
49 "Ignoring %s.",
50 (int)ARRAY_SIZE(bases)-1, argv[i]);
Johannes Schindelin6d297f82006-07-08 18:42:41 +020051 }
52 if (argc - i != 3) /* "--" "<head>" "<remote>" */
53 die("Not handling anything other than two heads merge.");
54
Miklos Vajna8a2fce12008-08-25 16:25:57 +020055 o.branch1 = argv[++i];
56 o.branch2 = argv[++i];
Johannes Schindelin6d297f82006-07-08 18:42:41 +020057
Miklos Vajna8a2fce12008-08-25 16:25:57 +020058 if (get_sha1(o.branch1, h1))
59 die("Could not resolve ref '%s'", o.branch1);
60 if (get_sha1(o.branch2, h2))
61 die("Could not resolve ref '%s'", o.branch2);
Johannes Schindelin6d297f82006-07-08 18:42:41 +020062
Miklos Vajna8a2fce12008-08-25 16:25:57 +020063 o.branch1 = better_branch_name(o.branch1);
64 o.branch2 = better_branch_name(o.branch2);
Shawn O. Pearce3f6ee2d2007-01-14 00:28:58 -050065
Miklos Vajna8a2fce12008-08-25 16:25:57 +020066 if (o.verbosity >= 3)
67 printf("Merging %s with %s\n", o.branch1, o.branch2);
Shawn O. Pearcee0ec1812006-12-23 03:44:47 -050068
Miklos Vajna8a2fce12008-08-25 16:25:57 +020069 failed = merge_recursive_generic(&o, h1, h2, bases_count, bases, &result);
Stephan Beyer73118f82008-08-12 22:13:59 +020070 if (failed < 0)
71 return 128; /* die() error code */
72 return failed;
Johannes Schindelin6d297f82006-07-08 18:42:41 +020073}