Skip to content

Commit 7c48b27

Browse files
newrengitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent de90581 commit 7c48b27

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

Documentation/git-merge-tree.txt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ After the merge completes, a new toplevel tree object is created. See
4040
OPTIONS
4141
-------
4242

43+
-z::
44+
Do not quote filenames in the <Conflicted file info> section,
45+
and end each filename with a NUL character rather than
46+
newline. Also begin the messages section with a NUL character
47+
instead of a newline. See <<OUTPUT>> below for more information.
48+
4349
--name-only::
4450
In the Conflicted file info section, instead of writing a list
4551
of (mode, oid, stage, path) tuples to output for conflicted
@@ -76,7 +82,8 @@ OID of toplevel tree
7682

7783
This is a tree object that represents what would be checked out in the
7884
working tree at the end of `git merge`. If there were conflicts, then
79-
files within this tree may have embedded conflict markers.
85+
files within this tree may have embedded conflict markers. This section
86+
is always followed by a newline (or NUL if `-z` is passed).
8087

8188
[[CFI]]
8289
Conflicted file info
@@ -89,20 +96,26 @@ This is a sequence of lines with the format
8996
The filename will be quoted as explained for the configuration
9097
variable `core.quotePath` (see linkgit:git-config[1]). However, if
9198
the `--name-only` option is passed, the mode, object, and stage will
92-
be omitted.
99+
be omitted. If `-z` is passed, the "lines" are terminated by a NUL
100+
character instead of a newline character.
93101

94102
[[IM]]
95103
Informational messages
96104
~~~~~~~~~~~~~~~~~~~~~~
97105

98-
This always starts with a blank line to separate it from the previous
99-
sections, and then has free-form messages about the merge, such as:
106+
This always starts with a blank line (or NUL if `-z` is passed) to
107+
separate it from the previous sections, and then has free-form
108+
messages about the merge, such as:
100109

101110
* "Auto-merging <file>"
102111
* "CONFLICT (rename/delete): <oldfile> renamed...but deleted in..."
103112
* "Failed to merge submodule <submodule> (<reason>)"
104113
* "Warning: cannot merge binary files: <filename>"
105114

115+
Note that these free-form messages will never have a NUL character
116+
in or between them, even if -z is passed. It is simply a large block
117+
of text taking up the remainder of the output.
118+
106119
EXIT STATUS
107120
-----------
108121

builtin/merge-tree.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ static int real_merge(struct merge_tree_options *o,
445445
if (o->show_messages == -1)
446446
o->show_messages = !result.clean;
447447

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

t/t4301-merge-tree-write-tree.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,46 @@ test_expect_success 'Check conflicted oids and modes without messages' '
173173
test_cmp conflicted-file-info actual
174174
'
175175

176+
test_expect_success 'NUL terminated conflicted file "lines"' '
177+
git checkout -b tweak1 side1 &&
178+
test_write_lines zero 1 2 3 4 5 6 >numbers &&
179+
git add numbers &&
180+
git mv numbers "Αυτά μου φαίνονται κινέζικα" &&
181+
git commit -m "Renamed numbers" &&
182+
183+
test_expect_code 1 git merge-tree --write-tree -z tweak1 side2 >out &&
184+
anonymize_hash out >actual &&
185+
printf "\\n" >>actual &&
186+
187+
# Expected results:
188+
# "greeting" should merge with conflicts
189+
# "whatever" has *both* a modify/delete and a file/directory conflict
190+
# "Αυτά μου φαίνονται κινέζικα" should have a conflict
191+
echo HASH | lf_to_nul >expect &&
192+
193+
q_to_tab <<-EOF | lf_to_nul >>expect &&
194+
100644 HASH 1Qgreeting
195+
100644 HASH 2Qgreeting
196+
100644 HASH 3Qgreeting
197+
100644 HASH 1Qwhatever~tweak1
198+
100644 HASH 2Qwhatever~tweak1
199+
100644 HASH 1QΑυτά μου φαίνονται κινέζικα
200+
100644 HASH 2QΑυτά μου φαίνονται κινέζικα
201+
100644 HASH 3QΑυτά μου φαίνονται κινέζικα
202+
203+
EOF
204+
205+
q_to_nul <<-EOF >>expect &&
206+
1QgreetingQAuto-mergingQAuto-merging greeting
207+
Q1QgreetingQCONFLICT (contents)QCONFLICT (content): Merge conflict in greeting
208+
Q2Qwhatever~tweak1QwhateverQCONFLICT (file/directory)QCONFLICT (file/directory): directory in the way of whatever from tweak1; moving it to whatever~tweak1 instead.
209+
Q1Qwhatever~tweak1QCONFLICT (modify/delete)QCONFLICT (modify/delete): whatever~tweak1 deleted in side2 and modified in tweak1. Version tweak1 of whatever~tweak1 left in tree.
210+
Q1QΑυτά μου φαίνονται κινέζικαQAuto-mergingQAuto-merging Αυτά μου φαίνονται κινέζικα
211+
Q1QΑυτά μου φαίνονται κινέζικαQCONFLICT (contents)QCONFLICT (content): Merge conflict in Αυτά μου φαίνονται κινέζικα
212+
Q
213+
EOF
214+
215+
test_cmp expect actual
216+
'
217+
176218
test_done

0 commit comments

Comments
 (0)