Merge branch 'tf/maint-doc-push'
[git] / builtin / verify-pack.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "run-command.h"
4 #include "parse-options.h"
5
6 #define VERIFY_PACK_VERBOSE 01
7 #define VERIFY_PACK_STAT_ONLY 02
8
9 static int verify_one_pack(const char *path, unsigned int flags)
10 {
11         struct child_process index_pack;
12         const char *argv[] = {"index-pack", NULL, NULL, NULL };
13         struct strbuf arg = STRBUF_INIT;
14         int verbose = flags & VERIFY_PACK_VERBOSE;
15         int stat_only = flags & VERIFY_PACK_STAT_ONLY;
16         int err;
17
18         if (stat_only)
19                 argv[1] = "--verify-stat-only";
20         else if (verbose)
21                 argv[1] = "--verify-stat";
22         else
23                 argv[1] = "--verify";
24
25         /*
26          * In addition to "foo.pack" we accept "foo.idx" and "foo";
27          * normalize these forms to "foo.pack" for "index-pack --verify".
28          */
29         strbuf_addstr(&arg, path);
30         if (strbuf_strip_suffix(&arg, ".idx") ||
31             !ends_with(arg.buf, ".pack"))
32                 strbuf_addstr(&arg, ".pack");
33         argv[2] = arg.buf;
34
35         memset(&index_pack, 0, sizeof(index_pack));
36         index_pack.argv = argv;
37         index_pack.git_cmd = 1;
38
39         err = run_command(&index_pack);
40
41         if (verbose || stat_only) {
42                 if (err)
43                         printf("%s: bad\n", arg.buf);
44                 else {
45                         if (!stat_only)
46                                 printf("%s: ok\n", arg.buf);
47                 }
48         }
49         strbuf_release(&arg);
50
51         return err;
52 }
53
54 static const char * const verify_pack_usage[] = {
55         N_("git verify-pack [-v|--verbose] [-s|--stat-only] <pack>..."),
56         NULL
57 };
58
59 int cmd_verify_pack(int argc, const char **argv, const char *prefix)
60 {
61         int err = 0;
62         unsigned int flags = 0;
63         int i;
64         const struct option verify_pack_options[] = {
65                 OPT_BIT('v', "verbose", &flags, N_("verbose"),
66                         VERIFY_PACK_VERBOSE),
67                 OPT_BIT('s', "stat-only", &flags, N_("show statistics only"),
68                         VERIFY_PACK_STAT_ONLY),
69                 OPT_END()
70         };
71
72         git_config(git_default_config, NULL);
73         argc = parse_options(argc, argv, prefix, verify_pack_options,
74                              verify_pack_usage, 0);
75         if (argc < 1)
76                 usage_with_options(verify_pack_usage, verify_pack_options);
77         for (i = 0; i < argc; i++) {
78                 if (verify_one_pack(argv[i], flags))
79                         err = 1;
80         }
81
82         return err;
83 }