2006-08-10 15:02:38 +00:00
|
|
|
#include "builtin.h"
|
2017-06-14 18:07:36 +00:00
|
|
|
#include "config.h"
|
2023-03-21 06:25:54 +00:00
|
|
|
#include "gettext.h"
|
2011-06-03 22:32:17 +00:00
|
|
|
#include "run-command.h"
|
2009-07-08 05:15:40 +00:00
|
|
|
#include "parse-options.h"
|
2023-05-16 06:34:02 +00:00
|
|
|
#include "strbuf.h"
|
2008-06-25 03:18:17 +00:00
|
|
|
|
2009-08-07 22:45:30 +00:00
|
|
|
#define VERIFY_PACK_VERBOSE 01
|
|
|
|
#define VERIFY_PACK_STAT_ONLY 02
|
|
|
|
|
2020-07-29 23:14:19 +00:00
|
|
|
static int verify_one_pack(const char *path, unsigned int flags, const char *hash_algo)
|
2005-06-29 09:51:27 +00:00
|
|
|
{
|
2014-08-19 19:09:35 +00:00
|
|
|
struct child_process index_pack = CHILD_PROCESS_INIT;
|
2020-08-12 01:04:11 +00:00
|
|
|
struct strvec *argv = &index_pack.args;
|
2011-06-03 22:32:17 +00:00
|
|
|
struct strbuf arg = STRBUF_INIT;
|
2009-08-07 22:45:30 +00:00
|
|
|
int verbose = flags & VERIFY_PACK_VERBOSE;
|
|
|
|
int stat_only = flags & VERIFY_PACK_STAT_ONLY;
|
2006-08-10 15:02:35 +00:00
|
|
|
int err;
|
2006-08-10 15:02:32 +00:00
|
|
|
|
2020-08-12 01:04:11 +00:00
|
|
|
strvec_push(argv, "index-pack");
|
2020-07-29 23:14:19 +00:00
|
|
|
|
2011-06-03 22:32:17 +00:00
|
|
|
if (stat_only)
|
2020-08-12 01:04:11 +00:00
|
|
|
strvec_push(argv, "--verify-stat-only");
|
2011-06-03 22:32:17 +00:00
|
|
|
else if (verbose)
|
2020-08-12 01:04:11 +00:00
|
|
|
strvec_push(argv, "--verify-stat");
|
2011-06-03 22:32:17 +00:00
|
|
|
else
|
2020-08-12 01:04:11 +00:00
|
|
|
strvec_push(argv, "--verify");
|
2020-07-29 23:14:19 +00:00
|
|
|
|
|
|
|
if (hash_algo)
|
2020-08-12 01:04:11 +00:00
|
|
|
strvec_pushf(argv, "--object-format=%s", hash_algo);
|
2006-08-10 15:02:34 +00:00
|
|
|
|
2006-08-10 15:02:36 +00:00
|
|
|
/*
|
2011-06-03 22:32:17 +00:00
|
|
|
* In addition to "foo.pack" we accept "foo.idx" and "foo";
|
|
|
|
* normalize these forms to "foo.pack" for "index-pack --verify".
|
2006-08-10 15:02:36 +00:00
|
|
|
*/
|
2011-06-03 22:32:17 +00:00
|
|
|
strbuf_addstr(&arg, path);
|
2014-06-30 17:02:05 +00:00
|
|
|
if (strbuf_strip_suffix(&arg, ".idx") ||
|
|
|
|
!ends_with(arg.buf, ".pack"))
|
|
|
|
strbuf_addstr(&arg, ".pack");
|
2020-08-12 01:04:11 +00:00
|
|
|
strvec_push(argv, arg.buf);
|
2006-08-10 15:02:34 +00:00
|
|
|
|
2011-06-03 22:32:17 +00:00
|
|
|
index_pack.git_cmd = 1;
|
2008-06-25 03:18:17 +00:00
|
|
|
|
2011-06-03 22:32:17 +00:00
|
|
|
err = run_command(&index_pack);
|
2009-08-07 22:45:30 +00:00
|
|
|
|
|
|
|
if (verbose || stat_only) {
|
2008-06-25 03:18:17 +00:00
|
|
|
if (err)
|
2011-06-03 22:32:17 +00:00
|
|
|
printf("%s: bad\n", arg.buf);
|
2008-06-25 03:18:17 +00:00
|
|
|
else {
|
2009-08-07 22:45:30 +00:00
|
|
|
if (!stat_only)
|
2011-06-03 22:32:17 +00:00
|
|
|
printf("%s: ok\n", arg.buf);
|
2008-06-25 03:18:17 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-03 22:32:17 +00:00
|
|
|
strbuf_release(&arg);
|
2006-08-10 15:02:35 +00:00
|
|
|
|
|
|
|
return err;
|
2005-06-29 09:51:27 +00:00
|
|
|
}
|
|
|
|
|
2009-07-08 05:15:40 +00:00
|
|
|
static const char * const verify_pack_usage[] = {
|
2022-10-13 15:39:11 +00:00
|
|
|
N_("git verify-pack [-v | --verbose] [-s | --stat-only] [--] <pack>.idx..."),
|
2009-07-08 05:15:40 +00:00
|
|
|
NULL
|
|
|
|
};
|
2005-07-01 00:15:39 +00:00
|
|
|
|
2006-08-10 15:02:38 +00:00
|
|
|
int cmd_verify_pack(int argc, const char **argv, const char *prefix)
|
2005-06-29 09:51:27 +00:00
|
|
|
{
|
2006-08-10 15:02:37 +00:00
|
|
|
int err = 0;
|
2009-08-07 22:45:30 +00:00
|
|
|
unsigned int flags = 0;
|
2020-07-29 23:14:19 +00:00
|
|
|
const char *object_format = NULL;
|
2009-07-08 05:15:40 +00:00
|
|
|
int i;
|
|
|
|
const struct option verify_pack_options[] = {
|
2012-08-20 12:32:51 +00:00
|
|
|
OPT_BIT('v', "verbose", &flags, N_("verbose"),
|
2009-08-07 22:45:30 +00:00
|
|
|
VERIFY_PACK_VERBOSE),
|
2012-08-20 12:32:51 +00:00
|
|
|
OPT_BIT('s', "stat-only", &flags, N_("show statistics only"),
|
2009-08-07 22:45:30 +00:00
|
|
|
VERIFY_PACK_STAT_ONLY),
|
2020-07-29 23:14:19 +00:00
|
|
|
OPT_STRING(0, "object-format", &object_format, N_("hash"),
|
|
|
|
N_("specify the hash algorithm to use")),
|
2009-07-08 05:15:40 +00:00
|
|
|
OPT_END()
|
|
|
|
};
|
2005-06-29 09:51:27 +00:00
|
|
|
|
2008-05-14 17:46:53 +00:00
|
|
|
git_config(git_default_config, NULL);
|
2009-07-08 05:15:40 +00:00
|
|
|
argc = parse_options(argc, argv, prefix, verify_pack_options,
|
|
|
|
verify_pack_usage, 0);
|
|
|
|
if (argc < 1)
|
|
|
|
usage_with_options(verify_pack_usage, verify_pack_options);
|
|
|
|
for (i = 0; i < argc; i++) {
|
2020-07-29 23:14:19 +00:00
|
|
|
if (verify_one_pack(argv[i], flags, object_format))
|
2009-07-08 05:15:40 +00:00
|
|
|
err = 1;
|
2005-06-29 09:51:27 +00:00
|
|
|
}
|
2006-08-10 15:02:31 +00:00
|
|
|
|
2006-08-10 15:02:37 +00:00
|
|
|
return err;
|
2005-06-29 09:51:27 +00:00
|
|
|
}
|