threads / patch / 59739

patch, 2 partsls-files: align format atoms with git ls-tree

Subject: [PATCH 0/2] ls-files: align format atoms with git ls-tree

## tl;dr

7 messages between May 13, 2023 and May 23, 2023. Diffs are folded; open one to read it.

replies: 6people: 2as markdown or json

ZheNing Hu via GitGitGadget· May 13, 2023, 09:11 UTC · lore

Users sometimes want all format atoms of git ls-files --format to be compatible with the format atoms of git ls-tree --format [1]. However, git ls-files --format lacks the %(objecttype) and %(objectsize), %(objectsize:padded) atoms compared to git ls-tree --format, causing incompatibility. Therefore, these atoms are added to the --format of git ls-files to resolve the issue of incompatibility.

%(objecttype): get the object type of the file which is recorded in the index. %(objectsize): get the object size of the file which is recorded in the index, ("-" if the object is a commit or tree). %(objectsize:padded): same as %(objectsize), but with a padded format.

v1: add %(objecttype) and %(objectsize) atos to git ls-files --format.

[1]: https://lore.kernel.org/git/21183ea9-84e2-fd89-eb9b-419556680c07@gnieh.org/T/#u

ZheNing Hu (2):
  ls-files: add %(objecttype) atom to format option
  ls-files: add %(objectsize) atom to format option
 Documentation/git-ls-files.txt |  6 ++++++
 builtin/ls-files.c             | 27 ++++++++++++++++++++++++++
 t/t3013-ls-files-format.sh     | 35 ++++++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+)
base-commit: 5bc069e383539824fd3a0d897100d44bbe1f8a24
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1533%2Fadlternative%2Fzh%2Fls-files-formats-expand-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1533/adlternative/zh/ls-files-formats-expand-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1533
-- 
gitgitgadget
ZheNing Hu via GitGitGadget· May 13, 2023, 09:11 UTC · re: ZheNing Hu via GitGitGadget · lore

[PATCH 1/2] ls-files: add %(objecttype) atom to format option

From: ZheNing Hu <adlternative@gmail.com>

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>
---
 Documentation/git-ls-files.txt | 2 ++
 builtin/ls-files.c             | 2 ++
 t/t3013-ls-files-format.sh     | 7 +++++++
 3 files changed, 11 insertions(+)
Show changes to 3 files +11 −0

Documentation/git-ls-files.txt, builtin/ls-files.c, t/t3013-ls-files-format.sh

diff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt
index 1abdd3c21c5..4356c094cec 100644
--- a/Documentation/git-ls-files.txt
+++ b/Documentation/git-ls-files.txt
@@ -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::
diff --git a/builtin/ls-files.c b/builtin/ls-files.c
index 625f48f0d61..6ff764cda18 100644
--- a/builtin/ls-files.c
+++ b/builtin/ls-files.c
@@ -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))
diff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh
index ef6fb53f7f1..3a1da3d6697 100755
--- a/t/t3013-ls-files-format.sh
+++ b/t/t3013-ls-files-format.sh
@@ -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 &&
-- 
gitgitgadget
Junio C Hamano· May 15, 2023, 05:00 UTC · re: ZheNing Hu via GitGitGadget · lore

Re: [PATCH 1/2] ls-files: add %(objecttype) atom to format option

"ZheNing Hu via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 6 quoted lines
> From: ZheNing Hu <adlternative@gmail.com>
>
> 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.

"Sometimes users may want to" sounds a bit awkward; even if no user notices that the two very similar commands supports different subset of the vocabulary without good reason, wouldn't we want to align the feature set of these two commands?

> 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.

And from that point of view, this conclusion has a bit more to think about. Is the %(objecttype) singled out here only because somebody happened to have complained on the list, or did somebody went into the list of supported atoms between two commands and considered what is missing from one but is supported by the other, and concluded that only adding this one atom to ls-files would make the two consistent?

I would not complain if it were the former, but it must be explained here in the proposed log message. That would encourage others to do a follow-on work to complete the comparison to fill the gaps on the both sides. If it were the former, saying so explicitly in the proposed log message will save others---otherwise they may try to do the comparison themselves only to find that this was the last one remaining discrepancy.

