提交 d52bc661 编写于 作者: P Pierre Habouzit 提交者: Junio C Hamano

mktree: Simplify write_tree() using strbuf API

Signed-off-by: NPierre Habouzit <madcoder@debian.org>
Signed-off-by: NJunio C Hamano <gitster@pobox.com>
上级 4a241d79
......@@ -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]";
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册