summaryrefslogtreecommitdiff
path: root/tests/repo/head.c
diff options
context:
space:
mode:
authorCarlos Martín Nieto <cmn@dwim.me>2014-03-18 19:41:03 +0100
committerCarlos Martín Nieto <cmn@dwim.me>2014-03-18 19:58:52 +0100
commitbac95e6e1e99b1e364c5ebd39887a8e24bc1ad9d (patch)
treeccbbd903a453f88cca47da9e8fbac4c9a87e8715 /tests/repo/head.c
parent5dd7d2432ed77651110bcbb37479b2d4e5b01b19 (diff)
downloadlibgit2-bac95e6e1e99b1e364c5ebd39887a8e24bc1ad9d.tar.gz
reflog: more comprehensive HEAD tests
The existing ones lack checking zeroed ids when switching back from an unborn branch as well as what happens when detaching. The reflog appending function mistakenly wrote zeros when dealing with a detached HEAD. This explicitly checks for those situations and fixes them.
Diffstat (limited to 'tests/repo/head.c')
-rw-r--r--tests/repo/head.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/repo/head.c b/tests/repo/head.c
index d88fd90d1..a246e6086 100644
--- a/tests/repo/head.c
+++ b/tests/repo/head.c
@@ -269,3 +269,75 @@ 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);
+}