commit: add repository argument to lookup_commit
[git/git.git] / builtin / commit-tree.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163 6#include "cache.h"
b2141fc1 7#include "config.h"
cbd53a21 8#include "object-store.h"
c1f5eb49 9#include "repository.h"
8e440259
PE
10#include "commit.h"
11#include "tree.h"
6d96ac18 12#include "builtin.h"
9e832665 13#include "utf8.h"
ba3c69a9 14#include "gpg-interface.h"
e83c5163 15
33e8fc87 16static const char commit_tree_usage[] = "git commit-tree [(-p <sha1>)...] [-S[<keyid>]] [-m <message>] [-F <file>] <sha1>";
c5bac17a 17
d95bfb12
NV
18static const char *sign_commit;
19
ef98c5ca 20static void new_parent(struct commit *parent, struct commit_list **parents_p)
b389237a 21{
f2fd0760 22 struct object_id *oid = &parent->object.oid;
ef98c5ca
JS
23 struct commit_list *parents;
24 for (parents = *parents_p; parents; parents = parents->next) {
25 if (parents->item == parent) {
f2fd0760 26 error("duplicate parent %s ignored", oid_to_hex(oid));
ef98c5ca 27 return;
b389237a 28 }
ef98c5ca 29 parents_p = &parents->next;
b389237a 30 }
ef98c5ca 31 commit_list_insert(parent, parents_p);
b389237a
LT
32}
33
ba3c69a9
JH
34static int commit_tree_config(const char *var, const char *value, void *cb)
35{
36 int status = git_gpg_config(var, value, NULL);
37 if (status)
38 return status;
39 return git_default_config(var, value, cb);
40}
41
7b9c0a69
MV
42int cmd_commit_tree(int argc, const char **argv, const char *prefix)
43{
79a9312c 44 int i, got_tree = 0;
7b9c0a69 45 struct commit_list *parents = NULL;
031cee5b 46 struct object_id tree_oid;
47 struct object_id commit_oid;
7b9c0a69
MV
48 struct strbuf buffer = STRBUF_INIT;
49
ba3c69a9 50 git_config(commit_tree_config, NULL);
7b9c0a69 51
6e9daeff 52 if (argc < 2 || !strcmp(argv[1], "-h"))
7b9c0a69 53 usage(commit_tree_usage);
7b9c0a69 54
79a9312c
JH
55 for (i = 1; i < argc; i++) {
56 const char *arg = argv[i];
57 if (!strcmp(arg, "-p")) {
031cee5b 58 struct object_id oid;
79a9312c
JH
59 if (argc <= ++i)
60 usage(commit_tree_usage);
e82caf38 61 if (get_oid_commit(argv[i], &oid))
79a9312c 62 die("Not a valid object name %s", argv[i]);
e816caa0 63 assert_oid_type(&oid, OBJ_COMMIT);
c1f5eb49
SB
64 new_parent(lookup_commit(the_repository, &oid),
65 &parents);
79a9312c
JH
66 continue;
67 }
7b9c0a69 68
8547e0f1 69 if (skip_prefix(arg, "-S", &sign_commit))
ba3c69a9 70 continue;
ba3c69a9 71
55ca3f99
JH
72 if (!strcmp(arg, "--no-gpg-sign")) {
73 sign_commit = NULL;
74 continue;
75 }
76
96b8d93a
JH
77 if (!strcmp(arg, "-m")) {
78 if (argc <= ++i)
79 usage(commit_tree_usage);
80 if (buffer.len)
81 strbuf_addch(&buffer, '\n');
82 strbuf_addstr(&buffer, argv[i]);
83 strbuf_complete_line(&buffer);
84 continue;
85 }
86
87 if (!strcmp(arg, "-F")) {
88 int fd;
89
90 if (argc <= ++i)
91 usage(commit_tree_usage);
92 if (buffer.len)
93 strbuf_addch(&buffer, '\n');
94 if (!strcmp(argv[i], "-"))
95 fd = 0;
96 else {
97 fd = open(argv[i], O_RDONLY);
98 if (fd < 0)
99 die_errno("git commit-tree: failed to open '%s'",
100 argv[i]);
101 }
102 if (strbuf_read(&buffer, fd, 0) < 0)
103 die_errno("git commit-tree: failed to read '%s'",
104 argv[i]);
105 if (fd && close(fd))
106 die_errno("git commit-tree: failed to close '%s'",
107 argv[i]);
96b8d93a
JH
108 continue;
109 }
110
e82caf38 111 if (get_oid_tree(arg, &tree_oid))
79a9312c
JH
112 die("Not a valid object name %s", arg);
113 if (got_tree)
114 die("Cannot give more than one trees");
115 got_tree = 1;
7b9c0a69
MV
116 }
117
96b8d93a
JH
118 if (!buffer.len) {
119 if (strbuf_read(&buffer, 0, 0) < 0)
120 die_errno("git commit-tree: failed to read");
121 }
7b9c0a69 122
5078f344
PO
123 if (commit_tree(buffer.buf, buffer.len, &tree_oid, parents, &commit_oid,
124 NULL, sign_commit)) {
79bc2af5 125 strbuf_release(&buffer);
7561d9f5 126 return 1;
79bc2af5
JN
127 }
128
031cee5b 129 printf("%s\n", oid_to_hex(&commit_oid));
79bc2af5
JN
130 strbuf_release(&buffer);
131 return 0;
e83c5163 132}