drop.c 3.29 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)
{
	git_reference *ref;
12

13 14
	g_repo = cl_git_sandbox_init("testrepo.git");
	cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD"));
15

16 17 18 19 20 21 22 23 24
	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);
25
	g_reflog = NULL;
26 27 28 29 30 31

	cl_git_sandbox_cleanup();
}

void test_refs_reflog_drop__dropping_a_non_exisiting_entry_from_the_log_returns_ENOTFOUND(void)
{
32
	cl_assert_equal_i(GIT_ENOTFOUND, git_reflog_drop(g_reflog, entrycount, 0));
33

34
	cl_assert_equal_sz(entrycount, git_reflog_entrycount(g_reflog));
35 36 37 38 39 40
}

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

41
	cl_git_pass(git_reflog_drop(g_reflog, 2, 0));
42
	cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog));
43 44 45 46
}

void test_refs_reflog_drop__can_drop_an_entry_and_rewrite_the_log_history(void)
{
47 48 49
	const git_reflog_entry *before_current;
	const git_reflog_entry *after_current;
	git_oid before_current_old_oid, before_current_cur_oid;
50 51 52

	cl_assert(entrycount > 4);

53 54
	before_current = git_reflog_entry_byindex(g_reflog, 1);

55 56
	git_oid_cpy(&before_current_old_oid, &before_current->oid_old);
	git_oid_cpy(&before_current_cur_oid, &before_current->oid_cur);
57

58 59
	cl_git_pass(git_reflog_drop(g_reflog, 1, 1));

60
	cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog));
61

62
	after_current = git_reflog_entry_byindex(g_reflog, 0);
63

64 65
	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));
66 67
}

68
void test_refs_reflog_drop__can_drop_the_oldest_entry(void)
69 70 71 72 73
{
	const git_reflog_entry *entry;

	cl_assert(entrycount > 2);

74
	cl_git_pass(git_reflog_drop(g_reflog, entrycount - 1, 0));
75
	cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog));
76

77
	entry = git_reflog_entry_byindex(g_reflog, entrycount - 2);
78 79 80
	cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_HEX_ZERO) != 0);
}

81
void test_refs_reflog_drop__can_drop_the_oldest_entry_and_rewrite_the_log_history(void)
82 83 84 85 86
{
	const git_reflog_entry *entry;

	cl_assert(entrycount > 2);

87
	cl_git_pass(git_reflog_drop(g_reflog, entrycount - 1, 1));
88
	cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog));
89

90
	entry = git_reflog_entry_byindex(g_reflog, entrycount - 2);
91 92 93 94 95 96 97 98
	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 	{
99 100
		cl_git_pass(git_reflog_drop(g_reflog, 0, 1));
	} while (--entrycount > 0);
101

102
	cl_git_pass(git_reflog_drop(g_reflog, 0, 1));
103

104
	cl_assert_equal_i(0, (int)git_reflog_entrycount(g_reflog));
105
}
106 107 108 109 110 111 112 113

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));
114
	cl_git_pass(git_reflog_drop(g_reflog, 0, 1));
115
	cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog));
116 117 118 119 120 121 122
	cl_git_pass(git_reflog_write(g_reflog));

	git_reflog_free(g_reflog);

	git_reflog_read(&g_reflog, ref);
	git_reference_free(ref);

123
	cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog));
124
}