drop.c 3.09 KB
Newer Older
1 2 3 4 5 6
#include "clar_libgit2.h"

#include "reflog.h"

static git_repository *g_repo;
static git_reflog *g_reflog;
7
static size_t entrycount;
8 9 10 11

void test_refs_reflog_drop__initialize(void)
{
	g_repo = cl_git_sandbox_init("testrepo.git");
12

13
	git_reflog_read(&g_reflog, g_repo, "HEAD");
14 15 16 17 18 19
	entrycount = git_reflog_entrycount(g_reflog);
}

void test_refs_reflog_drop__cleanup(void)
{
	git_reflog_free(g_reflog);
20
	g_reflog = NULL;
21 22 23 24 25 26

	cl_git_sandbox_cleanup();
}

void test_refs_reflog_drop__dropping_a_non_exisiting_entry_from_the_log_returns_ENOTFOUND(void)
{
27
	cl_assert_equal_i(GIT_ENOTFOUND, git_reflog_drop(g_reflog, entrycount, 0));
28

29
	cl_assert_equal_sz(entrycount, git_reflog_entrycount(g_reflog));
30 31 32 33 34 35
}

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

36
	cl_git_pass(git_reflog_drop(g_reflog, 2, 0));
37
	cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog));
38 39 40 41
}

void test_refs_reflog_drop__can_drop_an_entry_and_rewrite_the_log_history(void)
{
42 43 44
	const git_reflog_entry *before_current;
	const git_reflog_entry *after_current;
	git_oid before_current_old_oid, before_current_cur_oid;
45 46 47

	cl_assert(entrycount > 4);

48 49
	before_current = git_reflog_entry_byindex(g_reflog, 1);

50 51
	git_oid_cpy(&before_current_old_oid, &before_current->oid_old);
	git_oid_cpy(&before_current_cur_oid, &before_current->oid_cur);
52

53 54
	cl_git_pass(git_reflog_drop(g_reflog, 1, 1));

55
	cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog));
56

57
	after_current = git_reflog_entry_byindex(g_reflog, 0);
58

59 60
	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));
61 62
}

63
void test_refs_reflog_drop__can_drop_the_oldest_entry(void)
64 65 66 67 68
{
	const git_reflog_entry *entry;

	cl_assert(entrycount > 2);

69
	cl_git_pass(git_reflog_drop(g_reflog, entrycount - 1, 0));
70
	cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog));
71

72
	entry = git_reflog_entry_byindex(g_reflog, entrycount - 2);
73 74 75
	cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_HEX_ZERO) != 0);
}

76
void test_refs_reflog_drop__can_drop_the_oldest_entry_and_rewrite_the_log_history(void)
77 78 79 80 81
{
	const git_reflog_entry *entry;

	cl_assert(entrycount > 2);

82
	cl_git_pass(git_reflog_drop(g_reflog, entrycount - 1, 1));
83
	cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog));
84

85
	entry = git_reflog_entry_byindex(g_reflog, entrycount - 2);
86 87 88 89 90 91 92 93
	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 	{
94 95
		cl_git_pass(git_reflog_drop(g_reflog, 0, 1));
	} while (--entrycount > 0);
96

97
	cl_git_pass(git_reflog_drop(g_reflog, 0, 1));
98

99
	cl_assert_equal_i(0, (int)git_reflog_entrycount(g_reflog));
100
}
101 102 103 104 105

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

106
	cl_git_pass(git_reflog_drop(g_reflog, 0, 1));
107
	cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog));
108 109 110 111
	cl_git_pass(git_reflog_write(g_reflog));

	git_reflog_free(g_reflog);

112
	git_reflog_read(&g_reflog, g_repo, "HEAD");
113

114
	cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog));
115
}