head.c 13.5 KB
Newer Older
1 2
#include "clar_libgit2.h"
#include "refs.h"
3
#include "repo_helpers.h"
4
#include "posix.h"
5
#include "git2/annotated_commit.h"
6

7
static const char *g_email = "foo@example.com";
nulltoken committed
8
static git_repository *repo;
9 10 11 12

void test_repo_head__initialize(void)
{
	repo = cl_git_sandbox_init("testrepo.git");
13
	cl_git_pass(git_repository_set_ident(repo, "Foo Bar", g_email));
14 15 16 17 18 19 20
}

void test_repo_head__cleanup(void)
{
	cl_git_sandbox_cleanup();
}

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
static void check_last_reflog_entry(const char *email, const char *message)
{
	git_reflog *log;
	const git_reflog_entry *entry;

	cl_git_pass(git_reflog_read(&log, repo, GIT_HEAD_FILE));
	cl_assert(git_reflog_entrycount(log) > 0);
	entry = git_reflog_entry_byindex(log, 0);
	if (email)
		cl_assert_equal_s(email, git_reflog_entry_committer(entry)->email);
	if (message)
		cl_assert_equal_s(message, git_reflog_entry_message(entry));
	git_reflog_free(log);
}

36 37 38 39
void test_repo_head__head_detached(void)
{
	git_reference *ref;

40
	cl_assert_equal_i(false, git_repository_head_detached(repo));
41

42 43
	cl_git_pass(git_repository_detach_head(repo));
	check_last_reflog_entry(g_email, "checkout: moving from master to a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
44
	cl_assert_equal_i(true, git_repository_head_detached(repo));
45

46 47
	/* take the repo back to it's original state */
	cl_git_pass(git_reference_symbolic_create(&ref, repo, "HEAD", "refs/heads/master",
48
				true, "REATTACH"));
49
	git_reference_free(ref);
50

51
	check_last_reflog_entry(g_email, "REATTACH");
52
	cl_assert_equal_i(false, git_repository_head_detached(repo));
53 54
}

55
void test_repo_head__unborn_head(void)
56 57 58
{
	git_reference *ref;

nulltoken committed
59
	cl_git_pass(git_repository_head_detached(repo));
60

61
	make_head_unborn(repo, NON_EXISTING_HEAD);
62

63
	cl_assert(git_repository_head_unborn(repo) == 1);
64

65 66

	/* take the repo back to it's original state */
67
	cl_git_pass(git_reference_symbolic_create(&ref, repo, "HEAD", "refs/heads/master", 1, NULL));
68
	cl_assert(git_repository_head_unborn(repo) == 0);
69 70 71

	git_reference_free(ref);
}
72

73 74 75 76
void test_repo_head__set_head_Attaches_HEAD_to_un_unborn_branch_when_the_branch_doesnt_exist(void)
{
	git_reference *head;

77
	cl_git_pass(git_repository_set_head(repo, "refs/heads/doesnt/exist/yet"));
78 79 80

	cl_assert_equal_i(false, git_repository_head_detached(repo));

81
	cl_assert_equal_i(GIT_EUNBORNBRANCH, git_repository_head(&head, repo));
82 83 84 85
}

void test_repo_head__set_head_Returns_ENOTFOUND_when_the_reference_doesnt_exist(void)
{
86
	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_set_head(repo, "refs/tags/doesnt/exist/yet"));
87 88 89 90
}

void test_repo_head__set_head_Fails_when_the_reference_points_to_a_non_commitish(void)
{
91
	cl_git_fail(git_repository_set_head(repo, "refs/tags/point_to_blob"));
92 93 94 95 96 97
}

void test_repo_head__set_head_Attaches_HEAD_when_the_reference_points_to_a_branch(void)
{
	git_reference *head;

98
	cl_git_pass(git_repository_set_head(repo, "refs/heads/br2"));
99 100 101 102 103 104 105 106 107

	cl_assert_equal_i(false, git_repository_head_detached(repo));

	cl_git_pass(git_repository_head(&head, repo));
	cl_assert_equal_s("refs/heads/br2", git_reference_name(head));

	git_reference_free(head);
}

108 109 110 111 112 113 114 115 116
static void assert_head_is_correctly_detached(void)
{
	git_reference *head;
	git_object *commit;

	cl_assert_equal_i(true, git_repository_head_detached(repo));

	cl_git_pass(git_repository_head(&head, repo));

117
	cl_git_pass(git_object_lookup(&commit, repo, git_reference_target(head), GIT_OBJ_COMMIT));
118 119 120 121 122

	git_object_free(commit);
	git_reference_free(head);
}

