t/unit-tests: fix typos
[git/gitster.git] / builtin / verify-pack.c
blob34e4ed715f3e8e1eba31bb88b048b274b7ac1703
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "config.h"
4 #include "gettext.h"
5 #include "run-command.h"
6 #include "parse-options.h"
7 #include "strbuf.h"
9 #define VERIFY_PACK_VERBOSE 01
10 #define VERIFY_PACK_STAT_ONLY 02
12 static int verify_one_pack(const char *path, unsigned int flags, const char *hash_algo)
14 struct child_process index_pack = CHILD_PROCESS_INIT;
15 struct strvec *argv = &index_pack.args;
16 struct strbuf arg = STRBUF_INIT;
17 int verbose = flags & VERIFY_PACK_VERBOSE;
18 int stat_only = flags & VERIFY_PACK_STAT_ONLY;
19 int err;
21 strvec_push(argv, "index-pack");
23 if (stat_only)
24 strvec_push(argv, "--verify-stat-only");
25 else if (verbose)
26 strvec_push(argv, "--verify-stat");
27 else
28 strvec_push(argv, "--verify");
30 if (hash_algo)
31 strvec_pushf(argv, "--object-format=%s", hash_algo);
34 * In addition to "foo.pack" we accept "foo.idx" and "foo";
35 * normalize these forms to "foo.pack" for "index-pack --verify".
37 strbuf_addstr(&arg, path);
38 if (strbuf_strip_suffix(&arg, ".idx") ||
39 !ends_with(arg.buf, ".pack"))
40 strbuf_addstr(&arg, ".pack");
41 strvec_push(argv, arg.buf);
43 index_pack.git_cmd = 1;
45 err = run_command(&index_pack);
47 if (verbose || stat_only) {
48 if (err)
49 printf("%s: bad\n", arg.buf);
50 else {
51 if (!stat_only)
52 printf("%s: ok\n", arg.buf);
55 strbuf_release(&arg);
57 return err;
60 static const char * const verify_pack_usage[] = {
61 N_("git verify-pack [-v | --verbose] [-s | --stat-only] [--] <pack>.idx..."),
62 NULL
65 int cmd_verify_pack(int argc,
66 const char **argv,
67 const char *prefix,
68 struct repository *repo UNUSED)
70 int err = 0;
71 unsigned int flags = 0;
72 const char *object_format = NULL;
73 int i;
74 const struct option verify_pack_options[] = {
75 OPT_BIT('v', "verbose", &flags, N_("verbose"),
76 VERIFY_PACK_VERBOSE),
77 OPT_BIT('s', "stat-only", &flags, N_("show statistics only"),
78 VERIFY_PACK_STAT_ONLY),
79 OPT_STRING(0, "object-format", &object_format, N_("hash"),
80 N_("specify the hash algorithm to use")),
81 OPT_END()
84 git_config(git_default_config, NULL);
85 argc = parse_options(argc, argv, prefix, verify_pack_options,
86 verify_pack_usage, 0);
87 if (argc < 1)
88 usage_with_options(verify_pack_usage, verify_pack_options);
89 for (i = 0; i < argc; i++) {
90 if (verify_one_pack(argv[i], flags, object_format))
91 err = 1;
94 return err;