Add a "checkout-cache" command which does what the name suggests.
[gitweb.git] / read-tree.c
index 1b47742d8cbc0d98903777758b7b519980e7499e..af486ff33f6f66323b25efc5ce9e3540d5f5bab9 100644 (file)
@@ -1,5 +1,36 @@
+/*
+ * GIT - The information manager from hell
+ *
+ * Copyright (C) Linus Torvalds, 2005
+ */
 #include "cache.h"
 
+static void create_directories(const char *path)
+{
+       int len = strlen(path);
+       char *buf = malloc(len + 1);
+       const char *slash = path;
+
+       while ((slash = strchr(slash+1, '/')) != NULL) {
+               len = slash - path;
+               memcpy(buf, path, len);
+               buf[len] = 0;
+               mkdir(buf, 0700);
+       }
+}
+
+static int create_file(const char *path)
+{
+       int fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0600);
+       if (fd < 0) {
+               if (errno == ENOENT) {
+                       create_directories(path);
+                       fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0600);
+               }
+       }
+       return fd;
+}
+
 static int unpack(unsigned char *sha1)
 {
        void *buffer;
@@ -15,19 +46,32 @@ static int unpack(unsigned char *sha1)
                int len = strlen(buffer)+1;
                unsigned char *sha1 = buffer + len;
                char *path = strchr(buffer, ' ')+1;
+               char *data;
+               unsigned long filesize;
                unsigned int mode;
+               int fd;
+
                if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
                        usage("corrupt 'tree' file");
                buffer = sha1 + 20;
                size -= len + 20;
-               printf("%o %s (%s)\n", mode, path, sha1_to_hex(sha1));
+               data = read_sha1_file(sha1, type, &filesize);
+               if (!data || strcmp(type, "blob"))
+                       usage("tree file refers to bad file data");
+               fd = create_file(path);
+               if (fd < 0)
+                       usage("unable to create file");
+               if (write(fd, data, filesize) != filesize)
+                       usage("unable to write file");
+               fchmod(fd, mode);
+               close(fd);
+               free(data);
        }
        return 0;
 }
 
 int main(int argc, char **argv)
 {
-       int fd;
        unsigned char sha1[20];
 
        if (argc != 2)