create.c 4.87 KB
Newer Older
Ben Straub committed
1 2 3 4 5 6
#include "clar_libgit2.h"

#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"

7
static const char *current_master_tip = "099fabac3a9ea935598528c27f866e34089c2eff";
Ben Straub committed
8 9 10 11
static const char *current_head_target = "refs/heads/master";

static git_repository *g_repo;

12
void test_refs_create__initialize(void)
Ben Straub committed
13 14 15 16
{
   g_repo = cl_git_sandbox_init("testrepo");
}

17
void test_refs_create__cleanup(void)
Ben Straub committed
18 19 20 21
{
   cl_git_sandbox_cleanup();
}

22
void test_refs_create__symbolic(void)
Ben Straub committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
{
   // create a new symbolic reference
	git_reference *new_reference, *looked_up_ref, *resolved_ref;
	git_repository *repo2;
	git_oid id;
	git_buf ref_path = GIT_BUF_INIT;

	const char *new_head_tracker = "another-head-tracker";

	git_oid_fromstr(&id, current_master_tip);

	/* Retrieve the physical path to the symbolic ref for further cleaning */
	cl_git_pass(git_buf_joinpath(&ref_path, g_repo->path_repository, new_head_tracker));
	git_buf_free(&ref_path);

	/* Create and write the new symbolic reference */
	cl_git_pass(git_reference_create_symbolic(&new_reference, g_repo, new_head_tracker, current_head_target, 0));

	/* 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(git_reference_is_packed(looked_up_ref) == 0);
Vicent Martí committed
45
	cl_assert_equal_s(looked_up_ref->name, new_head_tracker);
Ben Straub committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

	/* ...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(git_oid_cmp(&id, git_reference_oid(resolved_ref)) == 0);
	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(git_oid_cmp(&id, git_reference_oid(resolved_ref)) == 0);

	git_repository_free(repo2);

	git_reference_free(new_reference);
	git_reference_free(looked_up_ref);
	git_reference_free(resolved_ref);
}

70
void test_refs_create__deep_symbolic(void)
Ben Straub committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
{
   // create a deep symbolic reference
	git_reference *new_reference, *looked_up_ref, *resolved_ref;
	git_oid id;
	git_buf ref_path = GIT_BUF_INIT;

	const char *new_head_tracker = "deep/rooted/tracker";

	git_oid_fromstr(&id, current_master_tip);

	cl_git_pass(git_buf_joinpath(&ref_path, g_repo->path_repository, new_head_tracker));
	cl_git_pass(git_reference_create_symbolic(&new_reference, g_repo, new_head_tracker, current_head_target, 0));
	cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head_tracker));
	cl_git_pass(git_reference_resolve(&resolved_ref, looked_up_ref));
	cl_assert(git_oid_cmp(&id, git_reference_oid(resolved_ref)) == 0);

	git_reference_free(new_reference);
	git_reference_free(looked_up_ref);
	git_reference_free(resolved_ref);
	git_buf_free(&ref_path);
}

93
void test_refs_create__oid(void)
Ben Straub committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
{
   // create a new OID reference
	git_reference *new_reference, *looked_up_ref;
	git_repository *repo2;
	git_oid id;
	git_buf ref_path = GIT_BUF_INIT;

	const char *new_head = "refs/heads/new-head";

	git_oid_fromstr(&id, current_master_tip);

	/* Retrieve the physical path to the symbolic ref for further cleaning */
	cl_git_pass(git_buf_joinpath(&ref_path, g_repo->path_repository, new_head));

	/* Create and write the new object id reference */
	cl_git_pass(git_reference_create_oid(&new_reference, g_repo, new_head, &id, 0));

	/* 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(git_reference_is_packed(looked_up_ref) == 0);
Vicent Martí committed
115
	cl_assert_equal_s(looked_up_ref->name, new_head);
Ben Straub committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

	/* ...and that it points to the current master tip */
	cl_assert(git_oid_cmp(&id, git_reference_oid(looked_up_ref)) == 0);
	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(git_oid_cmp(&id, git_reference_oid(looked_up_ref)) == 0);

	git_repository_free(repo2);

	git_reference_free(new_reference);
	git_reference_free(looked_up_ref);
	git_buf_free(&ref_path);
}

134
void test_refs_create__oid_unknown(void)
Ben Straub committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
{
   // Can not create a new OID reference which targets at an unknown id
	git_reference *new_reference, *looked_up_ref;
	git_oid id;

	const char *new_head = "refs/heads/new-head";

	git_oid_fromstr(&id, "deadbeef3f795b2b4353bcce3a527ad0a4f7f644");

	/* Create and write the new object id reference */
	cl_git_fail(git_reference_create_oid(&new_reference, g_repo, new_head, &id, 0));

	/* Ensure the reference can't be looked-up... */
	cl_git_fail(git_reference_lookup(&looked_up_ref, g_repo, new_head));
}