Skip to content

Commit

Permalink
ls-files: add %(objecttype) atom to format option
Browse files Browse the repository at this point in the history
Sometimes users may want to align the feature of
`git ls-files --format` with that of `git ls-tree --format`,
but the %(objecttype) atom is missing in the format option
of git ls-files compared to git ls-tree.

Therefore, the %(objecttype) atom is added to the format option
of git ls-files, which can be used to obtain the object type
of the file which is recorded in the index.

Signed-off-by: ZheNing Hu <adlternative@gmail.com>
  • Loading branch information
adlternative committed May 13, 2023
1 parent 5bc069e commit 3f88844
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Documentation/git-ls-files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ interpolated. The following "fieldname" are understood:

objectmode::
The mode of the file which is recorded in the index.
objecttype::
The object type of the file which is recorded in the index.
objectname::
The name of the file which is recorded in the index.
stage::
Expand Down
2 changes: 2 additions & 0 deletions builtin/ls-files.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ static size_t expand_show_index(struct strbuf *sb, const char *start,
strbuf_addf(sb, "%06o", data->ce->ce_mode);
else if (skip_prefix(start, "(objectname)", &p))
strbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);
else if (skip_prefix(start, "(objecttype)", &p))
strbuf_addstr(sb, type_name(object_type(data->ce->ce_mode)));
else if (skip_prefix(start, "(stage)", &p))
strbuf_addf(sb, "%d", ce_stage(data->ce));
else if (skip_prefix(start, "(eolinfo:index)", &p))
Expand Down
7 changes: 7 additions & 0 deletions t/t3013-ls-files-format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ test_expect_success 'git ls-files --format objectname v.s. -s' '
test_cmp expect actual
'

test_expect_success 'git ls-files --format objecttype' '
git ls-files --format="%(objectname)" o1.txt o4.txt o6.txt >objectname &&
git cat-file --batch-check="%(objecttype)" >expect <objectname &&
git ls-files --format="%(objecttype)" o1.txt o4.txt o6.txt >actual &&
test_cmp expect actual
'

test_expect_success 'git ls-files --format v.s. --eol' '
git ls-files --eol >tmp &&
sed -e "s/ / /g" -e "s/ */ /g" tmp >expect 2>err &&
Expand Down

0 comments on commit 3f88844

Please sign in to comment.