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