]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/commit-tree.c
Merge branch 'jk/ci-retire-allow-ref'
[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 */
bc5c5ec0 6#include "builtin.h"
b2141fc1 7#include "config.h"
f394e093 8#include "gettext.h"
41771fa4 9#include "hex.h"
dabab1d6 10#include "object-name.h"
a034e910 11#include "object-store-ll.h"
c1f5eb49 12#include "repository.h"
8e440259
PE
13#include "commit.h"
14#include "tree.h"
9e832665 15#include "utf8.h"
ba3c69a9 16#include "gpg-interface.h"
cbdeab98 17#include "parse-options.h"
e83c5163 18
cbdeab98 19static const char * const commit_tree_usage[] = {
d9054a19 20 N_("git commit-tree <tree> [(-p <parent>)...]"),
5af8b61c
ÆAB
21 N_("git commit-tree [(-p <parent>)...] [-S[<keyid>]] [(-m <message>)...]\n"
22 " [(-F <file>)...] <tree>"),
cbdeab98
BR
23 NULL
24};
c5bac17a 25
d95bfb12
NV
26static const char *sign_commit;
27
ef98c5ca 28static void new_parent(struct commit *parent, struct commit_list **parents_p)
b389237a 29{
f2fd0760 30 struct object_id *oid = &parent->object.oid;
ef98c5ca
JS
31 struct commit_list *parents;
32 for (parents = *parents_p; parents; parents = parents->next) {
33 if (parents->item == parent) {
cbdeab98 34 error(_("duplicate parent %s ignored"), oid_to_hex(oid));
ef98c5ca 35 return;
b389237a 36 }
ef98c5ca 37 parents_p = &parents->next;
b389237a 38 }
ef98c5ca 39 commit_list_insert(parent, parents_p);
b389237a
LT
40}
41
cbdeab98
BR
42static 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
48 BUG_ON_OPT_NEG_NOARG(unset, arg);
49
d850b7a5 50 if (repo_get_oid_commit(the_repository, arg, &oid))
cbdeab98
BR
51 die(_("not a valid object name %s"), arg);
52
53 assert_oid_type(&oid, OBJ_COMMIT);
54 new_parent(lookup_commit(the_repository, &oid), parents);
55 return 0;
56}
57
58static int parse_message_arg_callback(const struct option *opt,
59 const char *arg, int unset)
60{
61 struct strbuf *buf = opt->value;
62
63 BUG_ON_OPT_NEG_NOARG(unset, arg);
64
65 if (buf->len)
66 strbuf_addch(buf, '\n');
67 strbuf_addstr(buf, arg);
68 strbuf_complete_line(buf);
69
70 return 0;
71}
72
73static int parse_file_arg_callback(const struct option *opt,
74 const char *arg, int unset)
75{
76 int fd;
77 struct strbuf *buf = opt->value;
78
79 BUG_ON_OPT_NEG_NOARG(unset, arg);
80
81 if (buf->len)
82 strbuf_addch(buf, '\n');
83 if (!strcmp(arg, "-"))
84 fd = 0;
85 else {
66e905b7 86 fd = xopen(arg, O_RDONLY);
cbdeab98
BR
87 }
88 if (strbuf_read(buf, fd, 0) < 0)
89 die_errno(_("git commit-tree: failed to read '%s'"), arg);
90 if (fd && close(fd))
91 die_errno(_("git commit-tree: failed to close '%s'"), arg);
92
93 return 0;
94}
95
7b9c0a69
MV
96int cmd_commit_tree(int argc, const char **argv, const char *prefix)
97{
cbdeab98 98 static struct strbuf buffer = STRBUF_INIT;
7b9c0a69 99 struct commit_list *parents = NULL;
031cee5b 100 struct object_id tree_oid;
101 struct object_id commit_oid;
cbdeab98
BR
102
103 struct option options[] = {
203c8533 104 OPT_CALLBACK_F('p', NULL, &parents, N_("parent"),
cbdeab98 105 N_("id of a parent commit object"), PARSE_OPT_NONEG,
203c8533
DL
106 parse_parent_arg_callback),
107 OPT_CALLBACK_F('m', NULL, &buffer, N_("message"),
cbdeab98 108 N_("commit message"), PARSE_OPT_NONEG,
203c8533
DL
109 parse_message_arg_callback),
110 OPT_CALLBACK_F('F', NULL, &buffer, N_("file"),
cbdeab98 111 N_("read commit log message from file"), PARSE_OPT_NONEG,
203c8533 112 parse_file_arg_callback),
cbdeab98
BR
113 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
114 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
115 OPT_END()
116 };
7b9c0a69 117
cc5d1d32 118 git_config(git_default_config, NULL);
7b9c0a69 119
6e9daeff 120 if (argc < 2 || !strcmp(argv[1], "-h"))
cbdeab98 121 usage_with_options(commit_tree_usage, options);
7b9c0a69 122
cbdeab98 123 argc = parse_options(argc, argv, prefix, options, commit_tree_usage, 0);
70ddbd77 124
cbdeab98
BR
125 if (argc != 1)
126 die(_("must give exactly one tree"));
ba3c69a9 127
d850b7a5 128 if (repo_get_oid_tree(the_repository, argv[0], &tree_oid))
cbdeab98 129 die(_("not a valid object name %s"), argv[0]);
7b9c0a69 130
96b8d93a
JH
131 if (!buffer.len) {
132 if (strbuf_read(&buffer, 0, 0) < 0)
cbdeab98 133 die_errno(_("git commit-tree: failed to read"));
96b8d93a 134 }
7b9c0a69 135
5078f344
PO
136 if (commit_tree(buffer.buf, buffer.len, &tree_oid, parents, &commit_oid,
137 NULL, sign_commit)) {
79bc2af5 138 strbuf_release(&buffer);
7561d9f5 139 return 1;
79bc2af5
JN
140 }
141
031cee5b 142 printf("%s\n", oid_to_hex(&commit_oid));
79bc2af5
JN
143 strbuf_release(&buffer);
144 return 0;
e83c5163 145}