read.c 9.54 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 9

static const char *loose_tag_ref_name = "refs/tags/e90810b";
static const char *non_existing_tag_ref_name = "refs/tags/i-do-not-exist";
10
static const char *head_tracker_sym_ref_name = "HEAD_TRACKER";
Ben Straub committed
11 12 13 14 15 16 17
static const char *current_head_target = "refs/heads/master";
static const char *current_master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";
static const char *packed_head_name = "refs/heads/packed";
static const char *packed_test_head_name = "refs/heads/packed-test";

static git_repository *g_repo;

18
void test_refs_read__initialize(void)
Ben Straub committed
19
{
20
	cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
Ben Straub committed
21 22
}

23
void test_refs_read__cleanup(void)
Ben Straub committed
24
{
25
	git_repository_free(g_repo);
26
	g_repo = NULL;
Ben Straub committed
27 28
}

29
void test_refs_read__loose_tag(void)
Ben Straub committed
30 31 32 33 34 35 36 37
{
   // lookup a loose tag reference
	git_reference *reference;
	git_object *object;
	git_buf ref_name_from_tag_name = GIT_BUF_INIT;

	cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name));
	cl_assert(git_reference_type(reference) & GIT_REF_OID);
38
	cl_assert(reference_is_packed(reference) == 0);
Vicent Martí committed
39
	cl_assert_equal_s(reference->name, loose_tag_ref_name);
Ben Straub committed
40

41
	cl_git_pass(git_object_lookup(&object, g_repo, git_reference_target(reference), GIT_OBJ_ANY));
Ben Straub committed
42 43 44 45 46
	cl_assert(object != NULL);
	cl_assert(git_object_type(object) == GIT_OBJ_TAG);

	/* Ensure the name of the tag matches the name of the reference */
	cl_git_pass(git_buf_joinpath(&ref_name_from_tag_name, GIT_REFS_TAGS_DIR, git_tag_name((git_tag *)object)));
Vicent Martí committed
47
	cl_assert_equal_s(ref_name_from_tag_name.ptr, loose_tag_ref_name);
Ben Straub committed
48 49 50 51 52 53 54
	git_buf_free(&ref_name_from_tag_name);

	git_object_free(object);

	git_reference_free(reference);
}

55
void test_refs_read__nonexisting_tag(void)
Ben Straub committed
56 57 58 59 60 61 62 63 64 65
{
   // lookup a loose tag reference that doesn't exist
	git_reference *reference;

	cl_git_fail(git_reference_lookup(&reference, g_repo, non_existing_tag_ref_name));

	git_reference_free(reference);
}


66
void test_refs_read__symbolic(void)
Ben Straub committed
67 68 69 70 71 72 73 74
{
   // lookup a symbolic reference
	git_reference *reference, *resolved_ref;
	git_object *object;
	git_oid id;

	cl_git_pass(git_reference_lookup(&reference, g_repo, GIT_HEAD_FILE));
	cl_assert(git_reference_type(reference) & GIT_REF_SYMBOLIC);
75
	cl_assert(reference_is_packed(reference) == 0);
Vicent Martí committed
76
	cl_assert_equal_s(reference->name, GIT_HEAD_FILE);
Ben Straub committed
77 78 79 80

	cl_git_pass(git_reference_resolve(&resolved_ref, reference));
	cl_assert(git_reference_type(resolved_ref) == GIT_REF_OID);

81
	cl_git_pass(git_object_lookup(&object, g_repo, git_reference_target(resolved_ref), GIT_OBJ_ANY));
Ben Straub committed
82 83 84 85
	cl_assert(object != NULL);
	cl_assert(git_object_type(object) == GIT_OBJ_COMMIT);

	git_oid_fromstr(&id, current_master_tip);
86
	cl_assert_equal_oid(&id, git_object_id(object));
Ben Straub committed
87 88 89 90 91 92 93

	git_object_free(object);

	git_reference_free(reference);
	git_reference_free(resolved_ref);
}

