2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 static int read_one_entry(unsigned char *sha1, const char *pathname, unsigned mode)
10 int len = strlen(pathname);
11 unsigned int size = cache_entry_size(len);
12 struct cache_entry *ce = malloc(size);
18 memcpy(ce->name, pathname, len+1);
19 memcpy(ce->sha1, sha1, 20);
20 return add_cache_entry(ce);
23 static int read_tree(unsigned char *sha1)
29 buffer = read_sha1_file(sha1, type, &size);
32 if (strcmp(type, "tree"))
35 int len = strlen(buffer)+1;
36 unsigned char *sha1 = buffer + len;
37 char *path = strchr(buffer, ' ')+1;
40 if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
46 if (read_one_entry(sha1, path, mode) < 0)
52 int main(int argc, char **argv)
55 unsigned char sha1[20];
57 newfd = open(".dircache/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600);
59 usage("unable to create new cachefile");
61 for (i = 1; i < argc; i++) {
62 const char *arg = argv[i];
64 /* "-m" stands for "merge" current directory cache */
65 if (!strcmp(arg, "-m")) {
67 fprintf(stderr, "read-tree: cannot merge old cache on top of new\n");
70 if (read_cache() < 0) {
71 fprintf(stderr, "read-tree: corrupt directory cache\n");
76 if (get_sha1_hex(arg, sha1) < 0) {
77 fprintf(stderr, "read-tree [-m] <sha1>\n");
80 if (read_tree(sha1) < 0) {
81 fprintf(stderr, "failed to unpack tree object %s\n", arg);
85 if (!write_cache(newfd, active_cache, active_nr) && !rename(".dircache/index.lock", ".dircache/index"))
89 unlink(".dircache/index.lock");