void test_refs_pack__loose(void) { /* create a packfile from all the loose refs in a repo */ git_reference *reference; git_buf temp_path = GIT_BUF_INIT; /* Ensure a known loose ref can be looked up */ cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name)); cl_assert(reference_is_packed(reference) == 0); cl_assert_equal_s(reference->name, loose_tag_ref_name); git_reference_free(reference); /* * We are now trying to pack also a loose reference * called `points_to_blob`, to make sure we can properly * pack weak tags */ packall(); /* Ensure the packed-refs file exists */ cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), GIT_PACKEDREFS_FILE)); cl_assert(git_path_exists(temp_path.ptr)); /* Ensure the known ref can still be looked up but is now packed */ cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name)); cl_assert(reference_is_packed(reference)); cl_assert_equal_s(reference->name, loose_tag_ref_name); /* Ensure the known ref has been removed from the loose folder structure */ cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), loose_tag_ref_name)); cl_assert(!git_path_exists(temp_path.ptr)); git_reference_free(reference); git_buf_free(&temp_path); }
void test_refs_delete__packed_only(void) { // can delete a just packed reference git_reference *ref; git_refdb *refdb; git_oid id; const char *new_ref = "refs/heads/new_ref"; git_oid_fromstr(&id, current_master_tip); /* Create and write the new object id reference */ cl_git_pass(git_reference_create(&ref, g_repo, new_ref, &id, 0)); git_reference_free(ref); /* Lookup the reference */ cl_git_pass(git_reference_lookup(&ref, g_repo, new_ref)); /* Ensure it's a loose reference */ cl_assert(reference_is_packed(ref) == 0); /* Pack all existing references */ cl_git_pass(git_repository_refdb(&refdb, g_repo)); cl_git_pass(git_refdb_compress(refdb)); /* Reload the reference from disk */ git_reference_free(ref); cl_git_pass(git_reference_lookup(&ref, g_repo, new_ref)); /* Ensure it's a packed reference */ cl_assert(reference_is_packed(ref) == 1); /* This should pass */ cl_git_pass(git_reference_delete(ref)); git_reference_free(ref); }
void test_refs_delete__packed_loose(void) { // 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_buf temp_path = GIT_BUF_INIT; /* Ensure the loose reference exists on the file system */ cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, packed_test_head_name)); cl_assert(git_path_exists(temp_path.ptr)); /* Lookup the reference */ cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_test_head_name)); /* Ensure it's the loose version that has been found */ cl_assert(reference_is_packed(looked_up_ref) == 0); /* Now that the reference is deleted... */ cl_git_pass(git_reference_delete(looked_up_ref)); git_reference_free(looked_up_ref); /* Looking up the reference once again should not retrieve it */ cl_git_fail(git_reference_lookup(&another_looked_up_ref, g_repo, packed_test_head_name)); /* Ensure the loose reference doesn't exist any longer on the file system */ cl_assert(!git_path_exists(temp_path.ptr)); git_reference_free(another_looked_up_ref); git_buf_free(&temp_path); }
void test_refs_create__oid(void) { /* create a new OID reference */ git_reference *new_reference, *looked_up_ref; git_repository *repo2; git_oid id; const char *new_head = "refs/heads/new-head"; git_oid_fromstr(&id, current_master_tip); /* Create and write the new object id reference */ cl_git_pass(git_reference_create(&new_reference, g_repo, new_head, &id, 0, NULL)); /* Ensure the reference can be looked-up... */ cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head)); cl_assert(git_reference_type(looked_up_ref) & GIT_REF_OID); cl_assert(reference_is_packed(looked_up_ref) == 0); cl_assert_equal_s(looked_up_ref->name, new_head); /* ...and that it points to the current master tip */ cl_assert_equal_oid(&id, git_reference_target(looked_up_ref)); git_reference_free(looked_up_ref); /* Similar test with a fresh new repository */ cl_git_pass(git_repository_open(&repo2, "testrepo")); cl_git_pass(git_reference_lookup(&looked_up_ref, repo2, new_head)); cl_assert_equal_oid(&id, git_reference_target(looked_up_ref)); git_repository_free(repo2); git_reference_free(new_reference); git_reference_free(looked_up_ref); }
void test_refs_create__symbolic_with_arbitrary_content(void) { git_reference *new_reference, *looked_up_ref; git_repository *repo2; git_oid id; const char *new_head_tracker = "ANOTHER_HEAD_TRACKER"; const char *arbitrary_target = "ARBITRARY DATA"; git_oid_fromstr(&id, current_master_tip); /* Attempt to create symbolic ref with arbitrary data in target * fails by default */ cl_git_fail(git_reference_symbolic_create(&new_reference, g_repo, new_head_tracker, arbitrary_target, 0, NULL)); git_libgit2_opts(GIT_OPT_ENABLE_STRICT_SYMBOLIC_REF_CREATION, 0); /* With strict target validation disabled, ref creation succeeds */ cl_git_pass(git_reference_symbolic_create(&new_reference, g_repo, new_head_tracker, arbitrary_target, 0, NULL)); /* Ensure the reference can be looked-up... */ cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head_tracker)); cl_assert(git_reference_type(looked_up_ref) & GIT_REF_SYMBOLIC); cl_assert(reference_is_packed(looked_up_ref) == 0); cl_assert_equal_s(looked_up_ref->name, new_head_tracker); git_reference_free(looked_up_ref); /* Ensure the target is what we expect it to be */ cl_assert_equal_s(git_reference_symbolic_target(new_reference), arbitrary_target); /* Similar test with a fresh new repository object */ cl_git_pass(git_repository_open(&repo2, "testrepo")); /* Ensure the reference can be looked-up... */ cl_git_pass(git_reference_lookup(&looked_up_ref, repo2, new_head_tracker)); cl_assert(git_reference_type(looked_up_ref) & GIT_REF_SYMBOLIC); cl_assert(reference_is_packed(looked_up_ref) == 0); cl_assert_equal_s(looked_up_ref->name, new_head_tracker); /* Ensure the target is what we expect it to be */ cl_assert_equal_s(git_reference_symbolic_target(new_reference), arbitrary_target); git_repository_free(repo2); git_reference_free(new_reference); git_reference_free(looked_up_ref); }
void test_refs_rename__packed(void) { // rename a packed reference (should make it loose) git_reference *looked_up_ref, *new_ref, *another_looked_up_ref; git_buf temp_path = GIT_BUF_INIT; const char *brand_new_name = "refs/heads/brand_new_name"; /* Ensure the ref doesn't exist on the file system */ cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), packed_head_name)); cl_assert(!git_path_exists(temp_path.ptr)); /* The reference can however be looked-up... */ cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name)); /* .. and it's packed */ cl_assert(reference_is_packed(looked_up_ref) != 0); /* Now that the reference is renamed... */ cl_git_pass(git_reference_rename(&new_ref, looked_up_ref, brand_new_name, 0, NULL)); cl_assert_equal_s(new_ref->name, brand_new_name); git_reference_free(looked_up_ref); /* ...It can't be looked-up with the old name... */ cl_git_fail(git_reference_lookup(&another_looked_up_ref, g_repo, packed_head_name)); /* ...but the new name works ok... */ cl_git_pass(git_reference_lookup(&another_looked_up_ref, g_repo, brand_new_name)); cl_assert_equal_s(another_looked_up_ref->name, brand_new_name); /* .. the ref is no longer packed... */ cl_assert(reference_is_packed(another_looked_up_ref) == 0); cl_assert(reference_is_packed(new_ref) == 0); /* ...and the ref now happily lives in the file system */ cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), brand_new_name)); cl_assert(git_path_exists(temp_path.ptr)); git_reference_free(new_ref); git_reference_free(another_looked_up_ref); git_buf_free(&temp_path); }
void test_refs_rename__loose(void) { // rename a loose reference git_reference *looked_up_ref, *new_ref, *another_looked_up_ref; git_buf temp_path = GIT_BUF_INIT; const char *new_name = "refs/tags/Nemo/knows/refs.kung-fu"; /* Ensure the ref doesn't exist on the file system */ cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), new_name)); cl_assert(!git_path_exists(temp_path.ptr)); /* Retrieval of the reference to rename */ cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, loose_tag_ref_name)); /* ... which is indeed loose */ cl_assert(reference_is_packed(looked_up_ref) == 0); /* Now that the reference is renamed... */ cl_git_pass(git_reference_rename(&new_ref, looked_up_ref, new_name, 0, NULL)); cl_assert_equal_s(new_ref->name, new_name); git_reference_free(looked_up_ref); /* ...It can't be looked-up with the old name... */ cl_git_fail(git_reference_lookup(&another_looked_up_ref, g_repo, loose_tag_ref_name)); /* ...but the new name works ok... */ cl_git_pass(git_reference_lookup(&another_looked_up_ref, g_repo, new_name)); cl_assert_equal_s(new_ref->name, new_name); /* .. the new ref is loose... */ cl_assert(reference_is_packed(another_looked_up_ref) == 0); cl_assert(reference_is_packed(new_ref) == 0); /* ...and the ref can be found in the file system */ cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), new_name)); cl_assert(git_path_exists(temp_path.ptr)); git_reference_free(new_ref); git_reference_free(another_looked_up_ref); git_buf_free(&temp_path); }
void test_refs_rename__packed_doesnt_pack_others(void) { // renaming a packed reference does not pack another reference which happens to be in both loose and pack state git_reference *looked_up_ref, *another_looked_up_ref, *renamed_ref; git_buf temp_path = GIT_BUF_INIT; const char *brand_new_name = "refs/heads/brand_new_name"; /* Ensure the other reference exists on the file system */ cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), packed_test_head_name)); cl_assert(git_path_exists(temp_path.ptr)); /* Lookup the other reference */ cl_git_pass(git_reference_lookup(&another_looked_up_ref, g_repo, packed_test_head_name)); /* Ensure it's loose */ cl_assert(reference_is_packed(another_looked_up_ref) == 0); git_reference_free(another_looked_up_ref); /* Lookup the reference to rename */ cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name)); /* Ensure it's packed */ cl_assert(reference_is_packed(looked_up_ref) != 0); /* Now that the reference is renamed... */ cl_git_pass(git_reference_rename(&renamed_ref, looked_up_ref, brand_new_name, 0, NULL)); git_reference_free(looked_up_ref); /* Lookup the other reference */ cl_git_pass(git_reference_lookup(&another_looked_up_ref, g_repo, packed_test_head_name)); /* Ensure it's loose */ cl_assert(reference_is_packed(another_looked_up_ref) == 0); /* Ensure the other ref still exists on the file system */ cl_assert(git_path_exists(temp_path.ptr)); git_reference_free(renamed_ref); git_reference_free(another_looked_up_ref); git_buf_free(&temp_path); }
void test_refs_create__symbolic(void) { /* create a new symbolic reference */ git_reference *new_reference, *looked_up_ref, *resolved_ref; git_repository *repo2; git_oid id; const char *new_head_tracker = "ANOTHER_HEAD_TRACKER"; git_oid_fromstr(&id, current_master_tip); /* Create and write the new symbolic reference */ cl_git_pass(git_reference_symbolic_create(&new_reference, g_repo, new_head_tracker, current_head_target, 0, NULL)); /* Ensure the reference can be looked-up... */ cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head_tracker)); cl_assert(git_reference_type(looked_up_ref) & GIT_REF_SYMBOLIC); cl_assert(reference_is_packed(looked_up_ref) == 0); cl_assert_equal_s(looked_up_ref->name, new_head_tracker); /* ...peeled.. */ cl_git_pass(git_reference_resolve(&resolved_ref, looked_up_ref)); cl_assert(git_reference_type(resolved_ref) == GIT_REF_OID); /* ...and that it points to the current master tip */ cl_assert_equal_oid(&id, git_reference_target(resolved_ref)); git_reference_free(looked_up_ref); git_reference_free(resolved_ref); /* Similar test with a fresh new repository */ cl_git_pass(git_repository_open(&repo2, "testrepo")); cl_git_pass(git_reference_lookup(&looked_up_ref, repo2, new_head_tracker)); cl_git_pass(git_reference_resolve(&resolved_ref, looked_up_ref)); cl_assert_equal_oid(&id, git_reference_target(resolved_ref)); git_repository_free(repo2); git_reference_free(new_reference); git_reference_free(looked_up_ref); git_reference_free(resolved_ref); }