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

#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
6
#include "ref_helpers.h"
Ben Straub committed
7

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

static git_repository *g_repo;

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

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

23
void test_refs_create__symbolic(void)
Ben Straub committed
24 25 26 27 28 29
{
   // create a new symbolic reference
	git_reference *new_reference, *looked_up_ref, *resolved_ref;
	git_repository *repo2;
	git_oid id;

30
	const char *new_head_tracker = "ANOTHER_HEAD_TRACKER";
Ben Straub committed
31 32 33 34

	git_oid_fromstr(&id, current_master_tip);

	/* Create and write the new symbolic reference */
35
	cl_git_pass(git_reference_symbolic_create(&new_reference, g_repo, new_head_tracker, current_head_target, 0));
Ben Straub committed
36 37 38 39

	/* 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);
40
	cl_assert(reference_is_packed(looked_up_ref) == 0);
Vicent Martí committed
41
	cl_assert_equal_s(looked_up_ref->name, new_head_tracker);
Ben Straub committed
42 43 44 45 46 47

	/* ...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 */
48
	cl_assert(git_oid_cmp(&id, git_reference_target(resolved_ref)) == 0);
Ben Straub committed
49 50 51 52 53 54 55 56
	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));
57
	cl_assert(git_oid_cmp(&id, git_reference_target(resolved_ref)) == 0);
Ben Straub committed
58 59 60 61 62 63 64 65

	git_repository_free(repo2);

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

66
void test_refs_create__deep_symbolic(void)
Ben Straub committed
67 68 69 70 71 72 73 74 75
{
   // create a deep symbolic reference
	git_reference *new_reference, *looked_up_ref, *resolved_ref;
	git_oid id;

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

	git_oid_fromstr(&id, current_master_tip);

76
	cl_git_pass(git_reference_symbolic_create(&new_reference, g_repo, new_head_tracker, current_head_target, 0));
Ben Straub committed
77 78
	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));
79
	cl_assert(git_oid_cmp(&id, git_reference_target(resolved_ref)) == 0);
Ben Straub committed
80 81 82 83 84 85

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

86
void test_refs_create__oid(void)
Ben Straub committed
87 88 89 90 91 92 93 94 95 96 97
{
   // 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 */
98
	cl_git_pass(git_reference_create(&new_reference, g_repo, new_head, &id, 0));
Ben Straub committed
99 100 101 102

	/* 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);
103
	cl_assert(reference_is_packed(looked_up_ref) == 0);
Vicent Martí committed
104
	cl_assert_equal_s(looked_up_ref->name, new_head);
Ben Straub committed
105 106

	/* ...and that it points to the current master tip */
107
	cl_assert(git_oid_cmp(&id, git_reference_target(looked_up_ref)) == 0);
Ben Straub committed
108 109 110 111 112 113
	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));
114
	cl_assert(git_oid_cmp(&id, git_reference_target(looked_up_ref)) == 0);
Ben Straub committed
115 116 117 118 119 120 121

	git_repository_free(repo2);

	git_reference_free(new_reference);
	git_reference_free(looked_up_ref);
}

122
void test_refs_create__oid_unknown(void)
Ben Straub committed
123 124 125 126 127 128 129 130 131 132
{
   // 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 */
133
	cl_git_fail(git_reference_create(&new_reference, g_repo, new_head, &id, 0));
Ben Straub committed
134 135 136 137

	/* Ensure the reference can't be looked-up... */
	cl_git_fail(git_reference_lookup(&looked_up_ref, g_repo, new_head));
}
138 139 140 141 142 143 144 145 146

void test_refs_create__propagate_eexists(void)
{
	int error;
	git_oid oid;
	git_reference *ref;

	/* Make sure it works for oid and for symbolic both */
	git_oid_fromstr(&oid, current_master_tip);
147
	error = git_reference_create(&ref, g_repo, current_head_target, &oid, false);
148 149
	cl_assert(error == GIT_EEXISTS);

150
	error = git_reference_symbolic_create(&ref, g_repo, "HEAD", current_head_target, false);
151 152
	cl_assert(error == GIT_EEXISTS);
}
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

void test_refs_create__creating_a_reference_with_an_invalid_name_returns_EINVALIDSPEC(void)
{
	git_reference *new_reference;
	git_oid id;

	const char *name = "refs/heads/inv@{id";

	git_oid_fromstr(&id, current_master_tip);

	cl_assert_equal_i(GIT_EINVALIDSPEC, git_reference_create(
		&new_reference, g_repo, name, &id, 0));

	cl_assert_equal_i(GIT_EINVALIDSPEC, git_reference_symbolic_create(
		&new_reference, g_repo, name, current_head_target, 0));
}