7 #include "merge-recursive.h"
8 #include "object-name.h"
9 #include "xdiff-interface.h"
11 static const char builtin_merge_recursive_usage
[] =
12 "git %s <base>... -- <head> <remote> ...";
14 static char *better_branch_name(const char *branch
)
16 static char githead_env
[8 + GIT_MAX_HEXSZ
+ 1];
19 if (strlen(branch
) != the_hash_algo
->hexsz
)
20 return xstrdup(branch
);
21 xsnprintf(githead_env
, sizeof(githead_env
), "GITHEAD_%s", branch
);
22 name
= getenv(githead_env
);
23 return xstrdup(name
? name
: branch
);
26 int cmd_merge_recursive(int argc
, const char **argv
, const char *prefix
)
28 const struct object_id
*bases
[21];
29 unsigned bases_count
= 0;
31 struct object_id h1
, h2
;
32 struct merge_options o
;
33 char *better1
, *better2
;
34 struct commit
*result
;
36 init_merge_options(&o
, the_repository
);
37 if (argv
[0] && ends_with(argv
[0], "-subtree"))
41 usagef(builtin_merge_recursive_usage
, argv
[0]);
43 for (i
= 1; i
< argc
; ++i
) {
44 const char *arg
= argv
[i
];
46 if (starts_with(arg
, "--")) {
49 if (parse_merge_opt(&o
, arg
+ 2))
50 die(_("unknown option %s"), arg
);
53 if (bases_count
< ARRAY_SIZE(bases
)-1) {
54 struct object_id
*oid
= xmalloc(sizeof(struct object_id
));
55 if (repo_get_oid(the_repository
, argv
[i
], oid
))
56 die(_("could not parse object '%s'"), argv
[i
]);
57 bases
[bases_count
++] = oid
;
60 warning(Q_("cannot handle more than %d base. "
62 "cannot handle more than %d bases. "
65 (int)ARRAY_SIZE(bases
)-1, argv
[i
]);
67 if (argc
- i
!= 3) /* "--" "<head>" "<remote>" */
68 die(_("not handling anything other than two heads merge."));
70 if (repo_read_index_unmerged(the_repository
))
71 die_resolve_conflict("merge");
73 o
.branch1
= argv
[++i
];
74 o
.branch2
= argv
[++i
];
76 if (repo_get_oid(the_repository
, o
.branch1
, &h1
))
77 die(_("could not resolve ref '%s'"), o
.branch1
);
78 if (repo_get_oid(the_repository
, o
.branch2
, &h2
))
79 die(_("could not resolve ref '%s'"), o
.branch2
);
81 o
.branch1
= better1
= better_branch_name(o
.branch1
);
82 o
.branch2
= better2
= better_branch_name(o
.branch2
);
85 printf(_("Merging %s with %s\n"), o
.branch1
, o
.branch2
);
87 failed
= merge_recursive_generic(&o
, &h1
, &h2
, bases_count
, bases
, &result
);
93 return 128; /* die() error code */