Skip to content

Commit d6549f3

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 54b418f commit d6549f3

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

refs.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2930,9 +2930,13 @@ int delete_refs(struct string_list *refnames)
29302930
static int rename_tmp_log(const char *newrefname)
29312931
{
29322932
int attempts_remaining = 4;
2933+
struct strbuf path = STRBUF_INIT;
2934+
int ret = -1;
29332935

29342936
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)) {
29362940
case SCLD_OK:
29372941
break; /* success */
29382942
case SCLD_VANISHED:
@@ -2941,19 +2945,19 @@ static int rename_tmp_log(const char *newrefname)
29412945
/* fall through */
29422946
default:
29432947
error("unable to create directory for %s", newrefname);
2944-
return -1;
2948+
goto out;
29452949
}
29462950

2947-
if (rename(git_path(TMP_RENAMED_LOG), git_path("logs/%s", newrefname))) {
2951+
if (rename(git_path(TMP_RENAMED_LOG), path.buf)) {
29482952
if ((errno==EISDIR || errno==ENOTDIR) && --attempts_remaining > 0) {
29492953
/*
29502954
* rename(a, b) when b is an existing
29512955
* directory ought to result in ISDIR, but
29522956
* Solaris 5.8 gives ENOTDIR. Sheesh.
29532957
*/
2954-
if (remove_empty_directories(git_path("logs/%s", newrefname))) {
2958+
if (remove_empty_directories(path.buf)) {
29552959
error("Directory not empty: logs/%s", newrefname);
2956-
return -1;
2960+
goto out;
29572961
}
29582962
goto retry;
29592963
} else if (errno == ENOENT && --attempts_remaining > 0) {
@@ -2966,10 +2970,13 @@ static int rename_tmp_log(const char *newrefname)
29662970
} else {
29672971
error("unable to move logfile "TMP_RENAMED_LOG" to logs/%s: %s",
29682972
newrefname, strerror(errno));
2969-
return -1;
2973+
goto out;
29702974
}
29712975
}
2972-
return 0;
2976+
ret = 0;
2977+
out:
2978+
strbuf_release(&path);
2979+
return ret;
29732980
}
29742981

29752982
static int rename_ref_available(const char *oldname, const char *newname)

0 commit comments

Comments
 (0)