3 static int check_valid_sha1(unsigned char *sha1
)
5 char *filename
= sha1_file_name(sha1
);
8 /* If we were anal, we'd check that the sha1 of the contents actually matches */
9 ret
= access(filename
, R_OK
);
15 static int prepend_integer(char *buffer
, unsigned val
, int i
)
19 buffer
[--i
] = '0' + (val
% 10);
25 #define ORIG_OFFSET (40) /* Enough space to add the header of "tree <size>\0" */
27 int main(int argc
, char **argv
)
29 unsigned long size
, offset
, val
;
30 int i
, entries
= read_cache();
34 fprintf(stderr
, "No file-cache to create a tree of\n");
38 /* Guess at an initial size */
39 size
= entries
* 40 + 400;
40 buffer
= malloc(size
);
43 for (i
= 0; i
< entries
; i
++) {
44 struct cache_entry
*ce
= active_cache
[i
];
45 if (check_valid_sha1(ce
->sha1
) < 0)
47 if (offset
+ ce
->namelen
+ 60 > size
) {
48 size
= alloc_nr(offset
+ ce
->namelen
+ 60);
49 buffer
= realloc(buffer
, size
);
51 offset
+= sprintf(buffer
+ offset
, "%o %s", ce
->st_mode
, ce
->name
);
53 memcpy(buffer
+ offset
, ce
->sha1
, 20);
57 i
= prepend_integer(buffer
, offset
- ORIG_OFFSET
, ORIG_OFFSET
);
59 memcpy(buffer
+i
, "tree ", 5);
64 write_sha1_file(buffer
, offset
);