Commit Diff


commit - a77d6bed1e95077c47201094f95b13b2b913d3410beea2f058dd31c384e4c55c
commit + 74a837d76ae84c16268c08cb2ab0db4224d6b4085f6c58516cf86bea8f6a10d5
blob - 7cd4bcffebfcf9c9e5315e98907a60af40c1b43c93288d60e165c79a3288aee4
blob + b51edaeb5a67e5f4f2acbbe20753903d685f51bd2ed99964a518448d11df671c
--- lib/reference.c	
+++ lib/reference.c	
@@ -1535,7 +1535,7 @@ delete_loose_ref(struct got_reference *ref, struct got
 {
 	const struct got_error *err = NULL, *unlock_err = NULL;
 	const char *name = got_ref_get_name(ref);
-	char *path_refs = NULL, *path = NULL;
+	char *path_refs = NULL, *path = NULL, *parent = NULL;
 	struct got_lockfile *lf = NULL;
 
 	path_refs = get_refs_dir_path(repo, name);
@@ -1549,6 +1549,10 @@ delete_loose_ref(struct got_reference *ref, struct got
 		goto done;
 	}
 
+	err = got_path_dirname(&parent, path);
+	if (err)
+		goto done;
+
 	if (ref->lf == NULL) {
 		err = got_lockfile_lock(&lf, path, -1);
 		if (err)
@@ -1563,8 +1567,14 @@ done:
 	if (ref->lf == NULL && lf)
 		unlock_err = got_lockfile_unlock(lf, -1);
 
+	/* Remove empty directories to make parent reference name available. */
+	if (err == NULL && unlock_err == NULL &&
+	    rmdir(parent) == -1 && errno != ENOTEMPTY && errno != ENOENT)
+		err = got_error_from_errno2("rmdir", path);
+
 	free(path_refs);
 	free(path);
+	free(parent);
 	return err ? err : unlock_err;
 }