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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Aug 10, 2015 at 05:37 UTC 470e28d4e16dd994cd985914377fa8ccb5f86227
1 file changed +15 -19
refs.c
+15 -19
@@ -2290,25 +2290,14 @@ static int verify_lock(struct ref_lock *lock,
2290 return 0;
2291 }
2292
2293 -static int remove_empty_directories(const char *file)
2293 +static int remove_empty_directories(struct strbuf *path)
2294 {
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;
2297 * if that is an empty directory (or a directory that contains
2298 * only empty directories), remove them.
2299 */
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);
2301 }
2302
2303 /*
@@ -2440,7 +2429,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
2429 * to remain.
2430 */
2431 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)) {
2433 last_errno = errno;
2434 if (!verify_refname_available(orig_refname, extras, skip,
2435 get_loose_refs(&ref_cache), err))
@@ -2961,7 +2950,7 @@ static int rename_tmp_log(const char *newrefname)
2950 * directory ought to result in ISDIR, but
2951 * Solaris 5.8 gives ENOTDIR. Sheesh.
2952 */
2964 - if (remove_empty_directories(path.buf)) {
2953 + if (remove_empty_directories(&path)) {
2954 error("Directory not empty: logs/%s", newrefname);
2955 goto out;
2956 }
@@ -3046,7 +3035,14 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
3035 if (!read_ref_full(newrefname, RESOLVE_REF_READING, sha1, NULL) &&
3036 delete_ref(newrefname, sha1, REF_NODEREF)) {
3037 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) {
3046 error("Directory not empty: %s", newrefname);
3047 goto rollback;
3048 }
@@ -3183,7 +3179,7 @@ static int log_ref_setup(const char *refname, struct strbuf *logfile, struct str
3179 return 0;
3180
3181 if (errno == EISDIR) {
3186 - if (remove_empty_directories(logfile->buf)) {
3182 + if (remove_empty_directories(logfile)) {
3183 strbuf_addf(err, "There are still logs under "
3184 "'%s'", logfile->buf);
3185 return -1;