Skip to content

Commit 1f07146

Browse files
committed
Merge branch 'rs/ls-tree-path-expansion-fix' into maint-2.39
"git ls-tree --format='%(path) %(path)' $tree $path" showed the path three times, which has been corrected. * rs/ls-tree-path-expansion-fix: ls-tree: remove dead store and strbuf for quote_c_style() ls-tree: fix expansion of repeated %(path)
2 parents fa5958f + c388fcd commit 1f07146

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

builtin/ls-tree.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,16 @@ static size_t expand_show_tree(struct strbuf *sb, const char *start,
9494
} else if (skip_prefix(start, "(objectname)", &p)) {
9595
strbuf_add_unique_abbrev(sb, data->oid, abbrev);
9696
} else if (skip_prefix(start, "(path)", &p)) {
97-
const char *name = data->base->buf;
97+
const char *name;
9898
const char *prefix = chomp_prefix ? ls_tree_prefix : NULL;
99-
struct strbuf quoted = STRBUF_INIT;
10099
struct strbuf sbuf = STRBUF_INIT;
100+
size_t baselen = data->base->len;
101+
101102
strbuf_addstr(data->base, data->pathname);
102103
name = relative_path(data->base->buf, prefix, &sbuf);
103-
quote_c_style(name, &quoted, NULL, 0);
104-
strbuf_addbuf(sb, &quoted);
104+
quote_c_style(name, sb, NULL, 0);
105+
strbuf_setlen(data->base, baselen);
105106
strbuf_release(&sbuf);
106-
strbuf_release(&quoted);
107107
} else {
108108
errlen = (unsigned long)len;
109109
die(_("bad ls-tree format: %%%.*s"), errlen, start);
@@ -144,7 +144,6 @@ static int show_recursive(const char *base, size_t baselen, const char *pathname
144144
static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
145145
const char *pathname, unsigned mode, void *context UNUSED)
146146
{
147-
size_t baselen;
148147
int recurse = 0;
149148
struct strbuf sb = STRBUF_INIT;
150149
enum object_type type = object_type(mode);
@@ -164,12 +163,10 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
164163
if (type == OBJ_BLOB && (ls_options & LS_TREE_ONLY))
165164
return 0;
166165

167-
baselen = base->len;
168166
strbuf_expand(&sb, format, expand_show_tree, &data);
169167
strbuf_addch(&sb, line_termination);
170168
fwrite(sb.buf, sb.len, 1, stdout);
171169
strbuf_release(&sb);
172-
strbuf_setlen(base, baselen);
173170
return recurse;
174171
}
175172

t/t3104-ls-tree-format.sh

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

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

0 commit comments

Comments
 (0)