Skip to content

Commit 8a40af9

Browse files
committed
Merge branch 'rs/ls-tree-path-expansion-fix'
"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 b269563 + c388fcd commit 8a40af9

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

builtin/ls-tree.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ static size_t expand_show_tree(struct strbuf *sb, const char *start,
9494
} else if (skip_prefix(start, "(path)", &p)) {
9595
const char *name = data->base->buf;
9696
const char *prefix = options->chomp_prefix ? options->ls_tree_prefix : NULL;
97-
struct strbuf quoted = STRBUF_INIT;
9897
struct strbuf sbuf = STRBUF_INIT;
98+
size_t baselen = data->base->len;
99+
99100
strbuf_addstr(data->base, data->pathname);
100101
name = relative_path(data->base->buf, prefix, &sbuf);
101-
quote_c_style(name, &quoted, NULL, 0);
102-
strbuf_addbuf(sb, &quoted);
102+
quote_c_style(name, sb, NULL, 0);
103+
strbuf_setlen(data->base, baselen);
103104
strbuf_release(&sbuf);
104-
strbuf_release(&quoted);
105105
} else {
106106
errlen = (unsigned long)len;
107107
die(_("bad ls-tree format: %%%.*s"), errlen, start);
@@ -144,7 +144,6 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
144144
const char *pathname, unsigned mode, void *context)
145145
{
146146
struct ls_tree_options *options = context;
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 && (options->ls_options & LS_TREE_ONLY))
165164
return 0;
166165

167-
baselen = base->len;
168166
strbuf_expand(&sb, options->format, expand_show_tree, &cb_data);
169167
strbuf_addch(&sb, options->null_termination ? '\0' : '\n');
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
@@ -35,6 +35,12 @@ test_ls_tree_format () {
3535
'
3636
}
3737

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

0 commit comments

Comments
 (0)