Windows: boost startup by avoiding a static dependency on shell32.dll
[git/dscho.git] / builtin-commit-tree.c
blobddcb7a4bbbcd45dfd7437209f3187fff882e6e62
1 /*
2  * GIT - The information manager from hell
3  *
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"
12 #define BLOCKING (1ul << 14)
15  * FIXME! Share the code with "write-tree.c"
16  */
17 static void check_valid(unsigned char *sha1, enum object_type expect)
19         enum object_type type = sha1_object_info(sha1, NULL);
20         if (type < 0)
21                 die("%s is not a valid object", sha1_to_hex(sha1));
22         if (type != expect)
23                 die("%s is not a valid '%s' object", sha1_to_hex(sha1),
24                     typename(expect));
27 static const char commit_tree_usage[] = "git commit-tree <sha1> [-p <sha1>]* < changelog";
29 static void new_parent(struct commit *parent, struct commit_list **parents_p)
31         unsigned char *sha1 = parent->object.sha1;
32         struct commit_list *parents;
33         for (parents = *parents_p; parents; parents = parents->next) {
34                 if (parents->item == parent) {
35                         error("duplicate parent %s ignored", sha1_to_hex(sha1));
36                         return;
37                 }
38                 parents_p = &parents->next;
39         }
40         commit_list_insert(parent, parents_p);
43 static const char commit_utf8_warn[] =
44 "Warning: commit message does not conform to UTF-8.\n"
45 "You may want to amend it after fixing the message, or set the config\n"
46 "variable i18n.commitencoding to the encoding your project uses.\n";
48 int commit_tree(const char *msg, unsigned char *tree,
49                 struct commit_list *parents, unsigned char *ret,
50                 const char *author)
52         int result;
53         int encoding_is_utf8;
54         struct strbuf buffer;
56         check_valid(tree, OBJ_TREE);
58         /* Not having i18n.commitencoding is the same as having utf-8 */
59         encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
61         strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
62         strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
64         /*
65          * NOTE! This ordering means that the same exact tree merged with a
66          * different order of parents will be a _different_ changeset even
67          * if everything else stays the same.
68          */
69         while (parents) {
70                 struct commit_list *next = parents->next;
71                 strbuf_addf(&buffer, "parent %s\n",
72                         sha1_to_hex(parents->item->object.sha1));
73                 free(parents);
74                 parents = next;
75         }
77         /* Person/date information */
78         if (!author)
79                 author = git_author_info(IDENT_ERROR_ON_NO_NAME);
80         strbuf_addf(&buffer, "author %s\n", author);
81         strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
82         if (!encoding_is_utf8)
83                 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
84         strbuf_addch(&buffer, '\n');
86         /* And add the comment */
87         strbuf_addstr(&buffer, msg);
89         /* And check the encoding */
90         if (encoding_is_utf8 && !is_utf8(buffer.buf))
91                 fprintf(stderr, commit_utf8_warn);
93         result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
94         strbuf_release(&buffer);
95         return result;
98 int cmd_commit_tree(int argc, const char **argv, const char *prefix)
100         int i;
101         struct commit_list *parents = NULL;
102         unsigned char tree_sha1[20];
103         unsigned char commit_sha1[20];
104         struct strbuf buffer = STRBUF_INIT;
106         git_config(git_default_config, NULL);
108         if (argc < 2 || !strcmp(argv[1], "-h"))
109                 usage(commit_tree_usage);
110         if (get_sha1(argv[1], tree_sha1))
111                 die("Not a valid object name %s", argv[1]);
113         for (i = 2; i < argc; i += 2) {
114                 unsigned char sha1[20];
115                 const char *a, *b;
116                 a = argv[i]; b = argv[i+1];
117                 if (!b || strcmp(a, "-p"))
118                         usage(commit_tree_usage);
120                 if (get_sha1(b, sha1))
121                         die("Not a valid object name %s", b);
122                 check_valid(sha1, OBJ_COMMIT);
123                 new_parent(lookup_commit(sha1), &parents);
124         }
126         if (strbuf_read(&buffer, 0, 0) < 0)
127                 die_errno("git commit-tree: failed to read");
129         if (!commit_tree(buffer.buf, tree_sha1, parents, commit_sha1, NULL)) {
130                 printf("%s\n", sha1_to_hex(commit_sha1));
131                 return 0;
132         }
133         else
134                 return 1;