2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 #include "object-store.h"
9 #include "repository.h"
14 #include "gpg-interface.h"
16 static const char commit_tree_usage
[] = "git commit-tree [(-p <sha1>)...] [-S[<keyid>]] [-m <message>] [-F <file>] <sha1>";
18 static const char *sign_commit
;
20 static void new_parent(struct commit
*parent
, struct commit_list
**parents_p
)
22 struct object_id
*oid
= &parent
->object
.oid
;
23 struct commit_list
*parents
;
24 for (parents
= *parents_p
; parents
; parents
= parents
->next
) {
25 if (parents
->item
== parent
) {
26 error("duplicate parent %s ignored", oid_to_hex(oid
));
29 parents_p
= &parents
->next
;
31 commit_list_insert(parent
, parents_p
);
34 static int commit_tree_config(const char *var
, const char *value
, void *cb
)
36 int status
= git_gpg_config(var
, value
, NULL
);
39 return git_default_config(var
, value
, cb
);
42 int cmd_commit_tree(int argc
, const char **argv
, const char *prefix
)
45 struct commit_list
*parents
= NULL
;
46 struct object_id tree_oid
;
47 struct object_id commit_oid
;
48 struct strbuf buffer
= STRBUF_INIT
;
50 git_config(commit_tree_config
, NULL
);
52 if (argc
< 2 || !strcmp(argv
[1], "-h"))
53 usage(commit_tree_usage
);
55 for (i
= 1; i
< argc
; i
++) {
56 const char *arg
= argv
[i
];
57 if (!strcmp(arg
, "-p")) {
60 usage(commit_tree_usage
);
61 if (get_oid_commit(argv
[i
], &oid
))
62 die("Not a valid object name %s", argv
[i
]);
63 assert_oid_type(&oid
, OBJ_COMMIT
);
64 new_parent(lookup_commit(the_repository
, &oid
),
69 if (skip_prefix(arg
, "-S", &sign_commit
))
72 if (!strcmp(arg
, "--no-gpg-sign")) {
77 if (!strcmp(arg
, "-m")) {
79 usage(commit_tree_usage
);
81 strbuf_addch(&buffer
, '\n');
82 strbuf_addstr(&buffer
, argv
[i
]);
83 strbuf_complete_line(&buffer
);
87 if (!strcmp(arg
, "-F")) {
91 usage(commit_tree_usage
);
93 strbuf_addch(&buffer
, '\n');
94 if (!strcmp(argv
[i
], "-"))
97 fd
= open(argv
[i
], O_RDONLY
);
99 die_errno("git commit-tree: failed to open '%s'",
102 if (strbuf_read(&buffer
, fd
, 0) < 0)
103 die_errno("git commit-tree: failed to read '%s'",
106 die_errno("git commit-tree: failed to close '%s'",
111 if (get_oid_tree(arg
, &tree_oid
))
112 die("Not a valid object name %s", arg
);
114 die("Cannot give more than one trees");
119 if (strbuf_read(&buffer
, 0, 0) < 0)
120 die_errno("git commit-tree: failed to read");
123 if (commit_tree(buffer
.buf
, buffer
.len
, &tree_oid
, parents
, &commit_oid
,
124 NULL
, sign_commit
)) {
125 strbuf_release(&buffer
);
129 printf("%s\n", oid_to_hex(&commit_oid
));
130 strbuf_release(&buffer
);