Commit 93fdbe000c18c94f4f21b7a6613c80bd1dc29e21

schu 2011-09-20T11:11:59

refs: fix git_reference_rename() reference_rename() recently failed when renaming an existing reference refs/heads/foo/bar -> refs/heads/foo because of a change in the underlying functions / error codes. Fixes #412. Signed-off-by: schu <schu-github@schulog.org>

diff --git a/src/refs.c b/src/refs.c
index bc8827b..3711759 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -1287,8 +1287,13 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force)
 		error = git_reference_delete(new_ref);
 	}
 
-	if (error < GIT_SUCCESS && error != GIT_ENOTFOUND)
-		goto cleanup;
+	if (error < GIT_SUCCESS) {
+		git_path_join(aux_path, ref->owner->path_repository, new_name);
+		/* If we couldn't read the reference because it doesn't
+		 * exist it's ok - otherwise return */
+		if (git_futils_isfile(aux_path) == GIT_SUCCESS)
+			goto cleanup;
+	}
 
 	if ((error = reference_available(ref->owner, new_name, ref->name)) < GIT_SUCCESS)
 		return git__rethrow(error, "Failed to rename reference. Reference already exists");
@@ -1328,9 +1333,7 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force)
 		git_hashtable_remove(ref->owner->references.loose_cache, old_name);
 	}
 
-	/* build new path */
 	git_path_join(aux_path, ref->owner->path_repository, new_name);
-
 	if (git_futils_exists(aux_path) == GIT_SUCCESS) {
 		if (git_futils_isdir(aux_path) == GIT_SUCCESS) {
 			if ((error = git_futils_rmdir_r(aux_path, 0)) < GIT_SUCCESS)