reflog.c 5.72 KB
Newer Older
Ben Straub committed
1 2
#include "clar_libgit2.h"

3
#include "fileops.h"
Ben Straub committed
4 5 6 7 8 9
#include "git2/reflog.h"
#include "reflog.h"


static const char *new_ref = "refs/heads/test-reflog";
static const char *current_master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";
10
#define commit_msg "commit: bla bla"
Ben Straub committed
11 12 13 14 15

static git_repository *g_repo;


// helpers
16
static void assert_signature(git_signature *expected, git_signature *actual)
Ben Straub committed
17
{
18
	cl_assert(actual);
Vicent Martí committed
19 20
	cl_assert_equal_s(expected->name, actual->name);
	cl_assert_equal_s(expected->email, actual->email);
21 22
	cl_assert(expected->when.offset == actual->when.offset);
	cl_assert(expected->when.time == actual->when.time);
Ben Straub committed
23 24 25 26
}


// Fixture setup and teardown
27
void test_refs_reflog_reflog__initialize(void)
Ben Straub committed
28
{
29
   g_repo = cl_git_sandbox_init("testrepo.git");
Ben Straub committed
30 31
}

32
void test_refs_reflog_reflog__cleanup(void)
Ben Straub committed
33 34 35 36
{
   cl_git_sandbox_cleanup();
}

37
void test_refs_reflog_reflog__append_then_read(void)
Ben Straub committed
38 39
{
   // write a reflog for a given reference and ensure it can be read back
40
	git_repository *repo2;
Ben Straub committed
41 42 43 44
	git_reference *ref, *lookedup_ref;
	git_oid oid;
	git_signature *committer;
	git_reflog *reflog;
45
	const git_reflog_entry *entry;
Ben Straub committed
46 47 48

	/* Create a new branch pointing at the HEAD */
	git_oid_fromstr(&oid, current_master_tip);
49
	cl_git_pass(git_reference_create(&ref, g_repo, new_ref, &oid, 0));
Ben Straub committed
50 51 52

	cl_git_pass(git_signature_now(&committer, "foo", "foo@bar"));

53 54 55 56 57 58 59
	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);
Ben Straub committed
60 61

	/* Reopen a new instance of the repository */
62
	cl_git_pass(git_repository_open(&repo2, "testrepo.git"));
Ben Straub committed
63

64
	/* Lookup the previously created branch */
Ben Straub committed
65 66 67 68
	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));
69
	cl_assert_equal_i(2, (int)git_reflog_entrycount(reflog));
Ben Straub committed
70

71
	entry = git_reflog_entry_byindex(reflog, 1);
72
	assert_signature(committer, entry->committer);
73 74
	cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_HEX_ZERO) == 0);
	cl_assert(git_oid_cmp(&oid, &entry->oid_cur) == 0);
Ben Straub committed
75 76
	cl_assert(entry->msg == NULL);

77
	entry = git_reflog_entry_byindex(reflog, 0);
78
	assert_signature(committer, entry->committer);
79 80
	cl_assert(git_oid_cmp(&oid, &entry->oid_old) == 0);
	cl_assert(git_oid_cmp(&oid, &entry->oid_cur) == 0);
Vicent Martí committed
81
	cl_assert_equal_s(commit_msg, entry->msg);
Ben Straub committed
82 83 84 85 86 87 88 89 90

	git_signature_free(committer);
	git_reflog_free(reflog);
	git_repository_free(repo2);

	git_reference_free(ref);
	git_reference_free(lookedup_ref);
}

91
void test_refs_reflog_reflog__renaming_the_reference_moves_the_reflog(void)
92
{
93
	git_reference *master, *new_master;
94 95 96 97 98 99 100 101 102 103 104
	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"));
105 106
	cl_git_pass(git_reference_rename(&new_master, master, "refs/moved", 0));
	git_reference_free(master);
107 108 109 110

	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)));

111
	git_reference_free(new_master);
112 113 114
	git_buf_free(&moved_log_path);
	git_buf_free(&master_log_path);
}
115

116 117 118 119 120 121 122 123 124 125 126
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);
}

127
void test_refs_reflog_reflog__reference_has_reflog(void)
128 129 130 131 132
{
	assert_has_reflog(true, "HEAD");
	assert_has_reflog(true, "refs/heads/master");
	assert_has_reflog(false, "refs/heads/subtrees");
}
133 134 135 136 137 138 139 140 141 142 143 144 145 146

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));

147
	cl_assert_equal_i(0, (int)git_reflog_entrycount(reflog));
148 149 150 151 152

	git_reflog_free(reflog);
	git_reference_free(subtrees);
	git_buf_free(&subtrees_log_path);
}
153 154 155

void test_refs_reflog_reflog__cannot_write_a_moved_reflog(void)
{
156
	git_reference *master, *new_master;
157 158 159 160 161 162 163
	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));
164

165 166
	cl_git_pass(git_reference_rename(&new_master, master, "refs/moved", 0));
	git_reference_free(master);
167 168 169 170

	cl_git_fail(git_reflog_write(reflog));

	git_reflog_free(reflog);
171
	git_reference_free(new_master);
172 173 174
	git_buf_free(&moved_log_path);
	git_buf_free(&master_log_path);
}
175 176 177 178 179 180 181 182 183 184 185 186

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);
}