94
void test_refs_read__nested_symbolic(void)
Ben Straub committed
95 96 97 98 99 100 101 102
{
   // lookup a nested symbolic reference
	git_reference *reference, *resolved_ref;
	git_object *object;
	git_oid id;

	cl_git_pass(git_reference_lookup(&reference, g_repo, head_tracker_sym_ref_name));
	cl_assert(git_reference_type(reference) & GIT_REF_SYMBOLIC);
103
	cl_assert(reference_is_packed(reference) == 0);
Vicent Martí committed
104
	cl_assert_equal_s(reference->name, head_tracker_sym_ref_name);
Ben Straub committed
105 106 107 108

	cl_git_pass(git_reference_resolve(&resolved_ref, reference));
	cl_assert(git_reference_type(resolved_ref) == GIT_REF_OID);

109
	cl_git_pass(git_object_lookup(&object, g_repo, git_reference_target(resolved_ref), GIT_OBJ_ANY));
Ben Straub committed
110 111 112 113
	cl_assert(object != NULL);
	cl_assert(git_object_type(object) == GIT_OBJ_COMMIT);

	git_oid_fromstr(&id, current_master_tip);
114
	cl_assert_equal_oid(&id, git_object_id(object));
Ben Straub committed
115 116 117 118 119 120 121

	git_object_free(object);

	git_reference_free(reference);
	git_reference_free(resolved_ref);
}

122
void test_refs_read__head_then_master(void)
Ben Straub committed
123 124 125 126 127 128 129 130 131 132
{
   // lookup the HEAD and resolve the master branch
	git_reference *reference, *resolved_ref, *comp_base_ref;

	cl_git_pass(git_reference_lookup(&reference, g_repo, head_tracker_sym_ref_name));
	cl_git_pass(git_reference_resolve(&comp_base_ref, reference));
	git_reference_free(reference);

	cl_git_pass(git_reference_lookup(&reference, g_repo, GIT_HEAD_FILE));
	cl_git_pass(git_reference_resolve(&resolved_ref, reference));
133
	cl_assert_equal_oid(git_reference_target(comp_base_ref), git_reference_target(resolved_ref));
Ben Straub committed
134 135 136 137 138
	git_reference_free(reference);
	git_reference_free(resolved_ref);

	cl_git_pass(git_reference_lookup(&reference, g_repo, current_head_target));
	cl_git_pass(git_reference_resolve(&resolved_ref, reference));
139
	cl_assert_equal_oid(git_reference_target(comp_base_ref), git_reference_target(resolved_ref));
Ben Straub committed
140 141 142 143 144 145
	git_reference_free(reference);
	git_reference_free(resolved_ref);

	git_reference_free(comp_base_ref);
}

146
void test_refs_read__master_then_head(void)
Ben Straub committed
147 148 149 150 151 152 153 154
{
   // lookup the master branch and then the HEAD
	git_reference *reference, *master_ref, *resolved_ref;

	cl_git_pass(git_reference_lookup(&master_ref, g_repo, current_head_target));
	cl_git_pass(git_reference_lookup(&reference, g_repo, GIT_HEAD_FILE));

	cl_git_pass(git_reference_resolve(&resolved_ref, reference));
155
	cl_assert_equal_oid(git_reference_target(master_ref), git_reference_target(resolved_ref));
Ben Straub committed
156 157 158 159 160 161 162

	git_reference_free(reference);
	git_reference_free(resolved_ref);
	git_reference_free(master_ref);
}


163
void test_refs_read__packed(void)
Ben Straub committed
164 165 166 167 168 169 170
{
   // lookup a packed reference
	git_reference *reference;
	git_object *object;

	cl_git_pass(git_reference_lookup(&reference, g_repo, packed_head_name));
	cl_assert(git_reference_type(reference) & GIT_REF_OID);
171
	cl_assert(reference_is_packed(reference));
Vicent Martí committed
172
	cl_assert_equal_s(reference->name, packed_head_name);
Ben Straub committed
173

174
	cl_git_pass(git_object_lookup(&object, g_repo, git_reference_target(reference), GIT_OBJ_ANY));
Ben Straub committed
175 176 177 178 179 180 181 182
	cl_assert(object != NULL);
	cl_assert(git_object_type(object) == GIT_OBJ_COMMIT);

	git_object_free(object);

	git_reference_free(reference);
}

183
void test_refs_read__loose_first(void)
Ben Straub committed
184 185 186 187 188 189 190 191
{
   // assure that a loose reference is looked up before a packed reference
	git_reference *reference;

	cl_git_pass(git_reference_lookup(&reference, g_repo, packed_head_name));
	git_reference_free(reference);
	cl_git_pass(git_reference_lookup(&reference, g_repo, packed_test_head_name));
	cl_assert(git_reference_type(reference) & GIT_REF_OID);
192
	cl_assert(reference_is_packed(reference) == 0);
Vicent Martí committed
193
	cl_assert_equal_s(reference->name, packed_test_head_name);
Ben Straub committed
194 195 196

	git_reference_free(reference);
}
197

