2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
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
));
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
);
37 return git_default_config(var
, value
, cb
);
40 int cmd_commit_tree(int argc
, const char **argv
, const char *prefix
)
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")) {
58 usage(commit_tree_usage
);
59 if (get_oid_commit(argv
[i
], &oid
))
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
);
66 if (skip_prefix(arg
, "-S", &sign_commit
))
69 if (!strcmp(arg
, "--no-gpg-sign")) {
74 if (!strcmp(arg
, "-m")) {
76 usage(commit_tree_usage
);
78 strbuf_addch(&buffer
, '\n');
79 strbuf_addstr(&buffer
, argv
[i
]);
80 strbuf_complete_line(&buffer
);
84 if (!strcmp(arg
, "-F")) {
88 usage(commit_tree_usage
);
90 strbuf_addch(&buffer
, '\n');
91 if (!strcmp(argv
[i
], "-"))
94 fd
= open(argv
[i
], O_RDONLY
);
96 die_errno("git commit-tree: failed to open '%s'",
99 if (strbuf_read(&buffer
, fd
, 0) < 0)
100 die_errno("git commit-tree: failed to read '%s'",
103 die_errno("git commit-tree: failed to close '%s'",
108 if (get_oid_tree(arg
, &tree_oid
))
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
);