{"thread":{"id":"59739","subject":"[PATCH 0/2] ls-files: align format atoms with git ls-tree","startedAt":"2023-05-13T09:11:48Z","lastAt":"2023-05-23T09:00:30Z","messageCount":7,"participants":["ZheNing Hu via GitGitGadget","Junio C Hamano","ZheNing Hu"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"477251","messageId":"pull.1533.git.1683969100.gitgitgadget@gmail.com","threadId":"59739","inReplyTo":null,"subject":"[PATCH 0/2] ls-files: align format atoms with git ls-tree","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2023-05-13T09:11:37Z","receivedAt":"2023-05-13T09:11:48Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Users sometimes want all format atoms of git ls-files --format to be\ncompatible with the format atoms of git ls-tree --format [1]. However, git\nls-files --format lacks the %(objecttype) and %(objectsize),\n%(objectsize:padded) atoms compared to git ls-tree --format, causing\nincompatibility. Therefore, these atoms are added to the --format of git\nls-files to resolve the issue of incompatibility.\n\n%(objecttype): get the object type of the file which is recorded in the\nindex. %(objectsize): get the object size of the file which is recorded in\nthe index, (\"-\" if the object is a commit or tree). %(objectsize:padded):\nsame as %(objectsize), but with a padded format.\n\nv1: add %(objecttype) and %(objectsize) atos to git ls-files --format.\n\n[1]:\nhttps://lore.kernel.org/git/21183ea9-84e2-fd89-eb9b-419556680c07@gnieh.org/T/#u\n\nZheNing Hu (2):\n  ls-files: add %(objecttype) atom to format option\n  ls-files: add %(objectsize) atom to format option\n\n Documentation/git-ls-files.txt |  6 ++++++\n builtin/ls-files.c             | 27 ++++++++++++++++++++++++++\n t/t3013-ls-files-format.sh     | 35 ++++++++++++++++++++++++++++++++++\n 3 files changed, 68 insertions(+)\n\n\nbase-commit: 5bc069e383539824fd3a0d897100d44bbe1f8a24\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1533%2Fadlternative%2Fzh%2Fls-files-formats-expand-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1533/adlternative/zh/ls-files-formats-expand-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/1533\n-- \ngitgitgadget\n"},{"id":"477252","messageId":"3f8884457d3628439aa9e6800c976f5a597cdda3.1683969100.git.gitgitgadget@gmail.com","threadId":"59739","inReplyTo":"pull.1533.git.1683969100.gitgitgadget@gmail.com","subject":"[PATCH 1/2] ls-files: add %(objecttype) atom to format option","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2023-05-13T09:11:38Z","receivedAt":"2023-05-13T09:11:53Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nSometimes users may want to align the feature of\n`git ls-files --format` with that of `git ls-tree --format`,\nbut the %(objecttype) atom is missing in the format option\nof git ls-files compared to git ls-tree.\n\nTherefore, the %(objecttype) atom is added to the format option\nof git ls-files, which can be used to obtain the object type\nof the file which is recorded in the index.\n\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n Documentation/git-ls-files.txt | 2 ++\n builtin/ls-files.c             | 2 ++\n t/t3013-ls-files-format.sh     | 7 +++++++\n 3 files changed, 11 insertions(+)\n\ndiff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt\nindex 1abdd3c21c5..4356c094cec 100644\n--- a/Documentation/git-ls-files.txt\n+++ b/Documentation/git-ls-files.txt\n@@ -270,6 +270,8 @@ interpolated.  The following \"fieldname\" are understood:\n \n objectmode::\n \tThe mode of the file which is recorded in the index.\n+objecttype::\n+\tThe object type of the file which is recorded in the index.\n objectname::\n \tThe name of the file which is recorded in the index.\n stage::\ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex 625f48f0d61..6ff764cda18 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -272,6 +272,8 @@ static size_t expand_show_index(struct strbuf *sb, const char *start,\n \t\tstrbuf_addf(sb, \"%06o\", data->ce->ce_mode);\n \telse if (skip_prefix(start, \"(objectname)\", &p))\n \t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n+\telse if (skip_prefix(start, \"(objecttype)\", &p))\n+\t\tstrbuf_addstr(sb, type_name(object_type(data->ce->ce_mode)));\n \telse if (skip_prefix(start, \"(stage)\", &p))\n \t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n \telse if (skip_prefix(start, \"(eolinfo:index)\", &p))\ndiff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh\nindex ef6fb53f7f1..3a1da3d6697 100755\n--- a/t/t3013-ls-files-format.sh\n+++ b/t/t3013-ls-files-format.sh\n@@ -38,6 +38,13 @@ test_expect_success 'git ls-files --format objectname v.s. -s' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'git ls-files --format objecttype' '\n+\tgit ls-files --format=\"%(objectname)\" o1.txt o4.txt o6.txt >objectname &&\n+\tgit cat-file --batch-check=\"%(objecttype)\" >expect <objectname &&\n+\tgit ls-files --format=\"%(objecttype)\" o1.txt o4.txt o6.txt >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'git ls-files --format v.s. --eol' '\n \tgit ls-files --eol >tmp &&\n \tsed -e \"s/\t/ /g\" -e \"s/  */ /g\" tmp >expect 2>err &&\n-- \ngitgitgadget\n\n"},{"id":"477253","messageId":"95f1d7140814cc1598d52a4cfab33bf8aa0bf83c.1683969100.git.gitgitgadget@gmail.com","threadId":"59739","inReplyTo":"pull.1533.git.1683969100.gitgitgadget@gmail.com","subject":"[PATCH 2/2] ls-files: add %(objectsize) atom to format option","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2023-05-13T09:11:39Z","receivedAt":"2023-05-13T09:11:54Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nSometimes users may want to align the feature of\n`git ls-files --format` with that of `git ls-tree --format`,\nbut the %(objectsize) and %(objectsize:padded) are missing\nin the format option of git ls-files compared to git ls-tree.\n\nTherefore, the %(objecttsize) atom is added to the format\noption of git ls-files, which can be used to obtain the\nobject size of the file which is recorded in the index.\n(\"-\" if the object is a `commit` or `tree`) It also\nsupports a padded format of size with %(objectsize:padded).\n\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n Documentation/git-ls-files.txt |  4 ++++\n builtin/ls-files.c             | 25 +++++++++++++++++++++++++\n t/t3013-ls-files-format.sh     | 28 ++++++++++++++++++++++++++++\n 3 files changed, 57 insertions(+)\n\ndiff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt\nindex 4356c094cec..1bc0328bb78 100644\n--- a/Documentation/git-ls-files.txt\n+++ b/Documentation/git-ls-files.txt\n@@ -274,6 +274,10 @@ objecttype::\n \tThe object type of the file which is recorded in the index.\n objectname::\n \tThe name of the file which is recorded in the index.\n+objectsize[:padded]::\n+\tThe object size of the file which is recorded in the index\n+\t(\"-\" if the object is a `commit` or `tree`).\n+\tIt also supports a padded format of size with \"%(objectsize:padded)\".\n stage::\n \tThe stage of the file which is recorded in the index.\n eolinfo:index::\ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex 6ff764cda18..72012c0f0f7 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -25,6 +25,9 @@\n #include \"setup.h\"\n #include \"submodule.h\"\n #include \"submodule-config.h\"\n+#include \"object-store.h\"\n+#include \"hex.h\"\n+\n \n static int abbrev;\n static int show_deleted;\n@@ -241,6 +244,24 @@ static void show_submodule(struct repository *superproject,\n \trepo_clear(&subrepo);\n }\n \n+static void expand_objectsize(struct strbuf *line, const struct object_id *oid,\n+\t\t\t      const enum object_type type, unsigned int padded)\n+{\n+\tif (type == OBJ_BLOB) {\n+\t\tunsigned long size;\n+\t\tif (oid_object_info(the_repository, oid, &size) < 0)\n+\t\t\tdie(_(\"could not get object info about '%s'\"),\n+\t\t\t    oid_to_hex(oid));\n+\t\tif (padded)\n+\t\t\tstrbuf_addf(line, \"%7\"PRIuMAX, (uintmax_t)size);\n+\t\telse\n+\t\t\tstrbuf_addf(line, \"%\"PRIuMAX, (uintmax_t)size);\n+\t} else if (padded) {\n+\t\tstrbuf_addf(line, \"%7s\", \"-\");\n+\t} else {\n+\t\tstrbuf_addstr(line, \"-\");\n+\t}\n+}\n struct show_index_data {\n \tconst char *pathname;\n \tstruct index_state *istate;\n@@ -274,6 +295,10 @@ static size_t expand_show_index(struct strbuf *sb, const char *start,\n \t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n \telse if (skip_prefix(start, \"(objecttype)\", &p))\n \t\tstrbuf_addstr(sb, type_name(object_type(data->ce->ce_mode)));\n+\telse if (skip_prefix(start, \"(objectsize:padded)\", &p))\n+\t\texpand_objectsize(sb, &data->ce->oid, object_type(data->ce->ce_mode), 1);\n+\telse if (skip_prefix(start, \"(objectsize)\", &p))\n+\t\texpand_objectsize(sb, &data->ce->oid, object_type(data->ce->ce_mode), 0);\n \telse if (skip_prefix(start, \"(stage)\", &p))\n \t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n \telse if (skip_prefix(start, \"(eolinfo:index)\", &p))\ndiff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh\nindex 3a1da3d6697..6e6ea0b6f3c 100755\n--- a/t/t3013-ls-files-format.sh\n+++ b/t/t3013-ls-files-format.sh\n@@ -45,6 +45,34 @@ test_expect_success 'git ls-files --format objecttype' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'git ls-files --format objectsize' '\n+\tcat>expect <<-\\EOF &&\n+26\n+29\n+27\n+26\n+-\n+26\n+\tEOF\n+\tgit ls-files --format=\"%(objectsize)\" >actual &&\n+\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format objectsize:padded' '\n+\tcat>expect <<-\\EOF &&\n+     26\n+     29\n+     27\n+     26\n+      -\n+     26\n+\tEOF\n+\tgit ls-files --format=\"%(objectsize:padded)\" >actual &&\n+\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'git ls-files --format v.s. --eol' '\n \tgit ls-files --eol >tmp &&\n \tsed -e \"s/\t/ /g\" -e \"s/  */ /g\" tmp >expect 2>err &&\n-- \ngitgitgadget\n"},{"id":"477269","messageId":"xmqqednicho7.fsf@gitster.g","threadId":"59739","inReplyTo":"3f8884457d3628439aa9e6800c976f5a597cdda3.1683969100.git.gitgitgadget@gmail.com","subject":"Re: [PATCH 1/2] ls-files: add %(objecttype) atom to format option","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2023-05-15T05:00:56Z","receivedAt":"2023-05-15T05:01:02Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"ZheNing Hu via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: ZheNing Hu <adlternative@gmail.com>\n>\n> Sometimes users may want to align the feature of\n> `git ls-files --format` with that of `git ls-tree --format`,\n> but the %(objecttype) atom is missing in the format option\n> of git ls-files compared to git ls-tree.\n\n\"Sometimes users may want to\" sounds a bit awkward; even if no user\nnotices that the two very similar commands supports different subset\nof the vocabulary without good reason, wouldn't we want to align the\nfeature set of these two commands?\n\n> Therefore, the %(objecttype) atom is added to the format option\n> of git ls-files, which can be used to obtain the object type\n> of the file which is recorded in the index.\n\nAnd from that point of view, this conclusion has a bit more to think\nabout.  Is the %(objecttype) singled out here only because somebody\nhappened to have complained on the list, or did somebody went into\nthe list of supported atoms between two commands and considered what\nis missing from one but is supported by the other, and concluded that\nonly adding this one atom to ls-files would make the two consistent?\n\nI would not complain if it were the former, but it must be explained\nhere in the proposed log message.  That would encourage others to do\na follow-on work to complete the comparison to fill the gaps on the\nboth sides.  If it were the former, saying so explicitly in the\nproposed log message will save others---otherwise they may try to do\nthe comparison themselves only to find that this was the last one\nremaining discrepancy.\n\n> Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n> ---\n>  Documentation/git-ls-files.txt | 2 ++\n>  builtin/ls-files.c             | 2 ++\n>  t/t3013-ls-files-format.sh     | 7 +++++++\n>  3 files changed, 11 insertions(+)\n>\n> diff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt\n> index 1abdd3c21c5..4356c094cec 100644\n> --- a/Documentation/git-ls-files.txt\n> +++ b/Documentation/git-ls-files.txt\n> @@ -270,6 +270,8 @@ interpolated.  The following \"fieldname\" are understood:\n>  \n>  objectmode::\n>  \tThe mode of the file which is recorded in the index.\n> +objecttype::\n> +\tThe object type of the file which is recorded in the index.\n>  objectname::\n>  \tThe name of the file which is recorded in the index.\n>  stage::\n> diff --git a/builtin/ls-files.c b/builtin/ls-files.c\n> index 625f48f0d61..6ff764cda18 100644\n> --- a/builtin/ls-files.c\n> +++ b/builtin/ls-files.c\n> @@ -272,6 +272,8 @@ static size_t expand_show_index(struct strbuf *sb, const char *start,\n>  \t\tstrbuf_addf(sb, \"%06o\", data->ce->ce_mode);\n>  \telse if (skip_prefix(start, \"(objectname)\", &p))\n>  \t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n> +\telse if (skip_prefix(start, \"(objecttype)\", &p))\n> +\t\tstrbuf_addstr(sb, type_name(object_type(data->ce->ce_mode)));\n>  \telse if (skip_prefix(start, \"(stage)\", &p))\n>  \t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n>  \telse if (skip_prefix(start, \"(eolinfo:index)\", &p))\n> diff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh\n> index ef6fb53f7f1..3a1da3d6697 100755\n> --- a/t/t3013-ls-files-format.sh\n> +++ b/t/t3013-ls-files-format.sh\n> @@ -38,6 +38,13 @@ test_expect_success 'git ls-files --format objectname v.s. -s' '\n>  \ttest_cmp expect actual\n>  '\n>  \n> +test_expect_success 'git ls-files --format objecttype' '\n> +\tgit ls-files --format=\"%(objectname)\" o1.txt o4.txt o6.txt >objectname &&\n> +\tgit cat-file --batch-check=\"%(objecttype)\" >expect <objectname &&\n> +\tgit ls-files --format=\"%(objecttype)\" o1.txt o4.txt o6.txt >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n>  test_expect_success 'git ls-files --format v.s. --eol' '\n>  \tgit ls-files --eol >tmp &&\n>  \tsed -e \"s/\t/ /g\" -e \"s/  */ /g\" tmp >expect 2>err &&\n"},{"id":"477270","messageId":"xmqqa5y6chk2.fsf@gitster.g","threadId":"59739","inReplyTo":"95f1d7140814cc1598d52a4cfab33bf8aa0bf83c.1683969100.git.gitgitgadget@gmail.com","subject":"Re: [PATCH 2/2] ls-files: add %(objectsize) atom to format option","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2023-05-15T05:03:25Z","receivedAt":"2023-05-15T05:03:37Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"ZheNing Hu via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: ZheNing Hu <adlternative@gmail.com>\n>\n> Sometimes users may want to align the feature of ...\n\nExactly the same comment applies here to this patch.\n\nIf these two are the only ones missing and after these patches\nls-files and ls-tree become equivalent in the support of atoms,\nthen explaining that in the log message and do these in a single\npatch would make the most sense.\n\nThanks.\n\n"},{"id":"477510","messageId":"CAOLTT8TEN38j8k6fnzcTs3X_a7yTrKysi50Nt7sH2bdbgWkwEA@mail.gmail.com","threadId":"59739","inReplyTo":"xmqqednicho7.fsf@gitster.g","subject":"Re: [PATCH 1/2] ls-files: add %(objecttype) atom to format option","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2023-05-18T10:02:43Z","receivedAt":"2023-05-18T10:01:43Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Junio C Hamano <gitster@pobox.com> 于2023年5月15日周一 13:00写道：\n>\n> \"ZheNing Hu via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n>\n> > From: ZheNing Hu <adlternative@gmail.com>\n> >\n> > Sometimes users may want to align the feature of\n> > `git ls-files --format` with that of `git ls-tree --format`,\n> > but the %(objecttype) atom is missing in the format option\n> > of git ls-files compared to git ls-tree.\n>\n> \"Sometimes users may want to\" sounds a bit awkward; even if no user\n> notices that the two very similar commands supports different subset\n> of the vocabulary without good reason, wouldn't we want to align the\n> feature set of these two commands?\n>\n> > Therefore, the %(objecttype) atom is added to the format option\n> > of git ls-files, which can be used to obtain the object type\n> > of the file which is recorded in the index.\n>\n> And from that point of view, this conclusion has a bit more to think\n> about.  Is the %(objecttype) singled out here only because somebody\n> happened to have complained on the list, or did somebody went into\n> the list of supported atoms between two commands and considered what\n> is missing from one but is supported by the other, and concluded that\n> only adding this one atom to ls-files would make the two consistent?\n>\n> I would not complain if it were the former, but it must be explained\n> here in the proposed log message.  That would encourage others to do\n> a follow-on work to complete the comparison to fill the gaps on the\n> both sides.  If it were the former, saying so explicitly in the\n> proposed log message will save others---otherwise they may try to do\n> the comparison themselves only to find that this was the last one\n> remaining discrepancy.\n>\nI think the original requirement is that users wanted to obtain a similar\noutput format to the default output format of git ls-tree directly through\ngit ls-files --format=\"%(objectmode) %(objecttype) %(objectname)%x09%(path)\",\nbut found that the corresponding functionality was missing.\n\nHowever, from a deeper perspective, the results displayed by git ls-files\nfor the index and git ls-tree -r for the tree are very similar. Making\ngit ls-files compatible with the atoms of git ls-tree can provide a\nunified view here,\nand can also be used for some conversion between the index and tree, such as\ngit ls-files --format | git mktree.\n"},{"id":"477662","messageId":"pull.1533.v2.git.1684832418299.gitgitgadget@gmail.com","threadId":"59739","inReplyTo":"pull.1533.git.1683969100.gitgitgadget@gmail.com","subject":"[PATCH v2] ls-files: aligin format atoms wtih ls-tree","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2023-05-23T09:00:18Z","receivedAt":"2023-05-23T09:00:30Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\n\"git ls-files --format\" can be used to format the output of\nmultiple file entries in the index, while \"git ls-tree --format\"\ncan be used to format the contents of a tree object. However,\nthe current set of %(objecttype), \"(objectsize)\", and\n\"%(objectsize:padded)\" atoms supported by \"git ls-files --format\"\nis a subset of what is available in \"git ls-tree --format\".\n\nUsers sometimes need to establish a unified view between the index\nand tree, which can help with comparison or conversion between the two.\n\nTherefore, this patch adds the missing atoms to \"git ls-files --format\".\n\"%(objecttype)\" can be used to retrieve the object type corresponding\nto a file in the index, \"(objectsize)\" can be used to retrieve the\nobject size corresponding to a file in the index, and \"%(objectsize:padded)\"\nis the same as \"(objectsize)\", except with padded format.\n\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n    ls-files: align format atoms with git ls-tree\n    \n    Users sometimes want all format atoms of git ls-files --format to be\n    compatible with the format atoms of git ls-tree --format [1]. However,\n    git ls-files --format lacks the %(objecttype) and %(objectsize),\n    %(objectsize:padded) atoms compared to git ls-tree --format, causing\n    incompatibility. Therefore, these atoms are added to the --format of git\n    ls-files to resolve the issue of incompatibility.\n    \n    %(objecttype): get the object type of the file which is recorded in the\n    index. %(objectsize): get the object size of the file which is recorded\n    in the index, (\"-\" if the object is a commit or tree).\n    %(objectsize:padded): same as %(objectsize), but with a padded format.\n    \n    v1: add %(objecttype) and %(objectsize) atos to git ls-files --format.\n    v2:\n    \n     1. squash two commit into one.\n     2. rewrite commit messages.\n    \n    [1]:\n    https://lore.kernel.org/git/21183ea9-84e2-fd89-eb9b-419556680c07@gnieh.org/T/#u\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1533%2Fadlternative%2Fzh%2Fls-files-formats-expand-v2\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1533/adlternative/zh/ls-files-formats-expand-v2\nPull-Request: https://github.com/gitgitgadget/git/pull/1533\n\nRange-diff vs v1:\n\n 1:  3f8884457d3 < -:  ----------- ls-files: add %(objecttype) atom to format option\n 2:  95f1d714081 ! 1:  a99618c6048 ls-files: add %(objectsize) atom to format option\n     @@ Metadata\n      Author: ZheNing Hu <adlternative@gmail.com>\n      \n       ## Commit message ##\n     -    ls-files: add %(objectsize) atom to format option\n     +    ls-files: aligin format atoms wtih ls-tree\n      \n     -    Sometimes users may want to align the feature of\n     -    `git ls-files --format` with that of `git ls-tree --format`,\n     -    but the %(objectsize) and %(objectsize:padded) are missing\n     -    in the format option of git ls-files compared to git ls-tree.\n     +    \"git ls-files --format\" can be used to format the output of\n     +    multiple file entries in the index, while \"git ls-tree --format\"\n     +    can be used to format the contents of a tree object. However,\n     +    the current set of %(objecttype), \"(objectsize)\", and\n     +    \"%(objectsize:padded)\" atoms supported by \"git ls-files --format\"\n     +    is a subset of what is available in \"git ls-tree --format\".\n      \n     -    Therefore, the %(objecttsize) atom is added to the format\n     -    option of git ls-files, which can be used to obtain the\n     -    object size of the file which is recorded in the index.\n     -    (\"-\" if the object is a `commit` or `tree`) It also\n     -    supports a padded format of size with %(objectsize:padded).\n     +    Users sometimes need to establish a unified view between the index\n     +    and tree, which can help with comparison or conversion between the two.\n     +\n     +    Therefore, this patch adds the missing atoms to \"git ls-files --format\".\n     +    \"%(objecttype)\" can be used to retrieve the object type corresponding\n     +    to a file in the index, \"(objectsize)\" can be used to retrieve the\n     +    object size corresponding to a file in the index, and \"%(objectsize:padded)\"\n     +    is the same as \"(objectsize)\", except with padded format.\n      \n          Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n      \n       ## Documentation/git-ls-files.txt ##\n     -@@ Documentation/git-ls-files.txt: objecttype::\n     - \tThe object type of the file which is recorded in the index.\n     +@@ Documentation/git-ls-files.txt: interpolated.  The following \"fieldname\" are understood:\n     + \n     + objectmode::\n     + \tThe mode of the file which is recorded in the index.\n     ++objecttype::\n     ++\tThe object type of the file which is recorded in the index.\n       objectname::\n       \tThe name of the file which is recorded in the index.\n      +objectsize[:padded]::\n     @@ builtin/ls-files.c: static void show_submodule(struct repository *superproject,\n       \tconst char *pathname;\n       \tstruct index_state *istate;\n      @@ builtin/ls-files.c: static size_t expand_show_index(struct strbuf *sb, const char *start,\n     + \t\tstrbuf_addf(sb, \"%06o\", data->ce->ce_mode);\n     + \telse if (skip_prefix(start, \"(objectname)\", &p))\n       \t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n     - \telse if (skip_prefix(start, \"(objecttype)\", &p))\n     - \t\tstrbuf_addstr(sb, type_name(object_type(data->ce->ce_mode)));\n     ++\telse if (skip_prefix(start, \"(objecttype)\", &p))\n     ++\t\tstrbuf_addstr(sb, type_name(object_type(data->ce->ce_mode)));\n      +\telse if (skip_prefix(start, \"(objectsize:padded)\", &p))\n      +\t\texpand_objectsize(sb, &data->ce->oid, object_type(data->ce->ce_mode), 1);\n      +\telse if (skip_prefix(start, \"(objectsize)\", &p))\n     @@ builtin/ls-files.c: static size_t expand_show_index(struct strbuf *sb, const cha\n       \telse if (skip_prefix(start, \"(eolinfo:index)\", &p))\n      \n       ## t/t3013-ls-files-format.sh ##\n     -@@ t/t3013-ls-files-format.sh: test_expect_success 'git ls-files --format objecttype' '\n     +@@ t/t3013-ls-files-format.sh: test_expect_success 'git ls-files --format objectname v.s. -s' '\n       \ttest_cmp expect actual\n       '\n       \n     ++test_expect_success 'git ls-files --format objecttype' '\n     ++\tgit ls-files --format=\"%(objectname)\" o1.txt o4.txt o6.txt >objectname &&\n     ++\tgit cat-file --batch-check=\"%(objecttype)\" >expect <objectname &&\n     ++\tgit ls-files --format=\"%(objecttype)\" o1.txt o4.txt o6.txt >actual &&\n     ++\ttest_cmp expect actual\n     ++'\n     ++\n      +test_expect_success 'git ls-files --format objectsize' '\n      +\tcat>expect <<-\\EOF &&\n      +26\n\n\n Documentation/git-ls-files.txt |  6 ++++++\n builtin/ls-files.c             | 27 ++++++++++++++++++++++++++\n t/t3013-ls-files-format.sh     | 35 ++++++++++++++++++++++++++++++++++\n 3 files changed, 68 insertions(+)\n\ndiff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt\nindex 1abdd3c21c5..1bc0328bb78 100644\n--- a/Documentation/git-ls-files.txt\n+++ b/Documentation/git-ls-files.txt\n@@ -270,8 +270,14 @@ interpolated.  The following \"fieldname\" are understood:\n \n objectmode::\n \tThe mode of the file which is recorded in the index.\n+objecttype::\n+\tThe object type of the file which is recorded in the index.\n objectname::\n \tThe name of the file which is recorded in the index.\n+objectsize[:padded]::\n+\tThe object size of the file which is recorded in the index\n+\t(\"-\" if the object is a `commit` or `tree`).\n+\tIt also supports a padded format of size with \"%(objectsize:padded)\".\n stage::\n \tThe stage of the file which is recorded in the index.\n eolinfo:index::\ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex 625f48f0d61..72012c0f0f7 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -25,6 +25,9 @@\n #include \"setup.h\"\n #include \"submodule.h\"\n #include \"submodule-config.h\"\n+#include \"object-store.h\"\n+#include \"hex.h\"\n+\n \n static int abbrev;\n static int show_deleted;\n@@ -241,6 +244,24 @@ static void show_submodule(struct repository *superproject,\n \trepo_clear(&subrepo);\n }\n \n+static void expand_objectsize(struct strbuf *line, const struct object_id *oid,\n+\t\t\t      const enum object_type type, unsigned int padded)\n+{\n+\tif (type == OBJ_BLOB) {\n+\t\tunsigned long size;\n+\t\tif (oid_object_info(the_repository, oid, &size) < 0)\n+\t\t\tdie(_(\"could not get object info about '%s'\"),\n+\t\t\t    oid_to_hex(oid));\n+\t\tif (padded)\n+\t\t\tstrbuf_addf(line, \"%7\"PRIuMAX, (uintmax_t)size);\n+\t\telse\n+\t\t\tstrbuf_addf(line, \"%\"PRIuMAX, (uintmax_t)size);\n+\t} else if (padded) {\n+\t\tstrbuf_addf(line, \"%7s\", \"-\");\n+\t} else {\n+\t\tstrbuf_addstr(line, \"-\");\n+\t}\n+}\n struct show_index_data {\n \tconst char *pathname;\n \tstruct index_state *istate;\n@@ -272,6 +293,12 @@ static size_t expand_show_index(struct strbuf *sb, const char *start,\n \t\tstrbuf_addf(sb, \"%06o\", data->ce->ce_mode);\n \telse if (skip_prefix(start, \"(objectname)\", &p))\n \t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n+\telse if (skip_prefix(start, \"(objecttype)\", &p))\n+\t\tstrbuf_addstr(sb, type_name(object_type(data->ce->ce_mode)));\n+\telse if (skip_prefix(start, \"(objectsize:padded)\", &p))\n+\t\texpand_objectsize(sb, &data->ce->oid, object_type(data->ce->ce_mode), 1);\n+\telse if (skip_prefix(start, \"(objectsize)\", &p))\n+\t\texpand_objectsize(sb, &data->ce->oid, object_type(data->ce->ce_mode), 0);\n \telse if (skip_prefix(start, \"(stage)\", &p))\n \t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n \telse if (skip_prefix(start, \"(eolinfo:index)\", &p))\ndiff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh\nindex ef6fb53f7f1..6e6ea0b6f3c 100755\n--- a/t/t3013-ls-files-format.sh\n+++ b/t/t3013-ls-files-format.sh\n@@ -38,6 +38,41 @@ test_expect_success 'git ls-files --format objectname v.s. -s' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'git ls-files --format objecttype' '\n+\tgit ls-files --format=\"%(objectname)\" o1.txt o4.txt o6.txt >objectname &&\n+\tgit cat-file --batch-check=\"%(objecttype)\" >expect <objectname &&\n+\tgit ls-files --format=\"%(objecttype)\" o1.txt o4.txt o6.txt >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format objectsize' '\n+\tcat>expect <<-\\EOF &&\n+26\n+29\n+27\n+26\n+-\n+26\n+\tEOF\n+\tgit ls-files --format=\"%(objectsize)\" >actual &&\n+\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format objectsize:padded' '\n+\tcat>expect <<-\\EOF &&\n+     26\n+     29\n+     27\n+     26\n+      -\n+     26\n+\tEOF\n+\tgit ls-files --format=\"%(objectsize:padded)\" >actual &&\n+\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'git ls-files --format v.s. --eol' '\n \tgit ls-files --eol >tmp &&\n \tsed -e \"s/\t/ /g\" -e \"s/  */ /g\" tmp >expect 2>err &&\n\nbase-commit: 5bc069e383539824fd3a0d897100d44bbe1f8a24\n-- \ngitgitgadget\n"}]}