Blame tests/refs/read.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
Packit Service 20376f
#include "repository.h"
Packit Service 20376f
#include "git2/reflog.h"
Packit Service 20376f
#include "reflog.h"
Packit Service 20376f
#include "ref_helpers.h"
Packit Service 20376f
Packit Service 20376f
static const char *loose_tag_ref_name = "refs/tags/e90810b";
Packit Service 20376f
static const char *non_existing_tag_ref_name = "refs/tags/i-do-not-exist";
Packit Service 20376f
static const char *head_tracker_sym_ref_name = "HEAD_TRACKER";
Packit Service 20376f
static const char *current_head_target = "refs/heads/master";
Packit Service 20376f
static const char *current_master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";
Packit Service 20376f
static const char *packed_head_name = "refs/heads/packed";
Packit Service 20376f
static const char *packed_test_head_name = "refs/heads/packed-test";
Packit Service 20376f
Packit Service 20376f
static git_repository *g_repo;
Packit Service 20376f
Packit Service 20376f
void test_refs_read__initialize(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_refs_read__cleanup(void)
Packit Service 20376f
{
Packit Service 20376f
	git_repository_free(g_repo);
Packit Service 20376f
	g_repo = NULL;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_refs_read__loose_tag(void)
Packit Service 20376f
{
Packit Service 20376f
   // lookup a loose tag reference
Packit Service 20376f
	git_reference *reference;
Packit Service 20376f
	git_object *object;
Packit Service 20376f
	git_buf ref_name_from_tag_name = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name));
Packit Service 20376f
	cl_assert(git_reference_type(reference) & GIT_REF_OID);
Packit Service 20376f
	cl_assert(reference_is_packed(reference) == 0);
Packit Service 20376f
	cl_assert_equal_s(reference->name, loose_tag_ref_name);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_object_lookup(&object, g_repo, git_reference_target(reference), GIT_OBJ_ANY));
Packit Service 20376f
	cl_assert(object != NULL);
Packit Service 20376f
	cl_assert(git_object_type(object) == GIT_OBJ_TAG);
Packit Service 20376f
Packit Service 20376f
	/* Ensure the name of the tag matches the name of the reference */
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&ref_name_from_tag_name, GIT_REFS_TAGS_DIR, git_tag_name((git_tag *)object)));
Packit Service 20376f
	cl_assert_equal_s(ref_name_from_tag_name.ptr, loose_tag_ref_name);
Packit Service 20376f
	git_buf_free(&ref_name_from_tag_name);
Packit Service 20376f
Packit Service 20376f
	git_object_free(object);
Packit Service 20376f
Packit Service 20376f
	git_reference_free(reference);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_refs_read__nonexisting_tag(void)
Packit Service 20376f
{
Packit Service 20376f
   // lookup a loose tag reference that doesn't exist
Packit Service 20376f
	git_reference *reference;
Packit Service 20376f
Packit Service 20376f
	cl_git_fail(git_reference_lookup(&reference, g_repo, non_existing_tag_ref_name));
Packit Service 20376f
Packit Service 20376f
	git_reference_free(reference);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
void test_refs_read__symbolic(void)
Packit Service 20376f
{
Packit Service 20376f
   // lookup a symbolic reference
Packit Service 20376f
	git_reference *reference, *resolved_ref;
Packit Service 20376f
	git_object *object;
Packit Service 20376f
	git_oid id;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&reference, g_repo, GIT_HEAD_FILE));
Packit Service 20376f
	cl_assert(git_reference_type(reference) & GIT_REF_SYMBOLIC);
Packit Service 20376f
	cl_assert(reference_is_packed(reference) == 0);
Packit Service 20376f
	cl_assert_equal_s(reference->name, GIT_HEAD_FILE);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reference_resolve(&resolved_ref, reference));
Packit Service 20376f
	cl_assert(git_reference_type(resolved_ref) == GIT_REF_OID);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_object_lookup(&object, g_repo, git_reference_target(resolved_ref), GIT_OBJ_ANY));
Packit Service 20376f
	cl_assert(object != NULL);
Packit Service 20376f
	cl_assert(git_object_type(object) == GIT_OBJ_COMMIT);
Packit Service 20376f
Packit Service 20376f
	git_oid_fromstr(&id, current_master_tip);
Packit Service 20376f
	cl_assert_equal_oid(&id, git_object_id(object));
Packit Service 20376f
Packit Service 20376f
	git_object_free(object);
Packit Service 20376f
Packit Service 20376f
	git_reference_free(reference);
Packit Service 20376f
	git_reference_free(resolved_ref);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_refs_read__nested_symbolic(void)
