t-reftable-block: use reftable_record_key() instead of strbuf_addstr()
[git/gitster.git] / builtin / commit-tree.c
blob84bb4502229af0ef20365d329ae72865d4285f34
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "builtin.h"
7 #include "config.h"
8 #include "gettext.h"
9 #include "hex.h"
10 #include "object-name.h"
11 #include "object-store-ll.h"
12 #include "repository.h"
13 #include "commit.h"
14 #include "parse-options.h"
16 static const char * const commit_tree_usage[] = {
17 N_("git commit-tree <tree> [(-p <parent>)...]"),
18 N_("git commit-tree [(-p <parent>)...] [-S[<keyid>]] [(-m <message>)...]\n"
19 " [(-F <file>)...] <tree>"),
20 NULL
23 static const char *sign_commit;
25 static void new_parent(struct commit *parent, struct commit_list **parents_p)
27 struct object_id *oid = &parent->object.oid;
28 struct commit_list *parents;
29 for (parents = *parents_p; parents; parents = parents->next) {
30 if (parents->item == parent) {
31 error(_("duplicate parent %s ignored"), oid_to_hex(oid));
32 return;
34 parents_p = &parents->next;
36 commit_list_insert(parent, parents_p);
39 static int parse_parent_arg_callback(const struct option *opt,
40 const char *arg, int unset)
42 struct object_id oid;
43 struct commit_list **parents = opt->value;
45 BUG_ON_OPT_NEG_NOARG(unset, arg);
47 if (repo_get_oid_commit(the_repository, arg, &oid))
48 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;
55 static int parse_message_arg_callback(const struct option *opt,
56 const char *arg, int unset)
58 struct strbuf *buf = opt->value;
60 BUG_ON_OPT_NEG_NOARG(unset, arg);
62 if (buf->len)
63 strbuf_addch(buf, '\n');
64 strbuf_addstr(buf, arg);
65 strbuf_complete_line(buf);
67 return 0;
70 static int parse_file_arg_callback(const struct option *opt,
71 const char *arg, int unset)
73 int fd;
74 struct strbuf *buf = opt->value;
76 BUG_ON_OPT_NEG_NOARG(unset, arg);
78 if (buf->len)
79 strbuf_addch(buf, '\n');
80 if (!strcmp(arg, "-"))
81 fd = 0;
82 else {
83 fd = xopen(arg, O_RDONLY);
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);
90 return 0;
93 int cmd_commit_tree(int argc, const char **argv, const char *prefix)
95 static struct strbuf buffer = STRBUF_INIT;
96 struct commit_list *parents = NULL;
97 struct object_id tree_oid;
98 struct object_id commit_oid;
100 struct option options[] = {
101 OPT_CALLBACK_F('p', NULL, &parents, N_("parent"),
102 N_("id of a parent commit object"), PARSE_OPT_NONEG,
103 parse_parent_arg_callback),
104 OPT_CALLBACK_F('m', NULL, &buffer, N_("message"),
105 N_("commit message"), PARSE_OPT_NONEG,
106 parse_message_arg_callback),
107 OPT_CALLBACK_F('F', NULL, &buffer, N_("file"),
108 N_("read commit log message from file"), PARSE_OPT_NONEG,
109 parse_file_arg_callback),
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()
114 int ret;
116 git_config(git_default_config, NULL);
118 if (argc < 2 || !strcmp(argv[1], "-h"))
119 usage_with_options(commit_tree_usage, options);
121 argc = parse_options(argc, argv, prefix, options, commit_tree_usage, 0);
123 if (argc != 1)
124 die(_("must give exactly one tree"));
126 if (repo_get_oid_tree(the_repository, argv[0], &tree_oid))
127 die(_("not a valid object name %s"), argv[0]);
129 if (!buffer.len) {
130 if (strbuf_read(&buffer, 0, 0) < 0)
131 die_errno(_("git commit-tree: failed to read"));
134 if (commit_tree(buffer.buf, buffer.len, &tree_oid, parents, &commit_oid,
135 NULL, sign_commit)) {
136 ret = 1;
137 goto out;
140 printf("%s\n", oid_to_hex(&commit_oid));
141 ret = 0;
143 out:
144 free_commit_list(parents);
145 strbuf_release(&buffer);
146 return ret;