Show 51 quoted lines
> Signed-off-by: ZheNing Hu <adlternative@gmail.com>
> ---
>  Documentation/git-ls-files.txt | 2 ++
>  builtin/ls-files.c             | 2 ++
>  t/t3013-ls-files-format.sh     | 7 +++++++
>  3 files changed, 11 insertions(+)
>
> diff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt
> index 1abdd3c21c5..4356c094cec 100644
> --- a/Documentation/git-ls-files.txt
> +++ b/Documentation/git-ls-files.txt
> @@ -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::
> diff --git a/builtin/ls-files.c b/builtin/ls-files.c
> index 625f48f0d61..6ff764cda18 100644
> --- a/builtin/ls-files.c
> +++ b/builtin/ls-files.c
> @@ -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))
> diff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh
> index ef6fb53f7f1..3a1da3d6697 100755
> --- a/t/t3013-ls-files-format.sh
> +++ b/t/t3013-ls-files-format.sh
> @@ -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 &&
ZheNing Hu· May 18, 2023, 10:02 UTC · re: Junio C Hamano · lore

Re: [PATCH 1/2] ls-files: add %(objecttype) atom to format option

Junio C Hamano <gitster@pobox.com> 于2023年5月15日周一 13:00写道:
Show 34 quoted lines
>
> "ZheNing Hu via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
> > From: ZheNing Hu <adlternative@gmail.com>
> >
> > 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.
>
> "Sometimes users may want to" sounds a bit awkward; even if no user
> notices that the two very similar commands supports different subset
> of the vocabulary without good reason, wouldn't we want to align the
> feature set of these two commands?
>
> > 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.
>
> And from that point of view, this conclusion has a bit more to think
> about.  Is the %(objecttype) singled out here only because somebody
> happened to have complained on the list, or did somebody went into
> the list of supported atoms between two commands and considered what
> is missing from one but is supported by the other, and concluded that
> only adding this one atom to ls-files would make the two consistent?
>
> I would not complain if it were the former, but it must be explained
> here in the proposed log message.  That would encourage others to do
> a follow-on work to complete the comparison to fill the gaps on the
> both sides.  If it were the former, saying so explicitly in the
> proposed log message will save others---otherwise they may try to do
> the comparison themselves only to find that this was the last one
> remaining discrepancy.
>

I think the original requirement is that users wanted to obtain a similar output format to the default output format of git ls-tree directly through git ls-files --format="%(objectmode) %(objecttype) %(objectname)%x09%(path)", but found that the corresponding functionality was missing.

However, from a deeper perspective, the results displayed by git ls-files for the index and git ls-tree -r for the tree are very similar. Making git ls-files compatible with the atoms of git ls-tree can provide a unified view here, and can also be used for some conversion between the index and tree, such as git ls-files --format | git mktree.

ZheNing Hu via GitGitGadget· May 13, 2023, 09:11 UTC · re: ZheNing Hu via GitGitGadget · lore

[PATCH 2/2] ls-files: add %(objectsize) atom to format option

From: ZheNing Hu <adlternative@gmail.com>

Sometimes users may want to align the feature of `git ls-files --format` with that of `git ls-tree --format`, but the %(objectsize) and %(objectsize:padded) are missing in the format option of git ls-files compared to git ls-tree.

Therefore, the %(objecttsize) atom is added to the format option of git ls-files, which can be used to obtain the object size of the file which is recorded in the index. ("-" if the object is a `commit` or `tree`) It also supports a padded format of size with %(objectsize:padded).

Signed-off-by: ZheNing Hu <adlternative@gmail.com>
---
 Documentation/git-ls-files.txt |  4 ++++
 builtin/ls-files.c             | 25 +++++++++++++++++++++++++
 t/t3013-ls-files-format.sh     | 28 ++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+)
Show changes to 3 files +57 −0

Documentation/git-ls-files.txt, builtin/ls-files.c, t/t3013-ls-files-format.sh

diff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt
index 4356c094cec..1bc0328bb78 100644
--- a/Documentation/git-ls-files.txt
+++ b/Documentation/git-ls-files.txt
@@ -274,6 +274,10 @@ 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.
+objectsize[:padded]::
+	The object size of the file which is recorded in the index
+	("-" if the object is a `commit` or `tree`).
+	It also supports a padded format of size with "%(objectsize:padded)".
 stage::
 	The stage of the file which is recorded in the index.
 eolinfo:index::
