Linus Torvalds | 8bc9a0c | 2005-04-07 15:16:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | */ |
Elijah Newren | bc5c5ec | 2023-05-16 06:33:57 +0000 | [diff] [blame] | 6 | #include "builtin.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 7 | #include "config.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 8 | #include "gettext.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 9 | #include "hex.h" |
Elijah Newren | dabab1d | 2023-04-11 00:41:49 -0700 | [diff] [blame] | 10 | #include "object-name.h" |
Elijah Newren | a034e91 | 2023-05-16 06:34:06 +0000 | [diff] [blame] | 11 | #include "object-store-ll.h" |
Stefan Beller | c1f5eb4 | 2018-06-28 18:21:59 -0700 | [diff] [blame] | 12 | #include "repository.h" |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 13 | #include "commit.h" |
Brandon Richardson | cbdeab9 | 2019-03-07 11:44:09 -0400 | [diff] [blame] | 14 | #include "parse-options.h" |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 15 | |
Brandon Richardson | cbdeab9 | 2019-03-07 11:44:09 -0400 | [diff] [blame] | 16 | static const char * const commit_tree_usage[] = { |
Ævar Arnfjörð Bjarmason | d9054a1 | 2022-10-13 17:39:18 +0200 | [diff] [blame] | 17 | N_("git commit-tree <tree> [(-p <parent>)...]"), |
Ævar Arnfjörð Bjarmason | 5af8b61 | 2022-10-13 17:39:02 +0200 | [diff] [blame] | 18 | N_("git commit-tree [(-p <parent>)...] [-S[<keyid>]] [(-m <message>)...]\n" |
| 19 | " [(-F <file>)...] <tree>"), |
Brandon Richardson | cbdeab9 | 2019-03-07 11:44:09 -0400 | [diff] [blame] | 20 | NULL |
| 21 | }; |
Junio C Hamano | c5bac17 | 2005-04-20 19:49:16 -0700 | [diff] [blame] | 22 | |
Nicolas Vigier | d95bfb1 | 2013-11-05 00:14:41 +0100 | [diff] [blame] | 23 | static const char *sign_commit; |
| 24 | |
Johannes Schindelin | ef98c5c | 2008-06-27 13:24:47 +0100 | [diff] [blame] | 25 | static void new_parent(struct commit *parent, struct commit_list **parents_p) |
Linus Torvalds | b389237 | 2005-06-19 10:40:10 -0700 | [diff] [blame] | 26 | { |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 27 | struct object_id *oid = &parent->object.oid; |
Johannes Schindelin | ef98c5c | 2008-06-27 13:24:47 +0100 | [diff] [blame] | 28 | struct commit_list *parents; |
| 29 | for (parents = *parents_p; parents; parents = parents->next) { |
| 30 | if (parents->item == parent) { |
Brandon Richardson | cbdeab9 | 2019-03-07 11:44:09 -0400 | [diff] [blame] | 31 | error(_("duplicate parent %s ignored"), oid_to_hex(oid)); |
Johannes Schindelin | ef98c5c | 2008-06-27 13:24:47 +0100 | [diff] [blame] | 32 | return; |
Linus Torvalds | b389237 | 2005-06-19 10:40:10 -0700 | [diff] [blame] | 33 | } |
Johannes Schindelin | ef98c5c | 2008-06-27 13:24:47 +0100 | [diff] [blame] | 34 | parents_p = &parents->next; |
Linus Torvalds | b389237 | 2005-06-19 10:40:10 -0700 | [diff] [blame] | 35 | } |
Johannes Schindelin | ef98c5c | 2008-06-27 13:24:47 +0100 | [diff] [blame] | 36 | commit_list_insert(parent, parents_p); |
Linus Torvalds | b389237 | 2005-06-19 10:40:10 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Brandon Richardson | cbdeab9 | 2019-03-07 11:44:09 -0400 | [diff] [blame] | 39 | static int parse_parent_arg_callback(const struct option *opt, |
| 40 | const char *arg, int unset) |
| 41 | { |
| 42 | struct object_id oid; |
| 43 | struct commit_list **parents = opt->value; |
| 44 | |
| 45 | BUG_ON_OPT_NEG_NOARG(unset, arg); |
| 46 | |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 47 | if (repo_get_oid_commit(the_repository, arg, &oid)) |
Brandon Richardson | cbdeab9 | 2019-03-07 11:44:09 -0400 | [diff] [blame] | 48 | die(_("not a valid object name %s"), arg); |
| 49 | |
| 50 | assert_oid_type(&oid, OBJ_COMMIT); |
| 51 | new_parent(lookup_commit(the_repository, &oid), parents); |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static int parse_message_arg_callback(const struct option *opt, |
| 56 | const char *arg, int unset) |
| 57 | { |
| 58 | struct strbuf *buf = opt->value; |
| 59 | |
| 60 | BUG_ON_OPT_NEG_NOARG(unset, arg); |
| 61 | |
| 62 | if (buf->len) |
| 63 | strbuf_addch(buf, '\n'); |
| 64 | strbuf_addstr(buf, arg); |
| 65 | strbuf_complete_line(buf); |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int parse_file_arg_callback(const struct option *opt, |
| 71 | const char *arg, int unset) |
| 72 | { |
| 73 | int fd; |
| 74 | struct strbuf *buf = opt->value; |
| 75 | |
| 76 | BUG_ON_OPT_NEG_NOARG(unset, arg); |
| 77 | |
| 78 | if (buf->len) |
| 79 | strbuf_addch(buf, '\n'); |
| 80 | if (!strcmp(arg, "-")) |
| 81 | fd = 0; |
| 82 | else { |
René Scharfe | 66e905b | 2021-08-25 22:16:46 +0200 | [diff] [blame] | 83 | fd = xopen(arg, O_RDONLY); |
Brandon Richardson | cbdeab9 | 2019-03-07 11:44:09 -0400 | [diff] [blame] | 84 | } |
| 85 | if (strbuf_read(buf, fd, 0) < 0) |
| 86 | die_errno(_("git commit-tree: failed to read '%s'"), arg); |
| 87 | if (fd && close(fd)) |
| 88 | die_errno(_("git commit-tree: failed to close '%s'"), arg); |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 93 | int cmd_commit_tree(int argc, const char **argv, const char *prefix) |
| 94 | { |
Brandon Richardson | cbdeab9 | 2019-03-07 11:44:09 -0400 | [diff] [blame] | 95 | static struct strbuf buffer = STRBUF_INIT; |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 96 | struct commit_list *parents = NULL; |
brian m. carlson | 031cee5 | 2016-09-05 20:08:10 +0000 | [diff] [blame] | 97 | struct object_id tree_oid; |
| 98 | struct object_id commit_oid; |
Brandon Richardson | cbdeab9 | 2019-03-07 11:44:09 -0400 | [diff] [blame] | 99 | |
| 100 | struct option options[] = { |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 101 | OPT_CALLBACK_F('p', NULL, &parents, N_("parent"), |
Brandon Richardson | cbdeab9 | 2019-03-07 11:44:09 -0400 | [diff] [blame] | 102 | N_("id of a parent commit object"), PARSE_OPT_NONEG, |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 103 | parse_parent_arg_callback), |
| 104 | OPT_CALLBACK_F('m', NULL, &buffer, N_("message"), |
Brandon Richardson | cbdeab9 | 2019-03-07 11:44:09 -0400 | [diff] [blame] | 105 | N_("commit message"), PARSE_OPT_NONEG, |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 106 | parse_message_arg_callback), |
| 107 | OPT_CALLBACK_F('F', NULL, &buffer, N_("file"), |
Brandon Richardson | cbdeab9 | 2019-03-07 11:44:09 -0400 | [diff] [blame] | 108 | N_("read commit log message from file"), PARSE_OPT_NONEG, |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 109 | parse_file_arg_callback), |
Brandon Richardson | cbdeab9 | 2019-03-07 11:44:09 -0400 | [diff] [blame] | 110 | { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"), |
| 111 | N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, |
| 112 | OPT_END() |
| 113 | }; |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 114 | |
Jeff King | cc5d1d3 | 2023-02-26 17:40:46 -0500 | [diff] [blame] | 115 | git_config(git_default_config, NULL); |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 116 | |
Jonathan Nieder | 6e9daef | 2009-11-09 09:04:44 -0600 | [diff] [blame] | 117 | if (argc < 2 || !strcmp(argv[1], "-h")) |
Brandon Richardson | cbdeab9 | 2019-03-07 11:44:09 -0400 | [diff] [blame] | 118 | usage_with_options(commit_tree_usage, options); |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 119 | |
Brandon Richardson | cbdeab9 | 2019-03-07 11:44:09 -0400 | [diff] [blame] | 120 | argc = parse_options(argc, argv, prefix, options, commit_tree_usage, 0); |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 121 | |
Brandon Richardson | cbdeab9 | 2019-03-07 11:44:09 -0400 | [diff] [blame] | 122 | if (argc != 1) |
| 123 | die(_("must give exactly one tree")); |
Brandon Richardson | 70ddbd7 | 2019-01-19 19:23:34 -0400 | [diff] [blame] | 124 | |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 125 | if (repo_get_oid_tree(the_repository, argv[0], &tree_oid)) |
Brandon Richardson | cbdeab9 | 2019-03-07 11:44:09 -0400 | [diff] [blame] | 126 | die(_("not a valid object name %s"), argv[0]); |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 127 | |
Junio C Hamano | 96b8d93 | 2011-11-09 11:54:04 -0800 | [diff] [blame] | 128 | if (!buffer.len) { |
| 129 | if (strbuf_read(&buffer, 0, 0) < 0) |
Brandon Richardson | cbdeab9 | 2019-03-07 11:44:09 -0400 | [diff] [blame] | 130 | die_errno(_("git commit-tree: failed to read")); |
Junio C Hamano | 96b8d93 | 2011-11-09 11:54:04 -0800 | [diff] [blame] | 131 | } |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 132 | |
Patryk Obara | 5078f34 | 2018-01-28 01:13:16 +0100 | [diff] [blame] | 133 | if (commit_tree(buffer.buf, buffer.len, &tree_oid, parents, &commit_oid, |
| 134 | NULL, sign_commit)) { |
Jonathan Nieder | 79bc2af | 2010-10-02 03:41:00 -0500 | [diff] [blame] | 135 | strbuf_release(&buffer); |
Junio C Hamano | 7561d9f | 2006-03-24 22:23:25 -0800 | [diff] [blame] | 136 | return 1; |
Jonathan Nieder | 79bc2af | 2010-10-02 03:41:00 -0500 | [diff] [blame] | 137 | } |
| 138 | |
brian m. carlson | 031cee5 | 2016-09-05 20:08:10 +0000 | [diff] [blame] | 139 | printf("%s\n", oid_to_hex(&commit_oid)); |
Jonathan Nieder | 79bc2af | 2010-10-02 03:41:00 -0500 | [diff] [blame] | 140 | strbuf_release(&buffer); |
| 141 | return 0; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 142 | } |