{"thread":{"id":"44825","subject":"[PATCH v4 00/23] Delete directories left empty after ref deletion","startedAt":"2017-01-06T16:23:04Z","lastAt":"2017-01-06T19:45:03Z","messageCount":25,"participants":["Michael Haggerty","Jeff King"],"isPatch":true,"patchVersion":4,"patchTotal":23},"messages":[{"id":"308858","messageId":"cover.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":null,"subject":"[PATCH v4 00/23] Delete directories left empty after ref deletion","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:20Z","receivedAt":"2017-01-06T16:23:04Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"This is v4 of this patch series. Thanks to Peff, Junio, Jake, and\nPhilip for their feedback about v3. I believe I have addressed all of\nthe comments about v1 [1], v2 [2], and v3 [3].\n\nChanges since v3:\n\n* Insert a new first patch correcting the docstring of\n  `refname_is_safe()`.\n\n* In \"raceproof_create_file(): new function\", tweak a comment for\n  clarity as suggested by Peff.\n\n* In \"log_ref_setup(): separate code for create vs non-create\", add a\n  semicolon to an otherwise empty code block as requested by Junio.\n\n* Drop the patch \"try_remove_empty_parents(): don't accommodate\n  consecutive slashes\", for reasons discussed on the ML [4].\n\nMichael\n\n[1] http://public-inbox.org/git/cover.1455626201.git.mhagger@alum.mit.edu/T/#u\n[2] http://public-inbox.org/git/cover.1456405698.git.mhagger@alum.mit.edu/T/#u\n[3] http://public-inbox.org/git/cover.1483153436.git.mhagger@alum.mit.edu/T/#u\n[4] http://public-inbox.org/git/5051c78e-51f9-becd-e1a6-9c0b781d6912@alum.mit.edu/\nand surrounding thread.\n\nMichael Haggerty (23):\n  files_rename_ref(): tidy up whitespace\n  refname_is_safe(): correct docstring\n  t5505: use \"for-each-ref\" to test for the non-existence of references\n  safe_create_leading_directories_const(): preserve errno\n  safe_create_leading_directories(): set errno on SCLD_EXISTS\n  raceproof_create_file(): new function\n  lock_ref_sha1_basic(): inline constant\n  lock_ref_sha1_basic(): use raceproof_create_file()\n  rename_tmp_log(): use raceproof_create_file()\n  rename_tmp_log(): improve error reporting\n  log_ref_write(): inline function\n  log_ref_setup(): separate code for create vs non-create\n  log_ref_setup(): improve robustness against races\n  log_ref_setup(): pass the open file descriptor back to the caller\n  log_ref_write_1(): don't depend on logfile argument\n  log_ref_setup(): manage the name of the reflog file internally\n  log_ref_write_1(): inline function\n  delete_ref_loose(): derive loose reference path from lock\n  delete_ref_loose(): inline function\n  try_remove_empty_parents(): rename parameter \"name\" -> \"refname\"\n  try_remove_empty_parents(): don't trash argument contents\n  try_remove_empty_parents(): teach to remove parents of reflogs, too\n  files_transaction_commit(): clean up empty directories\n\n cache.h               |  48 ++++++-\n refs/files-backend.c  | 371 +++++++++++++++++++++++++-------------------------\n refs/refs-internal.h  |  22 ++-\n sha1_file.c           |  76 ++++++++++-\n t/t1400-update-ref.sh |  27 ++++\n t/t5505-remote.sh     |   2 +-\n 6 files changed, 351 insertions(+), 195 deletions(-)\n\n-- \n2.9.3\n\n"},{"id":"308859","messageId":"129f09835a10512ad532ff9521a1b279994324ff.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 01/23] files_rename_ref(): tidy up whitespace","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:21Z","receivedAt":"2017-01-06T16:23:17Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex f902393..be4ffdc 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2631,7 +2631,7 @@ static int files_rename_ref(struct ref_store *ref_store,\n \tif (!read_ref_full(newrefname, RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,\n \t\t\t   sha1, NULL) &&\n \t    delete_ref(newrefname, NULL, REF_NODEREF)) {\n-\t\tif (errno==EISDIR) {\n+\t\tif (errno == EISDIR) {\n \t\t\tstruct strbuf path = STRBUF_INIT;\n \t\t\tint result;\n \n-- \n2.9.3\n\n"},{"id":"308860","messageId":"5744cbeeb6374fdb1f11a5eb9825a68034779a24.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 03/23] t5505: use \"for-each-ref\" to test for the non-existence of references","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:23Z","receivedAt":"2017-01-06T16:23:19Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Instead of looking on the filesystem inside \".git/refs/remotes/origin\",\nuse \"git for-each-ref\" to check for leftover references under the\nremote's old name.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n t/t5505-remote.sh | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 8198d8e..65030fb 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -725,7 +725,7 @@ test_expect_success 'rename a remote' '\n \t(\n \t\tcd four &&\n \t\tgit remote rename origin upstream &&\n-\t\trmdir .git/refs/remotes/origin &&\n+\t\ttest -z \"$(git for-each-ref refs/remotes/origin)\" &&\n \t\ttest \"$(git symbolic-ref refs/remotes/upstream/HEAD)\" = \"refs/remotes/upstream/master\" &&\n \t\ttest \"$(git rev-parse upstream/master)\" = \"$(git rev-parse master)\" &&\n \t\ttest \"$(git config remote.upstream.fetch)\" = \"+refs/heads/*:refs/remotes/upstream/*\" &&\n-- \n2.9.3\n\n"},{"id":"308861","messageId":"dd5ffa31393a5b243fe83c5dc4472ee389f55658.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 06/23] raceproof_create_file(): new function","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:26Z","receivedAt":"2017-01-06T16:23:21Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Add a function that tries to create a file and any containing\ndirectories in a way that is robust against races with other processes\nthat might be cleaning up empty directories at the same time.\n\nThe actual file creation is done by a callback function, which, if it\nfails, should set errno to EISDIR or ENOENT according to the convention\nof open(). raceproof_create_file() detects such failures, and\nrespectively either tries to delete empty directories that might be in\nthe way of the file or tries to create the containing directories. Then\nit retries the callback function.\n\nThis function is not yet used.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n cache.h     | 43 ++++++++++++++++++++++++++++++++++++++\n sha1_file.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n 2 files changed, 111 insertions(+)\n\ndiff --git a/cache.h b/cache.h\nindex 8177c3a..95cde14 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1057,6 +1057,49 @@ enum scld_error {\n enum scld_error safe_create_leading_directories(char *path);\n enum scld_error safe_create_leading_directories_const(const char *path);\n \n+/*\n+ * Callback function for raceproof_create_file(). This function is\n+ * expected to do something that makes dirname(path) permanent despite\n+ * the fact that other processes might be cleaning up empty\n+ * directories at the same time. Usually it will create a file named\n+ * path, but alternatively it could create another file in that\n+ * directory, or even chdir() into that directory. The function should\n+ * return 0 if the action was completed successfully. On error, it\n+ * should return a nonzero result and set errno.\n+ * raceproof_create_file() treats two errno values specially:\n+ *\n+ * - ENOENT -- dirname(path) does not exist. In this case,\n+ *             raceproof_create_file() tries creating dirname(path)\n+ *             (and any parent directories, if necessary) and calls\n+ *             the function again.\n+ *\n+ * - EISDIR -- the file already exists and is a directory. In this\n+ *             case, raceproof_create_file() removes the directory if\n+ *             it is empty (and recursively any empty directories that\n+ *             it contains) and calls the function again.\n+ *\n+ * Any other errno causes raceproof_create_file() to fail with the\n+ * callback's return value and errno.\n+ *\n+ * Obviously, this function should be OK with being called again if it\n+ * fails with ENOENT or EISDIR. In other scenarios it will not be\n+ * called again.\n+ */\n+typedef int create_file_fn(const char *path, void *cb);\n+\n+/*\n+ * Create a file in dirname(path) by calling fn, creating leading\n+ * directories if necessary. Retry a few times in case we are racing\n+ * with another process that is trying to clean up the directory that\n+ * contains path. See the documentation for create_file_fn for more\n+ * details.\n+ *\n+ * Return the value and set the errno that resulted from the most\n+ * recent call of fn. fn is always called at least once, and will be\n+ * called more than once if it returns ENOENT or EISDIR.\n+ */\n+int raceproof_create_file(const char *path, create_file_fn fn, void *cb);\n+\n int mkdir_in_gitdir(const char *path);\n extern char *expand_user_path(const char *path);\n const char *enter_repo(const char *path, int strict);\ndiff --git a/sha1_file.c b/sha1_file.c\nindex ae8f0b4..b08f54c 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -179,6 +179,74 @@ enum scld_error safe_create_leading_directories_const(const char *path)\n \treturn result;\n }\n \n+int raceproof_create_file(const char *path, create_file_fn fn, void *cb)\n+{\n+\t/*\n+\t * The number of times we will try to remove empty directories\n+\t * in the way of path. This is only 1 because if another\n+\t * process is racily creating directories that conflict with\n+\t * us, we don't want to fight against them.\n+\t */\n+\tint remove_directories_remaining = 1;\n+\n+\t/*\n+\t * The number of times that we will try to create the\n+\t * directories containing path. We are willing to attempt this\n+\t * more than once, because another process could be trying to\n+\t * clean up empty directories at the same time as we are\n+\t * trying to create them.\n+\t */\n+\tint create_directories_remaining = 3;\n+\n+\t/* A scratch copy of path, filled lazily if we need it: */\n+\tstruct strbuf path_copy = STRBUF_INIT;\n+\n+\tint ret, save_errno;\n+\n+\t/* Sanity check: */\n+\tassert(*path);\n+\n+retry_fn:\n+\tret = fn(path, cb);\n+\tsave_errno = errno;\n+\tif (!ret)\n+\t\tgoto out;\n+\n+\tif (errno == EISDIR && remove_directories_remaining-- > 0) {\n+\t\t/*\n+\t\t * A directory is in the way. Maybe it is empty; try\n+\t\t * to remove it:\n+\t\t */\n+\t\tif (!path_copy.len)\n+\t\t\tstrbuf_addstr(&path_copy, path);\n+\n+\t\tif (!remove_dir_recursively(&path_copy, REMOVE_DIR_EMPTY_ONLY))\n+\t\t\tgoto retry_fn;\n+\t} else if (errno == ENOENT && create_directories_remaining-- > 0) {\n+\t\t/*\n+\t\t * Maybe the containing directory didn't exist, or\n+\t\t * maybe it was just deleted by a process that is\n+\t\t * racing with us to clean up empty directories. Try\n+\t\t * to create it:\n+\t\t */\n+\t\tenum scld_error scld_result;\n+\n+\t\tif (!path_copy.len)\n+\t\t\tstrbuf_addstr(&path_copy, path);\n+\n+\t\tdo {\n+\t\t\tscld_result = safe_create_leading_directories(path_copy.buf);\n+\t\t\tif (scld_result == SCLD_OK)\n+\t\t\t\tgoto retry_fn;\n+\t\t} while (scld_result == SCLD_VANISHED && create_directories_remaining-- > 0);\n+\t}\n+\n+out:\n+\tstrbuf_release(&path_copy);\n+\terrno = save_errno;\n+\treturn ret;\n+}\n+\n static void fill_sha1_path(struct strbuf *buf, const unsigned char *sha1)\n {\n \tint i;\n-- \n2.9.3\n\n"},{"id":"308862","messageId":"bda5573d25412123683c36c2ea6669aa6f380c61.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 02/23] refname_is_safe(): correct docstring","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:22Z","receivedAt":"2017-01-06T16:23:22Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"The behavior of refname_is_safe() was changed in\n\n    e40f355 \"refname_is_safe(): insist that the refname already be normalized\", 2016-04-27\n\nwithout a corresponding update to its docstring. The function is in fact\nstricter than documented, because it now insists that the result of\nnormalizing the part of a refname following \"refs/\" is identical to that\npart of the original refname. Fix the docstring.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/refs-internal.h | 11 ++++++-----\n 1 file changed, 6 insertions(+), 5 deletions(-)\n\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex 708b260..dc81acc 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -62,11 +62,12 @@\n  * This function does not check that the reference name is legal; for\n  * that, use check_refname_format().\n  *\n- * We consider a refname that starts with \"refs/\" to be safe as long\n- * as any \"..\" components that it might contain do not escape \"refs/\".\n- * Names that do not start with \"refs/\" are considered safe iff they\n- * consist entirely of upper case characters and '_' (like \"HEAD\" and\n- * \"MERGE_HEAD\" but not \"config\" or \"FOO/BAR\").\n+ * A refname that starts with \"refs/\" is considered safe iff it\n+ * doesn't contain any \".\" or \"..\" components or consecutive '/'\n+ * characters, end with '/', or (on Windows) contain any '\\'\n+ * characters. Names that do not start with \"refs/\" are considered\n+ * safe iff they consist entirely of upper case characters and '_'\n+ * (like \"HEAD\" and \"MERGE_HEAD\" but not \"config\" or \"FOO/BAR\").\n  */\n int refname_is_safe(const char *refname);\n \n-- \n2.9.3\n\n"},{"id":"308863","messageId":"f8c5608152fb041e064268af001a986200bbd712.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 05/23] safe_create_leading_directories(): set errno on SCLD_EXISTS","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:25Z","receivedAt":"2017-01-06T16:23:28Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"The exit path for SCLD_EXISTS wasn't setting errno, which some callers\nuse to generate error messages for the user. Fix the problem and\ndocument that the function sets errno correctly to help avoid similar\nregressions in the future.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n cache.h     | 5 +++--\n sha1_file.c | 4 +++-\n 2 files changed, 6 insertions(+), 3 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex a50a61a..8177c3a 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1031,8 +1031,9 @@ int adjust_shared_perm(const char *path);\n \n /*\n  * Create the directory containing the named path, using care to be\n- * somewhat safe against races.  Return one of the scld_error values\n- * to indicate success/failure.\n+ * somewhat safe against races. Return one of the scld_error values to\n+ * indicate success/failure. On error, set errno to describe the\n+ * problem.\n  *\n  * SCLD_VANISHED indicates that one of the ancestor directories of the\n  * path existed at one point during the function call and then\ndiff --git a/sha1_file.c b/sha1_file.c\nindex 10395e7..ae8f0b4 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -137,8 +137,10 @@ enum scld_error safe_create_leading_directories(char *path)\n \t\t*slash = '\\0';\n \t\tif (!stat(path, &st)) {\n \t\t\t/* path exists */\n-\t\t\tif (!S_ISDIR(st.st_mode))\n+\t\t\tif (!S_ISDIR(st.st_mode)) {\n+\t\t\t\terrno = ENOTDIR;\n \t\t\t\tret = SCLD_EXISTS;\n+\t\t\t}\n \t\t} else if (mkdir(path, 0777)) {\n \t\t\tif (errno == EEXIST &&\n \t\t\t    !stat(path, &st) && S_ISDIR(st.st_mode))\n-- \n2.9.3\n\n"},{"id":"308864","messageId":"787c14104716cd4dc007076471a2ad17c437ccea.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 10/23] rename_tmp_log(): improve error reporting","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:30Z","receivedAt":"2017-01-06T16:23:31Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"* Don't capitalize error strings\n* Report true paths of affected files\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 7 ++++---\n 1 file changed, 4 insertions(+), 3 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 3f18a01..49a119c 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2518,10 +2518,11 @@ static int rename_tmp_log(const char *newrefname)\n \tret = raceproof_create_file(path, rename_tmp_log_callback, &true_errno);\n \tif (ret) {\n \t\tif (errno == EISDIR)\n-\t\t\terror(\"Directory not empty: %s\", path);\n+\t\t\terror(\"directory not empty: %s\", path);\n \t\telse\n-\t\t\terror(\"unable to move logfile \"TMP_RENAMED_LOG\" to logs/%s: %s\",\n-\t\t\t\tnewrefname, strerror(true_errno));\n+\t\t\terror(\"unable to move logfile %s to %s: %s\",\n+\t\t\t      git_path(TMP_RENAMED_LOG), path,\n+\t\t\t      strerror(true_errno));\n \t}\n \n \tfree(path);\n-- \n2.9.3\n\n"},{"id":"308865","messageId":"b783c8055c36004c09361eddd14659d83633baad.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 09/23] rename_tmp_log(): use raceproof_create_file()","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:29Z","receivedAt":"2017-01-06T16:23:32Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Besides shortening the code, this saves an unnecessary call to\nsafe_create_leading_directories_const() in almost all cases.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 73 +++++++++++++++++++++-------------------------------\n 1 file changed, 30 insertions(+), 43 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 74de289..3f18a01 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2489,55 +2489,42 @@ static int files_delete_refs(struct ref_store *ref_store,\n  */\n #define TMP_RENAMED_LOG  \"logs/refs/.tmp-renamed-log\"\n \n-static int rename_tmp_log(const char *newrefname)\n+static int rename_tmp_log_callback(const char *path, void *cb)\n {\n-\tint attempts_remaining = 4;\n-\tstruct strbuf path = STRBUF_INIT;\n-\tint ret = -1;\n+\tint *true_errno = cb;\n \n- retry:\n-\tstrbuf_reset(&path);\n-\tstrbuf_git_path(&path, \"logs/%s\", newrefname);\n-\tswitch (safe_create_leading_directories_const(path.buf)) {\n-\tcase SCLD_OK:\n-\t\tbreak; /* success */\n-\tcase SCLD_VANISHED:\n-\t\tif (--attempts_remaining > 0)\n-\t\t\tgoto retry;\n-\t\t/* fall through */\n-\tdefault:\n-\t\terror(\"unable to create directory for %s\", newrefname);\n-\t\tgoto out;\n+\tif (rename(git_path(TMP_RENAMED_LOG), path)) {\n+\t\t/*\n+\t\t * rename(a, b) when b is an existing directory ought\n+\t\t * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.\n+\t\t * Sheesh. Record the true errno for error reporting,\n+\t\t * but report EISDIR to raceproof_create_file() so\n+\t\t * that it knows to retry.\n+\t\t */\n+\t\t*true_errno = errno;\n+\t\tif (errno == ENOTDIR)\n+\t\t\terrno = EISDIR;\n+\t\treturn -1;\n+\t} else {\n+\t\treturn 0;\n \t}\n+}\n \n-\tif (rename(git_path(TMP_RENAMED_LOG), path.buf)) {\n-\t\tif ((errno==EISDIR || errno==ENOTDIR) && --attempts_remaining > 0) {\n-\t\t\t/*\n-\t\t\t * rename(a, b) when b is an existing\n-\t\t\t * directory ought to result in ISDIR, but\n-\t\t\t * Solaris 5.8 gives ENOTDIR.  Sheesh.\n-\t\t\t */\n-\t\t\tif (remove_empty_directories(&path)) {\n-\t\t\t\terror(\"Directory not empty: logs/%s\", newrefname);\n-\t\t\t\tgoto out;\n-\t\t\t}\n-\t\t\tgoto retry;\n-\t\t} else if (errno == ENOENT && --attempts_remaining > 0) {\n-\t\t\t/*\n-\t\t\t * Maybe another process just deleted one of\n-\t\t\t * the directories in the path to newrefname.\n-\t\t\t * Try again from the beginning.\n-\t\t\t */\n-\t\t\tgoto retry;\n-\t\t} else {\n+static int rename_tmp_log(const char *newrefname)\n+{\n+\tchar *path = git_pathdup(\"logs/%s\", newrefname);\n+\tint ret, true_errno;\n+\n+\tret = raceproof_create_file(path, rename_tmp_log_callback, &true_errno);\n+\tif (ret) {\n+\t\tif (errno == EISDIR)\n+\t\t\terror(\"Directory not empty: %s\", path);\n+\t\telse\n \t\t\terror(\"unable to move logfile \"TMP_RENAMED_LOG\" to logs/%s: %s\",\n-\t\t\t\tnewrefname, strerror(errno));\n-\t\t\tgoto out;\n-\t\t}\n+\t\t\t\tnewrefname, strerror(true_errno));\n \t}\n-\tret = 0;\n-out:\n-\tstrbuf_release(&path);\n+\n+\tfree(path);\n \treturn ret;\n }\n \n-- \n2.9.3\n\n"},{"id":"308866","messageId":"964bc414e7e96642aacb248e06912befab2724cc.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 04/23] safe_create_leading_directories_const(): preserve errno","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:24Z","receivedAt":"2017-01-06T16:23:33Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Some implementations of free() change errno (even thought they\nshouldn't):\n\n  https://sourceware.org/bugzilla/show_bug.cgi?id=17924\n\nSo preserve the errno from safe_create_leading_directories() across the\ncall to free().\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n sha1_file.c | 4 ++++\n 1 file changed, 4 insertions(+)\n\ndiff --git a/sha1_file.c b/sha1_file.c\nindex 1173071..10395e7 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -166,10 +166,14 @@ enum scld_error safe_create_leading_directories(char *path)\n \n enum scld_error safe_create_leading_directories_const(const char *path)\n {\n+\tint save_errno;\n \t/* path points to cache entries, so xstrdup before messing with it */\n \tchar *buf = xstrdup(path);\n \tenum scld_error result = safe_create_leading_directories(buf);\n+\n+\tsave_errno = errno;\n \tfree(buf);\n+\terrno = save_errno;\n \treturn result;\n }\n \n-- \n2.9.3\n\n"},{"id":"308867","messageId":"7649c885fc567b44fbf1a858a6a69e7850357b9b.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 07/23] lock_ref_sha1_basic(): inline constant","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:27Z","receivedAt":"2017-01-06T16:23:36Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"`lflags` is set a single time then never changed, so just inline it.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 3 +--\n 1 file changed, 1 insertion(+), 2 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex be4ffdc..7d4658a 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2000,7 +2000,6 @@ static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,\n \tstruct strbuf ref_file = STRBUF_INIT;\n \tstruct ref_lock *lock;\n \tint last_errno = 0;\n-\tint lflags = LOCK_NO_DEREF;\n \tint mustexist = (old_sha1 && !is_null_sha1(old_sha1));\n \tint resolve_flags = RESOLVE_REF_NO_RECURSE;\n \tint attempts_remaining = 3;\n@@ -2083,7 +2082,7 @@ static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,\n \t\tgoto error_return;\n \t}\n \n-\tif (hold_lock_file_for_update(lock->lk, ref_file.buf, lflags) < 0) {\n+\tif (hold_lock_file_for_update(lock->lk, ref_file.buf, LOCK_NO_DEREF) < 0) {\n \t\tlast_errno = errno;\n \t\tif (errno == ENOENT && --attempts_remaining > 0)\n \t\t\t/*\n-- \n2.9.3\n\n"},{"id":"308868","messageId":"ad86aeda2873070eae142a0de32bd34ec9bcfe99.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 13/23] log_ref_setup(): improve robustness against races","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:33Z","receivedAt":"2017-01-06T16:23:39Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Change log_ref_setup() to use raceproof_create_file() to create the new\nlogfile. This makes it more robust against a race against another\nprocess that might be trying to clean up empty directories while we are\ntrying to create a new logfile.\n\nThis also means that it will only call create_leading_directories() if\nopen() fails, which should be a net win. Even in the cases where we are\nwilling to create a new logfile, it will usually be the case that the\nlogfile already exists, or if not then that the directory containing the\nlogfile already exists. In such cases, we will save some work that was\npreviously done unconditionally.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 41 ++++++++++++++++++-----------------------\n 1 file changed, 18 insertions(+), 23 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex c8f6d82..27d4fd3 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2710,6 +2710,14 @@ static int commit_ref(struct ref_lock *lock)\n \treturn 0;\n }\n \n+static int open_or_create_logfile(const char *path, void *cb)\n+{\n+\tint *fd = cb;\n+\n+\t*fd = open(path, O_APPEND | O_WRONLY | O_CREAT, 0666);\n+\treturn (*fd < 0) ? -1 : 0;\n+}\n+\n /*\n  * Create a reflog for a ref.  If force_create = 0, the reflog will\n  * only be created for certain refs (those for which\n@@ -2723,31 +2731,18 @@ static int log_ref_setup(const char *refname, struct strbuf *logfile, struct str\n \tstrbuf_git_path(logfile, \"logs/%s\", refname);\n \n \tif (force_create || should_autocreate_reflog(refname)) {\n-\t\tif (safe_create_leading_directories(logfile->buf) < 0) {\n-\t\t\tstrbuf_addf(err, \"unable to create directory for '%s': \"\n-\t\t\t\t    \"%s\", logfile->buf, strerror(errno));\n-\t\t\treturn -1;\n-\t\t}\n-\t\tlogfd = open(logfile->buf, O_APPEND | O_WRONLY | O_CREAT, 0666);\n-\t\tif (logfd < 0) {\n-\t\t\tif (errno == EISDIR) {\n-\t\t\t\t/*\n-\t\t\t\t * The directory that is in the way might be\n-\t\t\t\t * empty. Try to remove it.\n-\t\t\t\t */\n-\t\t\t\tif (remove_empty_directories(logfile)) {\n-\t\t\t\t\tstrbuf_addf(err, \"there are still logs under \"\n-\t\t\t\t\t\t    \"'%s'\", logfile->buf);\n-\t\t\t\t\treturn -1;\n-\t\t\t\t}\n-\t\t\t\tlogfd = open(logfile->buf, O_APPEND | O_WRONLY | O_CREAT, 0666);\n-\t\t\t}\n-\n-\t\t\tif (logfd < 0) {\n+\t\tif (raceproof_create_file(logfile->buf, open_or_create_logfile, &logfd)) {\n+\t\t\tif (errno == ENOENT)\n+\t\t\t\tstrbuf_addf(err, \"unable to create directory for '%s': \"\n+\t\t\t\t\t    \"%s\", logfile->buf, strerror(errno));\n+\t\t\telse if (errno == EISDIR)\n+\t\t\t\tstrbuf_addf(err, \"there are still logs under '%s'\",\n+\t\t\t\t\t    logfile->buf);\n+\t\t\telse\n \t\t\t\tstrbuf_addf(err, \"unable to append to '%s': %s\",\n \t\t\t\t\t    logfile->buf, strerror(errno));\n-\t\t\t\treturn -1;\n-\t\t\t}\n+\n+\t\t\treturn -1;\n \t\t}\n \t} else {\n \t\tlogfd = open(logfile->buf, O_APPEND | O_WRONLY, 0666);\n-- \n2.9.3\n\n"},{"id":"308869","messageId":"f3048a56ed5e273ac42964fd2345d67db6e7ddbe.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 08/23] lock_ref_sha1_basic(): use raceproof_create_file()","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:28Z","receivedAt":"2017-01-06T16:23:43Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Instead of coding the retry loop inline, use raceproof_create_file() to\nmake lock acquisition safe against directory creation/deletion races.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 35 +++++++++--------------------------\n 1 file changed, 9 insertions(+), 26 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 7d4658a..74de289 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1985,6 +1985,13 @@ static int remove_empty_directories(struct strbuf *path)\n \treturn remove_dir_recursively(path, REMOVE_DIR_EMPTY_ONLY);\n }\n \n+static int create_reflock(const char *path, void *cb)\n+{\n+\tstruct lock_file *lk = cb;\n+\n+\treturn hold_lock_file_for_update(lk, path, LOCK_NO_DEREF) < 0 ? -1 : 0;\n+}\n+\n /*\n  * Locks a ref returning the lock on success and NULL on failure.\n  * On failure errno is set to something meaningful.\n@@ -2002,7 +2009,6 @@ static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,\n \tint last_errno = 0;\n \tint mustexist = (old_sha1 && !is_null_sha1(old_sha1));\n \tint resolve_flags = RESOLVE_REF_NO_RECURSE;\n-\tint attempts_remaining = 3;\n \tint resolved;\n \n \tassert_main_repository(&refs->base, \"lock_ref_sha1_basic\");\n@@ -2067,35 +2073,12 @@ static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,\n \n \tlock->ref_name = xstrdup(refname);\n \n- retry:\n-\tswitch (safe_create_leading_directories_const(ref_file.buf)) {\n-\tcase SCLD_OK:\n-\t\tbreak; /* success */\n-\tcase SCLD_VANISHED:\n-\t\tif (--attempts_remaining > 0)\n-\t\t\tgoto retry;\n-\t\t/* fall through */\n-\tdefault:\n+\tif (raceproof_create_file(ref_file.buf, create_reflock, lock->lk)) {\n \t\tlast_errno = errno;\n-\t\tstrbuf_addf(err, \"unable to create directory for '%s'\",\n-\t\t\t    ref_file.buf);\n+\t\tunable_to_lock_message(ref_file.buf, errno, err);\n \t\tgoto error_return;\n \t}\n \n-\tif (hold_lock_file_for_update(lock->lk, ref_file.buf, LOCK_NO_DEREF) < 0) {\n-\t\tlast_errno = errno;\n-\t\tif (errno == ENOENT && --attempts_remaining > 0)\n-\t\t\t/*\n-\t\t\t * Maybe somebody just deleted one of the\n-\t\t\t * directories leading to ref_file.  Try\n-\t\t\t * again:\n-\t\t\t */\n-\t\t\tgoto retry;\n-\t\telse {\n-\t\t\tunable_to_lock_message(ref_file.buf, errno, err);\n-\t\t\tgoto error_return;\n-\t\t}\n-\t}\n \tif (verify_lock(lock, old_sha1, mustexist, err)) {\n \t\tlast_errno = errno;\n \t\tgoto error_return;\n-- \n2.9.3\n\n"},{"id":"308870","messageId":"cd97511ebe95a8957da6228d9a77c3f04b7430a9.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 11/23] log_ref_write(): inline function","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:31Z","receivedAt":"2017-01-06T16:24:02Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"This function doesn't do anything beyond call files_log_ref_write(), so\nreplace it with the latter at its call sites.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 24 ++++++++++--------------\n 1 file changed, 10 insertions(+), 14 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 49a119c..fd8a751 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2832,14 +2832,6 @@ static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,\n \treturn 0;\n }\n \n-static int log_ref_write(const char *refname, const unsigned char *old_sha1,\n-\t\t\t const unsigned char *new_sha1, const char *msg,\n-\t\t\t int flags, struct strbuf *err)\n-{\n-\treturn files_log_ref_write(refname, old_sha1, new_sha1, msg, flags,\n-\t\t\t\t   err);\n-}\n-\n int files_log_ref_write(const char *refname, const unsigned char *old_sha1,\n \t\t\tconst unsigned char *new_sha1, const char *msg,\n \t\t\tint flags, struct strbuf *err)\n@@ -2903,7 +2895,8 @@ static int commit_ref_update(struct files_ref_store *refs,\n \tassert_main_repository(&refs->base, \"commit_ref_update\");\n \n \tclear_loose_ref_cache(refs);\n-\tif (log_ref_write(lock->ref_name, lock->old_oid.hash, sha1, logmsg, 0, err)) {\n+\tif (files_log_ref_write(lock->ref_name, lock->old_oid.hash, sha1,\n+\t\t\t\tlogmsg, 0, err)) {\n \t\tchar *old_msg = strbuf_detach(err, NULL);\n \t\tstrbuf_addf(err, \"cannot update the ref '%s': %s\",\n \t\t\t    lock->ref_name, old_msg);\n@@ -2934,7 +2927,7 @@ static int commit_ref_update(struct files_ref_store *refs,\n \t\tif (head_ref && (head_flag & REF_ISSYMREF) &&\n \t\t    !strcmp(head_ref, lock->ref_name)) {\n \t\t\tstruct strbuf log_err = STRBUF_INIT;\n-\t\t\tif (log_ref_write(\"HEAD\", lock->old_oid.hash, sha1,\n+\t\t\tif (files_log_ref_write(\"HEAD\", lock->old_oid.hash, sha1,\n \t\t\t\t\t  logmsg, 0, &log_err)) {\n \t\t\t\terror(\"%s\", log_err.buf);\n \t\t\t\tstrbuf_release(&log_err);\n@@ -2973,7 +2966,8 @@ static void update_symref_reflog(struct ref_lock *lock, const char *refname,\n \tstruct strbuf err = STRBUF_INIT;\n \tunsigned char new_sha1[20];\n \tif (logmsg && !read_ref(target, new_sha1) &&\n-\t    log_ref_write(refname, lock->old_oid.hash, new_sha1, logmsg, 0, &err)) {\n+\t    files_log_ref_write(refname, lock->old_oid.hash, new_sha1,\n+\t\t\t\tlogmsg, 0, &err)) {\n \t\terror(\"%s\", err.buf);\n \t\tstrbuf_release(&err);\n \t}\n@@ -3748,9 +3742,11 @@ static int files_transaction_commit(struct ref_store *ref_store,\n \n \t\tif (update->flags & REF_NEEDS_COMMIT ||\n \t\t    update->flags & REF_LOG_ONLY) {\n-\t\t\tif (log_ref_write(lock->ref_name, lock->old_oid.hash,\n-\t\t\t\t\t  update->new_sha1,\n-\t\t\t\t\t  update->msg, update->flags, err)) {\n+\t\t\tif (files_log_ref_write(lock->ref_name,\n+\t\t\t\t\t\tlock->old_oid.hash,\n+\t\t\t\t\t\tupdate->new_sha1,\n+\t\t\t\t\t\tupdate->msg, update->flags,\n+\t\t\t\t\t\terr)) {\n \t\t\t\tchar *old_msg = strbuf_detach(err, NULL);\n \n \t\t\t\tstrbuf_addf(err, \"cannot update the ref '%s': %s\",\n-- \n2.9.3\n\n"},{"id":"308871","messageId":"dc7ea83d82cbdfe2693d5dc2eb3d5275a96a665f.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 23/23] files_transaction_commit(): clean up empty directories","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:43Z","receivedAt":"2017-01-06T16:24:05Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"When deleting/pruning references, remove any directories that are made\nempty by the deletion of loose references or of reflogs. Otherwise such\nempty directories can survive forever and accumulate over time. (Even\n'pack-refs', which is smart enough to remove the parent directories of\nloose references that it prunes, leaves directories that were already\nempty.)\n\nAnd now that files_transaction_commit() takes care of deleting the\nparent directories of loose references that it prunes, we don't have to\ndo that in prune_ref() anymore.\n\nThis change would be unwise if the *creation* of these directories could\nrace with our deletion of them. But the earlier changes in this patch\nseries made the creation paths robust against races, so now it is safe\nto tidy them up more aggressively.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c  | 34 ++++++++++++++++++++++++++++------\n refs/refs-internal.h  | 11 +++++++++--\n t/t1400-update-ref.sh | 27 +++++++++++++++++++++++++++\n 3 files changed, 64 insertions(+), 8 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex bce0022..c426575 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2346,7 +2346,6 @@ static void prune_ref(struct ref_to_prune *r)\n \t}\n \tref_transaction_free(transaction);\n \tstrbuf_release(&err);\n-\ttry_remove_empty_parents(r->name, REMOVE_EMPTY_PARENTS_REF);\n }\n \n static void prune_refs(struct ref_to_prune *r)\n@@ -3794,6 +3793,7 @@ static int files_transaction_commit(struct ref_store *ref_store,\n \t\t\t\t\tret = TRANSACTION_GENERIC_ERROR;\n \t\t\t\t\tgoto cleanup;\n \t\t\t\t}\n+\t\t\t\tupdate->flags |= REF_DELETED_LOOSE;\n \t\t\t}\n \n \t\t\tif (!(update->flags & REF_ISPRUNING))\n@@ -3806,16 +3806,38 @@ static int files_transaction_commit(struct ref_store *ref_store,\n \t\tret = TRANSACTION_GENERIC_ERROR;\n \t\tgoto cleanup;\n \t}\n-\tfor_each_string_list_item(ref_to_delete, &refs_to_delete)\n-\t\tunlink_or_warn(git_path(\"logs/%s\", ref_to_delete->string));\n+\n+\t/* Delete the reflogs of any references that were deleted: */\n+\tfor_each_string_list_item(ref_to_delete, &refs_to_delete) {\n+\t\tif (!unlink_or_warn(git_path(\"logs/%s\", ref_to_delete->string)))\n+\t\t\ttry_remove_empty_parents(ref_to_delete->string,\n+\t\t\t\t\t\t REMOVE_EMPTY_PARENTS_REFLOG);\n+\t}\n+\n \tclear_loose_ref_cache(refs);\n \n cleanup:\n \ttransaction->state = REF_TRANSACTION_CLOSED;\n \n-\tfor (i = 0; i < transaction->nr; i++)\n-\t\tif (transaction->updates[i]->backend_data)\n-\t\t\tunlock_ref(transaction->updates[i]->backend_data);\n+\tfor (i = 0; i < transaction->nr; i++) {\n+\t\tstruct ref_update *update = transaction->updates[i];\n+\t\tstruct ref_lock *lock = update->backend_data;\n+\n+\t\tif (lock)\n+\t\t\tunlock_ref(lock);\n+\n+\t\tif (update->flags & REF_DELETED_LOOSE) {\n+\t\t\t/*\n+\t\t\t * The loose reference was deleted. Delete any\n+\t\t\t * empty parent directories. (Note that this\n+\t\t\t * can only work because we have already\n+\t\t\t * removed the lockfile.)\n+\t\t\t */\n+\t\t\ttry_remove_empty_parents(update->refname,\n+\t\t\t\t\t\t REMOVE_EMPTY_PARENTS_REF);\n+\t\t}\n+\t}\n+\n \tstring_list_clear(&refs_to_delete, 0);\n \tfree(head_ref);\n \tstring_list_clear(&affected_refnames, 0);\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex dc81acc..15d5a1e 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -56,6 +56,12 @@\n #define REF_UPDATE_VIA_HEAD 0x100\n \n /*\n+ * Used as a flag in ref_update::flags when the loose reference has\n+ * been deleted.\n+ */\n+#define REF_DELETED_LOOSE 0x200\n+\n+/*\n  * Return true iff refname is minimally safe. \"Safe\" here means that\n  * deleting a loose reference by this name will not do any damage, for\n  * example by causing a file that is not a reference to be deleted.\n@@ -158,8 +164,9 @@ struct ref_update {\n \n \t/*\n \t * One or more of REF_HAVE_NEW, REF_HAVE_OLD, REF_NODEREF,\n-\t * REF_DELETING, REF_ISPRUNING, REF_LOG_ONLY, and\n-\t * REF_UPDATE_VIA_HEAD:\n+\t * REF_DELETING, REF_ISPRUNING, REF_LOG_ONLY,\n+\t * REF_UPDATE_VIA_HEAD, REF_NEEDS_COMMIT, and\n+\t * REF_DELETED_LOOSE:\n \t */\n \tunsigned int flags;\n \ndiff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh\nindex d4fb977..97d8793 100755\n--- a/t/t1400-update-ref.sh\n+++ b/t/t1400-update-ref.sh\n@@ -191,6 +191,33 @@ test_expect_success \\\n \t git update-ref HEAD'\" $A &&\n \t test $A\"' = $(cat .git/'\"$m\"')'\n \n+test_expect_success \"empty directory removal\" '\n+\tgit branch d1/d2/r1 HEAD &&\n+\tgit branch d1/r2 HEAD &&\n+\ttest -f .git/refs/heads/d1/d2/r1 &&\n+\ttest -f .git/logs/refs/heads/d1/d2/r1 &&\n+\tgit branch -d d1/d2/r1 &&\n+\t! test -e .git/refs/heads/d1/d2 &&\n+\t! test -e .git/logs/refs/heads/d1/d2 &&\n+\ttest -f .git/refs/heads/d1/r2 &&\n+\ttest -f .git/logs/refs/heads/d1/r2\n+'\n+\n+test_expect_success \"symref empty directory removal\" '\n+\tgit branch e1/e2/r1 HEAD &&\n+\tgit branch e1/r2 HEAD &&\n+\tgit checkout e1/e2/r1 &&\n+\ttest_when_finished \"git checkout master\" &&\n+\ttest -f .git/refs/heads/e1/e2/r1 &&\n+\ttest -f .git/logs/refs/heads/e1/e2/r1 &&\n+\tgit update-ref -d HEAD &&\n+\t! test -e .git/refs/heads/e1/e2 &&\n+\t! test -e .git/logs/refs/heads/e1/e2 &&\n+\ttest -f .git/refs/heads/e1/r2 &&\n+\ttest -f .git/logs/refs/heads/e1/r2 &&\n+\ttest -f .git/logs/HEAD\n+'\n+\n cat >expect <<EOF\n $Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000\tInitial Creation\n $A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150260 +0000\tSwitch\n-- \n2.9.3\n\n"},{"id":"308872","messageId":"8967c11634eeb911a056a9c1ed6b4409713e94d3.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 18/23] delete_ref_loose(): derive loose reference path from lock","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:38Z","receivedAt":"2017-01-06T16:24:06Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"It is simpler to derive the path to the file that must be deleted from\n\"lock->ref_name\" than from the lock_file object.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 5 +----\n 1 file changed, 1 insertion(+), 4 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 39d6f5b..4d55364 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2430,10 +2430,7 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)\n \t\t * loose.  The loose file name is the same as the\n \t\t * lockfile name, minus \".lock\":\n \t\t */\n-\t\tchar *loose_filename = get_locked_file_path(lock->lk);\n-\t\tint res = unlink_or_msg(loose_filename, err);\n-\t\tfree(loose_filename);\n-\t\tif (res)\n+\t\tif (unlink_or_msg(git_path(\"%s\", lock->ref_name), err))\n \t\t\treturn 1;\n \t}\n \treturn 0;\n-- \n2.9.3\n\n"},{"id":"308873","messageId":"17c81b14014932c31c22812f499a93a7afce88ac.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 19/23] delete_ref_loose(): inline function","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:39Z","receivedAt":"2017-01-06T16:24:08Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"It was hardly doing anything anymore, and had only one caller.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 25 +++++++------------------\n 1 file changed, 7 insertions(+), 18 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 4d55364..9abd7c3 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2421,21 +2421,6 @@ static int repack_without_refs(struct files_ref_store *refs,\n \treturn ret;\n }\n \n-static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)\n-{\n-\tassert(err);\n-\n-\tif (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) {\n-\t\t/*\n-\t\t * loose.  The loose file name is the same as the\n-\t\t * lockfile name, minus \".lock\":\n-\t\t */\n-\t\tif (unlink_or_msg(git_path(\"%s\", lock->ref_name), err))\n-\t\t\treturn 1;\n-\t}\n-\treturn 0;\n-}\n-\n static int files_delete_refs(struct ref_store *ref_store,\n \t\t\t     struct string_list *refnames, unsigned int flags)\n {\n@@ -3788,9 +3773,13 @@ static int files_transaction_commit(struct ref_store *ref_store,\n \n \t\tif (update->flags & REF_DELETING &&\n \t\t    !(update->flags & REF_LOG_ONLY)) {\n-\t\t\tif (delete_ref_loose(lock, update->type, err)) {\n-\t\t\t\tret = TRANSACTION_GENERIC_ERROR;\n-\t\t\t\tgoto cleanup;\n+\t\t\tif (!(update->type & REF_ISPACKED) ||\n+\t\t\t    update->type & REF_ISSYMREF) {\n+\t\t\t\t/* It is a loose reference. */\n+\t\t\t\tif (unlink_or_msg(git_path(\"%s\", lock->ref_name), err)) {\n+\t\t\t\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\t\t\t\tgoto cleanup;\n+\t\t\t\t}\n \t\t\t}\n \n \t\t\tif (!(update->flags & REF_ISPRUNING))\n-- \n2.9.3\n\n"},{"id":"308874","messageId":"2dab5bf3cf9a0da59bf6f10bea32ba4d6d7a0f53.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 22/23] try_remove_empty_parents(): teach to remove parents of reflogs, too","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:42Z","receivedAt":"2017-01-06T16:24:09Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Add a new \"flags\" parameter that tells the function whether to remove\nempty parent directories of the loose reference file, of the reflog\nfile, or both. The new functionality is not yet used.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 24 ++++++++++++++++++------\n 1 file changed, 18 insertions(+), 6 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 88f8c7a..bce0022 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2280,10 +2280,18 @@ static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)\n \treturn 0;\n }\n \n+enum {\n+\tREMOVE_EMPTY_PARENTS_REF = 0x01,\n+\tREMOVE_EMPTY_PARENTS_REFLOG = 0x02\n+};\n+\n /*\n- * Remove empty parents, but spare refs/ and immediate subdirs.\n+ * Remove empty parent directories associated with the specified\n+ * reference and/or its reflog, but spare [logs/]refs/ and immediate\n+ * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or\n+ * REMOVE_EMPTY_PARENTS_REFLOG.\n  */\n-static void try_remove_empty_parents(const char *refname)\n+static void try_remove_empty_parents(const char *refname, unsigned int flags)\n {\n \tstruct strbuf buf = STRBUF_INIT;\n \tchar *p, *q;\n@@ -2299,7 +2307,7 @@ static void try_remove_empty_parents(const char *refname)\n \t\t\tp++;\n \t}\n \tq = buf.buf + buf.len;\n-\twhile (1) {\n+\twhile (flags & (REMOVE_EMPTY_PARENTS_REF | REMOVE_EMPTY_PARENTS_REFLOG)) {\n \t\twhile (q > p && *q != '/')\n \t\t\tq--;\n \t\twhile (q > p && *(q-1) == '/')\n@@ -2307,8 +2315,12 @@ static void try_remove_empty_parents(const char *refname)\n \t\tif (q == p)\n \t\t\tbreak;\n \t\tstrbuf_setlen(&buf, q - buf.buf);\n-\t\tif (rmdir(git_path(\"%s\", buf.buf)))\n-\t\t\tbreak;\n+\t\tif ((flags & REMOVE_EMPTY_PARENTS_REF) &&\n+\t\t    rmdir(git_path(\"%s\", buf.buf)))\n+\t\t\tflags &= ~REMOVE_EMPTY_PARENTS_REF;\n+\t\tif ((flags & REMOVE_EMPTY_PARENTS_REFLOG) &&\n+\t\t    rmdir(git_path(\"logs/%s\", buf.buf)))\n+\t\t\tflags &= ~REMOVE_EMPTY_PARENTS_REFLOG;\n \t}\n \tstrbuf_release(&buf);\n }\n@@ -2334,7 +2346,7 @@ static void prune_ref(struct ref_to_prune *r)\n \t}\n \tref_transaction_free(transaction);\n \tstrbuf_release(&err);\n-\ttry_remove_empty_parents(r->name);\n+\ttry_remove_empty_parents(r->name, REMOVE_EMPTY_PARENTS_REF);\n }\n \n static void prune_refs(struct ref_to_prune *r)\n-- \n2.9.3\n\n"},{"id":"308875","messageId":"0bbc4f8f99c5915037e0f743c2f442988b91c7bd.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 21/23] try_remove_empty_parents(): don't trash argument contents","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:41Z","receivedAt":"2017-01-06T16:24:10Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"It's bad manners and surprising and therefore error-prone.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 16 +++++++++-------\n 1 file changed, 9 insertions(+), 7 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 92a9d99..88f8c7a 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2282,13 +2282,15 @@ static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)\n \n /*\n  * Remove empty parents, but spare refs/ and immediate subdirs.\n- * Note: munges *refname.\n  */\n-static void try_remove_empty_parents(char *refname)\n+static void try_remove_empty_parents(const char *refname)\n {\n+\tstruct strbuf buf = STRBUF_INIT;\n \tchar *p, *q;\n \tint i;\n-\tp = refname;\n+\n+\tstrbuf_addstr(&buf, refname);\n+\tp = buf.buf;\n \tfor (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */\n \t\twhile (*p && *p != '/')\n \t\t\tp++;\n@@ -2296,8 +2298,7 @@ static void try_remove_empty_parents(char *refname)\n \t\twhile (*p == '/')\n \t\t\tp++;\n \t}\n-\tfor (q = p; *q; q++)\n-\t\t;\n+\tq = buf.buf + buf.len;\n \twhile (1) {\n \t\twhile (q > p && *q != '/')\n \t\t\tq--;\n@@ -2305,10 +2306,11 @@ static void try_remove_empty_parents(char *refname)\n \t\t\tq--;\n \t\tif (q == p)\n \t\t\tbreak;\n-\t\t*q = '\\0';\n-\t\tif (rmdir(git_path(\"%s\", refname)))\n+\t\tstrbuf_setlen(&buf, q - buf.buf);\n+\t\tif (rmdir(git_path(\"%s\", buf.buf)))\n \t\t\tbreak;\n \t}\n+\tstrbuf_release(&buf);\n }\n \n /* make sure nobody touched the ref, and unlink */\n-- \n2.9.3\n\n"},{"id":"308876","messageId":"7a8a396e2539adc8cdcc24b1067cc968c158a19c.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 20/23] try_remove_empty_parents(): rename parameter \"name\" -> \"refname\"","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:40Z","receivedAt":"2017-01-06T16:24:11Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"This is the standard nomenclature.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 8 ++++----\n 1 file changed, 4 insertions(+), 4 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 9abd7c3..92a9d99 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2282,13 +2282,13 @@ static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)\n \n /*\n  * Remove empty parents, but spare refs/ and immediate subdirs.\n- * Note: munges *name.\n+ * Note: munges *refname.\n  */\n-static void try_remove_empty_parents(char *name)\n+static void try_remove_empty_parents(char *refname)\n {\n \tchar *p, *q;\n \tint i;\n-\tp = name;\n+\tp = refname;\n \tfor (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */\n \t\twhile (*p && *p != '/')\n \t\t\tp++;\n@@ -2306,7 +2306,7 @@ static void try_remove_empty_parents(char *name)\n \t\tif (q == p)\n \t\t\tbreak;\n \t\t*q = '\\0';\n-\t\tif (rmdir(git_path(\"%s\", name)))\n+\t\tif (rmdir(git_path(\"%s\", refname)))\n \t\t\tbreak;\n \t}\n }\n-- \n2.9.3\n\n"},{"id":"308877","messageId":"196b4cab62f2449a54723fb402138dd5840f3502.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 16/23] log_ref_setup(): manage the name of the reflog file internally","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:36Z","receivedAt":"2017-01-06T16:24:21Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Instead of writing the name of the reflog file into a strbuf that is\nsupplied by the caller but not needed there, write it into a local\ntemporary buffer and remove the strbuf parameter entirely.\n\nAnd while we're adjusting the function signature, reorder the arguments\nto move the input parameters before the output parameters.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 69 ++++++++++++++++++++++++++--------------------------\n 1 file changed, 34 insertions(+), 35 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 9c5e804..846380f 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2719,37 +2719,36 @@ static int open_or_create_logfile(const char *path, void *cb)\n }\n \n /*\n- * Create a reflog for a ref. Store its path to *logfile. If\n- * force_create = 0, only create the reflog for certain refs (those\n- * for which should_autocreate_reflog returns non-zero). Otherwise,\n- * create it regardless of the reference name. If the logfile already\n- * existed or was created, return 0 and set *logfd to the file\n- * descriptor opened for appending to the file. If no logfile exists\n- * and we decided not to create one, return 0 and set *logfd to -1. On\n- * failure, fill in *err, set *logfd to -1, and return -1.\n+ * Create a reflog for a ref. If force_create = 0, only create the\n+ * reflog for certain refs (those for which should_autocreate_reflog\n+ * returns non-zero). Otherwise, create it regardless of the reference\n+ * name. If the logfile already existed or was created, return 0 and\n+ * set *logfd to the file descriptor opened for appending to the file.\n+ * If no logfile exists and we decided not to create one, return 0 and\n+ * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and\n+ * return -1.\n  */\n-static int log_ref_setup(const char *refname,\n-\t\t\t struct strbuf *logfile, int *logfd,\n-\t\t\t struct strbuf *err, int force_create)\n+static int log_ref_setup(const char *refname, int force_create,\n+\t\t\t int *logfd, struct strbuf *err)\n {\n-\tstrbuf_git_path(logfile, \"logs/%s\", refname);\n+\tchar *logfile = git_pathdup(\"logs/%s\", refname);\n \n \tif (force_create || should_autocreate_reflog(refname)) {\n-\t\tif (raceproof_create_file(logfile->buf, open_or_create_logfile, logfd)) {\n+\t\tif (raceproof_create_file(logfile, open_or_create_logfile, logfd)) {\n \t\t\tif (errno == ENOENT)\n \t\t\t\tstrbuf_addf(err, \"unable to create directory for '%s': \"\n-\t\t\t\t\t    \"%s\", logfile->buf, strerror(errno));\n+\t\t\t\t\t    \"%s\", logfile, strerror(errno));\n \t\t\telse if (errno == EISDIR)\n \t\t\t\tstrbuf_addf(err, \"there are still logs under '%s'\",\n-\t\t\t\t\t    logfile->buf);\n+\t\t\t\t\t    logfile);\n \t\t\telse\n \t\t\t\tstrbuf_addf(err, \"unable to append to '%s': %s\",\n-\t\t\t\t\t    logfile->buf, strerror(errno));\n+\t\t\t\t\t    logfile, strerror(errno));\n \n-\t\t\treturn -1;\n+\t\t\tgoto error;\n \t\t}\n \t} else {\n-\t\t*logfd = open(logfile->buf, O_APPEND | O_WRONLY, 0666);\n+\t\t*logfd = open(logfile, O_APPEND | O_WRONLY, 0666);\n \t\tif (*logfd < 0) {\n \t\t\tif (errno == ENOENT || errno == EISDIR) {\n \t\t\t\t/*\n@@ -2761,34 +2760,39 @@ static int log_ref_setup(const char *refname,\n \t\t\t\t;\n \t\t\t} else {\n \t\t\t\tstrbuf_addf(err, \"unable to append to '%s': %s\",\n-\t\t\t\t\t    logfile->buf, strerror(errno));\n-\t\t\t\treturn -1;\n+\t\t\t\t\t    logfile, strerror(errno));\n+\t\t\t\tgoto error;\n \t\t\t}\n \t\t}\n \t}\n \n \tif (*logfd >= 0)\n-\t\tadjust_shared_perm(logfile->buf);\n+\t\tadjust_shared_perm(logfile);\n \n+\tfree(logfile);\n \treturn 0;\n+\n+error:\n+\tfree(logfile);\n+\treturn -1;\n }\n \n static int files_create_reflog(struct ref_store *ref_store,\n \t\t\t       const char *refname, int force_create,\n \t\t\t       struct strbuf *err)\n {\n-\tint ret;\n-\tstruct strbuf sb = STRBUF_INIT;\n \tint fd;\n \n \t/* Check validity (but we don't need the result): */\n \tfiles_downcast(ref_store, 0, \"create_reflog\");\n \n-\tret = log_ref_setup(refname, &sb, &fd, err, force_create);\n+\tif (log_ref_setup(refname, force_create, &fd, err))\n+\t\treturn -1;\n+\n \tif (fd >= 0)\n \t\tclose(fd);\n-\tstrbuf_release(&sb);\n-\treturn ret;\n+\n+\treturn 0;\n }\n \n static int log_ref_write_fd(int fd, const unsigned char *old_sha1,\n@@ -2819,16 +2823,15 @@ static int log_ref_write_fd(int fd, const unsigned char *old_sha1,\n \n static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,\n \t\t\t   const unsigned char *new_sha1, const char *msg,\n-\t\t\t   struct strbuf *logfile, int flags,\n-\t\t\t   struct strbuf *err)\n+\t\t\t   int flags, struct strbuf *err)\n {\n \tint logfd, result;\n \n \tif (log_all_ref_updates < 0)\n \t\tlog_all_ref_updates = !is_bare_repository();\n \n-\tresult = log_ref_setup(refname, logfile, &logfd, err,\n-\t\t\t       flags & REF_FORCE_CREATE_REFLOG);\n+\tresult = log_ref_setup(refname, flags & REF_FORCE_CREATE_REFLOG,\n+\t\t\t       &logfd, err);\n \n \tif (result)\n \t\treturn result;\n@@ -2859,11 +2862,7 @@ int files_log_ref_write(const char *refname, const unsigned char *old_sha1,\n \t\t\tconst unsigned char *new_sha1, const char *msg,\n \t\t\tint flags, struct strbuf *err)\n {\n-\tstruct strbuf sb = STRBUF_INIT;\n-\tint ret = log_ref_write_1(refname, old_sha1, new_sha1, msg, &sb, flags,\n-\t\t\t\t  err);\n-\tstrbuf_release(&sb);\n-\treturn ret;\n+\treturn log_ref_write_1(refname, old_sha1, new_sha1, msg, flags, err);\n }\n \n /*\n-- \n2.9.3\n\n"},{"id":"308878","messageId":"06ef507f76f8f894050d7524892de7cd417d1401.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 12/23] log_ref_setup(): separate code for create vs non-create","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:32Z","receivedAt":"2017-01-06T16:24:24Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"The behavior of this function (especially how it handles errors) is\nquite different depending on whether we are willing to create the reflog\nvs. whether we are only trying to open an existing reflog. So separate\nthe code paths.\n\nThis also simplifies the next steps.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 59 ++++++++++++++++++++++++++++++++++------------------\n 1 file changed, 39 insertions(+), 20 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex fd8a751..c8f6d82 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2718,45 +2718,64 @@ static int commit_ref(struct ref_lock *lock)\n  */\n static int log_ref_setup(const char *refname, struct strbuf *logfile, struct strbuf *err, int force_create)\n {\n-\tint logfd, oflags = O_APPEND | O_WRONLY;\n+\tint logfd;\n \n \tstrbuf_git_path(logfile, \"logs/%s\", refname);\n+\n \tif (force_create || should_autocreate_reflog(refname)) {\n \t\tif (safe_create_leading_directories(logfile->buf) < 0) {\n \t\t\tstrbuf_addf(err, \"unable to create directory for '%s': \"\n \t\t\t\t    \"%s\", logfile->buf, strerror(errno));\n \t\t\treturn -1;\n \t\t}\n-\t\toflags |= O_CREAT;\n-\t}\n-\n-\tlogfd = open(logfile->buf, oflags, 0666);\n-\tif (logfd < 0) {\n-\t\tif (!(oflags & O_CREAT) && (errno == ENOENT || errno == EISDIR))\n-\t\t\treturn 0;\n+\t\tlogfd = open(logfile->buf, O_APPEND | O_WRONLY | O_CREAT, 0666);\n+\t\tif (logfd < 0) {\n+\t\t\tif (errno == EISDIR) {\n+\t\t\t\t/*\n+\t\t\t\t * The directory that is in the way might be\n+\t\t\t\t * empty. Try to remove it.\n+\t\t\t\t */\n+\t\t\t\tif (remove_empty_directories(logfile)) {\n+\t\t\t\t\tstrbuf_addf(err, \"there are still logs under \"\n+\t\t\t\t\t\t    \"'%s'\", logfile->buf);\n+\t\t\t\t\treturn -1;\n+\t\t\t\t}\n+\t\t\t\tlogfd = open(logfile->buf, O_APPEND | O_WRONLY | O_CREAT, 0666);\n+\t\t\t}\n \n-\t\tif (errno == EISDIR) {\n-\t\t\tif (remove_empty_directories(logfile)) {\n-\t\t\t\tstrbuf_addf(err, \"there are still logs under \"\n-\t\t\t\t\t    \"'%s'\", logfile->buf);\n+\t\t\tif (logfd < 0) {\n+\t\t\t\tstrbuf_addf(err, \"unable to append to '%s': %s\",\n+\t\t\t\t\t    logfile->buf, strerror(errno));\n \t\t\t\treturn -1;\n \t\t\t}\n-\t\t\tlogfd = open(logfile->buf, oflags, 0666);\n \t\t}\n-\n+\t} else {\n+\t\tlogfd = open(logfile->buf, O_APPEND | O_WRONLY, 0666);\n \t\tif (logfd < 0) {\n-\t\t\tstrbuf_addf(err, \"unable to append to '%s': %s\",\n-\t\t\t\t    logfile->buf, strerror(errno));\n-\t\t\treturn -1;\n+\t\t\tif (errno == ENOENT || errno == EISDIR) {\n+\t\t\t\t/*\n+\t\t\t\t * The logfile doesn't already exist,\n+\t\t\t\t * but that is not an error; it only\n+\t\t\t\t * means that we won't write log\n+\t\t\t\t * entries to it.\n+\t\t\t\t */\n+\t\t\t\t;\n+\t\t\t} else {\n+\t\t\t\tstrbuf_addf(err, \"unable to append to '%s': %s\",\n+\t\t\t\t\t    logfile->buf, strerror(errno));\n+\t\t\t\treturn -1;\n+\t\t\t}\n \t\t}\n \t}\n \n-\tadjust_shared_perm(logfile->buf);\n-\tclose(logfd);\n+\tif (logfd >= 0) {\n+\t\tadjust_shared_perm(logfile->buf);\n+\t\tclose(logfd);\n+\t}\n+\n \treturn 0;\n }\n \n-\n static int files_create_reflog(struct ref_store *ref_store,\n \t\t\t       const char *refname, int force_create,\n \t\t\t       struct strbuf *err)\n-- \n2.9.3\n\n"},{"id":"308879","messageId":"8c0da0a941845d73988e2c1100f8afb91e272841.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 15/23] log_ref_write_1(): don't depend on logfile argument","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:35Z","receivedAt":"2017-01-06T16:24:25Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"It's unnecessary to pass a strbuf holding the reflog path up and down\nthe call stack now that it is hardly needed by the callers. Remove the\nplaces where log_ref_write_1() uses it, in preparation for making it\ninternal to log_ref_setup().\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 12 ++++++++----\n 1 file changed, 8 insertions(+), 4 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex f723834..9c5e804 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2838,14 +2838,18 @@ static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,\n \tresult = log_ref_write_fd(logfd, old_sha1, new_sha1,\n \t\t\t\t  git_committer_info(0), msg);\n \tif (result) {\n-\t\tstrbuf_addf(err, \"unable to append to '%s': %s\", logfile->buf,\n-\t\t\t    strerror(errno));\n+\t\tint save_errno = errno;\n+\n+\t\tstrbuf_addf(err, \"unable to append to '%s': %s\",\n+\t\t\t    git_path(\"logs/%s\", refname), strerror(save_errno));\n \t\tclose(logfd);\n \t\treturn -1;\n \t}\n \tif (close(logfd)) {\n-\t\tstrbuf_addf(err, \"unable to append to '%s': %s\", logfile->buf,\n-\t\t\t    strerror(errno));\n+\t\tint save_errno = errno;\n+\n+\t\tstrbuf_addf(err, \"unable to append to '%s': %s\",\n+\t\t\t    git_path(\"logs/%s\", refname), strerror(save_errno));\n \t\treturn -1;\n \t}\n \treturn 0;\n-- \n2.9.3\n\n"},{"id":"308880","messageId":"4315abb71f381c217702d70ea34e95fc3dfcc37e.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 17/23] log_ref_write_1(): inline function","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:37Z","receivedAt":"2017-01-06T16:24:26Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Now files_log_ref_write() doesn't do anything beyond call\nlog_ref_write_1(), so inline the latter into the former.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 13 +++----------\n 1 file changed, 3 insertions(+), 10 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 846380f..39d6f5b 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2821,9 +2821,9 @@ static int log_ref_write_fd(int fd, const unsigned char *old_sha1,\n \treturn 0;\n }\n \n-static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,\n-\t\t\t   const unsigned char *new_sha1, const char *msg,\n-\t\t\t   int flags, struct strbuf *err)\n+int files_log_ref_write(const char *refname, const unsigned char *old_sha1,\n+\t\t\tconst unsigned char *new_sha1, const char *msg,\n+\t\t\tint flags, struct strbuf *err)\n {\n \tint logfd, result;\n \n@@ -2858,13 +2858,6 @@ static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,\n \treturn 0;\n }\n \n-int files_log_ref_write(const char *refname, const unsigned char *old_sha1,\n-\t\t\tconst unsigned char *new_sha1, const char *msg,\n-\t\t\tint flags, struct strbuf *err)\n-{\n-\treturn log_ref_write_1(refname, old_sha1, new_sha1, msg, flags, err);\n-}\n-\n /*\n  * Write sha1 into the open lockfile, then close the lockfile. On\n  * errors, rollback the lockfile, fill in *err and\n-- \n2.9.3\n\n"},{"id":"308881","messageId":"500171920606ec6b58b603882a98b8d47739ccb7.1483719289.git.mhagger@alum.mit.edu","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"[PATCH v4 14/23] log_ref_setup(): pass the open file descriptor back to the caller","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2017-01-06T16:22:34Z","receivedAt":"2017-01-06T16:24:28Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"This function will most often be called by log_ref_write_1(), which\nwants to append to the reflog file. In that case, it is silly to close\nthe file only for the caller to reopen it immediately. So, in the case\nthat the file was opened, pass the open file descriptor back to the\ncaller.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 39 ++++++++++++++++++++++-----------------\n 1 file changed, 22 insertions(+), 17 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 27d4fd3..f723834 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2719,19 +2719,23 @@ static int open_or_create_logfile(const char *path, void *cb)\n }\n \n /*\n- * Create a reflog for a ref.  If force_create = 0, the reflog will\n- * only be created for certain refs (those for which\n- * should_autocreate_reflog returns non-zero.  Otherwise, create it\n- * regardless of the ref name.  Fill in *err and return -1 on failure.\n+ * Create a reflog for a ref. Store its path to *logfile. If\n+ * force_create = 0, only create the reflog for certain refs (those\n+ * for which should_autocreate_reflog returns non-zero). Otherwise,\n+ * create it regardless of the reference name. If the logfile already\n+ * existed or was created, return 0 and set *logfd to the file\n+ * descriptor opened for appending to the file. If no logfile exists\n+ * and we decided not to create one, return 0 and set *logfd to -1. On\n+ * failure, fill in *err, set *logfd to -1, and return -1.\n  */\n-static int log_ref_setup(const char *refname, struct strbuf *logfile, struct strbuf *err, int force_create)\n+static int log_ref_setup(const char *refname,\n+\t\t\t struct strbuf *logfile, int *logfd,\n+\t\t\t struct strbuf *err, int force_create)\n {\n-\tint logfd;\n-\n \tstrbuf_git_path(logfile, \"logs/%s\", refname);\n \n \tif (force_create || should_autocreate_reflog(refname)) {\n-\t\tif (raceproof_create_file(logfile->buf, open_or_create_logfile, &logfd)) {\n+\t\tif (raceproof_create_file(logfile->buf, open_or_create_logfile, logfd)) {\n \t\t\tif (errno == ENOENT)\n \t\t\t\tstrbuf_addf(err, \"unable to create directory for '%s': \"\n \t\t\t\t\t    \"%s\", logfile->buf, strerror(errno));\n@@ -2745,8 +2749,8 @@ static int log_ref_setup(const char *refname, struct strbuf *logfile, struct str\n \t\t\treturn -1;\n \t\t}\n \t} else {\n-\t\tlogfd = open(logfile->buf, O_APPEND | O_WRONLY, 0666);\n-\t\tif (logfd < 0) {\n+\t\t*logfd = open(logfile->buf, O_APPEND | O_WRONLY, 0666);\n+\t\tif (*logfd < 0) {\n \t\t\tif (errno == ENOENT || errno == EISDIR) {\n \t\t\t\t/*\n \t\t\t\t * The logfile doesn't already exist,\n@@ -2763,10 +2767,8 @@ static int log_ref_setup(const char *refname, struct strbuf *logfile, struct str\n \t\t}\n \t}\n \n-\tif (logfd >= 0) {\n+\tif (*logfd >= 0)\n \t\tadjust_shared_perm(logfile->buf);\n-\t\tclose(logfd);\n-\t}\n \n \treturn 0;\n }\n@@ -2777,11 +2779,14 @@ static int files_create_reflog(struct ref_store *ref_store,\n {\n \tint ret;\n \tstruct strbuf sb = STRBUF_INIT;\n+\tint fd;\n \n \t/* Check validity (but we don't need the result): */\n \tfiles_downcast(ref_store, 0, \"create_reflog\");\n \n-\tret = log_ref_setup(refname, &sb, err, force_create);\n+\tret = log_ref_setup(refname, &sb, &fd, err, force_create);\n+\tif (fd >= 0)\n+\t\tclose(fd);\n \tstrbuf_release(&sb);\n \treturn ret;\n }\n@@ -2817,17 +2822,17 @@ static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,\n \t\t\t   struct strbuf *logfile, int flags,\n \t\t\t   struct strbuf *err)\n {\n-\tint logfd, result, oflags = O_APPEND | O_WRONLY;\n+\tint logfd, result;\n \n \tif (log_all_ref_updates < 0)\n \t\tlog_all_ref_updates = !is_bare_repository();\n \n-\tresult = log_ref_setup(refname, logfile, err, flags & REF_FORCE_CREATE_REFLOG);\n+\tresult = log_ref_setup(refname, logfile, &logfd, err,\n+\t\t\t       flags & REF_FORCE_CREATE_REFLOG);\n \n \tif (result)\n \t\treturn result;\n \n-\tlogfd = open(logfile->buf, oflags);\n \tif (logfd < 0)\n \t\treturn 0;\n \tresult = log_ref_write_fd(logfd, old_sha1, new_sha1,\n-- \n2.9.3\n\n"},{"id":"308888","messageId":"20170106194457.3qbhggpo6yox34al@sigill.intra.peff.net","threadId":"44825","inReplyTo":"cover.1483719289.git.mhagger@alum.mit.edu","subject":"Re: [PATCH v4 00/23] Delete directories left empty after ref deletion","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-01-06T19:44:57Z","receivedAt":"2017-01-06T19:45:03Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Fri, Jan 06, 2017 at 05:22:20PM +0100, Michael Haggerty wrote:\n\n> This is v4 of this patch series. Thanks to Peff, Junio, Jake, and\n> Philip for their feedback about v3. I believe I have addressed all of\n> the comments about v1 [1], v2 [2], and v3 [3].\n\nThis version looks good to me.\n\n-Peff\n"}]}