refs.c: avoid repeated git_path calls in rename_tmp_log

Because it's not safe to store the static-buffer results of git_path for a long time, we end up formatting the same filename over and over. We can fix this by using a function-local strbuf to store the formatted pathname and avoid repeating ourselves. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Aug 10, 2015 at 05:36 UTC d6549f3655212bd2de52df0137ceb59180424061
1 file changed +14 -7
refs.c
+14 -7
@@ -2930,9 +2930,13 @@ out:
2930 static int rename_tmp_log(const char *newrefname)
2931 {
2932 int attempts_remaining = 4;
2933 + struct strbuf path = STRBUF_INIT;
2934 + int ret = -1;
2935
2936 retry:
2935 - switch (safe_create_leading_directories_const(git_path("logs/%s", newrefname))) {
2937 + strbuf_reset(&path);
2938 + strbuf_git_path(&path, "logs/%s", newrefname);
2939 + switch (safe_create_leading_directories_const(path.buf)) {
2940 case SCLD_OK:
2941 break; /* success */
2942 case SCLD_VANISHED:
@@ -2941,19 +2945,19 @@ static int rename_tmp_log(const char *newrefname)
2945 /* fall through */
2946 default:
2947 error("unable to create directory for %s", newrefname);
2944 - return -1;
2948 + goto out;
2949 }
2950
2947 - if (rename(git_path(TMP_RENAMED_LOG), git_path("logs/%s", newrefname))) {
2951 + if (rename(git_path(TMP_RENAMED_LOG), path.buf)) {
2952 if ((errno==EISDIR || errno==ENOTDIR) && --attempts_remaining > 0) {
2953 /*
2954 * rename(a, b) when b is an existing
2955 * directory ought to result in ISDIR, but
2956 * Solaris 5.8 gives ENOTDIR. Sheesh.
2957 */
2954 - if (remove_empty_directories(git_path("logs/%s", newrefname))) {
2958 + if (remove_empty_directories(path.buf)) {
2959 error("Directory not empty: logs/%s", newrefname);
2956 - return -1;
2960 + goto out;
2961 }
2962 goto retry;
2963 } else if (errno == ENOENT && --attempts_remaining > 0) {
@@ -2966,10 +2970,13 @@ static int rename_tmp_log(const char *newrefname)
2970 } else {
2971 error("unable to move logfile "TMP_RENAMED_LOG" to logs/%s: %s",
2972 newrefname, strerror(errno));
2969 - return -1;
2973 + goto out;
2974 }
2975 }
2972 - return 0;
2976 + ret = 0;
2977 +out:
2978 + strbuf_release(&path);
2979 + return ret;
2980 }
2981
2982 static int rename_ref_available(const char *oldname, const char *newname)