diff --git a/builtin/ls-files.c b/builtin/ls-files.c
index 6ff764cda18..72012c0f0f7 100644
--- a/builtin/ls-files.c
+++ b/builtin/ls-files.c
@@ -25,6 +25,9 @@
 #include "setup.h"
 #include "submodule.h"
 #include "submodule-config.h"
+#include "object-store.h"
+#include "hex.h"
+
 
 static int abbrev;
 static int show_deleted;
@@ -241,6 +244,24 @@ static void show_submodule(struct repository *superproject,
 	repo_clear(&subrepo);
 }
 
+static void expand_objectsize(struct strbuf *line, const struct object_id *oid,
+			      const enum object_type type, unsigned int padded)
+{
+	if (type == OBJ_BLOB) {
+		unsigned long size;
+		if (oid_object_info(the_repository, oid, &size) < 0)
+			die(_("could not get object info about '%s'"),
+			    oid_to_hex(oid));
+		if (padded)
+			strbuf_addf(line, "%7"PRIuMAX, (uintmax_t)size);
+		else
+			strbuf_addf(line, "%"PRIuMAX, (uintmax_t)size);
+	} else if (padded) {
+		strbuf_addf(line, "%7s", "-");
+	} else {
+		strbuf_addstr(line, "-");
+	}
+}
 struct show_index_data {
 	const char *pathname;
 	struct index_state *istate;
@@ -274,6 +295,10 @@ static size_t expand_show_index(struct strbuf *sb, const char *start,
 		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, "(objectsize:padded)", &p))
+		expand_objectsize(sb, &data->ce->oid, object_type(data->ce->ce_mode), 1);
+	else if (skip_prefix(start, "(objectsize)", &p))
+		expand_objectsize(sb, &data->ce->oid, object_type(data->ce->ce_mode), 0);
 	else if (skip_prefix(start, "(stage)", &p))
 		strbuf_addf(sb, "%d", ce_stage(data->ce));
 	else if (skip_prefix(start, "(eolinfo:index)", &p))
diff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh
index 3a1da3d6697..6e6ea0b6f3c 100755
--- a/t/t3013-ls-files-format.sh
+++ b/t/t3013-ls-files-format.sh
@@ -45,6 +45,34 @@ test_expect_success 'git ls-files --format objecttype' '
 	test_cmp expect actual
 '
 
+test_expect_success 'git ls-files --format objectsize' '
+	cat>expect <<-\EOF &&
+26
+29
+27
+26
+-
+26
+	EOF
+	git ls-files --format="%(objectsize)" >actual &&
+
+	test_cmp expect actual
+'
+
+test_expect_success 'git ls-files --format objectsize:padded' '
+	cat>expect <<-\EOF &&
+     26
+     29
+     27
+     26
+      -
+     26
+	EOF
+	git ls-files --format="%(objectsize:padded)" >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 &&
-- 
gitgitgadget
Junio C Hamano· May 15, 2023, 05:03 UTC · re: ZheNing Hu via GitGitGadget · lore

Re: [PATCH 2/2] ls-files: add %(objectsize) atom to format option

"ZheNing Hu via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: ZheNing Hu <adlternative@gmail.com>
>
> Sometimes users may want to align the feature of ...
Exactly the same comment applies here to this patch.

If these two are the only ones missing and after these patches ls-files and ls-tree become equivalent in the support of atoms, then explaining that in the log message and do these in a single patch would make the most sense.

Thanks.
ZheNing Hu via GitGitGadget· May 23, 2023, 09:00 UTC · re: ZheNing Hu via GitGitGadget · lore

[PATCH v2] ls-files: aligin format atoms wtih ls-tree

From: ZheNing Hu <adlternative@gmail.com>

"git ls-files --format" can be used to format the output of multiple file entries in the index, while "git ls-tree --format" can be used to format the contents of a tree object. However, the current set of %(objecttype), "(objectsize)", and "%(objectsize:padded)" atoms supported by "git ls-files --format" is a subset of what is available in "git ls-tree --format".

