]> git.ipfire.org Git - thirdparty/git.git/commitdiff
ls-files: fix "--format" output of relative paths
authorAdam Johnson <me@adamj.eu>
Fri, 10 Mar 2023 13:04:33 +0000 (13:04 +0000)
committerJunio C Hamano <gitster@pobox.com>
Fri, 10 Mar 2023 17:16:16 +0000 (09:16 -0800)
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>
builtin/ls-files.c
t/t3013-ls-files-format.sh

index 779dc18e59d56b46bcccba7ac9754726e6b1935e..c6484fdb45aadbdedae3255bfca1d62b29847cdf 100644 (file)
@@ -89,12 +89,15 @@ static void write_name(const char *name)
 
 static void write_name_to_buf(struct strbuf *sb, const char *name)
 {
-       const char *rel = relative_path(name, prefix_len ? prefix : NULL, sb);
+       struct strbuf buf = STRBUF_INIT;
+       const char *rel = relative_path(name, prefix_len ? prefix : NULL, &buf);
 
        if (line_terminator)
                quote_c_style(rel, sb, NULL, 0);
        else
                strbuf_addstr(sb, rel);
+
+       strbuf_release(&buf);
 }
 
 static const char *get_tag(const struct cache_entry *ce, const char *tag)
index efb7450bf1e9c14fc5563564b5b4e6913bd7c97c..ef6fb53f7f1cf53f6cf8c3727bc0b957ea7d9484 100755 (executable)
@@ -54,6 +54,22 @@ test_expect_success 'git ls-files --format path v.s. -s' '
        test_cmp expect actual
 '
 
+test_expect_success 'git ls-files --format with relative path' '
+       cat >expect <<-\EOF &&
+       ../o1.txt
+       ../o2.txt
+       ../o3.txt
+       ../o4.txt
+       ../o5.txt
+       ../o6.txt
+       EOF
+       mkdir sub &&
+       cd sub &&
+       git ls-files --format="%(path)" ":/" >../actual &&
+       cd .. &&
+       test_cmp expect actual
+'
+
 test_expect_success 'git ls-files --format with -m' '
        echo change >o1.txt &&
        cat >expect <<-\EOF &&