123 124
void test_repo_head__set_head_Detaches_HEAD_when_the_reference_doesnt_point_to_a_branch(void)
{
125
	cl_git_pass(git_repository_set_head(repo, "refs/tags/test"));
126 127 128 129 130 131

	cl_assert_equal_i(true, git_repository_head_detached(repo));

	assert_head_is_correctly_detached();
}

132 133 134 135 136 137
void test_repo_head__set_head_detached_Return_ENOTFOUND_when_the_object_doesnt_exist(void)
{
	git_oid oid;

	cl_git_pass(git_oid_fromstr(&oid, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"));

138
	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_set_head_detached(repo, &oid));
139 140 141 142 143 144 145 146
}

void test_repo_head__set_head_detached_Fails_when_the_object_isnt_a_commitish(void)
{
	git_object *blob;

	cl_git_pass(git_revparse_single(&blob, repo, "point_to_blob"));

147
	cl_git_fail(git_repository_set_head_detached(repo, git_object_id(blob)));
148 149 150 151 152 153 154 155 156 157 158

	git_object_free(blob);
}

void test_repo_head__set_head_detached_Detaches_HEAD_and_make_it_point_to_the_peeled_commit(void)
{
	git_object *tag;

	cl_git_pass(git_revparse_single(&tag, repo, "tags/test"));
	cl_assert_equal_i(GIT_OBJ_TAG, git_object_type(tag));

159
	cl_git_pass(git_repository_set_head_detached(repo, git_object_id(tag)));
160 161 162 163 164 165

	assert_head_is_correctly_detached();

	git_object_free(tag);
}

166 167 168 169
void test_repo_head__detach_head_Detaches_HEAD_and_make_it_point_to_the_peeled_commit(void)
{
	cl_assert_equal_i(false, git_repository_head_detached(repo));

170
	cl_git_pass(git_repository_detach_head(repo));
171 172 173 174 175 176 177 178

	assert_head_is_correctly_detached();
}

void test_repo_head__detach_head_Fails_if_HEAD_and_point_to_a_non_commitish(void)
{
	git_reference *head;

179
	cl_git_pass(git_reference_symbolic_create(&head, repo, GIT_HEAD_FILE, "refs/tags/point_to_blob", 1, NULL));
180

181
	cl_git_fail(git_repository_detach_head(repo));
182 183 184

	git_reference_free(head);
}
185

186
void test_repo_head__detaching_an_unborn_branch_returns_GIT_EUNBORNBRANCH(void)
187
{
188
	make_head_unborn(repo, NON_EXISTING_HEAD);
189

190
	cl_assert_equal_i(GIT_EUNBORNBRANCH, git_repository_detach_head(repo));
191 192
}

193
void test_repo_head__retrieving_an_unborn_branch_returns_GIT_EUNBORNBRANCH(void)
194 195 196
{
	git_reference *head;

197
	make_head_unborn(repo, NON_EXISTING_HEAD);
198

199
	cl_assert_equal_i(GIT_EUNBORNBRANCH, git_repository_head(&head, repo));
200
}
201

202 203 204 205 206 207 208 209 210
void test_repo_head__retrieving_a_missing_head_returns_GIT_ENOTFOUND(void)
{
	git_reference *head;

	delete_head(repo);

	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_head(&head, repo));
}

211
void test_repo_head__can_tell_if_an_unborn_head_is_detached(void)
212
{
213
	make_head_unborn(repo, NON_EXISTING_HEAD);
214 215 216

	cl_assert_equal_i(false, git_repository_head_detached(repo));
}
217

Ben Straub committed
218 219 220
static void test_reflog(git_repository *repo, size_t idx,
		const char *old_spec, const char *new_spec,
		const char *email, const char *message)
221 222
{
	git_reflog *log;
Ben Straub committed
223
	const git_reflog_entry *entry;
Ben Straub committed
224 225 226 227 228 229 230

	cl_git_pass(git_reflog_read(&log, repo, "HEAD"));
	entry = git_reflog_entry_byindex(log, idx);

	if (old_spec) {
		git_object *obj;
		cl_git_pass(git_revparse_single(&obj, repo, old_spec));
231
		cl_assert_equal_oid(git_object_id(obj), git_reflog_entry_id_old(entry));
Ben Straub committed
232 233 234 235 236
		git_object_free(obj);
	}
	if (new_spec) {
		git_object *obj;
		cl_git_pass(git_revparse_single(&obj, repo, new_spec));
237
		cl_assert_equal_oid(git_object_id(obj), git_reflog_entry_id_new(entry));
Ben Straub committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
		git_object_free(obj);
	}

	if (email) {
		cl_assert_equal_s(email, git_reflog_entry_committer(entry)->email);
	}
	if (message) {
		cl_assert_equal_s(message, git_reflog_entry_message(entry));
	}

	git_reflog_free(log);
}

