submodule: Use cat instead of echo to avoid DOS line-endings
[git/dscho.git] / builtin / commit-tree.c
bloba17811f958a5884abf1462772b0bec7646626df0
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "commit.h"
8 #include "tree.h"
9 #include "builtin.h"
10 #include "utf8.h"
11 #include "gpg-interface.h"
13 static const char commit_tree_usage[] = "git commit-tree [-S<signer>] <sha1> [(-p <sha1>)...] < changelog";
15 static void new_parent(struct commit *parent, struct commit_list **parents_p)
17 unsigned char *sha1 = parent->object.sha1;
18 struct commit_list *parents;
19 for (parents = *parents_p; parents; parents = parents->next) {
20 if (parents->item == parent) {
21 error("duplicate parent %s ignored", sha1_to_hex(sha1));
22 return;
24 parents_p = &parents->next;
26 commit_list_insert(parent, parents_p);
29 static int commit_tree_config(const char *var, const char *value, void *cb)
31 int status = git_gpg_config(var, value, NULL);
32 if (status)
33 return status;
34 return git_default_config(var, value, cb);
37 int cmd_commit_tree(int argc, const char **argv, const char *prefix)
39 int i;
40 struct commit_list *parents = NULL;
41 unsigned char tree_sha1[20];
42 unsigned char commit_sha1[20];
43 struct strbuf buffer = STRBUF_INIT;
44 const char *sign_commit = NULL;
46 git_config(commit_tree_config, NULL);
48 if (argc < 2 || !strcmp(argv[1], "-h"))
49 usage(commit_tree_usage);
51 if (!memcmp(argv[1], "-S", 2)) {
52 sign_commit = argv[1] + 2;
53 argv++;
54 argc--;
57 if (get_sha1(argv[1], tree_sha1))
58 die("Not a valid object name %s", argv[1]);
60 for (i = 2; i < argc; i += 2) {
61 unsigned char sha1[20];
62 const char *a, *b;
63 a = argv[i]; b = argv[i+1];
64 if (!b || strcmp(a, "-p"))
65 usage(commit_tree_usage);
67 if (get_sha1(b, sha1))
68 die("Not a valid object name %s", b);
69 assert_sha1_type(sha1, OBJ_COMMIT);
70 new_parent(lookup_commit(sha1), &parents);
73 if (strbuf_read(&buffer, 0, 0) < 0)
74 die_errno("git commit-tree: failed to read");
76 if (commit_tree(buffer.buf, tree_sha1, parents, commit_sha1,
77 NULL, sign_commit)) {
78 strbuf_release(&buffer);
79 return 1;
82 printf("%s\n", sha1_to_hex(commit_sha1));
83 strbuf_release(&buffer);
84 return 0;