2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 static int check_valid_sha1(unsigned char *sha1)
10 char *filename = sha1_file_name(sha1);
13 /* If we were anal, we'd check that the sha1 of the contents actually matches */
14 ret = access(filename, R_OK);
20 static int write_tree(struct cache_entry **cachep, int maxentries, const char *base, int baselen, unsigned char *returnsha1)
22 unsigned char subdir_sha1[20];
23 unsigned long size, offset;
27 /* Guess at some random initial size */
29 buffer = xmalloc(size);
33 while (nr < maxentries) {
34 struct cache_entry *ce = cachep[nr];
35 const char *pathname = ce->name, *filename, *dirname;
36 int pathlen = ce_namelen(ce), entrylen;
40 /* Did we hit the end of the directory? Return how many we wrote */
41 if (baselen >= pathlen || memcmp(base, pathname, baselen))
45 mode = ntohl(ce->ce_mode);
47 /* Do we have _further_ subdirectories? */
48 filename = pathname + baselen;
49 dirname = strchr(filename, '/');
53 subdir_written = write_tree(cachep + nr, maxentries - nr, pathname, dirname-pathname+1, subdir_sha1);
56 /* Now we need to write out the directory entry into this tree.. */
58 pathlen = dirname - pathname;
60 /* ..but the directory entry doesn't count towards the total count */
65 if (check_valid_sha1(sha1) < 0)
68 entrylen = pathlen - baselen;
69 if (offset + entrylen + 100 > size) {
70 size = alloc_nr(offset + entrylen + 100);
71 buffer = xrealloc(buffer, size);
73 offset += sprintf(buffer + offset, "%o %.*s", mode, entrylen, filename);
75 memcpy(buffer + offset, sha1, 20);
80 write_sha1_file(buffer, offset, "tree", returnsha1);
85 int main(int argc, char **argv)
88 int entries = read_cache();
89 unsigned char sha1[20];
92 die("write-tree: error reading cache");
94 /* Verify that the tree is merged */
96 for (i = 0; i < entries; i++) {
97 struct cache_entry *ce = active_cache[i];
98 if (ntohs(ce->ce_flags) & ~CE_NAMEMASK) {
100 fprintf(stderr, "...\n");
103 fprintf(stderr, "%s: unmerged (%s)\n", ce->name, sha1_to_hex(ce->sha1));
107 die("write-tree: not able to write tree");
109 /* Also verify that the cache does not have path and path/file
110 * at the same time. At this point we know the cache has only
114 for (i = 0; i < entries - 1; i++) {
115 /* path/file always comes after path because of the way
116 * the cache is sorted. Also path can appear only once,
117 * which means conflicting one would immediately follow.
119 const char *this_name = active_cache[i]->name;
120 const char *next_name = active_cache[i+1]->name;
121 int this_len = strlen(this_name);
122 if (this_len < strlen(next_name) &&
123 strncmp(this_name, next_name, this_len) == 0 &&
124 next_name[this_len] == '/') {
126 fprintf(stderr, "...\n");
129 fprintf(stderr, "You have both %s and %s\n",
130 this_name, next_name);
134 die("write-tree: not able to write tree");
136 /* Ok, write it out */
137 if (write_tree(active_cache, entries, "", 0, sha1) != entries)
138 die("write-tree: internal error");
139 printf("%s\n", sha1_to_hex(sha1));