2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
11 #include "gpg-interface.h"
13 static const char commit_tree_usage
[] = "git commit-tree [(-p <sha1>)...] [-S[<keyid>]] [-m <message>] [-F <file>] <sha1> <changelog";
15 static const char *sign_commit
;
17 static void new_parent(struct commit
*parent
, struct commit_list
**parents_p
)
19 unsigned char *sha1
= parent
->object
.sha1
;
20 struct commit_list
*parents
;
21 for (parents
= *parents_p
; parents
; parents
= parents
->next
) {
22 if (parents
->item
== parent
) {
23 error("duplicate parent %s ignored", sha1_to_hex(sha1
));
26 parents_p
= &parents
->next
;
28 commit_list_insert(parent
, parents_p
);
31 static int commit_tree_config(const char *var
, const char *value
, void *cb
)
33 int status
= git_gpg_config(var
, value
, NULL
);
36 if (!strcmp(var
, "commit.gpgsign")) {
37 sign_commit
= git_config_bool(var
, value
) ? "" : NULL
;
40 return git_default_config(var
, value
, cb
);
43 int cmd_commit_tree(int argc
, const char **argv
, const char *prefix
)
46 struct commit_list
*parents
= NULL
;
47 unsigned char tree_sha1
[20];
48 unsigned char commit_sha1
[20];
49 struct strbuf buffer
= STRBUF_INIT
;
51 git_config(commit_tree_config
, NULL
);
53 if (argc
< 2 || !strcmp(argv
[1], "-h"))
54 usage(commit_tree_usage
);
56 for (i
= 1; i
< argc
; i
++) {
57 const char *arg
= argv
[i
];
58 if (!strcmp(arg
, "-p")) {
59 unsigned char sha1
[20];
61 usage(commit_tree_usage
);
62 if (get_sha1_commit(argv
[i
], sha1
))
63 die("Not a valid object name %s", argv
[i
]);
64 assert_sha1_type(sha1
, OBJ_COMMIT
);
65 new_parent(lookup_commit(sha1
), &parents
);
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'",
108 strbuf_complete_line(&buffer
);
112 if (get_sha1_tree(arg
, tree_sha1
))
113 die("Not a valid object name %s", arg
);
115 die("Cannot give more than one trees");
120 if (strbuf_read(&buffer
, 0, 0) < 0)
121 die_errno("git commit-tree: failed to read");
124 if (commit_tree(buffer
.buf
, buffer
.len
, tree_sha1
, parents
,
125 commit_sha1
, NULL
, sign_commit
)) {
126 strbuf_release(&buffer
);
130 printf("%s\n", sha1_to_hex(commit_sha1
));
131 strbuf_release(&buffer
);