- if (argc != 2)
- usage("read-tree <key>");
- if (get_sha1_hex(argv[1], sha1) < 0)
- usage("read-tree <key>");
- sha1_file_directory = getenv(DB_ENVIRONMENT);
- if (!sha1_file_directory)
- sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
- if (unpack(sha1) < 0)
- usage("unpack failed");
- return 0;
+ newfd = open(".dircache/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600);
+ if (newfd < 0)
+ usage("unable to create new cachefile");
+
+ for (i = 1; i < argc; i++) {
+ const char *arg = argv[i];
+
+ /* "-m" stands for "merge" current directory cache */
+ if (!strcmp(arg, "-m")) {
+ if (active_cache) {
+ fprintf(stderr, "read-tree: cannot merge old cache on top of new\n");
+ goto out;
+ }
+ if (read_cache() < 0) {
+ fprintf(stderr, "read-tree: corrupt directory cache\n");
+ goto out;
+ }
+ continue;
+ }
+ if (get_sha1_hex(arg, sha1) < 0) {
+ fprintf(stderr, "read-tree [-m] <sha1>\n");
+ goto out;
+ }
+ if (read_tree(sha1) < 0) {
+ fprintf(stderr, "failed to unpack tree object %s\n", arg);
+ goto out;
+ }
+ }
+ if (!write_cache(newfd, active_cache, active_nr) && !rename(".dircache/index.lock", ".dircache/index"))
+ return 0;
+
+out:
+ unlink(".dircache/index.lock");
+ exit(1);