]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/commit-tree.c
The fifth batch
[thirdparty/git.git] / builtin / commit-tree.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
03eae9af 6#define USE_THE_REPOSITORY_VARIABLE
bc5c5ec0 7#include "builtin.h"
b2141fc1 8#include "config.h"
f394e093 9#include "gettext.h"
41771fa4 10#include "hex.h"
dabab1d6 11#include "object-name.h"
68cd492a 12#include "object-store.h"
03eae9af 13
8e440259 14#include "commit.h"
cbdeab98 15#include "parse-options.h"
e83c5163 16
cbdeab98 17static const char * const commit_tree_usage[] = {
d9054a19 18 N_("git commit-tree <tree> [(-p <parent>)...]"),
5af8b61c
ÆAB
19 N_("git commit-tree [(-p <parent>)...] [-S[<keyid>]] [(-m <message>)...]\n"
20 " [(-F <file>)...] <tree>"),
cbdeab98
BR
21 NULL
22};
c5bac17a 23
d95bfb12
NV
24static const char *sign_commit;
25
ef98c5ca 26static void new_parent(struct commit *parent, struct commit_list **parents_p)
b389237a 27{
f2fd0760 28 struct object_id *oid = &parent->object.oid;
ef98c5ca
JS
29 struct commit_list *parents;
30 for (parents = *parents_p; parents; parents = parents->next) {
31 if (parents->item == parent) {
cbdeab98 32 error(_("duplicate parent %s ignored"), oid_to_hex(oid));
ef98c5ca 33 return;
b389237a 34 }
ef98c5ca 35 parents_p = &parents->next;
b389237a 36 }
ef98c5ca 37 commit_list_insert(parent, parents_p);
b389237a
LT
38}
39
cbdeab98
BR
40static int parse_parent_arg_callback(const struct option *opt,
41 const char *arg, int unset)
42{
43 struct object_id oid;
44 struct commit_list **parents = opt->value;
45
46 BUG_ON_OPT_NEG_NOARG(unset, arg);
47
d850b7a5 48 if (repo_get_oid_commit(the_repository, arg, &oid))
cbdeab98
BR
49 die(_("not a valid object name %s"), arg);
50
51 assert_oid_type(&oid, OBJ_COMMIT);
52 new_parent(lookup_commit(the_repository, &oid), parents);
53 return 0;
54}
55
56static int parse_message_arg_callback(const struct option *opt,
57 const char *arg, int unset)
58{
59 struct strbuf *buf = opt->value;
60
61 BUG_ON_OPT_NEG_NOARG(unset, arg);
62
63 if (buf->len)
64 strbuf_addch(buf, '\n');
65 strbuf_addstr(buf, arg);
66 strbuf_complete_line(buf);
67
68 return 0;
69}
70
71static int parse_file_arg_callback(const struct option *opt,
72 const char *arg, int unset)
73{
74 int fd;
75 struct strbuf *buf = opt->value;
76
77 BUG_ON_OPT_NEG_NOARG(unset, arg);
78
79 if (buf->len)
80 strbuf_addch(buf, '\n');
81 if (!strcmp(arg, "-"))
82 fd = 0;
83 else {
66e905b7 84 fd = xopen(arg, O_RDONLY);
cbdeab98
BR
85 }
86 if (strbuf_read(buf, fd, 0) < 0)
87 die_errno(_("git commit-tree: failed to read '%s'"), arg);
88 if (fd && close(fd))
89 die_errno(_("git commit-tree: failed to close '%s'"), arg);
90
91 return 0;
92}
93
9b1cb507
JC
94int cmd_commit_tree(int argc,
95 const char **argv,
96 const char *prefix,
97 struct repository *repo UNUSED)
7b9c0a69 98{
cbdeab98 99 static struct strbuf buffer = STRBUF_INIT;
7b9c0a69 100 struct commit_list *parents = NULL;
031cee5b 101 struct object_id tree_oid;
102 struct object_id commit_oid;
cbdeab98
BR
103
104 struct option options[] = {
203c8533 105 OPT_CALLBACK_F('p', NULL, &parents, N_("parent"),
cbdeab98 106 N_("id of a parent commit object"), PARSE_OPT_NONEG,
203c8533
DL
107 parse_parent_arg_callback),
108 OPT_CALLBACK_F('m', NULL, &buffer, N_("message"),
cbdeab98 109 N_("commit message"), PARSE_OPT_NONEG,
203c8533
DL
110 parse_message_arg_callback),
111 OPT_CALLBACK_F('F', NULL, &buffer, N_("file"),
cbdeab98 112 N_("read commit log message from file"), PARSE_OPT_NONEG,
203c8533 113 parse_file_arg_callback),
d012ceb5
PS
114 {
115 .type = OPTION_STRING,
116 .short_name = 'S',
117 .long_name = "gpg-sign",
118 .value = &sign_commit,
119 .argh = N_("key-id"),
120 .help = N_("GPG sign commit"),
121 .flags = PARSE_OPT_OPTARG,
122 .defval = (intptr_t) "",
123 },
cbdeab98
BR
124 OPT_END()
125 };
63c9bd37 126 int ret;
7b9c0a69 127
cc5d1d32 128 git_config(git_default_config, NULL);
7b9c0a69 129
b821c999
JH
130 show_usage_with_options_if_asked(argc, argv,
131 commit_tree_usage, options);
7b9c0a69 132
cbdeab98 133 argc = parse_options(argc, argv, prefix, options, commit_tree_usage, 0);
70ddbd77 134
cbdeab98
BR
135 if (argc != 1)
136 die(_("must give exactly one tree"));
ba3c69a9 137
d850b7a5 138 if (repo_get_oid_tree(the_repository, argv[0], &tree_oid))
cbdeab98 139 die(_("not a valid object name %s"), argv[0]);
7b9c0a69 140
96b8d93a
JH
141 if (!buffer.len) {
142 if (strbuf_read(&buffer, 0, 0) < 0)
cbdeab98 143 die_errno(_("git commit-tree: failed to read"));
96b8d93a 144 }
7b9c0a69 145
5078f344
PO
146 if (commit_tree(buffer.buf, buffer.len, &tree_oid, parents, &commit_oid,
147 NULL, sign_commit)) {
63c9bd37
PS
148 ret = 1;
149 goto out;
79bc2af5
JN
150 }
151
031cee5b 152 printf("%s\n", oid_to_hex(&commit_oid));
63c9bd37
PS
153 ret = 0;
154
155out:
156 free_commit_list(parents);
79bc2af5 157 strbuf_release(&buffer);
63c9bd37 158 return ret;
e83c5163 159}