commit_ref(): if there is an empty dir in the way, delete it

Part of the bug revealed in the last commit is that resolve_ref_unsafe() incorrectly returns EISDIR if it finds a directory in the place where it is looking for a loose reference, even if the corresponding packed reference exists. lock_ref_sha1_basic() notices the bogus EISDIR, and use it as an indication that it should call remove_empty_directories() and call resolve_ref_unsafe() again. But resolve_ref_unsafe() shouldn't report EISDIR in this case. If we would simply make that change, then remove_empty_directories() wouldn't get called anymore, and the empty directory would get in the way when commit_ref() calls commit_lock_file() to rename the lockfile into place. So instead of relying on lock_ref_sha1_basic() to delete empty directories, teach commit_ref(), just before calling commit_lock_file(), to check whether a directory is in the way, and if so, try to delete it. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>

Michael Haggerty committed May 5, 2016 at 15:33 UTC 5387c0d8839e366c44838c808ccc20eb7f9bd358
1 file changed +24
refs/files-backend.c
+24
@@ -2457,6 +2457,30 @@ static int close_ref(struct ref_lock *lock)
2457
2458 static int commit_ref(struct ref_lock *lock)
2459 {
2460 + char *path = get_locked_file_path(lock->lk);
2461 + struct stat st;
2462 +
2463 + if (!lstat(path, &st) && S_ISDIR(st.st_mode)) {
2464 + /*
2465 + * There is a directory at the path we want to rename
2466 + * the lockfile to. Hopefully it is empty; try to
2467 + * delete it.
2468 + */
2469 + size_t len = strlen(path);
2470 + struct strbuf sb_path = STRBUF_INIT;
2471 +
2472 + strbuf_attach(&sb_path, path, len, len);
2473 +
2474 + /*
2475 + * If this fails, commit_lock_file() will also fail
2476 + * and will report the problem.
2477 + */
2478 + remove_empty_directories(&sb_path);
2479 + strbuf_release(&sb_path);
2480 + } else {
2481 + free(path);
2482 + }
2483 +
2484 if (commit_lock_file(lock->lk))
2485 return -1;
2486 return 0;