2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
11 #include "gpg-interface.h"
13 static const char commit_tree_usage[] = "git commit-tree [(-p <sha1>)...] [-S[<keyid>]] [-m <message>] [-F <file>] <sha1>";
15 static const char *sign_commit;
17 static void new_parent(struct commit *parent, struct commit_list **parents_p)
19 struct object_id *oid = &parent->object.oid;
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", oid_to_hex(oid));
26 parents_p = &parents->next;
28 commit_list_insert(parent, parents_p);
31 static int commit_tree_config(const char *var, const char *value, void *cb)
33 int status = git_gpg_config(var, value, NULL);
36 return git_default_config(var, value, cb);
39 int cmd_commit_tree(int argc, const char **argv, const char *prefix)
42 struct commit_list *parents = NULL;
43 struct object_id tree_oid;
44 struct object_id commit_oid;
45 struct strbuf buffer = STRBUF_INIT;
47 git_config(commit_tree_config, NULL);
49 if (argc < 2 || !strcmp(argv[1], "-h"))
50 usage(commit_tree_usage);
52 for (i = 1; i < argc; i++) {
53 const char *arg = argv[i];
54 if (!strcmp(arg, "-p")) {
57 usage(commit_tree_usage);
58 if (get_sha1_commit(argv[i], oid.hash))
59 die("Not a valid object name %s", argv[i]);
60 assert_sha1_type(oid.hash, OBJ_COMMIT);
61 new_parent(lookup_commit(&oid), &parents);
65 if (skip_prefix(arg, "-S", &sign_commit))
68 if (!strcmp(arg, "--no-gpg-sign")) {
73 if (!strcmp(arg, "-m")) {
75 usage(commit_tree_usage);
77 strbuf_addch(&buffer, '\n');
78 strbuf_addstr(&buffer, argv[i]);
79 strbuf_complete_line(&buffer);
83 if (!strcmp(arg, "-F")) {
87 usage(commit_tree_usage);
89 strbuf_addch(&buffer, '\n');
90 if (!strcmp(argv[i], "-"))
93 fd = open(argv[i], O_RDONLY);
95 die_errno("git commit-tree: failed to open '%s'",
98 if (strbuf_read(&buffer, fd, 0) < 0)
99 die_errno("git commit-tree: failed to read '%s'",
102 die_errno("git commit-tree: failed to close '%s'",
104 strbuf_complete_line(&buffer);
108 if (get_sha1_tree(arg, tree_oid.hash))
109 die("Not a valid object name %s", arg);
111 die("Cannot give more than one trees");
116 if (strbuf_read(&buffer, 0, 0) < 0)
117 die_errno("git commit-tree: failed to read");
120 if (commit_tree(buffer.buf, buffer.len, tree_oid.hash, parents,
121 commit_oid.hash, NULL, sign_commit)) {
122 strbuf_release(&buffer);
126 printf("%s\n", oid_to_hex(&commit_oid));
127 strbuf_release(&buffer);