t7064: avoid relying on a specific default branch name
[git] / builtin / verify-pack.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "run-command.h"
5 #include "parse-options.h"
6
7 #define VERIFY_PACK_VERBOSE 01
8 #define VERIFY_PACK_STAT_ONLY 02
9
10 static int verify_one_pack(const char *path, unsigned int flags, const char *hash_algo)
11 {
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;
18
19         strvec_push(argv, "index-pack");
20
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");
27
28         if (hash_algo)
29                 strvec_pushf(argv, "--object-format=%s", hash_algo);
30
31         /*
32          * In addition to "foo.pack" we accept "foo.idx" and "foo";
33          * normalize these forms to "foo.pack" for "index-pack --verify".
34          */
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);
40
41         index_pack.git_cmd = 1;
42
43         err = run_command(&index_pack);
44
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);
51                 }
52         }
53         strbuf_release(&arg);
54
55         return err;
56 }
57
58 static const char * const verify_pack_usage[] = {
59         N_("git verify-pack [-v | --verbose] [-s | --stat-only] <pack>..."),
60         NULL
61 };
62
63 int cmd_verify_pack(int argc, const char **argv, const char *prefix)
64 {
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()
77         };
78
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;
87         }
88
89         return err;
90 }