@@ -44,30 +44,23 @@ static int ent_compare(const void *a_, const void *b_)
4444
4545static void write_tree (unsigned char * sha1 )
4646{
47- char * buffer ;
48- unsigned long size , offset ;
47+ struct strbuf buf ;
48+ size_t size ;
4949 int i ;
5050
5151 qsort (entries , used , sizeof (* entries ), ent_compare );
5252 for (size = i = 0 ; i < used ; i ++ )
5353 size += 32 + entries [i ]-> len ;
54- buffer = xmalloc ( size );
55- offset = 0 ;
54+ strbuf_init ( & buf );
55+ strbuf_grow ( & buf , size ) ;
5656
5757 for (i = 0 ; i < used ; i ++ ) {
5858 struct treeent * ent = entries [i ];
59-
60- if (offset + ent -> len + 100 < size ) {
61- size = alloc_nr (offset + ent -> len + 100 );
62- buffer = xrealloc (buffer , size );
63- }
64- offset += sprintf (buffer + offset , "%o " , ent -> mode );
65- offset += sprintf (buffer + offset , "%s" , ent -> name );
66- buffer [offset ++ ] = 0 ;
67- hashcpy ((unsigned char * )buffer + offset , ent -> sha1 );
68- offset += 20 ;
59+ strbuf_addf (& buf , "%o %s%c" , ent -> mode , ent -> name , '\0' );
60+ strbuf_add (& buf , ent -> sha1 , 20 );
6961 }
70- write_sha1_file (buffer , offset , tree_type , sha1 );
62+
63+ write_sha1_file (buf .buf , buf .len , tree_type , sha1 );
7164}
7265
7366static const char mktree_usage [] = "git-mktree [-z]" ;
0 commit comments