Packit Service 20376f
{
Packit Service 20376f
   // lookup a nested symbolic reference
Packit Service 20376f
	git_reference *reference, *resolved_ref;
Packit Service 20376f
	git_object *object;
Packit Service 20376f
	git_oid id;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&reference, g_repo, head_tracker_sym_ref_name));
Packit Service 20376f
	cl_assert(git_reference_type(reference) & GIT_REF_SYMBOLIC);
Packit Service 20376f
	cl_assert(reference_is_packed(reference) == 0);
Packit Service 20376f
	cl_assert_equal_s(reference->name, head_tracker_sym_ref_name);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reference_resolve(&resolved_ref, reference));
Packit Service 20376f
	cl_assert(git_reference_type(resolved_ref) == GIT_REF_OID);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_object_lookup(&object, g_repo, git_reference_target(resolved_ref), GIT_OBJ_ANY));
Packit Service 20376f
	cl_assert(object != NULL);
Packit Service 20376f
	cl_assert(git_object_type(object) == GIT_OBJ_COMMIT);
Packit Service 20376f
Packit Service 20376f
	git_oid_fromstr(&id, current_master_tip);
Packit Service 20376f
	cl_assert_equal_oid(&id, git_object_id(object));
Packit Service 20376f
Packit Service 20376f
	git_object_free(object);
Packit Service 20376f
Packit Service 20376f
	git_reference_free(reference);
Packit Service 20376f
	git_reference_free(resolved_ref);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_refs_read__head_then_master(void)
Packit Service 20376f
{
Packit Service 20376f
   // lookup the HEAD and resolve the master branch
Packit Service 20376f
	git_reference *reference, *resolved_ref, *comp_base_ref;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&reference, g_repo, head_tracker_sym_ref_name));
Packit Service 20376f
	cl_git_pass(git_reference_resolve(&comp_base_ref, reference));
Packit Service 20376f
	git_reference_free(reference);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&reference, g_repo, GIT_HEAD_FILE));
Packit Service 20376f
	cl_git_pass(git_reference_resolve(&resolved_ref, reference));
Packit Service 20376f
	cl_assert_equal_oid(git_reference_target(comp_base_ref), git_reference_target(resolved_ref));
Packit Service 20376f
	git_reference_free(reference);
Packit Service 20376f
	git_reference_free(resolved_ref);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&reference, g_repo, current_head_target));
Packit Service 20376f
	cl_git_pass(git_reference_resolve(&resolved_ref, reference));
Packit Service 20376f
	cl_assert_equal_oid(git_reference_target(comp_base_ref), git_reference_target(resolved_ref));
Packit Service 20376f
	git_reference_free(reference);
Packit Service 20376f
	git_reference_free(resolved_ref);
Packit Service 20376f
Packit Service 20376f
	git_reference_free(comp_base_ref);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_refs_read__master_then_head(void)
Packit Service 20376f
{
Packit Service 20376f
   // lookup the master branch and then the HEAD
Packit Service 20376f
	git_reference *reference, *master_ref, *resolved_ref;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&master_ref, g_repo, current_head_target));
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&reference, g_repo, GIT_HEAD_FILE));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reference_resolve(&resolved_ref, reference));
Packit Service 20376f
	cl_assert_equal_oid(git_reference_target(master_ref), git_reference_target(resolved_ref));
Packit Service 20376f
Packit Service 20376f
	git_reference_free(reference);
Packit Service 20376f
	git_reference_free(resolved_ref);
Packit Service 20376f
	git_reference_free(master_ref);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
void test_refs_read__packed(void)
Packit Service 20376f
{
Packit Service 20376f
   // lookup a packed reference
Packit Service 20376f
	git_reference *reference;
Packit Service 20376f
	git_object *object;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&reference, g_repo, packed_head_name));
Packit Service 20376f
	cl_assert(git_reference_type(reference) & GIT_REF_OID);
Packit Service 20376f
	cl_assert(reference_is_packed(reference));
Packit Service 20376f
	cl_assert_equal_s(reference->name, packed_head_name);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_object_lookup(&object, g_repo, git_reference_target(reference), GIT_OBJ_ANY));
Packit Service 20376f
	cl_assert(object != NULL);
Packit Service 20376f
	cl_assert(git_object_type(object) == GIT_OBJ_COMMIT);
Packit Service 20376f
Packit Service 20376f
	git_object_free(object);
Packit Service 20376f
Packit Service 20376f
	git_reference_free(reference);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_refs_read__loose_first(void)
Packit Service 20376f
{
Packit Service 20376f
   // assure that a loose reference is looked up before a packed reference
Packit Service 20376f
	git_reference *reference;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&reference, g_repo, packed_head_name));
Packit Service 20376f
	git_reference_free(reference);
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&reference, g_repo, packed_test_head_name));
Packit Service 20376f
	cl_assert(git_reference_type(reference) & GIT_REF_OID);
