count-objects: report garbage files in pack directory too
[git/jrn.git] / builtin / commit-tree.c
blobeac901a0ee15f72eacffea32d1b327c7a336f19c
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 [(-p <sha1>)...] [-S<signer>] [-m <message>] [-F <file>] <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, got_tree = 0;
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 for (i = 1; i < argc; i++) {
52 const char *arg = argv[i];
53 if (!strcmp(arg, "-p")) {
54 unsigned char sha1[20];
55 if (argc <= ++i)
56 usage(commit_tree_usage);
57 if (get_sha1_commit(argv[i], sha1))
58 die("Not a valid object name %s", argv[i]);
59 assert_sha1_type(sha1, OBJ_COMMIT);
60 new_parent(lookup_commit(sha1), &parents);
61 continue;
64 if (!memcmp(arg, "-S", 2)) {
65 sign_commit = arg + 2;
66 continue;
69 if (!strcmp(arg, "-m")) {
70 if (argc <= ++i)
71 usage(commit_tree_usage);
72 if (buffer.len)
73 strbuf_addch(&buffer, '\n');
74 strbuf_addstr(&buffer, argv[i]);
75 strbuf_complete_line(&buffer);
76 continue;
79 if (!strcmp(arg, "-F")) {
80 int fd;
82 if (argc <= ++i)
83 usage(commit_tree_usage);
84 if (buffer.len)
85 strbuf_addch(&buffer, '\n');
86 if (!strcmp(argv[i], "-"))
87 fd = 0;
88 else {
89 fd = open(argv[i], O_RDONLY);
90 if (fd < 0)
91 die_errno("git commit-tree: failed to open '%s'",
92 argv[i]);
94 if (strbuf_read(&buffer, fd, 0) < 0)
95 die_errno("git commit-tree: failed to read '%s'",
96 argv[i]);
97 if (fd && close(fd))
98 die_errno("git commit-tree: failed to close '%s'",
99 argv[i]);
100 strbuf_complete_line(&buffer);
101 continue;
104 if (get_sha1_tree(arg, tree_sha1))
105 die("Not a valid object name %s", arg);
106 if (got_tree)
107 die("Cannot give more than one trees");
108 got_tree = 1;
111 if (!buffer.len) {
112 if (strbuf_read(&buffer, 0, 0) < 0)
113 die_errno("git commit-tree: failed to read");
116 if (commit_tree(&buffer, tree_sha1, parents, commit_sha1,
117 NULL, sign_commit)) {
118 strbuf_release(&buffer);
119 return 1;
122 printf("%s\n", sha1_to_hex(commit_sha1));
123 strbuf_release(&buffer);
124 return 0;