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