1/*
2* GIT - The information manager from hell
3*
4* Copyright (C) Linus Torvalds, 2005
5*/
6#include "cache.h"
7#include "config.h"
8#include "object-store.h"
9#include "repository.h"
10#include "commit.h"
11#include "tree.h"
12#include "builtin.h"
13#include "utf8.h"
14#include "gpg-interface.h"
1516
static const char commit_tree_usage[] = "git commit-tree [(-p <sha1>)...] [-S[<keyid>]] [-m <message>] [-F <file>] <sha1>";
1718
static const char *sign_commit;
1920
static void new_parent(struct commit *parent, struct commit_list **parents_p)
21{
22struct object_id *oid = &parent->object.oid;
23struct commit_list *parents;
24for (parents = *parents_p; parents; parents = parents->next) {
25if (parents->item == parent) {
26error("duplicate parent %s ignored", oid_to_hex(oid));
27return;
28}
29parents_p = &parents->next;
30}
31commit_list_insert(parent, parents_p);
32}
3334
static int commit_tree_config(const char *var, const char *value, void *cb)
35{
36int status = git_gpg_config(var, value, NULL);
37if (status)
38return status;
39return git_default_config(var, value, cb);
40}
4142
int cmd_commit_tree(int argc, const char **argv, const char *prefix)
43{
44int i, got_tree = 0;
45struct commit_list *parents = NULL;
46struct object_id tree_oid;
47struct object_id commit_oid;
48struct strbuf buffer = STRBUF_INIT;
4950
git_config(commit_tree_config, NULL);
5152
if (argc < 2 || !strcmp(argv[1], "-h"))
53usage(commit_tree_usage);
5455
for (i = 1; i < argc; i++) {
56const char *arg = argv[i];
57if (!strcmp(arg, "-p")) {
58struct object_id oid;
59if (argc <= ++i)
60usage(commit_tree_usage);
61if (get_oid_commit(argv[i], &oid))
62die("Not a valid object name %s", argv[i]);
63assert_oid_type(&oid, OBJ_COMMIT);
64new_parent(lookup_commit(the_repository, &oid),
65&parents);
66continue;
67}
6869
if (!strcmp(arg, "--gpg-sign")) {
70sign_commit = "";
71continue;
72}
7374
if (skip_prefix(arg, "-S", &sign_commit) ||
75skip_prefix(arg, "--gpg-sign=", &sign_commit))
76continue;
7778
if (!strcmp(arg, "--no-gpg-sign")) {
79sign_commit = NULL;
80continue;
81}
8283
if (!strcmp(arg, "-m")) {
84if (argc <= ++i)
85usage(commit_tree_usage);
86if (buffer.len)
87strbuf_addch(&buffer, '\n');
88strbuf_addstr(&buffer, argv[i]);
89strbuf_complete_line(&buffer);
90continue;
91}
9293
if (!strcmp(arg, "-F")) {
94int fd;
9596
if (argc <= ++i)
97usage(commit_tree_usage);
98if (buffer.len)
99strbuf_addch(&buffer, '\n');
100if (!strcmp(argv[i], "-"))
101fd = 0;
102else {
103fd = open(argv[i], O_RDONLY);
104if (fd < 0)
105die_errno("git commit-tree: failed to open '%s'",
106argv[i]);
107}
108if (strbuf_read(&buffer, fd, 0) < 0)
109die_errno("git commit-tree: failed to read '%s'",
110argv[i]);
111if (fd && close(fd))
112die_errno("git commit-tree: failed to close '%s'",
113argv[i]);
114continue;
115}
116117
if (get_oid_tree(arg, &tree_oid))
118die("Not a valid object name %s", arg);
119if (got_tree)
120die("Cannot give more than one trees");
121got_tree = 1;
122}
123124
if (!buffer.len) {
125if (strbuf_read(&buffer, 0, 0) < 0)
126die_errno("git commit-tree: failed to read");
127}
128129
if (commit_tree(buffer.buf, buffer.len, &tree_oid, parents, &commit_oid,
130NULL, sign_commit)) {
131strbuf_release(&buffer);
132return 1;
133}
134135
printf("%s\n", oid_to_hex(&commit_oid));
136strbuf_release(&buffer);
137return 0;
138}