Rene Scharfe | 2e3ed67 | 2006-08-10 17:02:38 +0200 | [diff] [blame] | 1 | #include "builtin.h" |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 2 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 3 | #include "config.h" |
Junio C Hamano | 3de89c9 | 2011-06-03 15:32:17 -0700 | [diff] [blame] | 4 | #include "run-command.h" |
Stephen Boyd | c9c3c67 | 2009-07-07 22:15:40 -0700 | [diff] [blame] | 5 | #include "parse-options.h" |
Nicolas Pitre | 77d3ece | 2008-06-24 23:18:17 -0400 | [diff] [blame] | 6 | |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 7 | #define VERIFY_PACK_VERBOSE 01 |
| 8 | #define VERIFY_PACK_STAT_ONLY 02 |
| 9 | |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 10 | static int verify_one_pack(const char *path, unsigned int flags) |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 11 | { |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 12 | struct child_process index_pack = CHILD_PROCESS_INIT; |
Junio C Hamano | 3de89c9 | 2011-06-03 15:32:17 -0700 | [diff] [blame] | 13 | const char *argv[] = {"index-pack", NULL, NULL, NULL }; |
| 14 | struct strbuf arg = STRBUF_INIT; |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 15 | int verbose = flags & VERIFY_PACK_VERBOSE; |
| 16 | int stat_only = flags & VERIFY_PACK_STAT_ONLY; |
Rene Scharfe | d0d619c | 2006-08-10 17:02:35 +0200 | [diff] [blame] | 17 | int err; |
Rene Scharfe | ae9c86f | 2006-08-10 17:02:32 +0200 | [diff] [blame] | 18 | |
Junio C Hamano | 3de89c9 | 2011-06-03 15:32:17 -0700 | [diff] [blame] | 19 | if (stat_only) |
| 20 | argv[1] = "--verify-stat-only"; |
| 21 | else if (verbose) |
| 22 | argv[1] = "--verify-stat"; |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 23 | else |
Junio C Hamano | 3de89c9 | 2011-06-03 15:32:17 -0700 | [diff] [blame] | 24 | argv[1] = "--verify"; |
| 25 | |
| 26 | /* |
| 27 | * In addition to "foo.pack" we accept "foo.idx" and "foo"; |
| 28 | * normalize these forms to "foo.pack" for "index-pack --verify". |
| 29 | */ |
| 30 | strbuf_addstr(&arg, path); |
Jeff King | d6cd00c | 2014-06-30 13:02:05 -0400 | [diff] [blame] | 31 | if (strbuf_strip_suffix(&arg, ".idx") || |
| 32 | !ends_with(arg.buf, ".pack")) |
| 33 | strbuf_addstr(&arg, ".pack"); |
Junio C Hamano | 3de89c9 | 2011-06-03 15:32:17 -0700 | [diff] [blame] | 34 | argv[2] = arg.buf; |
| 35 | |
Junio C Hamano | 3de89c9 | 2011-06-03 15:32:17 -0700 | [diff] [blame] | 36 | index_pack.argv = argv; |
| 37 | index_pack.git_cmd = 1; |
| 38 | |
| 39 | err = run_command(&index_pack); |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 40 | |
| 41 | if (verbose || stat_only) { |
Nicolas Pitre | 77d3ece | 2008-06-24 23:18:17 -0400 | [diff] [blame] | 42 | if (err) |
Junio C Hamano | 3de89c9 | 2011-06-03 15:32:17 -0700 | [diff] [blame] | 43 | printf("%s: bad\n", arg.buf); |
Nicolas Pitre | 77d3ece | 2008-06-24 23:18:17 -0400 | [diff] [blame] | 44 | else { |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 45 | if (!stat_only) |
Junio C Hamano | 3de89c9 | 2011-06-03 15:32:17 -0700 | [diff] [blame] | 46 | printf("%s: ok\n", arg.buf); |
Nicolas Pitre | 77d3ece | 2008-06-24 23:18:17 -0400 | [diff] [blame] | 47 | } |
| 48 | } |
Junio C Hamano | 3de89c9 | 2011-06-03 15:32:17 -0700 | [diff] [blame] | 49 | strbuf_release(&arg); |
Rene Scharfe | d0d619c | 2006-08-10 17:02:35 +0200 | [diff] [blame] | 50 | |
| 51 | return err; |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Stephen Boyd | c9c3c67 | 2009-07-07 22:15:40 -0700 | [diff] [blame] | 54 | static const char * const verify_pack_usage[] = { |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 55 | N_("git verify-pack [-v | --verbose] [-s | --stat-only] <pack>..."), |
Stephen Boyd | c9c3c67 | 2009-07-07 22:15:40 -0700 | [diff] [blame] | 56 | NULL |
| 57 | }; |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 58 | |
Rene Scharfe | 2e3ed67 | 2006-08-10 17:02:38 +0200 | [diff] [blame] | 59 | int cmd_verify_pack(int argc, const char **argv, const char *prefix) |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 60 | { |
Rene Scharfe | 0eaf22f | 2006-08-10 17:02:37 +0200 | [diff] [blame] | 61 | int err = 0; |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 62 | unsigned int flags = 0; |
Stephen Boyd | c9c3c67 | 2009-07-07 22:15:40 -0700 | [diff] [blame] | 63 | int i; |
| 64 | const struct option verify_pack_options[] = { |
Nguyễn Thái Ngọc Duy | 0a245e2 | 2012-08-20 19:32:51 +0700 | [diff] [blame] | 65 | OPT_BIT('v', "verbose", &flags, N_("verbose"), |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 66 | VERIFY_PACK_VERBOSE), |
Nguyễn Thái Ngọc Duy | 0a245e2 | 2012-08-20 19:32:51 +0700 | [diff] [blame] | 67 | OPT_BIT('s', "stat-only", &flags, N_("show statistics only"), |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 68 | VERIFY_PACK_STAT_ONLY), |
Stephen Boyd | c9c3c67 | 2009-07-07 22:15:40 -0700 | [diff] [blame] | 69 | OPT_END() |
| 70 | }; |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 71 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 72 | git_config(git_default_config, NULL); |
Stephen Boyd | c9c3c67 | 2009-07-07 22:15:40 -0700 | [diff] [blame] | 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++) { |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 78 | if (verify_one_pack(argv[i], flags)) |
Stephen Boyd | c9c3c67 | 2009-07-07 22:15:40 -0700 | [diff] [blame] | 79 | err = 1; |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 80 | } |
Rene Scharfe | 6f05b57 | 2006-08-10 17:02:31 +0200 | [diff] [blame] | 81 | |
Rene Scharfe | 0eaf22f | 2006-08-10 17:02:37 +0200 | [diff] [blame] | 82 | return err; |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 83 | } |