2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 #include "object-store.h"
13 #include "gpg-interface.h"
15 static const char commit_tree_usage
[] = "git commit-tree [(-p <sha1>)...] [-S[<keyid>]] [-m <message>] [-F <file>] <sha1>";
17 static const char *sign_commit
;
19 static void new_parent(struct commit
*parent
, struct commit_list
**parents_p
)
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
));
28 parents_p
= &parents
->next
;
30 commit_list_insert(parent
, parents_p
);
33 static int commit_tree_config(const char *var
, const char *value
, void *cb
)
35 int status
= git_gpg_config(var
, value
, NULL
);
38 return git_default_config(var
, value
, cb
);
41 int cmd_commit_tree(int argc
, const char **argv
, const char *prefix
)
44 struct commit_list
*parents
= NULL
;
45 struct object_id tree_oid
;
46 struct object_id commit_oid
;
47 struct strbuf buffer
= STRBUF_INIT
;
49 git_config(commit_tree_config
, NULL
);
51 if (argc
< 2 || !strcmp(argv
[1], "-h"))
52 usage(commit_tree_usage
);
54 for (i
= 1; i
< argc
; i
++) {
55 const char *arg
= argv
[i
];
56 if (!strcmp(arg
, "-p")) {
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
);
67 if (skip_prefix(arg
, "-S", &sign_commit
))
70 if (!strcmp(arg
, "--no-gpg-sign")) {
75 if (!strcmp(arg
, "-m")) {
77 usage(commit_tree_usage
);
79 strbuf_addch(&buffer
, '\n');
80 strbuf_addstr(&buffer
, argv
[i
]);
81 strbuf_complete_line(&buffer
);
85 if (!strcmp(arg
, "-F")) {
89 usage(commit_tree_usage
);
91 strbuf_addch(&buffer
, '\n');
92 if (!strcmp(argv
[i
], "-"))
95 fd
= open(argv
[i
], O_RDONLY
);
97 die_errno("git commit-tree: failed to open '%s'",
100 if (strbuf_read(&buffer
, fd
, 0) < 0)
101 die_errno("git commit-tree: failed to read '%s'",
104 die_errno("git commit-tree: failed to close '%s'",
109 if (get_oid_tree(arg
, &tree_oid
))
110 die("Not a valid object name %s", arg
);
112 die("Cannot give more than one trees");
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
, parents
, &commit_oid
,
122 NULL
, sign_commit
)) {
123 strbuf_release(&buffer
);
127 printf("%s\n", oid_to_hex(&commit_oid
));
128 strbuf_release(&buffer
);