aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre Habouzit <madcoder@debian.org>2007-09-06 13:20:08 +0200
committerJunio C Hamano <gitster@pobox.com>2007-09-06 23:57:44 -0700
commitd52bc66152834dff3fb5f32a54f6ed57730f58c6 (patch)
treec36afebf6797f7d3b3c5c01e8c8a12c44605cf1d
parent4a241d79c9020dcafb1f254774bcab200171ab46 (diff)
downloadgit-d52bc66152834dff3fb5f32a54f6ed57730f58c6.tar.gz
git-d52bc66152834dff3fb5f32a54f6ed57730f58c6.tar.xz
mktree: Simplify write_tree() using strbuf API
Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r--mktree.c23
1 files changed, 8 insertions, 15 deletions
diff --git a/mktree.c b/mktree.c
index 86de5eb5f..2e84889c0 100644
--- a/mktree.c
+++ b/mktree.c
@@ -44,30 +44,23 @@ static int ent_compare(const void *a_, const void *b_)
static void write_tree(unsigned char *sha1)
{
- char *buffer;
- unsigned long size, offset;
+ struct strbuf buf;
+ size_t size;
int i;
qsort(entries, used, sizeof(*entries), ent_compare);
for (size = i = 0; i < used; i++)
size += 32 + entries[i]->len;
- buffer = xmalloc(size);
- offset = 0;
+ strbuf_init(&buf);
+ strbuf_grow(&buf, size);
for (i = 0; i < used; i++) {
struct treeent *ent = entries[i];
-
- if (offset + ent->len + 100 < size) {
- size = alloc_nr(offset + ent->len + 100);
- buffer = xrealloc(buffer, size);
- }
- offset += sprintf(buffer + offset, "%o ", ent->mode);
- offset += sprintf(buffer + offset, "%s", ent->name);
- buffer[offset++] = 0;
- hashcpy((unsigned char*)buffer + offset, ent->sha1);
- offset += 20;
+ strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
+ strbuf_add(&buf, ent->sha1, 20);
}
- write_sha1_file(buffer, offset, tree_type, sha1);
+
+ write_sha1_file(buf.buf, buf.len, tree_type, sha1);
}
static const char mktree_usage[] = "git-mktree [-z]";