Add tests covering overwriting references Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
diff --git a/tests/t10-refs.c b/tests/t10-refs.c
index 565d636..e405364 100644
--- a/tests/t10-refs.c
+++ b/tests/t10-refs.c
@@ -295,6 +295,79 @@ BEGIN_TEST(create2, "create a new OID reference")
must_pass(gitfo_unlink(ref_path)); /* TODO: replace with git_reference_delete() when available */
END_TEST
+static const char *ref_name = "refs/heads/other";
+static const char *ref_master_name = "refs/heads/master";
+BEGIN_TEST(overwrite0, "Overwrite an existing symbolic reference")
+ git_reference *ref;
+ git_repository *repo;
+
+ must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
+
+ must_pass(git_reference_create_symbolic(&ref, repo, ref_name, ref_master_name));
+ must_fail(git_reference_create_symbolic(&ref, repo, ref_name, ref_master_name));
+ must_pass(git_reference_create_symbolic_force(&ref, repo, ref_name, ref_master_name));
+
+ git_reference_delete(ref);
+ git_repository_free(repo);
+END_TEST
+
+BEGIN_TEST(overwrite1, "Overwrite an existing object id reference")
+ git_reference *ref;
+ git_repository *repo;
+ git_oid id;
+
+ must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
+
+ must_pass(git_reference_lookup(&ref, repo, ref_master_name));
+ must_be_true(ref->type & GIT_REF_OID);
+ git_oid_cpy(&id, git_reference_oid(ref));
+
+ must_pass(git_reference_create_oid(&ref, repo, ref_name, &id));
+ must_fail(git_reference_create_oid(&ref, repo, ref_name, &id));
+ must_pass(git_reference_create_oid_force(&ref, repo, ref_name, &id));
+
+ git_reference_delete(ref);
+ git_repository_free(repo);
+END_TEST
+
+BEGIN_TEST(overwrite2, "Overwrite an existing object id reference with a symbolic one")
+ git_reference *ref;
+ git_repository *repo;
+ git_oid id;
+
+ must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
+
+ must_pass(git_reference_lookup(&ref, repo, ref_master_name));
+ must_be_true(ref->type & GIT_REF_OID);
+ git_oid_cpy(&id, git_reference_oid(ref));
+
+ must_pass(git_reference_create_oid(&ref, repo, ref_name, &id));
+ must_fail(git_reference_create_symbolic(&ref, repo, ref_name, ref_master_name));
+ must_pass(git_reference_create_symbolic_force(&ref, repo, ref_name, ref_master_name));
+
+ git_reference_delete(ref);
+ git_repository_free(repo);
+END_TEST
+
+BEGIN_TEST(overwrite3, "Overwrite an existing symbolic reference with an object id one")
+ git_reference *ref;
+ git_repository *repo;
+ git_oid id;
+
+ must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
+
+ must_pass(git_reference_lookup(&ref, repo, ref_master_name));
+ must_be_true(ref->type & GIT_REF_OID);
+ git_oid_cpy(&id, git_reference_oid(ref));
+
+ must_pass(git_reference_create_symbolic(&ref, repo, ref_name, ref_master_name));
+ must_fail(git_reference_create_oid(&ref, repo, ref_name, &id));
+ must_pass(git_reference_create_oid_force(&ref, repo, ref_name, &id));
+
+ git_reference_delete(ref);
+ git_repository_free(repo);
+END_TEST
+
BEGIN_TEST(pack0, "create a packfile for an empty folder")
git_repository *repo;
char temp_path[GIT_PATH_MAX];
@@ -500,6 +573,25 @@ 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")
+ git_reference *looked_up_ref;
+ git_repository *repo;
+
+ must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
+
+ /* An existing reference... */
+ must_pass(git_reference_lookup(&looked_up_ref, repo, packed_head_name));
+
+ /* Can not be renamed to the name of another existing reference. */
+ must_pass(git_reference_rename_force(looked_up_ref, packed_test_head_name));
+
+ /* Failure to rename it hasn't corrupted its state */
+ must_pass(git_reference_lookup(&looked_up_ref, repo, packed_head_name));
+ must_be_true(!strcmp(looked_up_ref->name, packed_head_name));
+
+ close_temp_repo(repo);
+END_TEST
+
BEGIN_TEST(delete0, "deleting a ref which is both packed and loose should remove both tracks in the filesystem")
git_reference *looked_up_ref, *another_looked_up_ref;
git_repository *repo;
@@ -761,6 +853,11 @@ BEGIN_SUITE(refs)
ADD_TEST(create1);
ADD_TEST(create2);
+ ADD_TEST(overwrite0);
+ ADD_TEST(overwrite1);
+ ADD_TEST(overwrite2);
+ ADD_TEST(overwrite3);
+
ADD_TEST(normalize0);
ADD_TEST(normalize1);
ADD_TEST(normalize2);
@@ -773,6 +870,7 @@ BEGIN_SUITE(refs)
ADD_TEST(rename2);
ADD_TEST(rename3);
ADD_TEST(rename4);
+ ADD_TEST(rename5);
ADD_TEST(delete0);
ADD_TEST(list0);