Skip to content

Commit 139ef37

Browse files
newrengitster
authored andcommitted
merge-recursive: enforce opt->ancestor != NULL when calling merge_trees()
We always want our conflict hunks to be labelled so that users can know where each came from. The previous commit fixed the one caller in the codebase which was not setting opt->ancestor (and thus not providing a label for the "merge base" conflict hunk in diff3-style conflict markers); add an assertion to prevent future codepaths from also overlooking this requirement. Enforcing this requirement also allows us to simplify the code for labelling the conflict hunks by no longer checking if the ancestor label is NULL. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 65c01c6 commit 139ef37

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

merge-recursive.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ static int merge_3way(struct merge_options *opt,
10191019
{
10201020
mmfile_t orig, src1, src2;
10211021
struct ll_merge_options ll_opts = {0};
1022-
char *base_name, *name1, *name2;
1022+
char *base, *name1, *name2;
10231023
int merge_status;
10241024

10251025
ll_opts.renormalize = opt->renormalize;
@@ -1043,16 +1043,13 @@ static int merge_3way(struct merge_options *opt,
10431043
}
10441044
}
10451045

1046-
assert(a->path && b->path && o->path);
1047-
if (strcmp(a->path, b->path) ||
1048-
(opt->ancestor != NULL && strcmp(a->path, o->path) != 0)) {
1049-
base_name = opt->ancestor == NULL ? NULL :
1050-
mkpathdup("%s:%s", opt->ancestor, o->path);
1046+
assert(a->path && b->path && o->path && opt->ancestor);
1047+
if (strcmp(a->path, b->path) || strcmp(a->path, o->path) != 0) {
1048+
base = mkpathdup("%s:%s", opt->ancestor, o->path);
10511049
name1 = mkpathdup("%s:%s", branch1, a->path);
10521050
name2 = mkpathdup("%s:%s", branch2, b->path);
10531051
} else {
1054-
base_name = opt->ancestor == NULL ? NULL :
1055-
mkpathdup("%s", opt->ancestor);
1052+
base = mkpathdup("%s", opt->ancestor);
10561053
name1 = mkpathdup("%s", branch1);
10571054
name2 = mkpathdup("%s", branch2);
10581055
}
@@ -1061,11 +1058,11 @@ static int merge_3way(struct merge_options *opt,
10611058
read_mmblob(&src1, &a->oid);
10621059
read_mmblob(&src2, &b->oid);
10631060

1064-
merge_status = ll_merge(result_buf, a->path, &orig, base_name,
1061+
merge_status = ll_merge(result_buf, a->path, &orig, base,
10651062
&src1, name1, &src2, name2,
10661063
opt->repo->index, &ll_opts);
10671064

1068-
free(base_name);
1065+
free(base);
10691066
free(name1);
10701067
free(name2);
10711068
free(orig.ptr);
@@ -3390,6 +3387,8 @@ int merge_trees(struct merge_options *opt,
33903387
int code, clean;
33913388
struct strbuf sb = STRBUF_INIT;
33923389

3390+
assert(opt->ancestor != NULL);
3391+
33933392
if (!opt->call_depth && repo_index_has_changes(opt->repo, head, &sb)) {
33943393
err(opt, _("Your local changes to the following files would be overwritten by merge:\n %s"),
33953394
sb.buf);

0 commit comments

Comments
 (0)