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 (skip_prefix(arg, "-S", &sign_commit))
70continue;
7172
if (!strcmp(arg, "--no-gpg-sign")) {
73sign_commit = NULL;
74continue;
75}
7677
if (!strcmp(arg, "-m")) {
78if (argc <= ++i)
79usage(commit_tree_usage);
80if (buffer.len)
81strbuf_addch(&buffer, '\n');
82strbuf_addstr(&buffer, argv[i]);
83strbuf_complete_line(&buffer);
84continue;
85}
8687
if (!strcmp(arg, "-F")) {
88int fd;
8990
if (argc <= ++i)
91usage(commit_tree_usage);
92if (buffer.len)
93strbuf_addch(&buffer, '\n');
94if (!strcmp(argv[i], "-"))
95fd = 0;
96else {
97fd = open(argv[i], O_RDONLY);
98if (fd < 0)
99die_errno("git commit-tree: failed to open '%s'",
100argv[i]);
101}
102if (strbuf_read(&buffer, fd, 0) < 0)
103die_errno("git commit-tree: failed to read '%s'",
104argv[i]);
105if (fd && close(fd))
106die_errno("git commit-tree: failed to close '%s'",
107argv[i]);
108continue;
109}
110111
if (get_oid_tree(arg, &tree_oid))
112die("Not a valid object name %s", arg);
113if (got_tree)
114die("Cannot give more than one trees");
115got_tree = 1;
116}
117118
if (!buffer.len) {
119if (strbuf_read(&buffer, 0, 0) < 0)
120die_errno("git commit-tree: failed to read");
121}
122123
if (commit_tree(buffer.buf, buffer.len, &tree_oid, parents, &commit_oid,
124NULL, sign_commit)) {
125strbuf_release(&buffer);
126return 1;
127}
128129
printf("%s\n", oid_to_hex(&commit_oid));
130strbuf_release(&buffer);
131return 0;
132}