Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1 | #include "cache.h" |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 2 | #include "commit.h" |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 3 | #include "tag.h" |
Daniel Barkalow | e1b3a2c | 2008-02-07 11:40:05 -0500 | [diff] [blame] | 4 | #include "merge-recursive.h" |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 5 | |
Shawn O. Pearce | 7ba3c07 | 2006-12-28 02:35:20 -0500 | [diff] [blame] | 6 | static 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 Barkalow | e1b3a2c | 2008-02-07 11:40:05 -0500 | [diff] [blame] | 18 | int cmd_merge_recursive(int argc, const char **argv, const char *prefix) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 19 | { |
Miklos Vajna | 8a2fce1 | 2008-08-25 16:25:57 +0200 | [diff] [blame] | 20 | const unsigned char *bases[21]; |
Stephan Beyer | 73118f8 | 2008-08-12 22:13:59 +0200 | [diff] [blame] | 21 | unsigned bases_count = 0; |
| 22 | int i, failed; |
Stephan Beyer | 73118f8 | 2008-08-12 22:13:59 +0200 | [diff] [blame] | 23 | unsigned char h1[20], h2[20]; |
Miklos Vajna | 8a2fce1 | 2008-08-25 16:25:57 +0200 | [diff] [blame] | 24 | struct merge_options o; |
| 25 | struct commit *result; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 26 | |
Miklos Vajna | 8a2fce1 | 2008-08-25 16:25:57 +0200 | [diff] [blame] | 27 | init_merge_options(&o); |
Junio C Hamano | 68faf68 | 2007-02-15 16:32:45 -0800 | [diff] [blame] | 28 | if (argv[0]) { |
| 29 | int namelen = strlen(argv[0]); |
| 30 | if (8 < namelen && |
| 31 | !strcmp(argv[0] + namelen - 8, "-subtree")) |
Miklos Vajna | 8a2fce1 | 2008-08-25 16:25:57 +0200 | [diff] [blame] | 32 | o.subtree_merge = 1; |
Junio C Hamano | 68faf68 | 2007-02-15 16:32:45 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 35 | if (argc < 4) |
Jonathan Nieder | 0874f46 | 2009-11-09 09:05:03 -0600 | [diff] [blame] | 36 | usagef("%s <base>... -- <head> <remote> ...", argv[0]); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 37 | |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 38 | for (i = 1; i < argc; ++i) { |
Miklos Vajna | 8a2fce1 | 2008-08-25 16:25:57 +0200 | [diff] [blame] | 39 | if (!strcmp(argv[i], "--")) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 40 | break; |
Miklos Vajna | 8a2fce1 | 2008-08-25 16:25:57 +0200 | [diff] [blame] | 41 | 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 Beyer | 73118f8 | 2008-08-12 22:13:59 +0200 | [diff] [blame] | 46 | } |
Stephan Beyer | 73118f8 | 2008-08-12 22:13:59 +0200 | [diff] [blame] | 47 | else |
Johannes Schindelin | b74d779 | 2009-05-23 10:04:51 +0200 | [diff] [blame] | 48 | warning("Cannot handle more than %d bases. " |
| 49 | "Ignoring %s.", |
| 50 | (int)ARRAY_SIZE(bases)-1, argv[i]); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 51 | } |
| 52 | if (argc - i != 3) /* "--" "<head>" "<remote>" */ |
| 53 | die("Not handling anything other than two heads merge."); |
| 54 | |
Miklos Vajna | 8a2fce1 | 2008-08-25 16:25:57 +0200 | [diff] [blame] | 55 | o.branch1 = argv[++i]; |
| 56 | o.branch2 = argv[++i]; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 57 | |
Miklos Vajna | 8a2fce1 | 2008-08-25 16:25:57 +0200 | [diff] [blame] | 58 | 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 Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 62 | |
Miklos Vajna | 8a2fce1 | 2008-08-25 16:25:57 +0200 | [diff] [blame] | 63 | o.branch1 = better_branch_name(o.branch1); |
| 64 | o.branch2 = better_branch_name(o.branch2); |
Shawn O. Pearce | 3f6ee2d | 2007-01-14 00:28:58 -0500 | [diff] [blame] | 65 | |
Miklos Vajna | 8a2fce1 | 2008-08-25 16:25:57 +0200 | [diff] [blame] | 66 | if (o.verbosity >= 3) |
| 67 | printf("Merging %s with %s\n", o.branch1, o.branch2); |
Shawn O. Pearce | e0ec181 | 2006-12-23 03:44:47 -0500 | [diff] [blame] | 68 | |
Miklos Vajna | 8a2fce1 | 2008-08-25 16:25:57 +0200 | [diff] [blame] | 69 | failed = merge_recursive_generic(&o, h1, h2, bases_count, bases, &result); |
Stephan Beyer | 73118f8 | 2008-08-12 22:13:59 +0200 | [diff] [blame] | 70 | if (failed < 0) |
| 71 | return 128; /* die() error code */ |
| 72 | return failed; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 73 | } |