]>
Commit | Line | Data |
---|---|---|
2e3ed670 | 1 | #include "builtin.h" |
b2141fc1 | 2 | #include "config.h" |
f394e093 | 3 | #include "gettext.h" |
3de89c9d | 4 | #include "run-command.h" |
c9c3c678 | 5 | #include "parse-options.h" |
88e4e183 | 6 | #include "strbuf.h" |
77d3ecee | 7 | |
6c4f3ec9 JH |
8 | #define VERIFY_PACK_VERBOSE 01 |
9 | #define VERIFY_PACK_STAT_ONLY 02 | |
10 | ||
e74b606d | 11 | static int verify_one_pack(const char *path, unsigned int flags, const char *hash_algo) |
f9253394 | 12 | { |
d3180279 | 13 | struct child_process index_pack = CHILD_PROCESS_INIT; |
e0ad9574 | 14 | struct strvec *argv = &index_pack.args; |
3de89c9d | 15 | struct strbuf arg = STRBUF_INIT; |
6c4f3ec9 JH |
16 | int verbose = flags & VERIFY_PACK_VERBOSE; |
17 | int stat_only = flags & VERIFY_PACK_STAT_ONLY; | |
d0d619c8 | 18 | int err; |
ae9c86f2 | 19 | |
e0ad9574 | 20 | strvec_push(argv, "index-pack"); |
e74b606d | 21 | |
3de89c9d | 22 | if (stat_only) |
e0ad9574 | 23 | strvec_push(argv, "--verify-stat-only"); |
3de89c9d | 24 | else if (verbose) |
e0ad9574 | 25 | strvec_push(argv, "--verify-stat"); |
3de89c9d | 26 | else |
e0ad9574 | 27 | strvec_push(argv, "--verify"); |
e74b606d | 28 | |
29 | if (hash_algo) | |
e0ad9574 | 30 | strvec_pushf(argv, "--object-format=%s", hash_algo); |
fc5fc509 | 31 | |
f711ab54 | 32 | /* |
3de89c9d JH |
33 | * In addition to "foo.pack" we accept "foo.idx" and "foo"; |
34 | * normalize these forms to "foo.pack" for "index-pack --verify". | |
f711ab54 | 35 | */ |
3de89c9d | 36 | strbuf_addstr(&arg, path); |
d6cd00c7 JK |
37 | if (strbuf_strip_suffix(&arg, ".idx") || |
38 | !ends_with(arg.buf, ".pack")) | |
39 | strbuf_addstr(&arg, ".pack"); | |
e0ad9574 | 40 | strvec_push(argv, arg.buf); |
fc5fc509 | 41 | |
3de89c9d | 42 | index_pack.git_cmd = 1; |
77d3ecee | 43 | |
3de89c9d | 44 | err = run_command(&index_pack); |
6c4f3ec9 JH |
45 | |
46 | if (verbose || stat_only) { | |
77d3ecee | 47 | if (err) |
3de89c9d | 48 | printf("%s: bad\n", arg.buf); |
77d3ecee | 49 | else { |
6c4f3ec9 | 50 | if (!stat_only) |
3de89c9d | 51 | printf("%s: ok\n", arg.buf); |
77d3ecee NP |
52 | } |
53 | } | |
3de89c9d | 54 | strbuf_release(&arg); |
d0d619c8 RS |
55 | |
56 | return err; | |
f9253394 JH |
57 | } |
58 | ||
c9c3c678 | 59 | static const char * const verify_pack_usage[] = { |
c08cfc39 | 60 | N_("git verify-pack [-v | --verbose] [-s | --stat-only] [--] <pack>.idx..."), |
c9c3c678 SB |
61 | NULL |
62 | }; | |
f3bf9224 | 63 | |
2e3ed670 | 64 | int cmd_verify_pack(int argc, const char **argv, const char *prefix) |
f9253394 | 65 | { |
0eaf22f4 | 66 | int err = 0; |
6c4f3ec9 | 67 | unsigned int flags = 0; |
e74b606d | 68 | const char *object_format = NULL; |
c9c3c678 SB |
69 | int i; |
70 | const struct option verify_pack_options[] = { | |
0a245e24 | 71 | OPT_BIT('v', "verbose", &flags, N_("verbose"), |
6c4f3ec9 | 72 | VERIFY_PACK_VERBOSE), |
0a245e24 | 73 | OPT_BIT('s', "stat-only", &flags, N_("show statistics only"), |
6c4f3ec9 | 74 | VERIFY_PACK_STAT_ONLY), |
e74b606d | 75 | OPT_STRING(0, "object-format", &object_format, N_("hash"), |
76 | N_("specify the hash algorithm to use")), | |
c9c3c678 SB |
77 | OPT_END() |
78 | }; | |
f9253394 | 79 | |
ef90d6d4 | 80 | git_config(git_default_config, NULL); |
c9c3c678 SB |
81 | argc = parse_options(argc, argv, prefix, verify_pack_options, |
82 | verify_pack_usage, 0); | |
83 | if (argc < 1) | |
84 | usage_with_options(verify_pack_usage, verify_pack_options); | |
85 | for (i = 0; i < argc; i++) { | |
e74b606d | 86 | if (verify_one_pack(argv[i], flags, object_format)) |
c9c3c678 | 87 | err = 1; |
f9253394 | 88 | } |
6f05b57d | 89 | |
0eaf22f4 | 90 | return err; |
f9253394 | 91 | } |