ls-files: fix "--format" output of relative paths
Fix a bug introduced with the "--format" option in ce74de93 (ls-files: introduce "--format" option, 2022-07-23), where relative paths were computed using the output buffer, which could lead to random garbage data in the output. Signed-off-by: Adam Johnson <me@adamj.eu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Adam Johnson committed
Mar 10, 2023 at 13:04 UTC
cfb62dd006ae82dd1e06fb177095c8885b40b1c3
2 files changed
+20
-1
builtin/ls-files.c
+4
-1
@@ -89,12 +89,15 @@ static void write_name(const char *name)
89
90
static void write_name_to_buf(struct strbuf *sb, const char *name)
91
{
92
- const char *rel = relative_path(name, prefix_len ? prefix : NULL, sb);
92
+ struct strbuf buf = STRBUF_INIT;
93
+ const char *rel = relative_path(name, prefix_len ? prefix : NULL, &buf);
94
95
if (line_terminator)
96
quote_c_style(rel, sb, NULL, 0);
97
else
98
strbuf_addstr(sb, rel);
99
+
100
+ strbuf_release(&buf);
101
}
102
103
static const char *get_tag(const struct cache_entry *ce, const char *tag)
t/t3013-ls-files-format.sh
+16
@@ -54,6 +54,22 @@ test_expect_success 'git ls-files --format path v.s. -s' '
54
test_cmp expect actual
55
'
56
57
+test_expect_success 'git ls-files --format with relative path' '
58
+ cat >expect <<-\EOF &&
59
+ ../o1.txt
60
+ ../o2.txt
61
+ ../o3.txt
62
+ ../o4.txt
63
+ ../o5.txt
64
+ ../o6.txt
65
+ EOF
66
+ mkdir sub &&
67
+ cd sub &&
68
+ git ls-files --format="%(path)" ":/" >../actual &&
69
+ cd .. &&
70
+ test_cmp expect actual
71
+'
72
+
73
test_expect_success 'git ls-files --format with -m' '
74
echo change >o1.txt &&
75
cat >expect <<-\EOF &&