summaryrefslogtreecommitdiff
path: root/refs.c
diff options
context:
space:
mode:
authorLibravatar Jeff King <peff@peff.net>2015-08-10 05:36:53 -0400
committerLibravatar Junio C Hamano <gitster@pobox.com>2015-08-10 15:37:13 -0700
commitd6549f3655212bd2de52df0137ceb59180424061 (patch)
tree89ea714cfc8f344ad008a4877f57e3b97ccd8890 /refs.c
parentrefs.c: simplify strbufs in reflog setup and writing (diff)
downloadtgif-d6549f3655212bd2de52df0137ceb59180424061.tar.xz
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>
Diffstat (limited to 'refs.c')
-rw-r--r--refs.c21
1 files changed, 14 insertions, 7 deletions
diff --git a/refs.c b/refs.c
index 105f271e48..086fb1ad02 100644
--- a/refs.c
+++ b/refs.c
@@ -2930,9 +2930,13 @@ out:
static int rename_tmp_log(const char *newrefname)
{
int attempts_remaining = 4;
+ struct strbuf path = STRBUF_INIT;
+ int ret = -1;
retry:
- switch (safe_create_leading_directories_const(git_path("logs/%s", newrefname))) {
+ strbuf_reset(&path);
+ strbuf_git_path(&path, "logs/%s", newrefname);
+ switch (safe_create_leading_directories_const(path.buf)) {
case SCLD_OK:
break; /* success */
case SCLD_VANISHED:
@@ -2941,19 +2945,19 @@ static int rename_tmp_log(const char *newrefname)
/* fall through */
default:
error("unable to create directory for %s", newrefname);
- return -1;
+ goto out;
}
- if (rename(git_path(TMP_RENAMED_LOG), git_path("logs/%s", newrefname))) {
+ if (rename(git_path(TMP_RENAMED_LOG), path.buf)) {
if ((errno==EISDIR || errno==ENOTDIR) && --attempts_remaining > 0) {
/*
* rename(a, b) when b is an existing
* directory ought to result in ISDIR, but
* Solaris 5.8 gives ENOTDIR. Sheesh.
*/
- if (remove_empty_directories(git_path("logs/%s", newrefname))) {
+ if (remove_empty_directories(path.buf)) {
error("Directory not empty: logs/%s", newrefname);
- return -1;
+ goto out;
}
goto retry;
} else if (errno == ENOENT && --attempts_remaining > 0) {
@@ -2966,10 +2970,13 @@ static int rename_tmp_log(const char *newrefname)
} else {
error("unable to move logfile "TMP_RENAMED_LOG" to logs/%s: %s",
newrefname, strerror(errno));
- return -1;
+ goto out;
}
}
- return 0;
+ ret = 0;
+out:
+ strbuf_release(&path);
+ return ret;
}
static int rename_ref_available(const char *oldname, const char *newname)