This implements the new "recursive tree" write-tree.
[git.git] / fsck-cache.c
index 0a97566..c35acaa 100644 (file)
@@ -20,20 +20,32 @@ static int mark_sha1_seen(unsigned char *sha1, char *tag)
 
 static int fsck_tree(unsigned char *sha1, void *data, unsigned long size)
 {
+       int warn_old_tree = 1;
+
        while (size) {
                int len = 1+strlen(data);
                unsigned char *file_sha1 = data + len;
                char *path = strchr(data, ' ');
-               if (size < len + 20 || !path)
+               unsigned int mode;
+               if (size < len + 20 || !path || sscanf(data, "%o", &mode) != 1)
                        return -1;
+
+               /* Warn about trees that don't do the recursive thing.. */
+               if (warn_old_tree && strchr(path, '/')) {
+                       fprintf(stderr, "warning: fsck-cache: tree %s has full pathnames in it\n", sha1_to_hex(sha1));
+                       warn_old_tree = 0;
+               }
+
                data += len + 20;
                size -= len + 20;
-               mark_needs_sha1(sha1, "blob", file_sha1);
+               mark_needs_sha1(sha1, S_ISDIR(mode) ? "tree" : "blob", file_sha1);
        }
+       return 0;
 }
 
 static int fsck_commit(unsigned char *sha1, void *data, unsigned long size)
 {
+       int parents;
        unsigned char tree_sha1[20];
        unsigned char parent_sha1[20];
 
@@ -43,12 +55,17 @@ static int fsck_commit(unsigned char *sha1, void *data, unsigned long size)
                return -1;
        mark_needs_sha1(sha1, "tree", tree_sha1);
        data += 5 + 40 + 1;     /* "tree " + <hex sha1> + '\n' */
+       parents = 0;
        while (!memcmp(data, "parent ", 7)) {
                if (get_sha1_hex(data + 7, parent_sha1) < 0)
                        return -1;
                mark_needs_sha1(sha1, "commit", parent_sha1);
                data += 7 + 40 + 1;     /* "parent " + <hex sha1> + '\n' */
+               parents++;
        }
+       if (!parents)
+               printf("root: %s\n", sha1_to_hex(sha1));
+       return 0;
 }
 
 static int fsck_entry(unsigned char *sha1, char *tag, void *data, unsigned long size)