@@ -44,30 +44,23 @@ static int ent_compare(const void *a_, const void *b_)
44
44
45
45
static void write_tree (unsigned char * sha1 )
46
46
{
47
- char * buffer ;
48
- unsigned long size , offset ;
47
+ struct strbuf buf ;
48
+ size_t size ;
49
49
int i ;
50
50
51
51
qsort (entries , used , sizeof (* entries ), ent_compare );
52
52
for (size = i = 0 ; i < used ; i ++ )
53
53
size += 32 + entries [i ]-> len ;
54
- buffer = xmalloc ( size );
55
- offset = 0 ;
54
+ strbuf_init ( & buf );
55
+ strbuf_grow ( & buf , size ) ;
56
56
57
57
for (i = 0 ; i < used ; i ++ ) {
58
58
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 );
69
61
}
70
- write_sha1_file (buffer , offset , tree_type , sha1 );
62
+
63
+ write_sha1_file (buf .buf , buf .len , tree_type , sha1 );
71
64
}
72
65
73
66
static const char mktree_usage [] = "git-mktree [-z]" ;
0 commit comments