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)
This commit is contained in:
Junio C Hamano 2023-02-14 14:15:52 -08:00
commit 1f071460d3
2 changed files with 11 additions and 8 deletions

View File

@ -94,16 +94,16 @@ static size_t expand_show_tree(struct strbuf *sb, const char *start,
} else if (skip_prefix(start, "(objectname)", &p)) { } else if (skip_prefix(start, "(objectname)", &p)) {
strbuf_add_unique_abbrev(sb, data->oid, abbrev); strbuf_add_unique_abbrev(sb, data->oid, abbrev);
} else if (skip_prefix(start, "(path)", &p)) { } else if (skip_prefix(start, "(path)", &p)) {
const char *name = data->base->buf; const char *name;
const char *prefix = chomp_prefix ? ls_tree_prefix : NULL; const char *prefix = chomp_prefix ? ls_tree_prefix : NULL;
struct strbuf quoted = STRBUF_INIT;
struct strbuf sbuf = STRBUF_INIT; struct strbuf sbuf = STRBUF_INIT;
size_t baselen = data->base->len;
strbuf_addstr(data->base, data->pathname); strbuf_addstr(data->base, data->pathname);
name = relative_path(data->base->buf, prefix, &sbuf); name = relative_path(data->base->buf, prefix, &sbuf);
quote_c_style(name, &quoted, NULL, 0); quote_c_style(name, sb, NULL, 0);
strbuf_addbuf(sb, &quoted); strbuf_setlen(data->base, baselen);
strbuf_release(&sbuf); strbuf_release(&sbuf);
strbuf_release(&quoted);
} else { } else {
errlen = (unsigned long)len; errlen = (unsigned long)len;
die(_("bad ls-tree format: %%%.*s"), errlen, start); 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
static int show_tree_fmt(const struct object_id *oid, struct strbuf *base, static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
const char *pathname, unsigned mode, void *context UNUSED) const char *pathname, unsigned mode, void *context UNUSED)
{ {
size_t baselen;
int recurse = 0; int recurse = 0;
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
enum object_type type = object_type(mode); enum object_type type = object_type(mode);
@ -164,12 +163,10 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
if (type == OBJ_BLOB && (ls_options & LS_TREE_ONLY)) if (type == OBJ_BLOB && (ls_options & LS_TREE_ONLY))
return 0; return 0;
baselen = base->len;
strbuf_expand(&sb, format, expand_show_tree, &data); strbuf_expand(&sb, format, expand_show_tree, &data);
strbuf_addch(&sb, line_termination); strbuf_addch(&sb, line_termination);
fwrite(sb.buf, sb.len, 1, stdout); fwrite(sb.buf, sb.len, 1, stdout);
strbuf_release(&sb); strbuf_release(&sb);
strbuf_setlen(base, baselen);
return recurse; return recurse;
} }

View File

@ -36,6 +36,12 @@ test_ls_tree_format () {
' '
} }
test_expect_success "ls-tree --format='%(path) %(path) %(path)' HEAD top-file" '
git ls-tree --format="%(path) %(path) %(path)" HEAD top-file.t >actual &&
echo top-file.t top-file.t top-file.t >expect &&
test_cmp expect actual
'
test_ls_tree_format \ test_ls_tree_format \
"%(objectmode) %(objecttype) %(objectname)%x09%(path)" \ "%(objectmode) %(objecttype) %(objectname)%x09%(path)" \
"" ""