blob: 63ea3229333c8766d51c34be9aec3f4d1bab3cd9 [file] [log] [blame]
Linus Torvalds8bc9a0c2005-04-07 15:16:10 -07001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
Linus Torvaldse83c5162005-04-07 15:13:13 -07006#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07007#include "config.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07008#include "object-store.h"
Stefan Bellerc1f5eb42018-06-28 18:21:59 -07009#include "repository.h"
Peter Eriksen8e440252006-04-02 14:44:09 +020010#include "commit.h"
11#include "tree.h"
Peter Eriksen6d96ac12006-05-23 14:15:33 +020012#include "builtin.h"
Johannes Schindelin9e832662006-12-22 22:06:08 +010013#include "utf8.h"
Junio C Hamanoba3c69a2011-10-05 17:23:20 -070014#include "gpg-interface.h"
Brandon Richardsoncbdeab92019-03-07 11:44:09 -040015#include "parse-options.h"
Linus Torvaldse83c5162005-04-07 15:13:13 -070016
Brandon Richardsoncbdeab92019-03-07 11:44:09 -040017static const char * const commit_tree_usage[] = {
18 N_("git commit-tree [(-p <parent>)...] [-S[<keyid>]] [(-m <message>)...] "
19 "[(-F <file>)...] <tree>"),
20 NULL
21};
Junio C Hamanoc5bac172005-04-20 19:49:16 -070022
Nicolas Vigierd95bfb12013-11-05 00:14:41 +010023static const char *sign_commit;
24
Johannes Schindelinef98c5c2008-06-27 13:24:47 +010025static void new_parent(struct commit *parent, struct commit_list **parents_p)
Linus Torvaldsb3892372005-06-19 10:40:10 -070026{
brian m. carlsonf2fd0762015-11-10 02:22:28 +000027 struct object_id *oid = &parent->object.oid;
Johannes Schindelinef98c5c2008-06-27 13:24:47 +010028 struct commit_list *parents;
29 for (parents = *parents_p; parents; parents = parents->next) {
30 if (parents->item == parent) {
Brandon Richardsoncbdeab92019-03-07 11:44:09 -040031 error(_("duplicate parent %s ignored"), oid_to_hex(oid));
Johannes Schindelinef98c5c2008-06-27 13:24:47 +010032 return;
Linus Torvaldsb3892372005-06-19 10:40:10 -070033 }
Johannes Schindelinef98c5c2008-06-27 13:24:47 +010034 parents_p = &parents->next;
Linus Torvaldsb3892372005-06-19 10:40:10 -070035 }
Johannes Schindelinef98c5c2008-06-27 13:24:47 +010036 commit_list_insert(parent, parents_p);
Linus Torvaldsb3892372005-06-19 10:40:10 -070037}
38
Junio C Hamanoba3c69a2011-10-05 17:23:20 -070039static int commit_tree_config(const char *var, const char *value, void *cb)
40{
41 int status = git_gpg_config(var, value, NULL);
42 if (status)
43 return status;
44 return git_default_config(var, value, cb);
45}
46
Brandon Richardsoncbdeab92019-03-07 11:44:09 -040047static int parse_parent_arg_callback(const struct option *opt,
48 const char *arg, int unset)
49{
50 struct object_id oid;
51 struct commit_list **parents = opt->value;
52
53 BUG_ON_OPT_NEG_NOARG(unset, arg);
54
55 if (get_oid_commit(arg, &oid))
56 die(_("not a valid object name %s"), arg);
57
58 assert_oid_type(&oid, OBJ_COMMIT);
59 new_parent(lookup_commit(the_repository, &oid), parents);
60 return 0;
61}
62
63static int parse_message_arg_callback(const struct option *opt,
64 const char *arg, int unset)
65{
66 struct strbuf *buf = opt->value;
67
68 BUG_ON_OPT_NEG_NOARG(unset, arg);
69
70 if (buf->len)
71 strbuf_addch(buf, '\n');
72 strbuf_addstr(buf, arg);
73 strbuf_complete_line(buf);
74
75 return 0;
76}
77
78static int parse_file_arg_callback(const struct option *opt,
79 const char *arg, int unset)
80{
81 int fd;
82 struct strbuf *buf = opt->value;
83
84 BUG_ON_OPT_NEG_NOARG(unset, arg);
85
86 if (buf->len)
87 strbuf_addch(buf, '\n');
88 if (!strcmp(arg, "-"))
89 fd = 0;
90 else {
René Scharfe66e905b2021-08-25 22:16:46 +020091 fd = xopen(arg, O_RDONLY);
Brandon Richardsoncbdeab92019-03-07 11:44:09 -040092 }
93 if (strbuf_read(buf, fd, 0) < 0)
94 die_errno(_("git commit-tree: failed to read '%s'"), arg);
95 if (fd && close(fd))
96 die_errno(_("git commit-tree: failed to close '%s'"), arg);
97
98 return 0;
99}
100
Miklos Vajna7b9c0a62008-07-01 04:37:49 +0200101int cmd_commit_tree(int argc, const char **argv, const char *prefix)
102{
Brandon Richardsoncbdeab92019-03-07 11:44:09 -0400103 static struct strbuf buffer = STRBUF_INIT;
Miklos Vajna7b9c0a62008-07-01 04:37:49 +0200104 struct commit_list *parents = NULL;
brian m. carlson031cee52016-09-05 20:08:10 +0000105 struct object_id tree_oid;
106 struct object_id commit_oid;
Brandon Richardsoncbdeab92019-03-07 11:44:09 -0400107
108 struct option options[] = {
Denton Liu203c8532020-04-28 04:36:28 -0400109 OPT_CALLBACK_F('p', NULL, &parents, N_("parent"),
Brandon Richardsoncbdeab92019-03-07 11:44:09 -0400110 N_("id of a parent commit object"), PARSE_OPT_NONEG,
Denton Liu203c8532020-04-28 04:36:28 -0400111 parse_parent_arg_callback),
112 OPT_CALLBACK_F('m', NULL, &buffer, N_("message"),
Brandon Richardsoncbdeab92019-03-07 11:44:09 -0400113 N_("commit message"), PARSE_OPT_NONEG,
Denton Liu203c8532020-04-28 04:36:28 -0400114 parse_message_arg_callback),
115 OPT_CALLBACK_F('F', NULL, &buffer, N_("file"),
Brandon Richardsoncbdeab92019-03-07 11:44:09 -0400116 N_("read commit log message from file"), PARSE_OPT_NONEG,
Denton Liu203c8532020-04-28 04:36:28 -0400117 parse_file_arg_callback),
Brandon Richardsoncbdeab92019-03-07 11:44:09 -0400118 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
119 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
120 OPT_END()
121 };
Miklos Vajna7b9c0a62008-07-01 04:37:49 +0200122
Junio C Hamanoba3c69a2011-10-05 17:23:20 -0700123 git_config(commit_tree_config, NULL);
Miklos Vajna7b9c0a62008-07-01 04:37:49 +0200124
Jonathan Nieder6e9daef2009-11-09 09:04:44 -0600125 if (argc < 2 || !strcmp(argv[1], "-h"))
Brandon Richardsoncbdeab92019-03-07 11:44:09 -0400126 usage_with_options(commit_tree_usage, options);
Miklos Vajna7b9c0a62008-07-01 04:37:49 +0200127
Brandon Richardsoncbdeab92019-03-07 11:44:09 -0400128 argc = parse_options(argc, argv, prefix, options, commit_tree_usage, 0);
Miklos Vajna7b9c0a62008-07-01 04:37:49 +0200129
Brandon Richardsoncbdeab92019-03-07 11:44:09 -0400130 if (argc != 1)
131 die(_("must give exactly one tree"));
Brandon Richardson70ddbd72019-01-19 19:23:34 -0400132
Brandon Richardsoncbdeab92019-03-07 11:44:09 -0400133 if (get_oid_tree(argv[0], &tree_oid))
134 die(_("not a valid object name %s"), argv[0]);
Miklos Vajna7b9c0a62008-07-01 04:37:49 +0200135
Junio C Hamano96b8d932011-11-09 11:54:04 -0800136 if (!buffer.len) {
137 if (strbuf_read(&buffer, 0, 0) < 0)
Brandon Richardsoncbdeab92019-03-07 11:44:09 -0400138 die_errno(_("git commit-tree: failed to read"));
Junio C Hamano96b8d932011-11-09 11:54:04 -0800139 }
Miklos Vajna7b9c0a62008-07-01 04:37:49 +0200140
Patryk Obara5078f342018-01-28 01:13:16 +0100141 if (commit_tree(buffer.buf, buffer.len, &tree_oid, parents, &commit_oid,
142 NULL, sign_commit)) {
Jonathan Nieder79bc2af2010-10-02 03:41:00 -0500143 strbuf_release(&buffer);
Junio C Hamano7561d9f2006-03-24 22:23:25 -0800144 return 1;
Jonathan Nieder79bc2af2010-10-02 03:41:00 -0500145 }
146
brian m. carlson031cee52016-09-05 20:08:10 +0000147 printf("%s\n", oid_to_hex(&commit_oid));
Jonathan Nieder79bc2af2010-10-02 03:41:00 -0500148 strbuf_release(&buffer);
149 return 0;
Linus Torvaldse83c5162005-04-07 15:13:13 -0700150}