Commit e2d426227f3a03ef6de6715d3c471c1490e4bd4c

nulltoken 2011-03-03T19:41:27

Fix reference renaming to prevent duplicated names

diff --git a/src/refs.c b/src/refs.c
index c532d51..674839b 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -1124,9 +1124,19 @@ int git_reference_rename(git_reference *ref, const char *new_name)
 	int error;
 	char *old_name;
 	char old_path[GIT_PATH_MAX], new_path[GIT_PATH_MAX];
+	git_reference *looked_up_ref;
 
 	assert(ref);
 
+	/* Ensure we're not going to overwrite an existing reference */
+	error = git_repository_lookup_ref(&looked_up_ref, ref->owner, new_name);
+	if (error == GIT_SUCCESS)
+		return GIT_EINVALIDREFNAME;
+
+	if (error != GIT_ENOTFOUND)
+		return error;
+
+
 	old_name = ref->name;
 	ref->name = git__strdup(new_name);