X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=read-tree.c;h=dc24e91b09b7468ca40d9142b18699b10193a2f6;hb=e2e5e98a40d6ed04b7acf791cc2243ff32923db3;hp=5784802e945e43612cbc5415681e9e08dc1cf1df;hpb=121481abf8d752ef871821d4ab9a3747595d86ae;p=git.git diff --git a/read-tree.c b/read-tree.c index 5784802e..dc24e91b 100644 --- a/read-tree.c +++ b/read-tree.c @@ -63,43 +63,44 @@ static int read_tree(unsigned char *sha1, const char *base, int baselen) return 0; } +static int remove_lock = 0; + +static void remove_lock_file(void) +{ + if (remove_lock) + unlink(".git/index.lock"); +} + int main(int argc, char **argv) { int i, newfd; unsigned char sha1[20]; - newfd = open(".dircache/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600); + newfd = open(".git/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600); if (newfd < 0) usage("unable to create new cachefile"); + atexit(remove_lock_file); + remove_lock = 1; 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; - } + if (active_cache) + usage("read-tree: cannot merge old cache on top of new"); + if (read_cache() < 0) + usage("read-tree: corrupt directory cache"); continue; } - if (get_sha1_hex(arg, sha1) < 0) { - fprintf(stderr, "read-tree [-m] \n"); - goto out; - } - if (read_tree(sha1, "", 0) < 0) { - fprintf(stderr, "failed to unpack tree object %s\n", arg); - goto out; - } + if (get_sha1_hex(arg, sha1) < 0) + usage("read-tree [-m] "); + if (read_tree(sha1, "", 0) < 0) + usage("failed to unpack tree object %s", arg); } - if (!write_cache(newfd, active_cache, active_nr) && !rename(".dircache/index.lock", ".dircache/index")) - return 0; - -out: - unlink(".dircache/index.lock"); - exit(1); + if (write_cache(newfd, active_cache, active_nr) || + rename(".git/index.lock", ".git/index")) + usage("unable to write new index file"); + remove_lock = 0; + return 0; }