drop.c 3.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include "clar_libgit2.h"

#include "reflog.h"

static git_repository *g_repo;
static git_reflog *g_reflog;
static unsigned int 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);

	cl_git_sandbox_cleanup();
}

void test_refs_reflog_drop__dropping_a_non_exisiting_entry_from_the_log_returns_ENOTFOUND(void)
{
31
	cl_assert_equal_i(GIT_ENOTFOUND, git_reflog_drop(g_reflog, entrycount, 0));
32 33 34 35 36 37 38 39

	cl_assert_equal_i(entrycount, git_reflog_entrycount(g_reflog));
}

void test_refs_reflog_drop__can_drop_an_entry(void)
{
	cl_assert(entrycount > 4);

40
	cl_git_pass(git_reflog_drop(g_reflog, 2, 0));
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	cl_assert_equal_i(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_previous, *before_next;
	const git_reflog_entry *after_next;
	git_oid before_next_old_oid;

	cl_assert(entrycount > 4);

	before_previous = git_reflog_entry_byindex(g_reflog, 3);
	before_next = git_reflog_entry_byindex(g_reflog, 1);
	git_oid_cpy(&before_next_old_oid, &before_next->oid_old);

56
	cl_git_pass(git_reflog_drop(g_reflog, 2, 1));
57 58 59 60 61 62 63 64 65 66 67 68 69
	cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog));

	after_next = git_reflog_entry_byindex(g_reflog, 1);

	cl_assert_equal_i(0, git_oid_cmp(&before_next->oid_cur, &after_next->oid_cur));
	cl_assert(git_oid_cmp(&before_next_old_oid, &after_next->oid_old) != 0);
	cl_assert_equal_i(0, git_oid_cmp(&before_previous->oid_cur, &after_next->oid_old));
}

void test_refs_reflog_drop__can_drop_the_first_entry(void)
{
	cl_assert(entrycount > 2);

70
	cl_git_pass(git_reflog_drop(g_reflog, 0, 0));
71 72 73 74 75 76 77 78 79
	cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog));
}

void test_refs_reflog_drop__can_drop_the_last_entry(void)
{
	const git_reflog_entry *entry;

	cl_assert(entrycount > 2);

80
	cl_git_pass(git_reflog_drop(g_reflog, entrycount - 1, 0));
81 82 83 84 85 86 87 88 89 90 91 92
	cl_assert_equal_i(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_last_entry_and_rewrite_the_log_history(void)
{
	const git_reflog_entry *entry;

	cl_assert(entrycount > 2);

93
	cl_git_pass(git_reflog_drop(g_reflog, entrycount - 1, 1));
94 95 96 97 98 99 100 101 102 103 104
	cl_assert_equal_i(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 	{
105
		cl_git_pass(git_reflog_drop(g_reflog, --entrycount, 1));
106 107
	} while (entrycount > 0);

108
	cl_git_pass(git_reflog_drop(g_reflog, 0, 1));
109 110 111

	cl_assert_equal_i(0, git_reflog_entrycount(g_reflog));
}
112 113 114 115 116 117 118 119

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));
120
	cl_git_pass(git_reflog_drop(g_reflog, entrycount - 1, 1));
121 122 123 124 125 126 127 128 129 130
	cl_assert_equal_i(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_i(entrycount - 1, git_reflog_entrycount(g_reflog));
}