void test_repo_head__setting_head_updates_reflog(void)
{
253
	git_object *tag;
254
	git_signature *sig;
255
	git_annotated_commit *annotated;
256

257 258
	cl_git_pass(git_signature_now(&sig, "me", "foo@example.com"));

259 260
	cl_git_pass(git_repository_set_head(repo, "refs/heads/haacked"));
	cl_git_pass(git_repository_set_head(repo, "refs/heads/unborn"));
261
	cl_git_pass(git_revparse_single(&tag, repo, "tags/test"));
262 263
	cl_git_pass(git_repository_set_head_detached(repo, git_object_id(tag)));
	cl_git_pass(git_repository_set_head(repo, "refs/heads/haacked"));
264

265 266 267
	test_reflog(repo, 2, NULL, "refs/heads/haacked", "foo@example.com", "checkout: moving from master to haacked");
	test_reflog(repo, 1, NULL, "tags/test^{commit}", "foo@example.com", "checkout: moving from unborn to e90810b8df3e80c413d903f631643c716887138d");
	test_reflog(repo, 0, "tags/test^{commit}", "refs/heads/haacked", "foo@example.com", "checkout: moving from e90810b8df3e80c413d903f631643c716887138d to haacked");
268

269 270 271 272 273 274
	cl_git_pass(git_annotated_commit_from_revspec(&annotated, repo, "haacked~0"));
	cl_git_pass(git_repository_set_head_detached_from_annotated(repo, annotated));

	test_reflog(repo, 0, NULL, "refs/heads/haacked", "foo@example.com", "checkout: moving from haacked to haacked~0");

	git_annotated_commit_free(annotated);
275
	git_object_free(tag);
276
	git_signature_free(sig);
277
}
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307

static void assert_head_reflog(git_repository *repo, size_t idx,
			       const char *old_id, const char *new_id, const char *message)
{
	git_reflog *log;
	const git_reflog_entry *entry;
	char id_str[GIT_OID_HEXSZ + 1] = {0};

	cl_git_pass(git_reflog_read(&log, repo, GIT_HEAD_FILE));
	entry = git_reflog_entry_byindex(log, idx);

	git_oid_fmt(id_str, git_reflog_entry_id_old(entry));
	cl_assert_equal_s(old_id, id_str);

	git_oid_fmt(id_str, git_reflog_entry_id_new(entry));
	cl_assert_equal_s(new_id, id_str);

	cl_assert_equal_s(message, git_reflog_entry_message(entry));

	git_reflog_free(log);
}

void test_repo_head__detaching_writes_reflog(void)
{
	git_signature *sig;
	git_oid id;
	const char *msg;

	cl_git_pass(git_signature_now(&sig, "me", "foo@example.com"));

308
	msg = "checkout: moving from master to e90810b8df3e80c413d903f631643c716887138d";
309
	git_oid_fromstr(&id, "e90810b8df3e80c413d903f631643c716887138d");
310
	cl_git_pass(git_repository_set_head_detached(repo, &id));
311 312 313
	assert_head_reflog(repo, 0, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
			   "e90810b8df3e80c413d903f631643c716887138d", msg);

314 315
	msg = "checkout: moving from e90810b8df3e80c413d903f631643c716887138d to haacked";
	cl_git_pass(git_repository_set_head(repo, "refs/heads/haacked"));
316 317 318 319 320 321 322 323 324 325 326 327
	assert_head_reflog(repo, 0, "e90810b8df3e80c413d903f631643c716887138d",
			   "258f0e2a959a364e40ed6603d5d44fbb24765b10", msg);

	git_signature_free(sig);
}

void test_repo_head__orphan_branch_does_not_count(void)
{
	git_oid id;
	const char *msg;

	/* Have something known */
328
	msg = "checkout: moving from master to e90810b8df3e80c413d903f631643c716887138d";
329
	git_oid_fromstr(&id, "e90810b8df3e80c413d903f631643c716887138d");
330
	cl_git_pass(git_repository_set_head_detached(repo, &id));
331 332 333 334
	assert_head_reflog(repo, 0, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
			   "e90810b8df3e80c413d903f631643c716887138d", msg);

	/* Switching to an orphan branch does not write tot he reflog */
335
	cl_git_pass(git_repository_set_head(repo, "refs/heads/orphan"));
336 337 338 339
	assert_head_reflog(repo, 0, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
			   "e90810b8df3e80c413d903f631643c716887138d", msg);

	/* And coming back, we set the source to zero */
340 341
	msg = "checkout: moving from orphan to haacked";
	cl_git_pass(git_repository_set_head(repo, "refs/heads/haacked"));
342 343 344
	assert_head_reflog(repo, 0, "0000000000000000000000000000000000000000",
			   "258f0e2a959a364e40ed6603d5d44fbb24765b10", msg);
}
345 346 347 348 349 350 351 352 353 354

