|
8 | 8 | #include "tree.h" |
9 | 9 | #include "builtin.h" |
10 | 10 | #include "utf8.h" |
| 11 | +#include "strbuf.h" |
11 | 12 |
|
12 | 13 | #define BLOCKING (1ul << 14) |
13 | 14 |
|
14 | 15 | /* |
15 | 16 | * FIXME! Share the code with "write-tree.c" |
16 | 17 | */ |
17 | | -static void init_buffer(char **bufp, unsigned int *sizep) |
18 | | -{ |
19 | | - *bufp = xmalloc(BLOCKING); |
20 | | - *sizep = 0; |
21 | | -} |
22 | | - |
23 | | -static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...) |
24 | | -{ |
25 | | - char one_line[2048]; |
26 | | - va_list args; |
27 | | - int len; |
28 | | - unsigned long alloc, size, newsize; |
29 | | - char *buf; |
30 | | - |
31 | | - va_start(args, fmt); |
32 | | - len = vsnprintf(one_line, sizeof(one_line), fmt, args); |
33 | | - va_end(args); |
34 | | - size = *sizep; |
35 | | - newsize = size + len + 1; |
36 | | - alloc = (size + 32767) & ~32767; |
37 | | - buf = *bufp; |
38 | | - if (newsize > alloc) { |
39 | | - alloc = (newsize + 32767) & ~32767; |
40 | | - buf = xrealloc(buf, alloc); |
41 | | - *bufp = buf; |
42 | | - } |
43 | | - *sizep = newsize - 1; |
44 | | - memcpy(buf + size, one_line, len); |
45 | | -} |
46 | | - |
47 | 18 | static void check_valid(unsigned char *sha1, enum object_type expect) |
48 | 19 | { |
49 | 20 | enum object_type type = sha1_object_info(sha1, NULL); |
@@ -87,9 +58,7 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix) |
87 | 58 | int parents = 0; |
88 | 59 | unsigned char tree_sha1[20]; |
89 | 60 | unsigned char commit_sha1[20]; |
90 | | - char comment[1000]; |
91 | | - char *buffer; |
92 | | - unsigned int size; |
| 61 | + struct strbuf buffer; |
93 | 62 | int encoding_is_utf8; |
94 | 63 |
|
95 | 64 | git_config(git_default_config); |
@@ -118,35 +87,34 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix) |
118 | 87 | /* Not having i18n.commitencoding is the same as having utf-8 */ |
119 | 88 | encoding_is_utf8 = is_encoding_utf8(git_commit_encoding); |
120 | 89 |
|
121 | | - init_buffer(&buffer, &size); |
122 | | - add_buffer(&buffer, &size, "tree %s\n", sha1_to_hex(tree_sha1)); |
| 90 | + strbuf_init(&buffer); |
| 91 | + strbuf_grow(&buffer, 8192); /* should avoid reallocs for the headers */ |
| 92 | + strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree_sha1)); |
123 | 93 |
|
124 | 94 | /* |
125 | 95 | * NOTE! This ordering means that the same exact tree merged with a |
126 | 96 | * different order of parents will be a _different_ changeset even |
127 | 97 | * if everything else stays the same. |
128 | 98 | */ |
129 | 99 | for (i = 0; i < parents; i++) |
130 | | - add_buffer(&buffer, &size, "parent %s\n", sha1_to_hex(parent_sha1[i])); |
| 100 | + strbuf_addf(&buffer, "parent %s\n", sha1_to_hex(parent_sha1[i])); |
131 | 101 |
|
132 | 102 | /* Person/date information */ |
133 | | - add_buffer(&buffer, &size, "author %s\n", git_author_info(1)); |
134 | | - add_buffer(&buffer, &size, "committer %s\n", git_committer_info(1)); |
| 103 | + strbuf_addf(&buffer, "author %s\n", git_author_info(1)); |
| 104 | + strbuf_addf(&buffer, "committer %s\n", git_committer_info(1)); |
135 | 105 | if (!encoding_is_utf8) |
136 | | - add_buffer(&buffer, &size, |
137 | | - "encoding %s\n", git_commit_encoding); |
138 | | - add_buffer(&buffer, &size, "\n"); |
| 106 | + strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding); |
| 107 | + strbuf_addch(&buffer, '\n'); |
139 | 108 |
|
140 | 109 | /* And add the comment */ |
141 | | - while (fgets(comment, sizeof(comment), stdin) != NULL) |
142 | | - add_buffer(&buffer, &size, "%s", comment); |
| 110 | + if (strbuf_read(&buffer, 0) < 0) |
| 111 | + die("git-commit-tree: read returned %s", strerror(errno)); |
143 | 112 |
|
144 | 113 | /* And check the encoding */ |
145 | | - buffer[size] = '\0'; |
146 | | - if (encoding_is_utf8 && !is_utf8(buffer)) |
| 114 | + if (encoding_is_utf8 && !is_utf8(buffer.buf)) |
147 | 115 | fprintf(stderr, commit_utf8_warn); |
148 | 116 |
|
149 | | - if (!write_sha1_file(buffer, size, commit_type, commit_sha1)) { |
| 117 | + if (!write_sha1_file(buffer.buf, buffer.len, commit_type, commit_sha1)) { |
150 | 118 | printf("%s\n", sha1_to_hex(commit_sha1)); |
151 | 119 | return 0; |
152 | 120 | } |
|
0 commit comments