diff options
Diffstat (limited to 'tests/repo/head.c')
-rw-r--r-- | tests/repo/head.c | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/tests/repo/head.c b/tests/repo/head.c index d88fd90d1..79892a3ea 100644 --- a/tests/repo/head.c +++ b/tests/repo/head.c @@ -269,3 +269,201 @@ void test_repo_head__setting_head_updates_reflog(void) git_object_free(tag); git_signature_free(sig); } + +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")); + + msg = "message1"; + git_oid_fromstr(&id, "e90810b8df3e80c413d903f631643c716887138d"); + cl_git_pass(git_repository_set_head_detached(repo, &id, sig, msg)); + assert_head_reflog(repo, 0, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", + "e90810b8df3e80c413d903f631643c716887138d", msg); + + msg = "message2"; + cl_git_pass(git_repository_set_head(repo, "refs/heads/haacked", sig, msg)); + assert_head_reflog(repo, 0, "e90810b8df3e80c413d903f631643c716887138d", + "258f0e2a959a364e40ed6603d5d44fbb24765b10", msg); + + git_signature_free(sig); +} + +void test_repo_head__orphan_branch_does_not_count(void) +{ + git_signature *sig; + git_oid id; + const char *msg; + + cl_git_pass(git_signature_now(&sig, "me", "foo@example.com")); + + /* Have something known */ + msg = "message1"; + git_oid_fromstr(&id, "e90810b8df3e80c413d903f631643c716887138d"); + cl_git_pass(git_repository_set_head_detached(repo, &id, sig, msg)); + assert_head_reflog(repo, 0, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", + "e90810b8df3e80c413d903f631643c716887138d", msg); + + /* Switching to an orphan branch does not write tot he reflog */ + cl_git_pass(git_repository_set_head(repo, "refs/heads/orphan", sig, "ignored message")); + assert_head_reflog(repo, 0, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", + "e90810b8df3e80c413d903f631643c716887138d", msg); + + /* And coming back, we set the source to zero */ + msg = "message2"; + cl_git_pass(git_repository_set_head(repo, "refs/heads/haacked", sig, msg)); + assert_head_reflog(repo, 0, "0000000000000000000000000000000000000000", + "258f0e2a959a364e40ed6603d5d44fbb24765b10", msg); + + git_signature_free(sig); +} + +void test_repo_head__set_to_current_target(void) +{ + git_signature *sig; + 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")); + + msg = "message 1"; + cl_git_pass(git_repository_set_head(repo, "refs/heads/haacked", sig, msg)); + cl_git_pass(git_repository_set_head(repo, "refs/heads/haacked", sig, msg)); + + 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); + +} + +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); + + msg = "message 1"; + cl_git_pass(git_repository_set_head(repo, "refs/heads/orphan", sig, msg)); + + 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); + +} + +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"; + cl_git_pass(git_reference_symbolic_create(&ref, repo, "refs/heads/master", "refs/heads/foo", 1, sig, msg)); + 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); + +} |