]>
Commit | Line | Data |
---|---|---|
6d297f81 | 1 | #include "cache.h" |
6d297f81 | 2 | #include "commit.h" |
6d297f81 | 3 | #include "tag.h" |
e1b3a2ca | 4 | #include "merge-recursive.h" |
6d297f81 | 5 | |
7ba3c078 SP |
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 | ||
e1b3a2ca | 18 | int cmd_merge_recursive(int argc, const char **argv, const char *prefix) |
6d297f81 | 19 | { |
8a2fce18 | 20 | const unsigned char *bases[21]; |
73118f89 SB |
21 | unsigned bases_count = 0; |
22 | int i, failed; | |
73118f89 | 23 | unsigned char h1[20], h2[20]; |
8a2fce18 MV |
24 | struct merge_options o; |
25 | struct commit *result; | |
6d297f81 | 26 | |
8a2fce18 | 27 | init_merge_options(&o); |
85e51b78 JH |
28 | if (argv[0] && !suffixcmp(argv[0], "-subtree")) |
29 | o.subtree_shift = ""; | |
68faf689 | 30 | |
6d297f81 | 31 | if (argc < 4) |
0874f46e | 32 | usagef("%s <base>... -- <head> <remote> ...", argv[0]); |
6d297f81 | 33 | |
6d297f81 | 34 | for (i = 1; i < argc; ++i) { |
8cc5b290 AP |
35 | const char *arg = argv[i]; |
36 | ||
37 | if (!prefixcmp(arg, "--")) { | |
38 | if (!arg[2]) | |
39 | break; | |
40 | if (!strcmp(arg+2, "ours")) | |
41 | o.recursive_variant = MERGE_RECURSIVE_OURS; | |
42 | else if (!strcmp(arg+2, "theirs")) | |
43 | o.recursive_variant = MERGE_RECURSIVE_THEIRS; | |
44 | else if (!strcmp(arg+2, "subtree")) | |
85e51b78 JH |
45 | o.subtree_shift = ""; |
46 | else if (!prefixcmp(arg+2, "subtree=")) | |
47 | o.subtree_shift = arg + 10; | |
8cc5b290 AP |
48 | else |
49 | die("Unknown option %s", arg); | |
50 | continue; | |
51 | } | |
8a2fce18 MV |
52 | if (bases_count < ARRAY_SIZE(bases)-1) { |
53 | unsigned char *sha = xmalloc(20); | |
54 | if (get_sha1(argv[i], sha)) | |
55 | die("Could not parse object '%s'", argv[i]); | |
56 | bases[bases_count++] = sha; | |
73118f89 | 57 | } |
73118f89 | 58 | else |
b74d779b JS |
59 | warning("Cannot handle more than %d bases. " |
60 | "Ignoring %s.", | |
61 | (int)ARRAY_SIZE(bases)-1, argv[i]); | |
6d297f81 JS |
62 | } |
63 | if (argc - i != 3) /* "--" "<head>" "<remote>" */ | |
64 | die("Not handling anything other than two heads merge."); | |
65 | ||
8a2fce18 MV |
66 | o.branch1 = argv[++i]; |
67 | o.branch2 = argv[++i]; | |
6d297f81 | 68 | |
8a2fce18 MV |
69 | if (get_sha1(o.branch1, h1)) |
70 | die("Could not resolve ref '%s'", o.branch1); | |
71 | if (get_sha1(o.branch2, h2)) | |
72 | die("Could not resolve ref '%s'", o.branch2); | |
6d297f81 | 73 | |
8a2fce18 MV |
74 | o.branch1 = better_branch_name(o.branch1); |
75 | o.branch2 = better_branch_name(o.branch2); | |
3f6ee2d1 | 76 | |
8a2fce18 MV |
77 | if (o.verbosity >= 3) |
78 | printf("Merging %s with %s\n", o.branch1, o.branch2); | |
e0ec1819 | 79 | |
8a2fce18 | 80 | failed = merge_recursive_generic(&o, h1, h2, bases_count, bases, &result); |
73118f89 SB |
81 | if (failed < 0) |
82 | return 128; /* die() error code */ | |
83 | return failed; | |
6d297f81 | 84 | } |