The second batch
[git.git] / builtin / verify-pack.c
blob011dddd2dc329292c363fbcfb10b71c218ffa034
1 #include "builtin.h"
2 #include "config.h"
3 #include "gettext.h"
4 #include "run-command.h"
5 #include "parse-options.h"
6 #include "strbuf.h"
8 #define VERIFY_PACK_VERBOSE 01
9 #define VERIFY_PACK_STAT_ONLY 02
11 static int verify_one_pack(const char *path, unsigned int flags, const char *hash_algo)
13 struct child_process index_pack = CHILD_PROCESS_INIT;
14 struct strvec *argv = &index_pack.args;
15 struct strbuf arg = STRBUF_INIT;
16 int verbose = flags & VERIFY_PACK_VERBOSE;
17 int stat_only = flags & VERIFY_PACK_STAT_ONLY;
18 int err;
20 strvec_push(argv, "index-pack");
22 if (stat_only)
23 strvec_push(argv, "--verify-stat-only");
24 else if (verbose)
25 strvec_push(argv, "--verify-stat");
26 else
27 strvec_push(argv, "--verify");
29 if (hash_algo)
30 strvec_pushf(argv, "--object-format=%s", hash_algo);
33 * In addition to "foo.pack" we accept "foo.idx" and "foo";
34 * normalize these forms to "foo.pack" for "index-pack --verify".
36 strbuf_addstr(&arg, path);
37 if (strbuf_strip_suffix(&arg, ".idx") ||
38 !ends_with(arg.buf, ".pack"))
39 strbuf_addstr(&arg, ".pack");
40 strvec_push(argv, arg.buf);
42 index_pack.git_cmd = 1;
44 err = run_command(&index_pack);
46 if (verbose || stat_only) {
47 if (err)
48 printf("%s: bad\n", arg.buf);
49 else {
50 if (!stat_only)
51 printf("%s: ok\n", arg.buf);
54 strbuf_release(&arg);
56 return err;
59 static const char * const verify_pack_usage[] = {
60 N_("git verify-pack [-v | --verbose] [-s | --stat-only] [--] <pack>.idx..."),
61 NULL
64 int cmd_verify_pack(int argc, const char **argv, const char *prefix)
66 int err = 0;
67 unsigned int flags = 0;
68 const char *object_format = NULL;
69 int i;
70 const struct option verify_pack_options[] = {
71 OPT_BIT('v', "verbose", &flags, N_("verbose"),
72 VERIFY_PACK_VERBOSE),
73 OPT_BIT('s', "stat-only", &flags, N_("show statistics only"),
74 VERIFY_PACK_STAT_ONLY),
75 OPT_STRING(0, "object-format", &object_format, N_("hash"),
76 N_("specify the hash algorithm to use")),
77 OPT_END()
80 git_config(git_default_config, NULL);
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++) {
86 if (verify_one_pack(argv[i], flags, object_format))
87 err = 1;
90 return err;