hard.c 6.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include "clar_libgit2.h"
#include "posix.h"
#include "reset_helpers.h"
#include "path.h"
#include "fileops.h"

static git_repository *repo;
static git_object *target;

void test_reset_hard__initialize(void)
{
	repo = cl_git_sandbox_init("status");
	target = NULL;
}

void test_reset_hard__cleanup(void)
{
18 19 20 21
	if (target != NULL) {
		git_object_free(target);
		target = NULL;
	}
22

23 24 25
	cl_git_sandbox_cleanup();
}

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
static int strequal_ignore_eol(const char *exp, const char *str)
{
	while (*exp && *str) {
		if (*exp != *str) {
			while (*exp == '\r' || *exp == '\n') ++exp;
			while (*str == '\r' || *str == '\n') ++str;
			if (*exp != *str)
				return false;
		} else {
			exp++; str++;
		}
	}
	return (!*exp && !*str);
}

Russell Belfer committed
41
void test_reset_hard__resetting_reverts_modified_files(void)
42
{
Russell Belfer committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	git_buf path = GIT_BUF_INIT, content = GIT_BUF_INIT;
	int i;
	static const char *files[4] = {
		"current_file",
		"modified_file",
		"staged_new_file",
		"staged_changes_modified_file" };
	static const char *before[4] = {
		"current_file\n",
		"modified_file\nmodified_file\n",
		"staged_new_file\n",
		"staged_changes_modified_file\nstaged_changes_modified_file\nstaged_changes_modified_file\n"
	};
	static const char *after[4] = {
		"current_file\n",
		"modified_file\n",
59
		NULL,
Russell Belfer committed
60 61 62 63 64 65 66 67 68 69 70 71
		"staged_changes_modified_file\n"
	};
	const char *wd = git_repository_workdir(repo);

	cl_assert(wd);

	for (i = 0; i < 4; ++i) {
		cl_git_pass(git_buf_joinpath(&path, wd, files[i]));
		cl_git_pass(git_futils_readbuffer(&content, path.ptr));
		cl_assert_equal_s(before[i], content.ptr);
	}

72
	cl_git_pass(git_revparse_single(&target, repo, "26a125e"));
73

74
	cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL));
75

Russell Belfer committed
76 77 78 79
	for (i = 0; i < 4; ++i) {
		cl_git_pass(git_buf_joinpath(&path, wd, files[i]));
		if (after[i]) {
			cl_git_pass(git_futils_readbuffer(&content, path.ptr));
80
			cl_assert(strequal_ignore_eol(after[i], content.ptr));
Russell Belfer committed
81 82 83 84 85 86 87
		} else {
			cl_assert(!git_path_exists(path.ptr));
		}
	}

	git_buf_free(&content);
	git_buf_free(&path);
88
}
89 90 91 92 93 94 95 96

void test_reset_hard__cannot_reset_in_a_bare_repository(void)
{
	git_repository *bare;

	cl_git_pass(git_repository_open(&bare, cl_fixture("testrepo.git")));
	cl_assert(git_repository_is_bare(bare) == true);

97
	cl_git_pass(git_revparse_single(&target, bare, KNOWN_COMMIT_IN_BARE_REPO));
98

99
	cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_HARD, NULL, NULL));
100 101 102

	git_repository_free(bare);
}
Edward Thomson committed
103

104 105 106
static void index_entry_init(git_index *index, int side, git_oid *oid)
{
	git_index_entry entry;
nulltoken committed
107

108
	memset(&entry, 0x0, sizeof(git_index_entry));
nulltoken committed
109

110 111 112
	entry.path = "conflicting_file";
	entry.flags = (side << GIT_IDXENTRY_STAGESHIFT);
	entry.mode = 0100644;
113
	git_oid_cpy(&entry.id, oid);
nulltoken committed
114

115 116 117 118 119 120 121 122 123
	cl_git_pass(git_index_add(index, &entry));
}

static void unmerged_index_init(git_index *index, int entries)
{
	int write_ancestor = 1;
	int write_ours = 2;
	int write_theirs = 4;
	git_oid ancestor, ours, theirs;
nulltoken committed
124

125 126 127
	git_oid_fromstr(&ancestor, "6bb0d9f700543ba3d318ba7075fc3bd696b4287b");
	git_oid_fromstr(&ours, "b19a1e93bec1317dc6097229e12afaffbfa74dc2");
	git_oid_fromstr(&theirs, "950b81b7eee953d050aa05a641f8e056c85dd1bd");
nulltoken committed
128

129
	cl_git_rewritefile("status/conflicting_file", "conflicting file\n");
nulltoken committed
130

131 132
	if (entries & write_ancestor)
		index_entry_init(index, 1, &ancestor);
nulltoken committed
133

134 135
	if (entries & write_ours)
		index_entry_init(index, 2, &ours);
nulltoken committed
136

137 138 139 140 141 142 143 144
	if (entries & write_theirs)
		index_entry_init(index, 3, &theirs);
}

