verify-commit.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Builtin "git commit-commit"
  3. *
  4. * Copyright (c) 2014 Michael J Gruber <git@drmicha.warpmail.net>
  5. *
  6. * Based on git-verify-tag
  7. */
  8. #include "cache.h"
  9. #include "config.h"
  10. #include "builtin.h"
  11. #include "object-store.h"
  12. #include "repository.h"
  13. #include "commit.h"
  14. #include "run-command.h"
  15. #include "parse-options.h"
  16. #include "gpg-interface.h"
  17. static const char * const verify_commit_usage[] = {
  18. N_("git verify-commit [-v | --verbose] <commit>..."),
  19. NULL
  20. };
  21. static int run_gpg_verify(struct commit *commit, unsigned flags)
  22. {
  23. struct signature_check signature_check;
  24. int ret;
  25. memset(&signature_check, 0, sizeof(signature_check));
  26. ret = check_commit_signature(commit, &signature_check);
  27. print_signature_buffer(&signature_check, flags);
  28. signature_check_clear(&signature_check);
  29. return ret;
  30. }
  31. static int verify_commit(const char *name, unsigned flags)
  32. {
  33. struct object_id oid;
  34. struct object *obj;
  35. if (get_oid(name, &oid))
  36. return error("commit '%s' not found.", name);
  37. obj = parse_object(the_repository, &oid);
  38. if (!obj)
  39. return error("%s: unable to read file.", name);
  40. if (obj->type != OBJ_COMMIT)
  41. return error("%s: cannot verify a non-commit object of type %s.",
  42. name, type_name(obj->type));
  43. return run_gpg_verify((struct commit *)obj, flags);
  44. }
  45. static int git_verify_commit_config(const char *var, const char *value, void *cb)
  46. {
  47. int status = git_gpg_config(var, value, cb);
  48. if (status)
  49. return status;
  50. return git_default_config(var, value, cb);
  51. }
  52. int cmd_verify_commit(int argc, const char **argv, const char *prefix)
  53. {
  54. int i = 1, verbose = 0, had_error = 0;
  55. unsigned flags = 0;
  56. const struct option verify_commit_options[] = {
  57. OPT__VERBOSE(&verbose, N_("print commit contents")),
  58. OPT_BIT(0, "raw", &flags, N_("print raw gpg status output"), GPG_VERIFY_RAW),
  59. OPT_END()
  60. };
  61. git_config(git_verify_commit_config, NULL);
  62. argc = parse_options(argc, argv, prefix, verify_commit_options,
  63. verify_commit_usage, PARSE_OPT_KEEP_ARGV0);
  64. if (argc <= i)
  65. usage_with_options(verify_commit_usage, verify_commit_options);
  66. if (verbose)
  67. flags |= GPG_VERIFY_VERBOSE;
  68. /* sometimes the program was terminated because this signal
  69. * was received in the process of writing the gpg input: */
  70. signal(SIGPIPE, SIG_IGN);
  71. while (i < argc)
  72. if (verify_commit(argv[i++], flags))
  73. had_error = 1;
  74. return had_error;
  75. }