Users sometimes need to establish a unified view between the index and tree, which can help with comparison or conversion between the two.

Therefore, this patch adds the missing atoms to "git ls-files --format". "%(objecttype)" can be used to retrieve the object type corresponding to a file in the index, "(objectsize)" can be used to retrieve the object size corresponding to a file in the index, and "%(objectsize:padded)" is the same as "(objectsize)", except with padded format.

Signed-off-by: ZheNing Hu <adlternative@gmail.com>
---
    ls-files: align format atoms with git ls-tree
    
    Users sometimes want all format atoms of git ls-files --format to be
    compatible with the format atoms of git ls-tree --format [1]. However,
    git ls-files --format lacks the %(objecttype) and %(objectsize),
    %(objectsize:padded) atoms compared to git ls-tree --format, causing
    incompatibility. Therefore, these atoms are added to the --format of git
    ls-files to resolve the issue of incompatibility.
    
    %(objecttype): get the object type of the file which is recorded in the
    index. %(objectsize): get the object size of the file which is recorded
    in the index, ("-" if the object is a commit or tree).
    %(objectsize:padded): same as %(objectsize), but with a padded format.
    
    v1: add %(objecttype) and %(objectsize) atos to git ls-files --format.
    v2:
    
     1. squash two commit into one.
     2. rewrite commit messages.
    
    [1]:
    https://lore.kernel.org/git/21183ea9-84e2-fd89-eb9b-419556680c07@gnieh.org/T/#u
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1533%2Fadlternative%2Fzh%2Fls-files-formats-expand-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1533/adlternative/zh/ls-files-formats-expand-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/1533
Range-diff vs v1:
 1:  3f8884457d3 < -:  ----------- ls-files: add %(objecttype) atom to format option
 2:  95f1d714081 ! 1:  a99618c6048 ls-files: add %(objectsize) atom to format option
     @@ Metadata
      Author: ZheNing Hu <adlternative@gmail.com>
      
       ## Commit message ##
     -    ls-files: add %(objectsize) atom to format option
     +    ls-files: aligin format atoms wtih ls-tree
      
     -    Sometimes users may want to align the feature of
     -    `git ls-files --format` with that of `git ls-tree --format`,
     -    but the %(objectsize) and %(objectsize:padded) are missing
     -    in the format option of git ls-files compared to git ls-tree.
     +    "git ls-files --format" can be used to format the output of
     +    multiple file entries in the index, while "git ls-tree --format"
     +    can be used to format the contents of a tree object. However,
     +    the current set of %(objecttype), "(objectsize)", and
     +    "%(objectsize:padded)" atoms supported by "git ls-files --format"
     +    is a subset of what is available in "git ls-tree --format".
      
     -    Therefore, the %(objecttsize) atom is added to the format
     -    option of git ls-files, which can be used to obtain the
     -    object size of the file which is recorded in the index.
     -    ("-" if the object is a `commit` or `tree`) It also
     -    supports a padded format of size with %(objectsize:padded).
     +    Users sometimes need to establish a unified view between the index
     +    and tree, which can help with comparison or conversion between the two.
     +
     +    Therefore, this patch adds the missing atoms to "git ls-files --format".
     +    "%(objecttype)" can be used to retrieve the object type corresponding
     +    to a file in the index, "(objectsize)" can be used to retrieve the
     +    object size corresponding to a file in the index, and "%(objectsize:padded)"
     +    is the same as "(objectsize)", except with padded format.
      
          Signed-off-by: ZheNing Hu <adlternative@gmail.com>
      
       ## Documentation/git-ls-files.txt ##
     -@@ Documentation/git-ls-files.txt: objecttype::
     - 	The object type of the file which is recorded in the index.
     +@@ Documentation/git-ls-files.txt: 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.
      +objectsize[:padded]::
     @@ builtin/ls-files.c: static void show_submodule(struct repository *superproject,
       	const char *pathname;
       	struct index_state *istate;
      @@ builtin/ls-files.c: 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, "(objecttype)", &p))
     ++		strbuf_addstr(sb, type_name(object_type(data->ce->ce_mode)));
      +	else if (skip_prefix(start, "(objectsize:padded)", &p))
      +		expand_objectsize(sb, &data->ce->oid, object_type(data->ce->ce_mode), 1);
      +	else if (skip_prefix(start, "(objectsize)", &p))
     @@ builtin/ls-files.c: static size_t expand_show_index(struct strbuf *sb, const cha
       	else if (skip_prefix(start, "(eolinfo:index)", &p))
      
       ## t/t3013-ls-files-format.sh ##
     -@@ t/t3013-ls-files-format.sh: test_expect_success 'git ls-files --format objecttype' '
     +@@ t/t3013-ls-files-format.sh: 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 objectsize' '
      +	cat>expect <<-\EOF &&
      +26
 Documentation/git-ls-files.txt |  6 ++++++
 builtin/ls-files.c             | 27 ++++++++++++++++++++++++++
 t/t3013-ls-files-format.sh     | 35 ++++++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+)
