git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2] count-objects: add human-readable size option

From
Marcus Griep <marcus@griep.us>
Date
Aug 14, 2008, 04:21 UTC
Message-ID
<1218687684-11671-1-git-send-email-marcus@griep.us>
In-Reply-To
<1218657910-22096-1-git-send-email-marcus@griep.us>

Adds a human readable size option to the verbose output of count-objects for loose and pack object size totals.

Updates documentation to match.
Signed-off-by: Marcus Griep <marcus@griep.us>
---
 I thought that this would be a nice addition to 'git count-objects'.
 This patch depends upon my earlier patch to count-objects, but this
 patch should be easily separable from its dependency should that one
 not be accepted.
 Documentation/git-count-objects.txt |   13 +++++++---
 builtin-count-objects.c             |   41 +++++++++++++++++++++++++++++++---
 2 files changed, 46 insertions(+), 8 deletions(-)
diff --git a/Documentation/git-count-objects.txt b/Documentation/git-count-objects.txt
index 75a8da1..291bc5e 100644
--- a/Documentation/git-count-objects.txt
+++ b/Documentation/git-count-objects.txt
@@ -7,7 +7,7 @@ git-count-objects - Count unpacked number of objects and their disk consumption
 
 SYNOPSIS
 --------
-'git count-objects' [-v]
+'git count-objects' [-v [-H]]
 
 DESCRIPTION
 -----------
@@ -21,9 +21,14 @@ OPTIONS
 --verbose::
 	In addition to the number of loose objects and disk
 	space consumed, it reports the number of in-pack
-	objects, number of packs, and number of objects that can be
-	removed by running `git prune-packed`.
-
+	objects, number of packs, disk space consumed by those packs
+	and number of objects that can be removed by running
+	`git prune-packed`.
+
+-H::
+--human-sizes::
+	Displays sizes reported by `--verbose` in a more
+	human-readable format. (e.g. 22M or 1.5G)
 
 Author
 ------
diff --git a/builtin-count-objects.c b/builtin-count-objects.c
index 249040b..1eb73be 100644
--- a/builtin-count-objects.c
+++ b/builtin-count-objects.c
@@ -67,13 +67,30 @@ static void count_objects(DIR *d, char *path, int len, int verbose,
 }
 
 static char const * const count_objects_usage[] = {
-	"git count-objects [-v]",
+	"git count-objects [-v [-H]]",
 	NULL
 };
 
+void human_readable_size(char *buf, int buf_size, double size /* in bytes */)
+{
+	char human_readable_prefixes[10] = "BKMGTPEZY";
+	if (buf_size < 5)
+		die("insufficient buffer size");
+	int i = 0;
+	for (; i < 8 && size >= 1000 ; ++i, size = size / 1024)
+		;
+	if (size >= 1000)
+		die("size greater than 999Y");
+	sprintf(buf, "%.*f%c",
+		size < 10 ? 1 : 0,
+		size,
+		human_readable_prefixes[i]
+		);
+}
+
 int cmd_count_objects(int argc, const char **argv, const char *prefix)
 {
-	int i, verbose = 0;
+	int i, verbose = 0, human_readable = 0;
 	const char *objdir = get_object_directory();
 	int len = strlen(objdir);
 	char *path = xmalloc(len + 50);
@@ -81,6 +98,8 @@ int cmd_count_objects(int argc, const char **argv, const char *prefix)
 	unsigned long loose_size = 0;
 	struct option opts[] = {
 		OPT__VERBOSE(&verbose),
+		OPT_BOOLEAN('H', "--human-sizes", &human_readable,
+			"displays sizes in human readable format"),
 		OPT_END(),
 	};
 
@@ -117,10 +136,24 @@ int cmd_count_objects(int argc, const char **argv, const char *prefix)
 			num_pack++;
 		}
 		printf("count: %lu\n", loose);
-		printf("size: %lu\n", loose_size / 2);
+		printf("size: ");
+		if (human_readable) {
+			char buf[sizeof("999Y")];
+			human_readable_size(buf, sizeof(buf), loose_size * 512);
+			printf("%s\n", buf);
+		}
+		else
+			printf("%lu\n", loose_size / 2);
 		printf("in-pack: %lu\n", packed);
 		printf("packs: %lu\n", num_pack);
-		printf("size-pack: %lu\n", size_pack / 1024);
+		printf("size-pack: ");
+		if (human_readable) {
+			char buf[sizeof("999Y")];
+			human_readable_size(buf, sizeof(buf), size_pack);
+			printf("%s\n", buf);
+		}
+		else
+			printf("%lu\n", size_pack / 1024);
 		printf("prune-packable: %lu\n", packed_loose);
 		printf("garbage: %lu\n", garbage);
 	}
-- 
1.6.0.rc2.6.g8eda3
Previous: Marcus GriepNext: Shawn O. Pearce
Message 2 of 18 in “count-objects: Add total pack size to verbose output”
  1. count-objects: Add total pack size to verbose outputMarcus Griep, Aug 13, 2008
  2. count-objects: add human-readable size optionMarcus Griep, Aug 14, 2008
  3. Shawn O. PearceAug 14, 2008
  4. Marcus GriepAug 14, 2008
  5. Junio C HamanoAug 14, 2008
  6. Johannes SchindelinAug 14, 2008
  7. Marcus GriepAug 14, 2008
  8. Alex RiesenAug 14, 2008
  9. Marcus GriepAug 14, 2008
  10. Alex RiesenAug 14, 2008
  11. Pierre HabouzitAug 15, 2008
  12. Alex RiesenAug 18, 2008
  13. Johannes SixtAug 14, 2008
  14. Marcus GriepAug 14, 2008
  15. Petr BaudisAug 14, 2008
  16. Marcus GriepAug 14, 2008
  17. Petr BaudisAug 14, 2008
  18. Marcus GriepAug 14, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.