diff options
Diffstat (limited to 'tests-clar/refs/reflog')
-rw-r--r-- | tests-clar/refs/reflog/drop.c | 124 | ||||
-rw-r--r-- | tests-clar/refs/reflog/reflog.c | 186 |
2 files changed, 310 insertions, 0 deletions
diff --git a/tests-clar/refs/reflog/drop.c b/tests-clar/refs/reflog/drop.c new file mode 100644 index 000000000..21cc847bf --- /dev/null +++ b/tests-clar/refs/reflog/drop.c @@ -0,0 +1,124 @@ +#include "clar_libgit2.h" + +#include "reflog.h" + +static git_repository *g_repo; +static git_reflog *g_reflog; +static size_t entrycount; + +void test_refs_reflog_drop__initialize(void) +{ + git_reference *ref; + + g_repo = cl_git_sandbox_init("testrepo.git"); + cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD")); + + git_reflog_read(&g_reflog, ref); + entrycount = git_reflog_entrycount(g_reflog); + + git_reference_free(ref); +} + +void test_refs_reflog_drop__cleanup(void) +{ + git_reflog_free(g_reflog); + g_reflog = NULL; + + cl_git_sandbox_cleanup(); +} + +void test_refs_reflog_drop__dropping_a_non_exisiting_entry_from_the_log_returns_ENOTFOUND(void) +{ + cl_assert_equal_i(GIT_ENOTFOUND, git_reflog_drop(g_reflog, entrycount, 0)); + + cl_assert_equal_sz(entrycount, git_reflog_entrycount(g_reflog)); +} + +void test_refs_reflog_drop__can_drop_an_entry(void) +{ + cl_assert(entrycount > 4); + + cl_git_pass(git_reflog_drop(g_reflog, 2, 0)); + cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog)); +} + +void test_refs_reflog_drop__can_drop_an_entry_and_rewrite_the_log_history(void) +{ + const git_reflog_entry *before_current; + const git_reflog_entry *after_current; + git_oid before_current_old_oid, before_current_cur_oid; + + cl_assert(entrycount > 4); + + before_current = git_reflog_entry_byindex(g_reflog, 1); + + git_oid_cpy(&before_current_old_oid, &before_current->oid_old); + git_oid_cpy(&before_current_cur_oid, &before_current->oid_cur); + + cl_git_pass(git_reflog_drop(g_reflog, 1, 1)); + + cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog)); + + after_current = git_reflog_entry_byindex(g_reflog, 0); + + cl_assert_equal_i(0, git_oid_cmp(&before_current_old_oid, &after_current->oid_old)); + cl_assert(0 != git_oid_cmp(&before_current_cur_oid, &after_current->oid_cur)); +} + +void test_refs_reflog_drop__can_drop_the_oldest_entry(void) +{ + const git_reflog_entry *entry; + + cl_assert(entrycount > 2); + + cl_git_pass(git_reflog_drop(g_reflog, entrycount - 1, 0)); + cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog)); + + entry = git_reflog_entry_byindex(g_reflog, entrycount - 2); + cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_HEX_ZERO) != 0); +} + +void test_refs_reflog_drop__can_drop_the_oldest_entry_and_rewrite_the_log_history(void) +{ + const git_reflog_entry *entry; + + cl_assert(entrycount > 2); + + cl_git_pass(git_reflog_drop(g_reflog, entrycount - 1, 1)); + cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog)); + + entry = git_reflog_entry_byindex(g_reflog, entrycount - 2); + cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_HEX_ZERO) == 0); +} + +void test_refs_reflog_drop__can_drop_all_the_entries(void) +{ + cl_assert(--entrycount > 0); + + do { + cl_git_pass(git_reflog_drop(g_reflog, 0, 1)); + } while (--entrycount > 0); + + cl_git_pass(git_reflog_drop(g_reflog, 0, 1)); + + cl_assert_equal_i(0, (int)git_reflog_entrycount(g_reflog)); +} + +void test_refs_reflog_drop__can_persist_deletion_on_disk(void) +{ + git_reference *ref; + + cl_assert(entrycount > 2); + + cl_git_pass(git_reference_lookup(&ref, g_repo, g_reflog->ref_name)); + cl_git_pass(git_reflog_drop(g_reflog, 0, 1)); + cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog)); + cl_git_pass(git_reflog_write(g_reflog)); + + git_reflog_free(g_reflog); + + git_reflog_read(&g_reflog, ref); + git_reference_free(ref); + + cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog)); +} diff --git a/tests-clar/refs/reflog/reflog.c b/tests-clar/refs/reflog/reflog.c new file mode 100644 index 000000000..1cd0ddd92 --- /dev/null +++ b/tests-clar/refs/reflog/reflog.c @@ -0,0 +1,186 @@ +#include "clar_libgit2.h" + +#include "repository.h" +#include "git2/reflog.h" +#include "reflog.h" + + +static const char *new_ref = "refs/heads/test-reflog"; +static const char *current_master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"; +#define commit_msg "commit: bla bla" + +static git_repository *g_repo; + + +// helpers +static void assert_signature(git_signature *expected, git_signature *actual) +{ + cl_assert(actual); + cl_assert_equal_s(expected->name, actual->name); + cl_assert_equal_s(expected->email, actual->email); + cl_assert(expected->when.offset == actual->when.offset); + cl_assert(expected->when.time == actual->when.time); +} + + +// Fixture setup and teardown +void test_refs_reflog_reflog__initialize(void) +{ + g_repo = cl_git_sandbox_init("testrepo.git"); +} + +void test_refs_reflog_reflog__cleanup(void) +{ + cl_git_sandbox_cleanup(); +} + +void test_refs_reflog_reflog__append_then_read(void) +{ + // write a reflog for a given reference and ensure it can be read back + git_repository *repo2; + git_reference *ref, *lookedup_ref; + git_oid oid; + git_signature *committer; + git_reflog *reflog; + const git_reflog_entry *entry; + + /* Create a new branch pointing at the HEAD */ + git_oid_fromstr(&oid, current_master_tip); + cl_git_pass(git_reference_create(&ref, g_repo, new_ref, &oid, 0)); + + cl_git_pass(git_signature_now(&committer, "foo", "foo@bar")); + + cl_git_pass(git_reflog_read(&reflog, ref)); + + cl_git_fail(git_reflog_append(reflog, &oid, committer, "no inner\nnewline")); + cl_git_pass(git_reflog_append(reflog, &oid, committer, NULL)); + cl_git_pass(git_reflog_append(reflog, &oid, committer, commit_msg "\n")); + cl_git_pass(git_reflog_write(reflog)); + git_reflog_free(reflog); + + /* Reopen a new instance of the repository */ + cl_git_pass(git_repository_open(&repo2, "testrepo.git")); + + /* Lookup the previously created branch */ + cl_git_pass(git_reference_lookup(&lookedup_ref, repo2, new_ref)); + + /* Read and parse the reflog for this branch */ + cl_git_pass(git_reflog_read(&reflog, lookedup_ref)); + cl_assert_equal_i(2, (int)git_reflog_entrycount(reflog)); + + entry = git_reflog_entry_byindex(reflog, 1); + assert_signature(committer, entry->committer); + cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_HEX_ZERO) == 0); + cl_assert(git_oid_cmp(&oid, &entry->oid_cur) == 0); + cl_assert(entry->msg == NULL); + + entry = git_reflog_entry_byindex(reflog, 0); + assert_signature(committer, entry->committer); + cl_assert(git_oid_cmp(&oid, &entry->oid_old) == 0); + cl_assert(git_oid_cmp(&oid, &entry->oid_cur) == 0); + cl_assert_equal_s(commit_msg, entry->msg); + + git_signature_free(committer); + git_reflog_free(reflog); + git_repository_free(repo2); + + git_reference_free(ref); + git_reference_free(lookedup_ref); +} + +void test_refs_reflog_reflog__renaming_the_reference_moves_the_reflog(void) +{ + git_reference *master, *new_master; + git_buf master_log_path = GIT_BUF_INIT, moved_log_path = GIT_BUF_INIT; + + git_buf_joinpath(&master_log_path, git_repository_path(g_repo), GIT_REFLOG_DIR); + git_buf_puts(&moved_log_path, git_buf_cstr(&master_log_path)); + git_buf_joinpath(&master_log_path, git_buf_cstr(&master_log_path), "refs/heads/master"); + git_buf_joinpath(&moved_log_path, git_buf_cstr(&moved_log_path), "refs/moved"); + + cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&master_log_path))); + cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&moved_log_path))); + + cl_git_pass(git_reference_lookup(&master, g_repo, "refs/heads/master")); + cl_git_pass(git_reference_rename(&new_master, master, "refs/moved", 0)); + git_reference_free(master); + + cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&master_log_path))); + cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&moved_log_path))); + + git_reference_free(new_master); + git_buf_free(&moved_log_path); + git_buf_free(&master_log_path); +} + +static void assert_has_reflog(bool expected_result, const char *name) +{ + git_reference *ref; + + cl_git_pass(git_reference_lookup(&ref, g_repo, name)); + + cl_assert_equal_i(expected_result, git_reference_has_log(ref)); + + git_reference_free(ref); +} + +void test_refs_reflog_reflog__reference_has_reflog(void) +{ + assert_has_reflog(true, "HEAD"); + assert_has_reflog(true, "refs/heads/master"); + assert_has_reflog(false, "refs/heads/subtrees"); +} + +void test_refs_reflog_reflog__reading_the_reflog_from_a_reference_with_no_log_returns_an_empty_one(void) +{ + git_reference *subtrees; + git_reflog *reflog; + git_buf subtrees_log_path = GIT_BUF_INIT; + + cl_git_pass(git_reference_lookup(&subtrees, g_repo, "refs/heads/subtrees")); + + git_buf_join_n(&subtrees_log_path, '/', 3, git_repository_path(g_repo), GIT_REFLOG_DIR, git_reference_name(subtrees)); + cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&subtrees_log_path))); + + cl_git_pass(git_reflog_read(&reflog, subtrees)); + + cl_assert_equal_i(0, (int)git_reflog_entrycount(reflog)); + + git_reflog_free(reflog); + git_reference_free(subtrees); + git_buf_free(&subtrees_log_path); +} + +void test_refs_reflog_reflog__cannot_write_a_moved_reflog(void) +{ + git_reference *master, *new_master; + git_buf master_log_path = GIT_BUF_INIT, moved_log_path = GIT_BUF_INIT; + git_reflog *reflog; + + cl_git_pass(git_reference_lookup(&master, g_repo, "refs/heads/master")); + cl_git_pass(git_reflog_read(&reflog, master)); + + cl_git_pass(git_reflog_write(reflog)); + + cl_git_pass(git_reference_rename(&new_master, master, "refs/moved", 0)); + git_reference_free(master); + + cl_git_fail(git_reflog_write(reflog)); + + git_reflog_free(reflog); + git_reference_free(new_master); + git_buf_free(&moved_log_path); + git_buf_free(&master_log_path); +} + +void test_refs_reflog_reflog__renaming_with_an_invalid_name_returns_EINVALIDSPEC(void) +{ + git_reference *master; + + cl_git_pass(git_reference_lookup(&master, g_repo, "refs/heads/master")); + + cl_assert_equal_i(GIT_EINVALIDSPEC, + git_reflog_rename(master, "refs/heads/Inv@{id")); + + git_reference_free(master); +} |