This implements the new "recursive tree" write-tree.
[git.git] / write-tree.c
1 /*
2  * GIT - The information manager from hell
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  */
6 #include "cache.h"
7
8 static int check_valid_sha1(unsigned char *sha1)
9 {
10         char *filename = sha1_file_name(sha1);
11         int ret;
12
13         /* If we were anal, we'd check that the sha1 of the contents actually matches */
14         ret = access(filename, R_OK);
15         if (ret)
16                 perror(filename);
17         return ret;
18 }
19
20 static int prepend_integer(char *buffer, unsigned val, int i)
21 {
22         buffer[--i] = '\0';
23         do {
24                 buffer[--i] = '0' + (val % 10);
25                 val /= 10;
26         } while (val);
27         return i;
28 }
29
30 #define ORIG_OFFSET (40)        /* Enough space to add the header of "tree <size>\0" */
31
32 static int write_tree(struct cache_entry **cachep, int maxentries, const char *base, int baselen, unsigned char *returnsha1)
33 {
34         unsigned char subdir_sha1[20];
35         unsigned long size, offset;
36         char *buffer;
37         int i, nr;
38
39         /* Guess at some random initial size */
40         size = 8192;
41         buffer = malloc(size);
42         offset = ORIG_OFFSET;
43
44         nr = 0;
45         do {
46                 struct cache_entry *ce = cachep[nr];
47                 const char *pathname = ce->name, *filename, *dirname;
48                 int pathlen = ce->namelen, entrylen;
49                 unsigned char *sha1;
50                 unsigned int mode;
51
52                 /* Did we hit the end of the directory? Return how many we wrote */
53                 if (baselen >= pathlen || memcmp(base, pathname, baselen))
54                         break;
55
56                 sha1 = ce->sha1;
57                 mode = ce->st_mode;
58
59                 /* Do we have _further_ subdirectories? */
60                 filename = pathname + baselen;
61                 dirname = strchr(filename, '/');
62                 if (dirname) {
63                         int subdir_written;
64
65                         subdir_written = write_tree(cachep + nr, maxentries - nr, pathname, dirname-pathname+1, subdir_sha1);
66                         fprintf(stderr, "Wrote %d entries from subdirectory '%.*s'\n", 
67                                 subdir_written, dirname-pathname, pathname);
68                         nr += subdir_written;
69
70                         /* Now we need to write out the directory entry into this tree.. */
71                         mode = S_IFDIR;
72                         pathlen = dirname - pathname;
73
74                         /* ..but the directory entry doesn't count towards the total count */
75                         nr--;
76                         sha1 = subdir_sha1;
77                 }
78
79                 if (check_valid_sha1(sha1) < 0)
80                         exit(1);
81
82                 entrylen = pathlen - baselen;
83                 if (offset + entrylen + 100 > size) {
84                         size = alloc_nr(offset + entrylen + 100);
85                         buffer = realloc(buffer, size);
86                 }
87                 offset += sprintf(buffer + offset, "%o %.*s", mode, entrylen, filename);
88                 buffer[offset++] = 0;
89                 memcpy(buffer + offset, sha1, 20);
90                 offset += 20;
91                 nr++;
92         } while (nr < maxentries);
93
94         i = prepend_integer(buffer, offset - ORIG_OFFSET, ORIG_OFFSET);
95         i -= 5;
96         memcpy(buffer+i, "tree ", 5);
97
98         buffer += i;
99         offset -= i;
100
101         write_sha1_file(buffer, offset, returnsha1);
102         return nr;
103 }
104
105 int main(int argc, char **argv)
106 {
107         int entries = read_cache();
108         unsigned char sha1[20];
109
110         if (entries <= 0)
111                 usage("no cache contents to write");
112         if (write_tree(active_cache, entries, "", 0, sha1) != entries)
113                 usage("write-tree: internal error");
114         printf("%s\n", sha1_to_hex(sha1));
115         return 0;
116 }