void test_reset_hard__resetting_reverts_unmerged(void)
{
	git_index *index;
	int entries;
nulltoken committed
145

146 147 148 149
	/* Ensure every permutation of non-zero stage entries results in the
	 * path being cleaned up. */
	for (entries = 1; entries < 8; entries++) {
		cl_git_pass(git_repository_index(&index, repo));
nulltoken committed
150

151 152
		unmerged_index_init(index, entries);
		cl_git_pass(git_index_write(index));
nulltoken committed
153

154
		cl_git_pass(git_revparse_single(&target, repo, "26a125e"));
155
		cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL));
nulltoken committed
156

157
		cl_assert(git_path_exists("status/conflicting_file") == 0);
nulltoken committed
158

159 160
		git_object_free(target);
		target = NULL;
nulltoken committed
161 162

		git_index_free(index);
163 164 165
	}
}

Edward Thomson committed
166 167 168 169 170 171 172 173 174 175 176
void test_reset_hard__cleans_up_merge(void)
{
	git_buf merge_head_path = GIT_BUF_INIT,
		merge_msg_path = GIT_BUF_INIT,
		merge_mode_path = GIT_BUF_INIT,
		orig_head_path = GIT_BUF_INIT;

	cl_git_pass(git_buf_joinpath(&merge_head_path, git_repository_path(repo), "MERGE_HEAD"));
	cl_git_mkfile(git_buf_cstr(&merge_head_path), "beefbeefbeefbeefbeefbeefbeefbeefbeefbeef\n");

	cl_git_pass(git_buf_joinpath(&merge_msg_path, git_repository_path(repo), "MERGE_MSG"));
177
	cl_git_mkfile(git_buf_cstr(&merge_msg_path), "Merge commit 0017bd4ab1ec30440b17bae1680cff124ab5f1f6\n");
Edward Thomson committed
178

179 180
	cl_git_pass(git_buf_joinpath(&merge_mode_path, git_repository_path(repo), "MERGE_MODE"));
	cl_git_mkfile(git_buf_cstr(&merge_mode_path), "");
Edward Thomson committed
181 182 183 184

	cl_git_pass(git_buf_joinpath(&orig_head_path, git_repository_path(repo), "ORIG_HEAD"));
	cl_git_mkfile(git_buf_cstr(&orig_head_path), "0017bd4ab1ec30440b17bae1680cff124ab5f1f6");

185
	cl_git_pass(git_revparse_single(&target, repo, "0017bd4"));
186
	cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL));
Edward Thomson committed
187 188 189 190 191 192 193 194 195 196 197 198 199

	cl_assert(!git_path_exists(git_buf_cstr(&merge_head_path)));
	cl_assert(!git_path_exists(git_buf_cstr(&merge_msg_path)));
	cl_assert(!git_path_exists(git_buf_cstr(&merge_mode_path)));

	cl_assert(git_path_exists(git_buf_cstr(&orig_head_path)));
	cl_git_pass(p_unlink(git_buf_cstr(&orig_head_path)));

	git_buf_free(&merge_head_path);
	git_buf_free(&merge_msg_path);
	git_buf_free(&merge_mode_path);
	git_buf_free(&orig_head_path);
}
Ben Straub committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

void test_reset_hard__reflog_is_correct(void)
{
	const char *exp_msg = "commit: Add a file which name should appear before the "
		"\"subdir/\" folder while being dealt with by the treewalker";

	reflog_check(repo, "HEAD", 3, "emeric.fermas@gmail.com", exp_msg);
	reflog_check(repo, "refs/heads/master", 3, "emeric.fermas@gmail.com", exp_msg);

	/* Branch not moving, no reflog entry */
	cl_git_pass(git_revparse_single(&target, repo, "HEAD^{commit}"));
	cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL));
	reflog_check(repo, "HEAD", 3, "emeric.fermas@gmail.com", exp_msg);
	reflog_check(repo, "refs/heads/master", 3, "emeric.fermas@gmail.com", exp_msg);

215 216
	git_object_free(target);

Ben Straub committed
217
	/* Moved branch, expect default message */
218
	exp_msg = "reset: moving";
Ben Straub committed
219 220
	cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
	cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL));
221 222
	reflog_check(repo, "HEAD", 4, NULL, exp_msg);
	reflog_check(repo, "refs/heads/master", 4, NULL, exp_msg);
Ben Straub committed
223

224 225
	git_object_free(target);

Ben Straub committed
226
	/* Moved branch, expect custom message */
227
	exp_msg = "message1";
Ben Straub committed
228 229
	cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
	cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, "message1"));
230 231
	reflog_check(repo, "HEAD", 5, NULL, exp_msg);
	reflog_check(repo, "refs/heads/master", 5, NULL, exp_msg);
Ben Straub committed
232
}