]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/verify-pack.c
Sync with maint
[thirdparty/git.git] / builtin / verify-pack.c
CommitLineData
2e3ed670 1#include "builtin.h"
f9253394 2#include "cache.h"
b2141fc1 3#include "config.h"
3de89c9d 4#include "run-command.h"
c9c3c678 5#include "parse-options.h"
77d3ecee 6
6c4f3ec9
JH
7#define VERIFY_PACK_VERBOSE 01
8#define VERIFY_PACK_STAT_ONLY 02
9
6c4f3ec9 10static int verify_one_pack(const char *path, unsigned int flags)
f9253394 11{
d3180279 12 struct child_process index_pack = CHILD_PROCESS_INIT;
3de89c9d
JH
13 const char *argv[] = {"index-pack", NULL, NULL, NULL };
14 struct strbuf arg = STRBUF_INIT;
6c4f3ec9
JH
15 int verbose = flags & VERIFY_PACK_VERBOSE;
16 int stat_only = flags & VERIFY_PACK_STAT_ONLY;
d0d619c8 17 int err;
ae9c86f2 18
3de89c9d
JH
19 if (stat_only)
20 argv[1] = "--verify-stat-only";
21 else if (verbose)
22 argv[1] = "--verify-stat";
23 else
24 argv[1] = "--verify";
fc5fc509 25
f711ab54 26 /*
3de89c9d
JH
27 * In addition to "foo.pack" we accept "foo.idx" and "foo";
28 * normalize these forms to "foo.pack" for "index-pack --verify".
f711ab54 29 */
3de89c9d 30 strbuf_addstr(&arg, path);
d6cd00c7
JK
31 if (strbuf_strip_suffix(&arg, ".idx") ||
32 !ends_with(arg.buf, ".pack"))
33 strbuf_addstr(&arg, ".pack");
3de89c9d 34 argv[2] = arg.buf;
fc5fc509 35
3de89c9d
JH
36 index_pack.argv = argv;
37 index_pack.git_cmd = 1;
77d3ecee 38
3de89c9d 39 err = run_command(&index_pack);
6c4f3ec9
JH
40
41 if (verbose || stat_only) {
77d3ecee 42 if (err)
3de89c9d 43 printf("%s: bad\n", arg.buf);
77d3ecee 44 else {
6c4f3ec9 45 if (!stat_only)
3de89c9d 46 printf("%s: ok\n", arg.buf);
77d3ecee
NP
47 }
48 }
3de89c9d 49 strbuf_release(&arg);
d0d619c8
RS
50
51 return err;
f9253394
JH
52}
53
c9c3c678 54static const char * const verify_pack_usage[] = {
9c9b4f2f 55 N_("git verify-pack [-v | --verbose] [-s | --stat-only] <pack>..."),
c9c3c678
SB
56 NULL
57};
f3bf9224 58
2e3ed670 59int cmd_verify_pack(int argc, const char **argv, const char *prefix)
f9253394 60{
0eaf22f4 61 int err = 0;
6c4f3ec9 62 unsigned int flags = 0;
c9c3c678
SB
63 int i;
64 const struct option verify_pack_options[] = {
0a245e24 65 OPT_BIT('v', "verbose", &flags, N_("verbose"),
6c4f3ec9 66 VERIFY_PACK_VERBOSE),
0a245e24 67 OPT_BIT('s', "stat-only", &flags, N_("show statistics only"),
6c4f3ec9 68 VERIFY_PACK_STAT_ONLY),
c9c3c678
SB
69 OPT_END()
70 };
f9253394 71
ef90d6d4 72 git_config(git_default_config, NULL);
c9c3c678
SB
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++) {
6c4f3ec9 78 if (verify_one_pack(argv[i], flags))
c9c3c678 79 err = 1;
f9253394 80 }
6f05b57d 81
0eaf22f4 82 return err;
f9253394 83}