verify-pack.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "builtin.h"
  2. #include "cache.h"
  3. #include "config.h"
  4. #include "run-command.h"
  5. #include "parse-options.h"
  6. #define VERIFY_PACK_VERBOSE 01
  7. #define VERIFY_PACK_STAT_ONLY 02
  8. static int verify_one_pack(const char *path, unsigned int flags, const char *hash_algo)
  9. {
  10. struct child_process index_pack = CHILD_PROCESS_INIT;
  11. struct strvec *argv = &index_pack.args;
  12. struct strbuf arg = STRBUF_INIT;
  13. int verbose = flags & VERIFY_PACK_VERBOSE;
  14. int stat_only = flags & VERIFY_PACK_STAT_ONLY;
  15. int err;
  16. strvec_push(argv, "index-pack");
  17. if (stat_only)
  18. strvec_push(argv, "--verify-stat-only");
  19. else if (verbose)
  20. strvec_push(argv, "--verify-stat");
  21. else
  22. strvec_push(argv, "--verify");
  23. if (hash_algo)
  24. strvec_pushf(argv, "--object-format=%s", hash_algo);
  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 (strbuf_strip_suffix(&arg, ".idx") ||
  31. !ends_with(arg.buf, ".pack"))
  32. strbuf_addstr(&arg, ".pack");
  33. strvec_push(argv, arg.buf);
  34. index_pack.git_cmd = 1;
  35. err = run_command(&index_pack);
  36. if (verbose || stat_only) {
  37. if (err)
  38. printf("%s: bad\n", arg.buf);
  39. else {
  40. if (!stat_only)
  41. printf("%s: ok\n", arg.buf);
  42. }
  43. }
  44. strbuf_release(&arg);
  45. return err;
  46. }
  47. static const char * const verify_pack_usage[] = {
  48. N_("git verify-pack [-v | --verbose] [-s | --stat-only] <pack>..."),
  49. NULL
  50. };
  51. int cmd_verify_pack(int argc, const char **argv, const char *prefix)
  52. {
  53. int err = 0;
  54. unsigned int flags = 0;
  55. const char *object_format = NULL;
  56. int i;
  57. const struct option verify_pack_options[] = {
  58. OPT_BIT('v', "verbose", &flags, N_("verbose"),
  59. VERIFY_PACK_VERBOSE),
  60. OPT_BIT('s', "stat-only", &flags, N_("show statistics only"),
  61. VERIFY_PACK_STAT_ONLY),
  62. OPT_STRING(0, "object-format", &object_format, N_("hash"),
  63. N_("specify the hash algorithm to use")),
  64. OPT_END()
  65. };
  66. git_config(git_default_config, NULL);
  67. argc = parse_options(argc, argv, prefix, verify_pack_options,
  68. verify_pack_usage, 0);
  69. if (argc < 1)
  70. usage_with_options(verify_pack_usage, verify_pack_options);
  71. for (i = 0; i < argc; i++) {
  72. if (verify_one_pack(argv[i], flags, object_format))
  73. err = 1;
  74. }
  75. return err;
  76. }