Thirteenth batch for 2.14
[git/raj.git] / builtin / commit-tree.c
bloba4a923d7c0b688e162c8e4d0411ff9b72748fb5c
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "config.h"
8 #include "commit.h"
9 #include "tree.h"
10 #include "builtin.h"
11 #include "utf8.h"
12 #include "gpg-interface.h"
14 static const char commit_tree_usage[] = "git commit-tree [(-p <sha1>)...] [-S[<keyid>]] [-m <message>] [-F <file>] <sha1>";
16 static const char *sign_commit;
18 static void new_parent(struct commit *parent, struct commit_list **parents_p)
20 struct object_id *oid = &parent->object.oid;
21 struct commit_list *parents;
22 for (parents = *parents_p; parents; parents = parents->next) {
23 if (parents->item == parent) {
24 error("duplicate parent %s ignored", oid_to_hex(oid));
25 return;
27 parents_p = &parents->next;
29 commit_list_insert(parent, parents_p);
32 static int commit_tree_config(const char *var, const char *value, void *cb)
34 int status = git_gpg_config(var, value, NULL);
35 if (status)
36 return status;
37 return git_default_config(var, value, cb);
40 int cmd_commit_tree(int argc, const char **argv, const char *prefix)
42 int i, got_tree = 0;
43 struct commit_list *parents = NULL;
44 struct object_id tree_oid;
45 struct object_id commit_oid;
46 struct strbuf buffer = STRBUF_INIT;
48 git_config(commit_tree_config, NULL);
50 if (argc < 2 || !strcmp(argv[1], "-h"))
51 usage(commit_tree_usage);
53 for (i = 1; i < argc; i++) {
54 const char *arg = argv[i];
55 if (!strcmp(arg, "-p")) {
56 struct object_id oid;
57 if (argc <= ++i)
58 usage(commit_tree_usage);
59 if (get_sha1_commit(argv[i], oid.hash))
60 die("Not a valid object name %s", argv[i]);
61 assert_sha1_type(oid.hash, OBJ_COMMIT);
62 new_parent(lookup_commit(&oid), &parents);
63 continue;
66 if (skip_prefix(arg, "-S", &sign_commit))
67 continue;
69 if (!strcmp(arg, "--no-gpg-sign")) {
70 sign_commit = NULL;
71 continue;
74 if (!strcmp(arg, "-m")) {
75 if (argc <= ++i)
76 usage(commit_tree_usage);
77 if (buffer.len)
78 strbuf_addch(&buffer, '\n');
79 strbuf_addstr(&buffer, argv[i]);
80 strbuf_complete_line(&buffer);
81 continue;
84 if (!strcmp(arg, "-F")) {
85 int fd;
87 if (argc <= ++i)
88 usage(commit_tree_usage);
89 if (buffer.len)
90 strbuf_addch(&buffer, '\n');
91 if (!strcmp(argv[i], "-"))
92 fd = 0;
93 else {
94 fd = open(argv[i], O_RDONLY);
95 if (fd < 0)
96 die_errno("git commit-tree: failed to open '%s'",
97 argv[i]);
99 if (strbuf_read(&buffer, fd, 0) < 0)
100 die_errno("git commit-tree: failed to read '%s'",
101 argv[i]);
102 if (fd && close(fd))
103 die_errno("git commit-tree: failed to close '%s'",
104 argv[i]);
105 strbuf_complete_line(&buffer);
106 continue;
109 if (get_sha1_tree(arg, tree_oid.hash))
110 die("Not a valid object name %s", arg);
111 if (got_tree)
112 die("Cannot give more than one trees");
113 got_tree = 1;
116 if (!buffer.len) {
117 if (strbuf_read(&buffer, 0, 0) < 0)
118 die_errno("git commit-tree: failed to read");
121 if (commit_tree(buffer.buf, buffer.len, tree_oid.hash, parents,
122 commit_oid.hash, NULL, sign_commit)) {
123 strbuf_release(&buffer);
124 return 1;
127 printf("%s\n", oid_to_hex(&commit_oid));
128 strbuf_release(&buffer);
129 return 0;