Initial revision of "git", the information manager from hell
[git.git] / read-tree.c
1 #include "cache.h"
2
3 static int unpack(unsigned char *sha1)
4 {
5         void *buffer;
6         unsigned long size;
7         char type[20];
8
9         buffer = read_sha1_file(sha1, type, &size);
10         if (!buffer)
11                 usage("unable to read sha1 file");
12         if (strcmp(type, "tree"))
13                 usage("expected a 'tree' node");
14         while (size) {
15                 int len = strlen(buffer)+1;
16                 unsigned char *sha1 = buffer + len;
17                 char *path = strchr(buffer, ' ')+1;
18                 unsigned int mode;
19                 if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
20                         usage("corrupt 'tree' file");
21                 buffer = sha1 + 20;
22                 size -= len + 20;
23                 printf("%o %s (%s)\n", mode, path, sha1_to_hex(sha1));
24         }
25         return 0;
26 }
27
28 int main(int argc, char **argv)
29 {
30         int fd;
31         unsigned char sha1[20];
32
33         if (argc != 2)
34                 usage("read-tree <key>");
35         if (get_sha1_hex(argv[1], sha1) < 0)
36                 usage("read-tree <key>");
37         sha1_file_directory = getenv(DB_ENVIRONMENT);
38         if (!sha1_file_directory)
39                 sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
40         if (unpack(sha1) < 0)
41                 usage("unpack failed");
42         return 0;
43 }