@matheusbsbs / matheus-bagliano / commits / e74b606d47
Fork 0

builtin/verify-pack: implement an --object-format option

A recently added test in t5702 started using git verify-pack outside of a repository. While this poses no problems with SHA-1, with SHA-256 we implicitly rely on the setup of the repository to initialize our hash algorithm settings. Since we're not in a repository here, we need to provide git verify-pack help to set things up properly. git index-pack already knows an --object-format option, so let's accept one as well and pass it down to our git index-pack invocation. Since we're now dynamically adjusting the elements in argv, let's switch to using struct argv_array to manage them. Finally, let's make t5702 pass the proper argument on down to its git verify-pack caller. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Reviewed-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Jul 29, 2020 at 23:14 UTC e74b606d47ff65de497b06ac6c8ace6761cceea1
2 files changed +16 -9
builtin/verify-pack.c
+15 -8
@@ -7,21 +7,26 @@
7 #define VERIFY_PACK_VERBOSE 01
8 #define VERIFY_PACK_STAT_ONLY 02
9
10 -static int verify_one_pack(const char *path, unsigned int flags)
10 +static int verify_one_pack(const char *path, unsigned int flags, const char *hash_algo)
11 {
12 struct child_process index_pack = CHILD_PROCESS_INIT;
13 - const char *argv[] = {"index-pack", NULL, NULL, NULL };
13 + struct argv_array *argv = &index_pack.args;
14 struct strbuf arg = STRBUF_INIT;
15 int verbose = flags & VERIFY_PACK_VERBOSE;
16 int stat_only = flags & VERIFY_PACK_STAT_ONLY;
17 int err;
18
19 + argv_array_push(argv, "index-pack");
20 +
21 if (stat_only)
20 - argv[1] = "--verify-stat-only";
22 + argv_array_push(argv, "--verify-stat-only");
23 else if (verbose)
22 - argv[1] = "--verify-stat";
24 + argv_array_push(argv, "--verify-stat");
25 else
24 - argv[1] = "--verify";
26 + argv_array_push(argv, "--verify");
27 +
28 + if (hash_algo)
29 + argv_array_pushf(argv, "--object-format=%s", hash_algo);
30
31 /*
32 * In addition to "foo.pack" we accept "foo.idx" and "foo";
@@ -31,9 +36,8 @@ static int verify_one_pack(const char *path, unsigned int flags)
36 if (strbuf_strip_suffix(&arg, ".idx") ||
37 !ends_with(arg.buf, ".pack"))
38 strbuf_addstr(&arg, ".pack");
34 - argv[2] = arg.buf;
39 + argv_array_push(argv, arg.buf);
40
36 - index_pack.argv = argv;
41 index_pack.git_cmd = 1;
42
43 err = run_command(&index_pack);
@@ -60,12 +64,15 @@ int cmd_verify_pack(int argc, const char **argv, const char *prefix)
64 {
65 int err = 0;
66 unsigned int flags = 0;
67 + const char *object_format = NULL;
68 int i;
69 const struct option verify_pack_options[] = {
70 OPT_BIT('v', "verbose", &flags, N_("verbose"),
71 VERIFY_PACK_VERBOSE),
72 OPT_BIT('s', "stat-only", &flags, N_("show statistics only"),
73 VERIFY_PACK_STAT_ONLY),
74 + OPT_STRING(0, "object-format", &object_format, N_("hash"),
75 + N_("specify the hash algorithm to use")),
76 OPT_END()
77 };
78
@@ -75,7 +82,7 @@ int cmd_verify_pack(int argc, const char **argv, const char *prefix)
82 if (argc < 1)
83 usage_with_options(verify_pack_usage, verify_pack_options);
84 for (i = 0; i < argc; i++) {
78 - if (verify_one_pack(argv[i], flags))
85 + if (verify_one_pack(argv[i], flags, object_format))
86 err = 1;
87 }
88
t/t5702-protocol-v2.sh
+1 -1
@@ -829,7 +829,7 @@ test_expect_success 'part of packfile response provided as URI' '
829 # Ensure that my-blob and other-blob are in separate packfiles.
830 for idx in http_child/.git/objects/pack/*.idx
831 do
832 - git verify-pack --verbose $idx >out &&
832 + git verify-pack --object-format=$(test_oid algo) --verbose $idx >out &&
833 {
834 grep "^[0-9a-f]\{16,\} " out || :
835 } >out.objectlist &&