2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 static int read_one_entry(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode)
10 int len = strlen(pathname);
11 unsigned int size = cache_entry_size(baselen + len);
12 struct cache_entry *ce = malloc(size);
17 ce->namelen = baselen + len;
18 memcpy(ce->name, base, baselen);
19 memcpy(ce->name + baselen, pathname, len+1);
20 memcpy(ce->sha1, sha1, 20);
21 return add_cache_entry(ce);
24 static int read_tree(unsigned char *sha1, const char *base, int baselen)
30 buffer = read_sha1_file(sha1, type, &size);
33 if (strcmp(type, "tree"))
36 int len = strlen(buffer)+1;
37 unsigned char *sha1 = buffer + len;
38 char *path = strchr(buffer, ' ')+1;
41 if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
49 int pathlen = strlen(path);
50 char *newbase = malloc(baselen + 1 + pathlen);
51 memcpy(newbase, base, baselen);
52 memcpy(newbase + baselen, path, pathlen);
53 newbase[baselen + pathlen] = '/';
54 retval = read_tree(sha1, newbase, baselen + pathlen + 1);
60 if (read_one_entry(sha1, base, baselen, path, mode) < 0)
66 int main(int argc, char **argv)
69 unsigned char sha1[20];
71 newfd = open(".dircache/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600);
73 usage("unable to create new cachefile");
75 for (i = 1; i < argc; i++) {
76 const char *arg = argv[i];
78 /* "-m" stands for "merge" current directory cache */
79 if (!strcmp(arg, "-m")) {
81 fprintf(stderr, "read-tree: cannot merge old cache on top of new\n");
84 if (read_cache() < 0) {
85 fprintf(stderr, "read-tree: corrupt directory cache\n");
90 if (get_sha1_hex(arg, sha1) < 0) {
91 fprintf(stderr, "read-tree [-m] <sha1>\n");
94 if (read_tree(sha1, "", 0) < 0) {
95 fprintf(stderr, "failed to unpack tree object %s\n", arg);
99 if (!write_cache(newfd, active_cache, active_nr) && !rename(".dircache/index.lock", ".dircache/index"))
103 unlink(".dircache/index.lock");