merge-tree: allow ls-files -u style info to be NUL terminated

Much as `git ls-files` has a -z option, let's add one to merge-tree so
that the conflict-info section can be NUL terminated (and avoid quoting
of unusual filenames).

Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Elijah Newren 2022-06-18 00:20:58 +00:00 committed by Junio C Hamano
parent de90581141
commit 7c48b27822
3 changed files with 62 additions and 5 deletions

View File

@ -40,6 +40,12 @@ After the merge completes, a new toplevel tree object is created. See
OPTIONS
-------
-z::
Do not quote filenames in the <Conflicted file info> section,
and end each filename with a NUL character rather than
newline. Also begin the messages section with a NUL character
instead of a newline. See <<OUTPUT>> below for more information.
--name-only::
In the Conflicted file info section, instead of writing a list
of (mode, oid, stage, path) tuples to output for conflicted
@ -76,7 +82,8 @@ OID of toplevel tree
This is a tree object that represents what would be checked out in the
working tree at the end of `git merge`. If there were conflicts, then
files within this tree may have embedded conflict markers.
files within this tree may have embedded conflict markers. This section
is always followed by a newline (or NUL if `-z` is passed).
[[CFI]]
Conflicted file info
@ -89,20 +96,26 @@ This is a sequence of lines with the format
The filename will be quoted as explained for the configuration
variable `core.quotePath` (see linkgit:git-config[1]). However, if
the `--name-only` option is passed, the mode, object, and stage will
be omitted.
be omitted. If `-z` is passed, the "lines" are terminated by a NUL
character instead of a newline character.
[[IM]]
Informational messages
~~~~~~~~~~~~~~~~~~~~~~
This always starts with a blank line to separate it from the previous
sections, and then has free-form messages about the merge, such as:
This always starts with a blank line (or NUL if `-z` is passed) to
separate it from the previous sections, and then has free-form
messages about the merge, such as:
* "Auto-merging <file>"
* "CONFLICT (rename/delete): <oldfile> renamed...but deleted in..."
* "Failed to merge submodule <submodule> (<reason>)"
* "Warning: cannot merge binary files: <filename>"
Note that these free-form messages will never have a NUL character
in or between them, even if -z is passed. It is simply a large block
of text taking up the remainder of the output.
EXIT STATUS
-----------

View File

@ -445,7 +445,7 @@ static int real_merge(struct merge_tree_options *o,
if (o->show_messages == -1)
o->show_messages = !result.clean;
puts(oid_to_hex(&result.tree->object.oid));
printf("%s%c", oid_to_hex(&result.tree->object.oid), line_termination);
if (!result.clean) {
struct string_list conflicted_files = STRING_LIST_INIT_NODUP;
const char *last = NULL;
@ -494,6 +494,8 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
N_("do a trivial merge only"), MODE_TRIVIAL),
OPT_BOOL(0, "messages", &o.show_messages,
N_("also show informational/conflict messages")),
OPT_SET_INT('z', NULL, &line_termination,
N_("separate paths with the NUL character"), '\0'),
OPT_BOOL_F(0, "name-only",
&o.name_only,
N_("list filenames without modes/oids/stages"),

View File

@ -173,4 +173,46 @@ test_expect_success 'Check conflicted oids and modes without messages' '
test_cmp conflicted-file-info actual
'
test_expect_success 'NUL terminated conflicted file "lines"' '
git checkout -b tweak1 side1 &&
test_write_lines zero 1 2 3 4 5 6 >numbers &&
git add numbers &&
git mv numbers "Αυτά μου φαίνονται κινέζικα" &&
git commit -m "Renamed numbers" &&
test_expect_code 1 git merge-tree --write-tree -z tweak1 side2 >out &&
anonymize_hash out >actual &&
printf "\\n" >>actual &&
# Expected results:
# "greeting" should merge with conflicts
# "whatever" has *both* a modify/delete and a file/directory conflict
# "Αυτά μου φαίνονται κινέζικα" should have a conflict
echo HASH | lf_to_nul >expect &&
q_to_tab <<-EOF | lf_to_nul >>expect &&
100644 HASH 1Qgreeting
100644 HASH 2Qgreeting
100644 HASH 3Qgreeting
100644 HASH 1Qwhatever~tweak1
100644 HASH 2Qwhatever~tweak1
100644 HASH 1QΑυτά μου φαίνονται κινέζικα
100644 HASH 2QΑυτά μου φαίνονται κινέζικα
100644 HASH 3QΑυτά μου φαίνονται κινέζικα
EOF
q_to_nul <<-EOF >>expect &&
1QgreetingQAuto-mergingQAuto-merging greeting
Q1QgreetingQCONFLICT (contents)QCONFLICT (content): Merge conflict in greeting
Q2Qwhatever~tweak1QwhateverQCONFLICT (file/directory)QCONFLICT (file/directory): directory in the way of whatever from tweak1; moving it to whatever~tweak1 instead.
Q1Qwhatever~tweak1QCONFLICT (modify/delete)QCONFLICT (modify/delete): whatever~tweak1 deleted in side2 and modified in tweak1. Version tweak1 of whatever~tweak1 left in tree.
Q1QΑυτά μου φαίνονται κινέζικαQAuto-mergingQAuto-merging Αυτά μου φαίνονται κινέζικα
Q1QΑυτά μου φαίνονται κινέζικαQCONFLICT (contents)QCONFLICT (content): Merge conflict in Αυτά μου φαίνονται κινέζικα
Q
EOF
test_cmp expect actual
'
test_done