commit-tree.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * GIT - The information manager from hell
  3. *
  4. * Copyright (C) Linus Torvalds, 2005
  5. */
  6. #include "cache.h"
  7. #include "config.h"
  8. #include "object-store.h"
  9. #include "repository.h"
  10. #include "commit.h"
  11. #include "tree.h"
  12. #include "builtin.h"
  13. #include "utf8.h"
  14. #include "gpg-interface.h"
  15. #include "parse-options.h"
  16. static const char * const commit_tree_usage[] = {
  17. N_("git commit-tree [(-p <parent>)...] [-S[<keyid>]] [(-m <message>)...] "
  18. "[(-F <file>)...] <tree>"),
  19. NULL
  20. };
  21. static const char *sign_commit;
  22. static void new_parent(struct commit *parent, struct commit_list **parents_p)
  23. {
  24. struct object_id *oid = &parent->object.oid;
  25. struct commit_list *parents;
  26. for (parents = *parents_p; parents; parents = parents->next) {
  27. if (parents->item == parent) {
  28. error(_("duplicate parent %s ignored"), oid_to_hex(oid));
  29. return;
  30. }
  31. parents_p = &parents->next;
  32. }
  33. commit_list_insert(parent, parents_p);
  34. }
  35. static int commit_tree_config(const char *var, const char *value, void *cb)
  36. {
  37. int status = git_gpg_config(var, value, NULL);
  38. if (status)
  39. return status;
  40. return git_default_config(var, value, cb);
  41. }
  42. static int parse_parent_arg_callback(const struct option *opt,
  43. const char *arg, int unset)
  44. {
  45. struct object_id oid;
  46. struct commit_list **parents = opt->value;
  47. BUG_ON_OPT_NEG_NOARG(unset, arg);
  48. if (get_oid_commit(arg, &oid))
  49. die(_("not a valid object name %s"), arg);
  50. assert_oid_type(&oid, OBJ_COMMIT);
  51. new_parent(lookup_commit(the_repository, &oid), parents);
  52. return 0;
  53. }
  54. static int parse_message_arg_callback(const struct option *opt,
  55. const char *arg, int unset)
  56. {
  57. struct strbuf *buf = opt->value;
  58. BUG_ON_OPT_NEG_NOARG(unset, arg);
  59. if (buf->len)
  60. strbuf_addch(buf, '\n');
  61. strbuf_addstr(buf, arg);
  62. strbuf_complete_line(buf);
  63. return 0;
  64. }
  65. static int parse_file_arg_callback(const struct option *opt,
  66. const char *arg, int unset)
  67. {
  68. int fd;
  69. struct strbuf *buf = opt->value;
  70. BUG_ON_OPT_NEG_NOARG(unset, arg);
  71. if (buf->len)
  72. strbuf_addch(buf, '\n');
  73. if (!strcmp(arg, "-"))
  74. fd = 0;
  75. else {
  76. fd = open(arg, O_RDONLY);
  77. if (fd < 0)
  78. die_errno(_("git commit-tree: failed to open '%s'"), arg);
  79. }
  80. if (strbuf_read(buf, fd, 0) < 0)
  81. die_errno(_("git commit-tree: failed to read '%s'"), arg);
  82. if (fd && close(fd))
  83. die_errno(_("git commit-tree: failed to close '%s'"), arg);
  84. return 0;
  85. }
  86. int cmd_commit_tree(int argc, const char **argv, const char *prefix)
  87. {
  88. static struct strbuf buffer = STRBUF_INIT;
  89. struct commit_list *parents = NULL;
  90. struct object_id tree_oid;
  91. struct object_id commit_oid;
  92. struct option options[] = {
  93. OPT_CALLBACK_F('p', NULL, &parents, N_("parent"),
  94. N_("id of a parent commit object"), PARSE_OPT_NONEG,
  95. parse_parent_arg_callback),
  96. OPT_CALLBACK_F('m', NULL, &buffer, N_("message"),
  97. N_("commit message"), PARSE_OPT_NONEG,
  98. parse_message_arg_callback),
  99. OPT_CALLBACK_F('F', NULL, &buffer, N_("file"),
  100. N_("read commit log message from file"), PARSE_OPT_NONEG,
  101. parse_file_arg_callback),
  102. { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
  103. N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
  104. OPT_END()
  105. };
  106. git_config(commit_tree_config, NULL);
  107. if (argc < 2 || !strcmp(argv[1], "-h"))
  108. usage_with_options(commit_tree_usage, options);
  109. argc = parse_options(argc, argv, prefix, options, commit_tree_usage, 0);
  110. if (argc != 1)
  111. die(_("must give exactly one tree"));
  112. if (get_oid_tree(argv[0], &tree_oid))
  113. die(_("not a valid object name %s"), argv[0]);
  114. if (!buffer.len) {
  115. if (strbuf_read(&buffer, 0, 0) < 0)
  116. die_errno(_("git commit-tree: failed to read"));
  117. }
  118. if (commit_tree(buffer.buf, buffer.len, &tree_oid, parents, &commit_oid,
  119. NULL, sign_commit)) {
  120. strbuf_release(&buffer);
  121. return 1;
  122. }
  123. printf("%s\n", oid_to_hex(&commit_oid));
  124. strbuf_release(&buffer);
  125. return 0;
  126. }