blob: 66cd2df0f878d362a86e8dbc3fcd49b0c2f64c0c [file] [log] [blame]
Rene Scharfe2e3ed672006-08-10 17:02:38 +02001#include "builtin.h"
Junio C Hamanof9253392005-06-29 02:51:27 -07002#include "cache.h"
Junio C Hamano3de89c92011-06-03 15:32:17 -07003#include "run-command.h"
Stephen Boydc9c3c672009-07-07 22:15:40 -07004#include "parse-options.h"
Nicolas Pitre77d3ece2008-06-24 23:18:17 -04005
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -07006#define VERIFY_PACK_VERBOSE 01
7#define VERIFY_PACK_STAT_ONLY 02
8
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -07009static int verify_one_pack(const char *path, unsigned int flags)
Junio C Hamanof9253392005-06-29 02:51:27 -070010{
Junio C Hamano3de89c92011-06-03 15:32:17 -070011 struct child_process index_pack;
12 const char *argv[] = {"index-pack", NULL, NULL, NULL };
13 struct strbuf arg = STRBUF_INIT;
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -070014 int verbose = flags & VERIFY_PACK_VERBOSE;
15 int stat_only = flags & VERIFY_PACK_STAT_ONLY;
Rene Scharfed0d619c2006-08-10 17:02:35 +020016 int err;
Rene Scharfeae9c86f2006-08-10 17:02:32 +020017
Junio C Hamano3de89c92011-06-03 15:32:17 -070018 if (stat_only)
19 argv[1] = "--verify-stat-only";
20 else if (verbose)
21 argv[1] = "--verify-stat";
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -070022 else
Junio C Hamano3de89c92011-06-03 15:32:17 -070023 argv[1] = "--verify";
24
25 /*
26 * In addition to "foo.pack" we accept "foo.idx" and "foo";
27 * normalize these forms to "foo.pack" for "index-pack --verify".
28 */
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;
35
36 memset(&index_pack, 0, sizeof(index_pack));
37 index_pack.argv = argv;
38 index_pack.git_cmd = 1;
39
40 err = run_command(&index_pack);
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -070041
42 if (verbose || stat_only) {
Nicolas Pitre77d3ece2008-06-24 23:18:17 -040043 if (err)
Junio C Hamano3de89c92011-06-03 15:32:17 -070044 printf("%s: bad\n", arg.buf);
Nicolas Pitre77d3ece2008-06-24 23:18:17 -040045 else {
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -070046 if (!stat_only)
Junio C Hamano3de89c92011-06-03 15:32:17 -070047 printf("%s: ok\n", arg.buf);
Nicolas Pitre77d3ece2008-06-24 23:18:17 -040048 }
49 }
Junio C Hamano3de89c92011-06-03 15:32:17 -070050 strbuf_release(&arg);
Rene Scharfed0d619c2006-08-10 17:02:35 +020051
52 return err;
Junio C Hamanof9253392005-06-29 02:51:27 -070053}
54
Stephen Boydc9c3c672009-07-07 22:15:40 -070055static const char * const verify_pack_usage[] = {
Nguyễn Thái Ngọc Duy0a245e22012-08-20 19:32:51 +070056 N_("git verify-pack [-v|--verbose] [-s|--stat-only] <pack>..."),
Stephen Boydc9c3c672009-07-07 22:15:40 -070057 NULL
58};
Junio C Hamanof3bf9222005-06-30 17:15:39 -070059
Rene Scharfe2e3ed672006-08-10 17:02:38 +020060int cmd_verify_pack(int argc, const char **argv, const char *prefix)
Junio C Hamanof9253392005-06-29 02:51:27 -070061{
Rene Scharfe0eaf22f2006-08-10 17:02:37 +020062 int err = 0;
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -070063 unsigned int flags = 0;
Stephen Boydc9c3c672009-07-07 22:15:40 -070064 int i;
65 const struct option verify_pack_options[] = {
Nguyễn Thái Ngọc Duy0a245e22012-08-20 19:32:51 +070066 OPT_BIT('v', "verbose", &flags, N_("verbose"),
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -070067 VERIFY_PACK_VERBOSE),
Nguyễn Thái Ngọc Duy0a245e22012-08-20 19:32:51 +070068 OPT_BIT('s', "stat-only", &flags, N_("show statistics only"),
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -070069 VERIFY_PACK_STAT_ONLY),
Stephen Boydc9c3c672009-07-07 22:15:40 -070070 OPT_END()
71 };
Junio C Hamanof9253392005-06-29 02:51:27 -070072
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010073 git_config(git_default_config, NULL);
Stephen Boydc9c3c672009-07-07 22:15:40 -070074 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++) {
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -070079 if (verify_one_pack(argv[i], flags))
Stephen Boydc9c3c672009-07-07 22:15:40 -070080 err = 1;
Junio C Hamanof9253392005-06-29 02:51:27 -070081 }
Rene Scharfe6f05b572006-08-10 17:02:31 +020082
Rene Scharfe0eaf22f2006-08-10 17:02:37 +020083 return err;
Junio C Hamanof9253392005-06-29 02:51:27 -070084}