Git Mailing List Archive mirror
 help / color / mirror / Atom feed
From: "ZheNing Hu via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>,
	"Martin Monperrus" <martin.monperrus@gnieh.org>,
	"ZheNing Hu" <adlternative@gmail.com>,
	"ZheNing Hu" <adlternative@gmail.com>
Subject: [PATCH 2/2] ls-files: add %(objectsize) atom to format option
Date: Sat, 13 May 2023 09:11:39 +0000	[thread overview]
Message-ID: <95f1d7140814cc1598d52a4cfab33bf8aa0bf83c.1683969100.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1533.git.1683969100.gitgitgadget@gmail.com>

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(+)

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

  parent reply	other threads:[~2023-05-13  9:11 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-13  9:11 [PATCH 0/2] ls-files: align format atoms with git ls-tree ZheNing Hu via GitGitGadget
2023-05-13  9:11 ` [PATCH 1/2] ls-files: add %(objecttype) atom to format option ZheNing Hu via GitGitGadget
2023-05-15  5:00   ` Junio C Hamano
2023-05-18 10:02     ` ZheNing Hu
2023-05-13  9:11 ` ZheNing Hu via GitGitGadget [this message]
2023-05-15  5:03   ` [PATCH 2/2] ls-files: add %(objectsize) " Junio C Hamano
2023-05-23  9:00 ` [PATCH v2] ls-files: aligin format atoms wtih ls-tree ZheNing Hu via GitGitGadget

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=95f1d7140814cc1598d52a4cfab33bf8aa0bf83c.1683969100.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=adlternative@gmail.com \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=martin.monperrus@gnieh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).