Packit Service 20376f
	cl_assert(reference_is_packed(reference) == 0);
Packit Service 20376f
	cl_assert_equal_s(reference->name, packed_test_head_name);
Packit Service 20376f
Packit Service 20376f
	git_reference_free(reference);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_refs_read__chomped(void)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *test, *chomped;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&test, g_repo, "refs/heads/test"));
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&chomped, g_repo, "refs/heads/chomped"));
Packit Service 20376f
	cl_assert_equal_oid(git_reference_target(test), git_reference_target(chomped));
Packit Service 20376f
Packit Service 20376f
	git_reference_free(test);
Packit Service 20376f
	git_reference_free(chomped);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_refs_read__trailing(void)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *test, *trailing;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&test, g_repo, "refs/heads/test"));
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&trailing, g_repo, "refs/heads/trailing"));
Packit Service 20376f
	cl_assert_equal_oid(git_reference_target(test), git_reference_target(trailing));
Packit Service 20376f
	git_reference_free(trailing);
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&trailing, g_repo, "FETCH_HEAD"));
Packit Service 20376f
Packit Service 20376f
	git_reference_free(test);
Packit Service 20376f
	git_reference_free(trailing);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_refs_read__unfound_return_ENOTFOUND(void)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *reference;
Packit Service 20376f
	git_oid id;
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(GIT_ENOTFOUND,
Packit Service 20376f
		git_reference_lookup(&reference, g_repo, "TEST_MASTER"));
Packit Service 20376f
	cl_assert_equal_i(GIT_ENOTFOUND,
Packit Service 20376f
		git_reference_lookup(&reference, g_repo, "refs/test/master"));
Packit Service 20376f
	cl_assert_equal_i(GIT_ENOTFOUND,
Packit Service 20376f
		git_reference_lookup(&reference, g_repo, "refs/tags/test/master"));
Packit Service 20376f
	cl_assert_equal_i(GIT_ENOTFOUND,
Packit Service 20376f
		git_reference_lookup(&reference, g_repo, "refs/tags/test/farther/master"));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(GIT_ENOTFOUND,
Packit Service 20376f
		git_reference_name_to_id(&id, g_repo, "refs/tags/test/farther/master"));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void assert_is_branch(const char *name, bool expected_branchness)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *reference;
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&reference, g_repo, name));
Packit Service 20376f
	cl_assert_equal_i(expected_branchness, git_reference_is_branch(reference));
Packit Service 20376f
	git_reference_free(reference);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_refs_read__can_determine_if_a_reference_is_a_local_branch(void)
Packit Service 20376f
{
Packit Service 20376f
	assert_is_branch("refs/heads/master", true);
Packit Service 20376f
	assert_is_branch("refs/heads/packed", true);
Packit Service 20376f
	assert_is_branch("refs/remotes/test/master", false);
Packit Service 20376f
	assert_is_branch("refs/tags/e90810b", false);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void assert_is_tag(const char *name, bool expected_tagness)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *reference;
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&reference, g_repo, name));
Packit Service 20376f
	cl_assert_equal_i(expected_tagness, git_reference_is_tag(reference));
Packit Service 20376f
	git_reference_free(reference);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_refs_read__can_determine_if_a_reference_is_a_tag(void)
Packit Service 20376f
{
Packit Service 20376f
	assert_is_tag("refs/tags/e90810b", true);
Packit Service 20376f
	assert_is_tag("refs/tags/test", true);
Packit Service 20376f
	assert_is_tag("refs/heads/packed", false);
Packit Service 20376f
	assert_is_tag("refs/remotes/test/master", false);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void assert_is_note(const char *name, bool expected_noteness)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *reference;
Packit Service 20376f
	cl_git_pass(git_reference_lookup(&reference, g_repo, name));
Packit Service 20376f
	cl_assert_equal_i(expected_noteness, git_reference_is_note(reference));
Packit Service 20376f
	git_reference_free(reference);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_refs_read__can_determine_if_a_reference_is_a_note(void)
Packit Service 20376f
{
Packit Service 20376f
	assert_is_note("refs/notes/fanout", true);
Packit Service 20376f
	assert_is_note("refs/heads/packed", false);
Packit Service 20376f
	assert_is_note("refs/remotes/test/master", false);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_refs_read__invalid_name_returns_EINVALIDSPEC(void)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *reference;
Packit Service 20376f
	git_oid id;
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(GIT_EINVALIDSPEC,
Packit Service 20376f
		git_reference_lookup(&reference, g_repo, "refs/heads/Inv@{id"));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(GIT_EINVALIDSPEC,
Packit Service 20376f
		git_reference_name_to_id(&id, g_repo, "refs/heads/Inv@{id"));
Packit Service 20376f
}