]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/verify-pack.c
Merge branch 'pb/ref-filter-with-crlf'
[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
e74b606d 10static int verify_one_pack(const char *path, unsigned int flags, const char *hash_algo)
f9253394 11{
d3180279 12 struct child_process index_pack = CHILD_PROCESS_INIT;
e0ad9574 13 struct strvec *argv = &index_pack.args;
3de89c9d 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
e0ad9574 19 strvec_push(argv, "index-pack");
e74b606d 20
3de89c9d 21 if (stat_only)
e0ad9574 22 strvec_push(argv, "--verify-stat-only");
3de89c9d 23 else if (verbose)
e0ad9574 24 strvec_push(argv, "--verify-stat");
3de89c9d 25 else
e0ad9574 26 strvec_push(argv, "--verify");
e74b606d 27
28 if (hash_algo)
e0ad9574 29 strvec_pushf(argv, "--object-format=%s", hash_algo);
fc5fc509 30
f711ab54 31 /*
3de89c9d
JH
32 * In addition to "foo.pack" we accept "foo.idx" and "foo";
33 * normalize these forms to "foo.pack" for "index-pack --verify".
f711ab54 34 */
3de89c9d 35 strbuf_addstr(&arg, path);
d6cd00c7
JK
36 if (strbuf_strip_suffix(&arg, ".idx") ||
37 !ends_with(arg.buf, ".pack"))
38 strbuf_addstr(&arg, ".pack");
e0ad9574 39 strvec_push(argv, arg.buf);
fc5fc509 40
3de89c9d 41 index_pack.git_cmd = 1;
77d3ecee 42
3de89c9d 43 err = run_command(&index_pack);
6c4f3ec9
JH
44
45 if (verbose || stat_only) {
77d3ecee 46 if (err)
3de89c9d 47 printf("%s: bad\n", arg.buf);
77d3ecee 48 else {
6c4f3ec9 49 if (!stat_only)
3de89c9d 50 printf("%s: ok\n", arg.buf);
77d3ecee
NP
51 }
52 }
3de89c9d 53 strbuf_release(&arg);
d0d619c8
RS
54
55 return err;
f9253394
JH
56}
57
c9c3c678 58static const char * const verify_pack_usage[] = {
9c9b4f2f 59 N_("git verify-pack [-v | --verbose] [-s | --stat-only] <pack>..."),
c9c3c678
SB
60 NULL
61};
f3bf9224 62
2e3ed670 63int cmd_verify_pack(int argc, const char **argv, const char *prefix)
f9253394 64{
0eaf22f4 65 int err = 0;
6c4f3ec9 66 unsigned int flags = 0;
e74b606d 67 const char *object_format = NULL;
c9c3c678
SB
68 int i;
69 const struct option verify_pack_options[] = {
0a245e24 70 OPT_BIT('v', "verbose", &flags, N_("verbose"),
6c4f3ec9 71 VERIFY_PACK_VERBOSE),
0a245e24 72 OPT_BIT('s', "stat-only", &flags, N_("show statistics only"),
6c4f3ec9 73 VERIFY_PACK_STAT_ONLY),
e74b606d 74 OPT_STRING(0, "object-format", &object_format, N_("hash"),
75 N_("specify the hash algorithm to use")),
c9c3c678
SB
76 OPT_END()
77 };
f9253394 78
ef90d6d4 79 git_config(git_default_config, NULL);
c9c3c678
SB
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++) {
e74b606d 85 if (verify_one_pack(argv[i], flags, object_format))
c9c3c678 86 err = 1;
f9253394 87 }
6f05b57d 88
0eaf22f4 89 return err;
f9253394 90}