Skip to content

Commit 470e28d

Browse files
peffgitster
authored andcommitted
refs.c: remove_empty_directories can take a strbuf
The first thing we do in this function is copy the input into a strbuf. Of the 4 callers, 3 of them already have a strbuf we could use. Let's just take the strbuf, and convert the remaining caller to use a strbuf, rather than a raw git_path. This is safer, anyway, as remove_dir_recursively is a non-trivial function that might use the pathname buffers itself (this is _probably_ OK, as the likely culprit would be calling resolve_gitlink_ref, but we do not pass the proper flags to ask it to avoid blowing away gitlinks). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5f8ef5b commit 470e28d

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

refs.c

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,25 +2290,14 @@ static int verify_lock(struct ref_lock *lock,
22902290
return 0;
22912291
}
22922292

2293-
static int remove_empty_directories(const char *file)
2293+
static int remove_empty_directories(struct strbuf *path)
22942294
{
2295-
/* we want to create a file but there is a directory there;
2295+
/*
2296+
* we want to create a file but there is a directory there;
22962297
* if that is an empty directory (or a directory that contains
22972298
* only empty directories), remove them.
22982299
*/
2299-
struct strbuf path;
2300-
int result, save_errno;
2301-
2302-
strbuf_init(&path, 20);
2303-
strbuf_addstr(&path, file);
2304-
2305-
result = remove_dir_recursively(&path, REMOVE_DIR_EMPTY_ONLY);
2306-
save_errno = errno;
2307-
2308-
strbuf_release(&path);
2309-
errno = save_errno;
2310-
2311-
return result;
2300+
return remove_dir_recursively(path, REMOVE_DIR_EMPTY_ONLY);
23122301
}
23132302

23142303
/*
@@ -2440,7 +2429,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
24402429
* to remain.
24412430
*/
24422431
strbuf_git_path(&orig_ref_file, "%s", orig_refname);
2443-
if (remove_empty_directories(orig_ref_file.buf)) {
2432+
if (remove_empty_directories(&orig_ref_file)) {
24442433
last_errno = errno;
24452434
if (!verify_refname_available(orig_refname, extras, skip,
24462435
get_loose_refs(&ref_cache), err))
@@ -2961,7 +2950,7 @@ static int rename_tmp_log(const char *newrefname)
29612950
* directory ought to result in ISDIR, but
29622951
* Solaris 5.8 gives ENOTDIR. Sheesh.
29632952
*/
2964-
if (remove_empty_directories(path.buf)) {
2953+
if (remove_empty_directories(&path)) {
29652954
error("Directory not empty: logs/%s", newrefname);
29662955
goto out;
29672956
}
@@ -3046,7 +3035,14 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
30463035
if (!read_ref_full(newrefname, RESOLVE_REF_READING, sha1, NULL) &&
30473036
delete_ref(newrefname, sha1, REF_NODEREF)) {
30483037
if (errno==EISDIR) {
3049-
if (remove_empty_directories(git_path("%s", newrefname))) {
3038+
struct strbuf path = STRBUF_INIT;
3039+
int result;
3040+
3041+
strbuf_git_path(&path, "%s", newrefname);
3042+
result = remove_empty_directories(&path);
3043+
strbuf_release(&path);
3044+
3045+
if (result) {
30503046
error("Directory not empty: %s", newrefname);
30513047
goto rollback;
30523048
}
@@ -3183,7 +3179,7 @@ static int log_ref_setup(const char *refname, struct strbuf *logfile, struct str
31833179
return 0;
31843180

31853181
if (errno == EISDIR) {
3186-
if (remove_empty_directories(logfile->buf)) {
3182+
if (remove_empty_directories(logfile)) {
31873183
strbuf_addf(err, "There are still logs under "
31883184
"'%s'", logfile->buf);
31893185
return -1;

0 commit comments

Comments
 (0)