1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
7#include "commit.h"
8#include "tree.h"
9#include "builtin.h"
10#include "utf8.h"
11#include "gpg-interface.h"
12
13static const char commit_tree_usage[] = "git commit-tree [(-p <sha1>)...] [-S[<keyid>]] [-m <message>] [-F <file>] <sha1> <changelog";
14
15static const char *sign_commit;
16
17static void new_parent(struct commit *parent, struct commit_list **parents_p)
18{
19 unsigned char *sha1 = parent->object.sha1;
20 struct commit_list *parents;
21 for (parents = *parents_p; parents; parents = parents->next) {
22 if (parents->item == parent) {
23 error("duplicate parent %s ignored", sha1_to_hex(sha1));
24 return;
25 }
26 parents_p = &parents->next;
27 }
28 commit_list_insert(parent, parents_p);
29}
30
31static int commit_tree_config(const char *var, const char *value, void *cb)
32{
33 int status = git_gpg_config(var, value, NULL);
34 if (status)
35 return status;
36 if (!strcmp(var, "commit.gpgsign")) {
37 sign_commit = git_config_bool(var, value) ? "" : NULL;
38 return 0;
39 }
40 return git_default_config(var, value, cb);
41}
42
43int cmd_commit_tree(int argc, const char **argv, const char *prefix)
44{
45 int i, got_tree = 0;
46 struct commit_list *parents = NULL;
47 unsigned char tree_sha1[20];
48 unsigned char commit_sha1[20];
49 struct strbuf buffer = STRBUF_INIT;
50
51 git_config(commit_tree_config, NULL);
52
53 if (argc < 2 || !strcmp(argv[1], "-h"))
54 usage(commit_tree_usage);
55
56 for (i = 1; i < argc; i++) {
57 const char *arg = argv[i];
58 if (!strcmp(arg, "-p")) {
59 unsigned char sha1[20];
60 if (argc <= ++i)
61 usage(commit_tree_usage);
62 if (get_sha1_commit(argv[i], sha1))
63 die("Not a valid object name %s", argv[i]);
64 assert_sha1_type(sha1, OBJ_COMMIT);
65 new_parent(lookup_commit(sha1), &parents);
66 continue;
67 }
68
69 if (!memcmp(arg, "-S", 2)) {
70 sign_commit = arg + 2;
71 continue;
72 }
73
74 if (!strcmp(arg, "--no-gpg-sign")) {
75 sign_commit = NULL;
76 continue;
77 }
78
79 if (!strcmp(arg, "-m")) {
80 if (argc <= ++i)
81 usage(commit_tree_usage);
82 if (buffer.len)
83 strbuf_addch(&buffer, '\n');
84 strbuf_addstr(&buffer, argv[i]);
85 strbuf_complete_line(&buffer);
86 continue;
87 }
88
89 if (!strcmp(arg, "-F")) {
90 int fd;
91
92 if (argc <= ++i)
93 usage(commit_tree_usage);
94 if (buffer.len)
95 strbuf_addch(&buffer, '\n');
96 if (!strcmp(argv[i], "-"))
97 fd = 0;
98 else {
99 fd = open(argv[i], O_RDONLY);
100 if (fd < 0)
101 die_errno("git commit-tree: failed to open '%s'",
102 argv[i]);
103 }
104 if (strbuf_read(&buffer, fd, 0) < 0)
105 die_errno("git commit-tree: failed to read '%s'",
106 argv[i]);
107 if (fd && close(fd))
108 die_errno("git commit-tree: failed to close '%s'",
109 argv[i]);
110 strbuf_complete_line(&buffer);
111 continue;
112 }
113
114 if (get_sha1_tree(arg, tree_sha1))
115 die("Not a valid object name %s", arg);
116 if (got_tree)
117 die("Cannot give more than one trees");
118 got_tree = 1;
119 }
120
121 if (!buffer.len) {
122 if (strbuf_read(&buffer, 0, 0) < 0)
123 die_errno("git commit-tree: failed to read");
124 }
125
126 if (commit_tree(&buffer, tree_sha1, parents, commit_sha1,
127 NULL, sign_commit)) {
128 strbuf_release(&buffer);
129 return 1;
130 }
131
132 printf("%s\n", sha1_to_hex(commit_sha1));
133 strbuf_release(&buffer);
134 return 0;
135}