Skip to content

Commit 16fb5c5

Browse files
rscharfegitster
authored andcommitted
ls-tree: fix expansion of repeated %(path)
expand_show_tree() borrows the base strbuf given to us by read_tree() to build the full path of the current entry when handling %(path). Only its indirect caller, show_tree_fmt(), removes the added entry name. That works fine as long as %(path) is only included once in the format string, but accumulates duplicates if it's repeated: $ git ls-tree --format='%(path) %(path) %(path)' HEAD M* Makefile MakefileMakefile MakefileMakefileMakefile Reset the length after each use to get the same expansion every time; here's the behavior with this patch: $ ./git ls-tree --format='%(path) %(path) %(path)' HEAD M* Makefile Makefile Makefile Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 455923e commit 16fb5c5

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

builtin/ls-tree.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,12 @@ static size_t expand_show_tree(struct strbuf *sb, const char *start,
9797
const char *prefix = chomp_prefix ? ls_tree_prefix : NULL;
9898
struct strbuf quoted = STRBUF_INIT;
9999
struct strbuf sbuf = STRBUF_INIT;
100+
size_t baselen = data->base->len;
101+
100102
strbuf_addstr(data->base, data->pathname);
101103
name = relative_path(data->base->buf, prefix, &sbuf);
102104
quote_c_style(name, &quoted, NULL, 0);
105+
strbuf_setlen(data->base, baselen);
103106
strbuf_addbuf(sb, &quoted);
104107
strbuf_release(&sbuf);
105108
strbuf_release(&quoted);
@@ -143,7 +146,6 @@ static int show_recursive(const char *base, size_t baselen, const char *pathname
143146
static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
144147
const char *pathname, unsigned mode, void *context)
145148
{
146-
size_t baselen;
147149
int recurse = 0;
148150
struct strbuf sb = STRBUF_INIT;
149151
enum object_type type = object_type(mode);
@@ -163,12 +165,10 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
163165
if (type == OBJ_BLOB && (ls_options & LS_TREE_ONLY))
164166
return 0;
165167

166-
baselen = base->len;
167168
strbuf_expand(&sb, format, expand_show_tree, &data);
168169
strbuf_addch(&sb, line_termination);
169170
fwrite(sb.buf, sb.len, 1, stdout);
170171
strbuf_release(&sb);
171-
strbuf_setlen(base, baselen);
172172
return recurse;
173173
}
174174

t/t3104-ls-tree-format.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ test_ls_tree_format () {
3737
'
3838
}
3939

40+
test_expect_success "ls-tree --format='%(path) %(path) %(path)' HEAD top-file" '
41+
git ls-tree --format="%(path) %(path) %(path)" HEAD top-file.t >actual &&
42+
echo top-file.t top-file.t top-file.t >expect &&
43+
test_cmp expect actual
44+
'
45+
4046
test_ls_tree_format \
4147
"%(objectmode) %(objecttype) %(objectname)%x09%(path)" \
4248
""

0 commit comments

Comments
 (0)