From 6d4f090df63e06a01a0606c368aaa8e6c9104ade Mon Sep 17 00:00:00 2001 From: nulltoken Date: Thu, 7 Jul 2011 17:49:55 +0200 Subject: [PATCH 1/2] reference_renaming: add additional tests Add some more test checking forced reference renaming. Signed-off-by: nulltoken Acked-by: schu --- tests/t10-refs.c | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/tests/t10-refs.c b/tests/t10-refs.c index 83ccf300b..3310bc72c 100644 --- a/tests/t10-refs.c +++ b/tests/t10-refs.c @@ -635,14 +635,16 @@ BEGIN_TEST(rename4, "can not rename a reference with an invalid name") close_temp_repo(repo); END_TEST -BEGIN_TEST(rename5, "can force-rename a reference with the name of an existing reference") +BEGIN_TEST(rename5, "can force-rename a packed reference with the name of an existing loose and packed reference") git_reference *looked_up_ref; git_repository *repo; + git_oid oid; must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER)); /* An existing reference... */ must_pass(git_reference_lookup(&looked_up_ref, repo, packed_head_name)); + git_oid_cpy(&oid, git_reference_oid(looked_up_ref)); /* Can be force-renamed to the name of another existing reference. */ must_pass(git_reference_rename(looked_up_ref, packed_test_head_name, 1)); @@ -650,6 +652,35 @@ BEGIN_TEST(rename5, "can force-rename a reference with the name of an existing r /* Check we actually renamed it */ must_pass(git_reference_lookup(&looked_up_ref, repo, packed_test_head_name)); must_be_true(!strcmp(looked_up_ref->name, packed_test_head_name)); + must_be_true(!git_oid_cmp(&oid, git_reference_oid(looked_up_ref))); + + /* And that the previous one doesn't exist any longer */ + must_fail(git_reference_lookup(&looked_up_ref, repo, packed_head_name)); + + close_temp_repo(repo); +END_TEST + +BEGIN_TEST(rename6, "can force-rename a loose reference with the name of an existing loose reference") + git_reference *looked_up_ref; + git_repository *repo; + git_oid oid; + + must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER)); + + /* An existing reference... */ + must_pass(git_reference_lookup(&looked_up_ref, repo, "refs/heads/br2")); + git_oid_cpy(&oid, git_reference_oid(looked_up_ref)); + + /* Can be force-renamed to the name of another existing reference. */ + must_pass(git_reference_rename(looked_up_ref, "refs/heads/test", 1)); + + /* Check we actually renamed it */ + must_pass(git_reference_lookup(&looked_up_ref, repo, "refs/heads/test")); + must_be_true(!strcmp(looked_up_ref->name, "refs/heads/test")); + must_be_true(!git_oid_cmp(&oid, git_reference_oid(looked_up_ref))); + + /* And that the previous one doesn't exist any longer */ + must_fail(git_reference_lookup(&looked_up_ref, repo, "refs/heads/br2")); close_temp_repo(repo); END_TEST @@ -658,7 +689,7 @@ static const char *ref_one_name = "refs/heads/one/branch"; static const char *ref_one_name_new = "refs/heads/two/branch"; static const char *ref_two_name = "refs/heads/two"; -BEGIN_TEST(rename6, "can not overwrite name of existing reference") +BEGIN_TEST(rename7, "can not overwrite name of existing reference") git_reference *ref, *ref_one, *ref_one_new, *ref_two; git_repository *repo; git_oid id; @@ -688,7 +719,7 @@ END_TEST static const char *ref_two_name_new = "refs/heads/two/two"; -BEGIN_TEST(rename7, "can be renamed to a new name prefixed with the old name") +BEGIN_TEST(rename8, "can be renamed to a new name prefixed with the old name") git_reference *ref, *ref_two, *looked_up_ref; git_repository *repo; git_oid id; @@ -1000,6 +1031,7 @@ BEGIN_SUITE(refs) ADD_TEST(rename5); ADD_TEST(rename6); ADD_TEST(rename7); + ADD_TEST(rename8); ADD_TEST(delete0); ADD_TEST(list0); From 73294339443011dce770c0d35a72b71113f0830b Mon Sep 17 00:00:00 2001 From: schu Date: Thu, 7 Jul 2011 21:24:12 +0200 Subject: [PATCH 2/2] reference_rename: fix flaw in force-renaming reference_rename didn't respect the force flag. Fixed. Reported-by: nulltoken Signed-off-by: schu --- src/refs.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/refs.c b/src/refs.c index 5e0fe09b2..843c69a32 100644 --- a/src/refs.c +++ b/src/refs.c @@ -1309,8 +1309,12 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force) new_name = normalized; error = git_reference_lookup(&new_ref, ref->owner, new_name); - if (error == GIT_SUCCESS && !force) - return git__throw(GIT_EEXISTS, "Failed to rename reference. Reference already exists"); + if (error == GIT_SUCCESS) { + if (!force) + return git__throw(GIT_EEXISTS, "Failed to rename reference. Reference already exists"); + + error = git_reference_delete(new_ref); + } if (error < GIT_SUCCESS && error != GIT_ENOTFOUND) goto cleanup;