blob: 011dddd2dc329292c363fbcfb10b71c218ffa034 [file] [log] [blame]
Rene Scharfe2e3ed672006-08-10 17:02:38 +02001#include "builtin.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00003#include "gettext.h"
Junio C Hamano3de89c92011-06-03 15:32:17 -07004#include "run-command.h"
Stephen Boydc9c3c672009-07-07 22:15:40 -07005#include "parse-options.h"
Elijah Newren88e4e182023-05-16 06:34:02 +00006#include "strbuf.h"
Nicolas Pitre77d3ece2008-06-24 23:18:17 -04007
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -07008#define VERIFY_PACK_VERBOSE 01
9#define VERIFY_PACK_STAT_ONLY 02
10
brian m. carlsone74b6062020-07-29 23:14:19 +000011static int verify_one_pack(const char *path, unsigned int flags, const char *hash_algo)
Junio C Hamanof9253392005-06-29 02:51:27 -070012{
René Scharfed3180272014-08-19 21:09:35 +020013 struct child_process index_pack = CHILD_PROCESS_INIT;
Junio C Hamanoe0ad9572020-08-11 18:04:11 -070014 struct strvec *argv = &index_pack.args;
Junio C Hamano3de89c92011-06-03 15:32:17 -070015 struct strbuf arg = STRBUF_INIT;
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -070016 int verbose = flags & VERIFY_PACK_VERBOSE;
17 int stat_only = flags & VERIFY_PACK_STAT_ONLY;
Rene Scharfed0d619c2006-08-10 17:02:35 +020018 int err;
Rene Scharfeae9c86f2006-08-10 17:02:32 +020019
Junio C Hamanoe0ad9572020-08-11 18:04:11 -070020 strvec_push(argv, "index-pack");
brian m. carlsone74b6062020-07-29 23:14:19 +000021
Junio C Hamano3de89c92011-06-03 15:32:17 -070022 if (stat_only)
Junio C Hamanoe0ad9572020-08-11 18:04:11 -070023 strvec_push(argv, "--verify-stat-only");
Junio C Hamano3de89c92011-06-03 15:32:17 -070024 else if (verbose)
Junio C Hamanoe0ad9572020-08-11 18:04:11 -070025 strvec_push(argv, "--verify-stat");
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -070026 else
Junio C Hamanoe0ad9572020-08-11 18:04:11 -070027 strvec_push(argv, "--verify");
brian m. carlsone74b6062020-07-29 23:14:19 +000028
29 if (hash_algo)
Junio C Hamanoe0ad9572020-08-11 18:04:11 -070030 strvec_pushf(argv, "--object-format=%s", hash_algo);
Junio C Hamano3de89c92011-06-03 15:32:17 -070031
32 /*
33 * In addition to "foo.pack" we accept "foo.idx" and "foo";
34 * normalize these forms to "foo.pack" for "index-pack --verify".
35 */
36 strbuf_addstr(&arg, path);
Jeff Kingd6cd00c2014-06-30 13:02:05 -040037 if (strbuf_strip_suffix(&arg, ".idx") ||
38 !ends_with(arg.buf, ".pack"))
39 strbuf_addstr(&arg, ".pack");
Junio C Hamanoe0ad9572020-08-11 18:04:11 -070040 strvec_push(argv, arg.buf);
Junio C Hamano3de89c92011-06-03 15:32:17 -070041
Junio C Hamano3de89c92011-06-03 15:32:17 -070042 index_pack.git_cmd = 1;
43
44 err = run_command(&index_pack);
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -070045
46 if (verbose || stat_only) {
Nicolas Pitre77d3ece2008-06-24 23:18:17 -040047 if (err)
Junio C Hamano3de89c92011-06-03 15:32:17 -070048 printf("%s: bad\n", arg.buf);
Nicolas Pitre77d3ece2008-06-24 23:18:17 -040049 else {
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -070050 if (!stat_only)
Junio C Hamano3de89c92011-06-03 15:32:17 -070051 printf("%s: ok\n", arg.buf);
Nicolas Pitre77d3ece2008-06-24 23:18:17 -040052 }
53 }
Junio C Hamano3de89c92011-06-03 15:32:17 -070054 strbuf_release(&arg);
Rene Scharfed0d619c2006-08-10 17:02:35 +020055
56 return err;
Junio C Hamanof9253392005-06-29 02:51:27 -070057}
58
Stephen Boydc9c3c672009-07-07 22:15:40 -070059static const char * const verify_pack_usage[] = {
Ævar Arnfjörð Bjarmasonc08cfc32022-10-13 17:39:11 +020060 N_("git verify-pack [-v | --verbose] [-s | --stat-only] [--] <pack>.idx..."),
Stephen Boydc9c3c672009-07-07 22:15:40 -070061 NULL
62};
Junio C Hamanof3bf9222005-06-30 17:15:39 -070063
Rene Scharfe2e3ed672006-08-10 17:02:38 +020064int cmd_verify_pack(int argc, const char **argv, const char *prefix)
Junio C Hamanof9253392005-06-29 02:51:27 -070065{
Rene Scharfe0eaf22f2006-08-10 17:02:37 +020066 int err = 0;
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -070067 unsigned int flags = 0;
brian m. carlsone74b6062020-07-29 23:14:19 +000068 const char *object_format = NULL;
Stephen Boydc9c3c672009-07-07 22:15:40 -070069 int i;
70 const struct option verify_pack_options[] = {
Nguyễn Thái Ngọc Duy0a245e22012-08-20 19:32:51 +070071 OPT_BIT('v', "verbose", &flags, N_("verbose"),
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -070072 VERIFY_PACK_VERBOSE),
Nguyễn Thái Ngọc Duy0a245e22012-08-20 19:32:51 +070073 OPT_BIT('s', "stat-only", &flags, N_("show statistics only"),
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -070074 VERIFY_PACK_STAT_ONLY),
brian m. carlsone74b6062020-07-29 23:14:19 +000075 OPT_STRING(0, "object-format", &object_format, N_("hash"),
76 N_("specify the hash algorithm to use")),
Stephen Boydc9c3c672009-07-07 22:15:40 -070077 OPT_END()
78 };
Junio C Hamanof9253392005-06-29 02:51:27 -070079
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010080 git_config(git_default_config, NULL);
Stephen Boydc9c3c672009-07-07 22:15:40 -070081 argc = parse_options(argc, argv, prefix, verify_pack_options,
82 verify_pack_usage, 0);
83 if (argc < 1)
84 usage_with_options(verify_pack_usage, verify_pack_options);
85 for (i = 0; i < argc; i++) {
brian m. carlsone74b6062020-07-29 23:14:19 +000086 if (verify_one_pack(argv[i], flags, object_format))
Stephen Boydc9c3c672009-07-07 22:15:40 -070087 err = 1;
Junio C Hamanof9253392005-06-29 02:51:27 -070088 }
Rene Scharfe6f05b572006-08-10 17:02:31 +020089
Rene Scharfe0eaf22f2006-08-10 17:02:37 +020090 return err;
Junio C Hamanof9253392005-06-29 02:51:27 -070091}