void test_repo_head__set_to_current_target(void)
{
	git_reflog *log;
	size_t nentries, nentries_after;

	cl_git_pass(git_reflog_read(&log, repo, GIT_HEAD_FILE));
	nentries = git_reflog_entrycount(log);
	git_reflog_free(log);

355 356
	cl_git_pass(git_repository_set_head(repo, "refs/heads/haacked"));
	cl_git_pass(git_repository_set_head(repo, "refs/heads/haacked"));
357 358 359 360 361 362 363

	cl_git_pass(git_reflog_read(&log, repo, GIT_HEAD_FILE));
	nentries_after = git_reflog_entrycount(log);
	git_reflog_free(log);

	cl_assert_equal_i(nentries + 1, nentries_after);
}
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

void test_repo_head__branch_birth(void)
{
	git_signature *sig;
	git_oid id;
	git_tree *tree;
	git_reference *ref;
	const char *msg;
	git_reflog *log;
	size_t nentries, nentries_after;

	cl_git_pass(git_reflog_read(&log, repo, GIT_HEAD_FILE));
	nentries = git_reflog_entrycount(log);
	git_reflog_free(log);

	cl_git_pass(git_signature_now(&sig, "me", "foo@example.com"));

	cl_git_pass(git_repository_head(&ref, repo));
	cl_git_pass(git_reference_peel((git_object **) &tree, ref, GIT_OBJ_TREE));
	git_reference_free(ref);

385
	cl_git_pass(git_repository_set_head(repo, "refs/heads/orphan"));
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

	cl_git_pass(git_reflog_read(&log, repo, GIT_HEAD_FILE));
	nentries_after = git_reflog_entrycount(log);
	git_reflog_free(log);

	cl_assert_equal_i(nentries, nentries_after);

	msg = "message 2";
	cl_git_pass(git_commit_create(&id, repo, "HEAD", sig, sig, NULL, msg, tree, 0, NULL));

	git_tree_free(tree);

	cl_git_pass(git_reflog_read(&log, repo, "refs/heads/orphan"));
	cl_assert_equal_i(1, git_reflog_entrycount(log));
	git_reflog_free(log);

	cl_git_pass(git_reflog_read(&log, repo, GIT_HEAD_FILE));
	nentries_after = git_reflog_entrycount(log);
	git_reflog_free(log);

	cl_assert_equal_i(nentries + 1, nentries_after);

	git_signature_free(sig);

}
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443

static size_t entrycount(git_repository *repo, const char *name)
{
	git_reflog *log;
	size_t ret;

	cl_git_pass(git_reflog_read(&log, repo, name));
	ret = git_reflog_entrycount(log);
	git_reflog_free(log);

	return ret;
}

void test_repo_head__symref_chain(void)
{
	git_signature *sig;
	git_oid id;
	git_tree *tree;
	git_reference *ref;
	const char *msg;
	size_t nentries, nentries_master;

	nentries = entrycount(repo, GIT_HEAD_FILE);

	cl_git_pass(git_signature_now(&sig, "me", "foo@example.com"));

	cl_git_pass(git_repository_head(&ref, repo));
	cl_git_pass(git_reference_peel((git_object **) &tree, ref, GIT_OBJ_TREE));
	git_reference_free(ref);

	nentries_master = entrycount(repo, "refs/heads/master");

	msg = "message 1";
444
	cl_git_pass(git_reference_symbolic_create(&ref, repo, "refs/heads/master", "refs/heads/foo", 1, msg));
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
	git_reference_free(ref);

	cl_assert_equal_i(0, entrycount(repo, "refs/heads/foo"));
	cl_assert_equal_i(nentries, entrycount(repo, GIT_HEAD_FILE));
	cl_assert_equal_i(nentries_master, entrycount(repo, "refs/heads/master"));

	msg = "message 2";
	cl_git_pass(git_commit_create(&id, repo, "HEAD", sig, sig, NULL, msg, tree, 0, NULL));
	git_tree_free(tree);

	cl_assert_equal_i(1, entrycount(repo, "refs/heads/foo"));
	cl_assert_equal_i(nentries +1, entrycount(repo, GIT_HEAD_FILE));
	cl_assert_equal_i(nentries_master, entrycount(repo, "refs/heads/master"));

	git_signature_free(sig);

}