Merge branch 'hn/reftable-coverity-fixes'
[git/debian.git] / builtin / verify-pack.c
blob05c52135946b77ba6014f69b203d3e979df94ffd
1 #include "builtin.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "run-command.h"
5 #include "parse-options.h"
7 #define VERIFY_PACK_VERBOSE 01
8 #define VERIFY_PACK_STAT_ONLY 02
10 static int verify_one_pack(const char *path, unsigned int flags, const char *hash_algo)
12 struct child_process index_pack = CHILD_PROCESS_INIT;
13 struct strvec *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;
19 strvec_push(argv, "index-pack");
21 if (stat_only)
22 strvec_push(argv, "--verify-stat-only");
23 else if (verbose)
24 strvec_push(argv, "--verify-stat");
25 else
26 strvec_push(argv, "--verify");
28 if (hash_algo)
29 strvec_pushf(argv, "--object-format=%s", hash_algo);
32 * In addition to "foo.pack" we accept "foo.idx" and "foo";
33 * normalize these forms to "foo.pack" for "index-pack --verify".
35 strbuf_addstr(&arg, path);
36 if (strbuf_strip_suffix(&arg, ".idx") ||
37 !ends_with(arg.buf, ".pack"))
38 strbuf_addstr(&arg, ".pack");
39 strvec_push(argv, arg.buf);
41 index_pack.git_cmd = 1;
43 err = run_command(&index_pack);
45 if (verbose || stat_only) {
46 if (err)
47 printf("%s: bad\n", arg.buf);
48 else {
49 if (!stat_only)
50 printf("%s: ok\n", arg.buf);
53 strbuf_release(&arg);
55 return err;
58 static const char * const verify_pack_usage[] = {
59 N_("git verify-pack [-v | --verbose] [-s | --stat-only] <pack>..."),
60 NULL
63 int cmd_verify_pack(int argc, const char **argv, const char *prefix)
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()
79 git_config(git_default_config, NULL);
80 argc = parse_options(argc, argv, prefix, verify_pack_options,
81 verify_pack_usage, 0);
82 if (argc < 1)
83 usage_with_options(verify_pack_usage, verify_pack_options);
84 for (i = 0; i < argc; i++) {
85 if (verify_one_pack(argv[i], flags, object_format))
86 err = 1;
89 return err;