]> git.ipfire.org Git - thirdparty/git.git/blame_incremental - builtin/commit-tree.c
The fifth batch
[thirdparty/git.git] / builtin / commit-tree.c
... / ...
CommitLineData
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#define USE_THE_REPOSITORY_VARIABLE
7#include "builtin.h"
8#include "config.h"
9#include "gettext.h"
10#include "hex.h"
11#include "object-name.h"
12#include "object-store.h"
13
14#include "commit.h"
15#include "parse-options.h"
16
17static const char * const commit_tree_usage[] = {
18 N_("git commit-tree <tree> [(-p <parent>)...]"),
19 N_("git commit-tree [(-p <parent>)...] [-S[<keyid>]] [(-m <message>)...]\n"
20 " [(-F <file>)...] <tree>"),
21 NULL
22};
23
24static const char *sign_commit;
25
26static void new_parent(struct commit *parent, struct commit_list **parents_p)
27{
28 struct object_id *oid = &parent->object.oid;
29 struct commit_list *parents;
30 for (parents = *parents_p; parents; parents = parents->next) {
31 if (parents->item == parent) {
32 error(_("duplicate parent %s ignored"), oid_to_hex(oid));
33 return;
34 }
35 parents_p = &parents->next;
36 }
37 commit_list_insert(parent, parents_p);
38}
39
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
48 if (repo_get_oid_commit(the_repository, arg, &oid))
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 {
84 fd = xopen(arg, O_RDONLY);
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
94int cmd_commit_tree(int argc,
95 const char **argv,
96 const char *prefix,
97 struct repository *repo UNUSED)
98{
99 static struct strbuf buffer = STRBUF_INIT;
100 struct commit_list *parents = NULL;
101 struct object_id tree_oid;
102 struct object_id commit_oid;
103
104 struct option options[] = {
105 OPT_CALLBACK_F('p', NULL, &parents, N_("parent"),
106 N_("id of a parent commit object"), PARSE_OPT_NONEG,
107 parse_parent_arg_callback),
108 OPT_CALLBACK_F('m', NULL, &buffer, N_("message"),
109 N_("commit message"), PARSE_OPT_NONEG,
110 parse_message_arg_callback),
111 OPT_CALLBACK_F('F', NULL, &buffer, N_("file"),
112 N_("read commit log message from file"), PARSE_OPT_NONEG,
113 parse_file_arg_callback),
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 },
124 OPT_END()
125 };
126 int ret;
127
128 git_config(git_default_config, NULL);
129
130 show_usage_with_options_if_asked(argc, argv,
131 commit_tree_usage, options);
132
133 argc = parse_options(argc, argv, prefix, options, commit_tree_usage, 0);
134
135 if (argc != 1)
136 die(_("must give exactly one tree"));
137
138 if (repo_get_oid_tree(the_repository, argv[0], &tree_oid))
139 die(_("not a valid object name %s"), argv[0]);
140
141 if (!buffer.len) {
142 if (strbuf_read(&buffer, 0, 0) < 0)
143 die_errno(_("git commit-tree: failed to read"));
144 }
145
146 if (commit_tree(buffer.buf, buffer.len, &tree_oid, parents, &commit_oid,
147 NULL, sign_commit)) {
148 ret = 1;
149 goto out;
150 }
151
152 printf("%s\n", oid_to_hex(&commit_oid));
153 ret = 0;
154
155out:
156 free_commit_list(parents);
157 strbuf_release(&buffer);
158 return ret;
159}