Show changes to 3 files +68 −0

Documentation/git-ls-files.txt, builtin/ls-files.c, t/t3013-ls-files-format.sh

diff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt
index 1abdd3c21c5..1bc0328bb78 100644
--- a/Documentation/git-ls-files.txt
+++ b/Documentation/git-ls-files.txt
@@ -270,8 +270,14 @@ 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.
+objectsize[:padded]::
+	The object size of the file which is recorded in the index
+	("-" if the object is a `commit` or `tree`).
+	It also supports a padded format of size with "%(objectsize:padded)".
 stage::
 	The stage of the file which is recorded in the index.
 eolinfo:index::
diff --git a/builtin/ls-files.c b/builtin/ls-files.c
index 625f48f0d61..72012c0f0f7 100644
--- a/builtin/ls-files.c
+++ b/builtin/ls-files.c
@@ -25,6 +25,9 @@
 #include "setup.h"
 #include "submodule.h"
 #include "submodule-config.h"
+#include "object-store.h"
+#include "hex.h"
+
 
 static int abbrev;
 static int show_deleted;
@@ -241,6 +244,24 @@ static void show_submodule(struct repository *superproject,
 	repo_clear(&subrepo);
 }
 
+static void expand_objectsize(struct strbuf *line, const struct object_id *oid,
+			      const enum object_type type, unsigned int padded)
+{
+	if (type == OBJ_BLOB) {
+		unsigned long size;
+		if (oid_object_info(the_repository, oid, &size) < 0)
+			die(_("could not get object info about '%s'"),
+			    oid_to_hex(oid));
+		if (padded)
+			strbuf_addf(line, "%7"PRIuMAX, (uintmax_t)size);
+		else
+			strbuf_addf(line, "%"PRIuMAX, (uintmax_t)size);
+	} else if (padded) {
+		strbuf_addf(line, "%7s", "-");
+	} else {
+		strbuf_addstr(line, "-");
+	}
+}
 struct show_index_data {
 	const char *pathname;
 	struct index_state *istate;
@@ -272,6 +293,12 @@ 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, "(objectsize:padded)", &p))
+		expand_objectsize(sb, &data->ce->oid, object_type(data->ce->ce_mode), 1);
+	else if (skip_prefix(start, "(objectsize)", &p))
+		expand_objectsize(sb, &data->ce->oid, object_type(data->ce->ce_mode), 0);
 	else if (skip_prefix(start, "(stage)", &p))
 		strbuf_addf(sb, "%d", ce_stage(data->ce));
 	else if (skip_prefix(start, "(eolinfo:index)", &p))
diff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh
index ef6fb53f7f1..6e6ea0b6f3c 100755
--- a/t/t3013-ls-files-format.sh
+++ b/t/t3013-ls-files-format.sh
@@ -38,6 +38,41 @@ 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 objectsize' '
+	cat>expect <<-\EOF &&
+26
+29
+27
+26
+-
+26
+	EOF
+	git ls-files --format="%(objectsize)" >actual &&
+
+	test_cmp expect actual
+'
+
+test_expect_success 'git ls-files --format objectsize:padded' '
+	cat>expect <<-\EOF &&
+     26
+     29
+     27
+     26
+      -
+     26
+	EOF
+	git ls-files --format="%(objectsize:padded)" >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 &&

base-commit: 5bc069e383539824fd3a0d897100d44bbe1f8a24
-- 
gitgitgadget

← back to recent threads