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

Re: [PATCH 1/3] test-tool: add pack-deltas helper

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Apr 25, 2025, 09:34 UTC
Message-ID
<090ef16f-42a7-8de6-a79e-5a1958e2c103@gmx.de>
In-Reply-To
<aAsQwSfr-YvS2Mvh@pks.im>
Hi Patrick,
On Fri, 25 Apr 2025, Patrick Steinhardt wrote:
Show 25 quoted lines
> On Wed, Apr 23, 2025 at 05:40:02PM +0000, Derrick Stolee via GitGitGadget wrote:
> > diff --git a/t/helper/test-pack-deltas.c b/t/helper/test-pack-deltas.c
> > new file mode 100644
> > index 00000000000..db7d1c3cd1f
> > --- /dev/null
> > +++ b/t/helper/test-pack-deltas.c
> > @@ -0,0 +1,140 @@
> [snip]
> > +int cmd__pack_deltas(int argc, const char **argv)
> > +{
> > +	int N;
> > +	struct hashfile *f;
> > +	struct strbuf line = STRBUF_INIT;
> > +
> > +	if (argc != 2) {
> > +		usage(usage_str);
> > +		return -1;
> > +	}
> > +
> > +	N = atoi(argv[1]);
> 
> Is there a reason why we don't use `parse_options()` here? It might make
> this tool easier to use and extend going forward, and we wouldn't have
> to care about invalid arguments. Right now, we silently accept a
> non-integer argument and do the wrong thing.
I think that `parse_options()` would be overkill here because:
- This is a _mandatory_ argument, not an optional one.
- The required data type is `uint32_t`, and `parse_options()` has no
  support for that.

But you do have a good point in that we may want to validate the data type (even if technically, this is not a user-facing program, it's a test helper that is used under tight control by Git's own test suite).

Consequently, I would suggest this fixup instead:
-- snipsnap --
Subject: [PATCH] fixup! test-tool: add pack-deltas helper

Let's make the command-line parsing a bit more stringent. We _could_ use `parse_options()`, but that would be overkill for a single, non-optional argument. Besides, it would not bring any benefit, as the parsed value needs to fit in the `uint32_t` type, and `parse_options()` has no provision to ensure that.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 t/helper/test-pack-deltas.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/t/helper/test-pack-deltas.c b/t/helper/test-pack-deltas.c
index 4af69bdc05d3..f95d8ee16768 100644
--- a/t/helper/test-pack-deltas.c
+++ b/t/helper/test-pack-deltas.c
@@ -8,11 +8,12 @@
 #include "hex.h"
 #include "pack.h"
 #include "pack-objects.h"
+#include "parse.h"
 #include "setup.h"
 #include "strbuf.h"
 #include "string-list.h"
 
-static const char usage_str[] = "test-tool pack-deltas <n>";
+static const char usage_str[] = "test-tool pack-deltas <nr_entries>";
 
 static unsigned long do_compress(void **pptr, unsigned long size)
 {
@@ -79,7 +80,7 @@ static void write_ref_delta(struct hashfile *f,
 
 int cmd__pack_deltas(int argc, const char **argv)
 {
-	int N;
+	unsigned long n;
 	struct hashfile *f;
 	struct strbuf line = STRBUF_INIT;
 
@@ -88,12 +89,13 @@ int cmd__pack_deltas(int argc, const char **argv)
 		return -1;
 	}
 
-	N = atoi(argv[1]);
+	if (!git_parse_ulong(argv[1], &n) || n != (uint32_t)n)
+		die("invalid number of objects: %s", argv[1]);
 
 	setup_git_directory();
 
 	f = hashfd(1, "<stdout>");
-	write_pack_header(f, N);
+	write_pack_header(f, n);
 
 	/* Read each line from stdin into 'line' */
 	while (strbuf_getline_lf(&line, stdin) != EOF) {
-- 
2.49.0.windows.1
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 9 of 29 in “Fix REF_DELTA chain bug in 'git index-pack'”
  1. 0/3 Fix REF_DELTA chain bug in 'git index-pack'Derrick Stolee via GitGitGadget, Apr 23, 2025
  2. 1/3 test-tool: add pack-deltas helperDerrick Stolee via GitGitGadget, Apr 23, 2025
  3. Junio C HamanoApr 23, 2025
  4. Derrick StoleeApr 23, 2025
  5. Junio C HamanoApr 24, 2025
  6. Derrick StoleeApr 24, 2025
  7. Junio C HamanoApr 24, 2025
  8. Patrick SteinhardtApr 25, 2025
  9. Johannes SchindelinApr 25, 2025
  10. Patrick SteinhardtApr 25, 2025
  11. Johannes SchindelinApr 25, 2025
  12. Junio C HamanoApr 25, 2025
  13. Derrick StoleeApr 28, 2025
  14. Junio C HamanoApr 28, 2025
  15. Derrick StoleeApr 28, 2025
  16. Junio C HamanoApr 28, 2025
  17. 2/3 t5309: create failing test for 'git index-pack'Derrick Stolee via GitGitGadget, Apr 23, 2025
  18. Junio C HamanoApr 23, 2025
  19. 3/3 index-pack: allow revisiting REF_DELTA chainsDerrick Stolee via GitGitGadget, Apr 23, 2025
  20. Junio C HamanoApr 24, 2025
  21. Derrick StoleeApr 25, 2025
  22. 0/3 Fix REF_DELTA chain bug in 'git index-pack'Derrick Stolee via GitGitGadget, Apr 28, 2025
  23. 1/3 test-tool: add pack-deltas helperDerrick Stolee via GitGitGadget, Apr 28, 2025
  24. 2/3 t5309: create failing test for 'git index-pack'Derrick Stolee via GitGitGadget, Apr 28, 2025
  25. 3/3 index-pack: allow revisiting REF_DELTA chainsDerrick Stolee via GitGitGadget, Apr 28, 2025
  26. Taylor BlauMay 7, 2025
  27. Derrick StoleeMay 7, 2025
  28. Junio C HamanoApr 28, 2025
  29. Patrick SteinhardtApr 29, 2025

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.