]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/commit-tree.c
object-store: move object access functions to object-store.h
[thirdparty/git.git] / builtin / commit-tree.c
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 "commit.h"
10 #include "tree.h"
11 #include "builtin.h"
12 #include "utf8.h"
13 #include "gpg-interface.h"
14
15 static const char commit_tree_usage[] = "git commit-tree [(-p <sha1>)...] [-S[<keyid>]] [-m <message>] [-F <file>] <sha1>";
16
17 static const char *sign_commit;
18
19 static void new_parent(struct commit *parent, struct commit_list **parents_p)
20 {
21 struct object_id *oid = &parent->object.oid;
22 struct commit_list *parents;
23 for (parents = *parents_p; parents; parents = parents->next) {
24 if (parents->item == parent) {
25 error("duplicate parent %s ignored", oid_to_hex(oid));
26 return;
27 }
28 parents_p = &parents->next;
29 }
30 commit_list_insert(parent, parents_p);
31 }
32
33 static int commit_tree_config(const char *var, const char *value, void *cb)
34 {
35 int status = git_gpg_config(var, value, NULL);
36 if (status)
37 return status;
38 return git_default_config(var, value, cb);
39 }
40
41 int cmd_commit_tree(int argc, const char **argv, const char *prefix)
42 {
43 int i, got_tree = 0;
44 struct commit_list *parents = NULL;
45 struct object_id tree_oid;
46 struct object_id commit_oid;
47 struct strbuf buffer = STRBUF_INIT;
48
49 git_config(commit_tree_config, NULL);
50
51 if (argc < 2 || !strcmp(argv[1], "-h"))
52 usage(commit_tree_usage);
53
54 for (i = 1; i < argc; i++) {
55 const char *arg = argv[i];
56 if (!strcmp(arg, "-p")) {
57 struct object_id oid;
58 if (argc <= ++i)
59 usage(commit_tree_usage);
60 if (get_oid_commit(argv[i], &oid))
61 die("Not a valid object name %s", argv[i]);
62 assert_oid_type(&oid, OBJ_COMMIT);
63 new_parent(lookup_commit(&oid), &parents);
64 continue;
65 }
66
67 if (skip_prefix(arg, "-S", &sign_commit))
68 continue;
69
70 if (!strcmp(arg, "--no-gpg-sign")) {
71 sign_commit = NULL;
72 continue;
73 }
74
75 if (!strcmp(arg, "-m")) {
76 if (argc <= ++i)
77 usage(commit_tree_usage);
78 if (buffer.len)
79 strbuf_addch(&buffer, '\n');
80 strbuf_addstr(&buffer, argv[i]);
81 strbuf_complete_line(&buffer);
82 continue;
83 }
84
85 if (!strcmp(arg, "-F")) {
86 int fd;
87
88 if (argc <= ++i)
89 usage(commit_tree_usage);
90 if (buffer.len)
91 strbuf_addch(&buffer, '\n');
92 if (!strcmp(argv[i], "-"))
93 fd = 0;
94 else {
95 fd = open(argv[i], O_RDONLY);
96 if (fd < 0)
97 die_errno("git commit-tree: failed to open '%s'",
98 argv[i]);
99 }
100 if (strbuf_read(&buffer, fd, 0) < 0)
101 die_errno("git commit-tree: failed to read '%s'",
102 argv[i]);
103 if (fd && close(fd))
104 die_errno("git commit-tree: failed to close '%s'",
105 argv[i]);
106 continue;
107 }
108
109 if (get_oid_tree(arg, &tree_oid))
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;
114 }
115
116 if (!buffer.len) {
117 if (strbuf_read(&buffer, 0, 0) < 0)
118 die_errno("git commit-tree: failed to read");
119 }
120
121 if (commit_tree(buffer.buf, buffer.len, &tree_oid, parents, &commit_oid,
122 NULL, sign_commit)) {
123 strbuf_release(&buffer);
124 return 1;
125 }
126
127 printf("%s\n", oid_to_hex(&commit_oid));
128 strbuf_release(&buffer);
129 return 0;
130 }