198 199 200 201 202 203
void test_refs_read__chomped(void)
{
	git_reference *test, *chomped;

	cl_git_pass(git_reference_lookup(&test, g_repo, "refs/heads/test"));
	cl_git_pass(git_reference_lookup(&chomped, g_repo, "refs/heads/chomped"));
204
	cl_assert_equal_oid(git_reference_target(test), git_reference_target(chomped));
205 206 207 208 209

	git_reference_free(test);
	git_reference_free(chomped);
}

210 211 212 213 214 215
void test_refs_read__trailing(void)
{
	git_reference *test, *trailing;

	cl_git_pass(git_reference_lookup(&test, g_repo, "refs/heads/test"));
	cl_git_pass(git_reference_lookup(&trailing, g_repo, "refs/heads/trailing"));
216
	cl_assert_equal_oid(git_reference_target(test), git_reference_target(trailing));
217
	git_reference_free(trailing);
218
	cl_git_pass(git_reference_lookup(&trailing, g_repo, "FETCH_HEAD"));
219 220 221 222 223

	git_reference_free(test);
	git_reference_free(trailing);
}

224 225 226
void test_refs_read__unfound_return_ENOTFOUND(void)
{
	git_reference *reference;
227
	git_oid id;
228

229 230 231 232 233 234 235 236
	cl_assert_equal_i(GIT_ENOTFOUND,
		git_reference_lookup(&reference, g_repo, "TEST_MASTER"));
	cl_assert_equal_i(GIT_ENOTFOUND,
		git_reference_lookup(&reference, g_repo, "refs/test/master"));
	cl_assert_equal_i(GIT_ENOTFOUND,
		git_reference_lookup(&reference, g_repo, "refs/tags/test/master"));
	cl_assert_equal_i(GIT_ENOTFOUND,
		git_reference_lookup(&reference, g_repo, "refs/tags/test/farther/master"));
237 238 239

	cl_assert_equal_i(GIT_ENOTFOUND,
		git_reference_name_to_id(&id, g_repo, "refs/tags/test/farther/master"));
240
}
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

static void assert_is_branch(const char *name, bool expected_branchness)
{
	git_reference *reference;
	cl_git_pass(git_reference_lookup(&reference, g_repo, name));
	cl_assert_equal_i(expected_branchness, git_reference_is_branch(reference));
	git_reference_free(reference);
}

void test_refs_read__can_determine_if_a_reference_is_a_local_branch(void)
{
	assert_is_branch("refs/heads/master", true);
	assert_is_branch("refs/heads/packed", true);
	assert_is_branch("refs/remotes/test/master", false);
	assert_is_branch("refs/tags/e90810b", false);
}
257

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
static void assert_is_tag(const char *name, bool expected_tagness)
{
	git_reference *reference;
	cl_git_pass(git_reference_lookup(&reference, g_repo, name));
	cl_assert_equal_i(expected_tagness, git_reference_is_tag(reference));
	git_reference_free(reference);
}

void test_refs_read__can_determine_if_a_reference_is_a_tag(void)
{
	assert_is_tag("refs/tags/e90810b", true);
	assert_is_tag("refs/tags/test", true);
	assert_is_tag("refs/heads/packed", false);
	assert_is_tag("refs/remotes/test/master", false);
}

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
static void assert_is_note(const char *name, bool expected_noteness)
{
	git_reference *reference;
	cl_git_pass(git_reference_lookup(&reference, g_repo, name));
	cl_assert_equal_i(expected_noteness, git_reference_is_note(reference));
	git_reference_free(reference);
}

void test_refs_read__can_determine_if_a_reference_is_a_note(void)
{
	assert_is_note("refs/notes/fanout", true);
	assert_is_note("refs/heads/packed", false);
	assert_is_note("refs/remotes/test/master", false);
}

289 290 291 292 293 294 295 296 297 298 299
void test_refs_read__invalid_name_returns_EINVALIDSPEC(void)
{
	git_reference *reference;
	git_oid id;

	cl_assert_equal_i(GIT_EINVALIDSPEC,
		git_reference_lookup(&reference, g_repo, "refs/heads/Inv@{id"));

	cl_assert_equal_i(GIT_EINVALIDSPEC,
		git_reference_name_to_id(&id, g_repo, "refs/heads/Inv@{id"));
}