1 #include "clar_libgit2.h"
2 #include "posix.h"
3 #include "reset_helpers.h"
4 #include "path.h"
5 #include "futils.h"
6 
7 static git_repository *repo;
8 static git_object *target;
9 
test_reset_hard__initialize(void)10 void test_reset_hard__initialize(void)
11 {
12 	repo = cl_git_sandbox_init("status");
13 	target = NULL;
14 }
15 
test_reset_hard__cleanup(void)16 void test_reset_hard__cleanup(void)
17 {
18 	if (target != NULL) {
19 		git_object_free(target);
20 		target = NULL;
21 	}
22 
23 	cl_git_sandbox_cleanup();
24 }
25 
strequal_ignore_eol(const char * exp,const char * str)26 static int strequal_ignore_eol(const char *exp, const char *str)
27 {
28 	while (*exp && *str) {
29 		if (*exp != *str) {
30 			while (*exp == '\r' || *exp == '\n') ++exp;
31 			while (*str == '\r' || *str == '\n') ++str;
32 			if (*exp != *str)
33 				return false;
34 		} else {
35 			exp++; str++;
36 		}
37 	}
38 	return (!*exp && !*str);
39 }
40 
test_reset_hard__resetting_reverts_modified_files(void)41 void test_reset_hard__resetting_reverts_modified_files(void)
42 {
43 	git_buf path = GIT_BUF_INIT, content = GIT_BUF_INIT;
44 	int i;
45 	static const char *files[4] = {
46 		"current_file",
47 		"modified_file",
48 		"staged_new_file",
49 		"staged_changes_modified_file" };
50 	static const char *before[4] = {
51 		"current_file\n",
52 		"modified_file\nmodified_file\n",
53 		"staged_new_file\n",
54 		"staged_changes_modified_file\nstaged_changes_modified_file\nstaged_changes_modified_file\n"
55 	};
56 	static const char *after[4] = {
57 		"current_file\n",
58 		"modified_file\n",
59 		NULL,
60 		"staged_changes_modified_file\n"
61 	};
62 	const char *wd = git_repository_workdir(repo);
63 
64 	cl_assert(wd);
65 
66 	for (i = 0; i < 4; ++i) {
67 		cl_git_pass(git_buf_joinpath(&path, wd, files[i]));
68 		cl_git_pass(git_futils_readbuffer(&content, path.ptr));
69 		cl_assert_equal_s(before[i], content.ptr);
70 	}
71 
72 	cl_git_pass(git_revparse_single(&target, repo, "26a125e"));
73 
74 	cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
75 
76 	for (i = 0; i < 4; ++i) {
77 		cl_git_pass(git_buf_joinpath(&path, wd, files[i]));
78 		if (after[i]) {
79 			cl_git_pass(git_futils_readbuffer(&content, path.ptr));
80 			cl_assert(strequal_ignore_eol(after[i], content.ptr));
81 		} else {
82 			cl_assert(!git_path_exists(path.ptr));
83 		}
84 	}
85 
86 	git_buf_dispose(&content);
87 	git_buf_dispose(&path);
88 }
89 
test_reset_hard__cannot_reset_in_a_bare_repository(void)90 void test_reset_hard__cannot_reset_in_a_bare_repository(void)
91 {
92 	git_repository *bare;
93 
94 	cl_git_pass(git_repository_open(&bare, cl_fixture("testrepo.git")));
95 	cl_assert(git_repository_is_bare(bare) == true);
96 
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));
100 
101 	git_repository_free(bare);
102 }
103 
index_entry_init(git_index * index,int side,git_oid * oid)104 static void index_entry_init(git_index *index, int side, git_oid *oid)
105 {
106 	git_index_entry entry;
107 
108 	memset(&entry, 0x0, sizeof(git_index_entry));
109 
110 	entry.path = "conflicting_file";
111 	GIT_INDEX_ENTRY_STAGE_SET(&entry, side);
112 	entry.mode = 0100644;
113 	git_oid_cpy(&entry.id, oid);
114 
115 	cl_git_pass(git_index_add(index, &entry));
116 }
117 
unmerged_index_init(git_index * index,int entries)118 static void unmerged_index_init(git_index *index, int entries)
119 {
120 	int write_ancestor = 1;
121 	int write_ours = 2;
122 	int write_theirs = 4;
123 	git_oid ancestor, ours, theirs;
124 
125 	git_oid_fromstr(&ancestor, "452e4244b5d083ddf0460acf1ecc74db9dcfa11a");
126 	git_oid_fromstr(&ours, "32504b727382542f9f089e24fddac5e78533e96c");
127 	git_oid_fromstr(&theirs, "061d42a44cacde5726057b67558821d95db96f19");
128 
129 	cl_git_rewritefile("status/conflicting_file", "conflicting file\n");
130 
131 	if (entries & write_ancestor)
132 		index_entry_init(index, 1, &ancestor);
133 
134 	if (entries & write_ours)
135 		index_entry_init(index, 2, &ours);
136 
137 	if (entries & write_theirs)
138 		index_entry_init(index, 3, &theirs);
139 }
140 
test_reset_hard__resetting_reverts_unmerged(void)141 void test_reset_hard__resetting_reverts_unmerged(void)
142 {
143 	git_index *index;
144 	int entries;
145 
146 	/* Ensure every permutation of non-zero stage entries results in the
147 	 * path being cleaned up. */
148 	for (entries = 1; entries < 8; entries++) {
149 		cl_git_pass(git_repository_index(&index, repo));
150 
151 		unmerged_index_init(index, entries);
152 		cl_git_pass(git_index_write(index));
153 
154 		cl_git_pass(git_revparse_single(&target, repo, "26a125e"));
155 		cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
156 
157 		cl_assert(git_path_exists("status/conflicting_file") == 0);
158 
159 		git_object_free(target);
160 		target = NULL;
161 
162 		git_index_free(index);
163 	}
164 }
165 
test_reset_hard__cleans_up_merge(void)166 void test_reset_hard__cleans_up_merge(void)
167 {
168 	git_buf merge_head_path = GIT_BUF_INIT,
169 		merge_msg_path = GIT_BUF_INIT,
170 		merge_mode_path = GIT_BUF_INIT,
171 		orig_head_path = GIT_BUF_INIT;
172 
173 	cl_git_pass(git_buf_joinpath(&merge_head_path, git_repository_path(repo), "MERGE_HEAD"));
174 	cl_git_mkfile(git_buf_cstr(&merge_head_path), "beefbeefbeefbeefbeefbeefbeefbeefbeefbeef\n");
175 
176 	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");
178 
179 	cl_git_pass(git_buf_joinpath(&merge_mode_path, git_repository_path(repo), "MERGE_MODE"));
180 	cl_git_mkfile(git_buf_cstr(&merge_mode_path), "");
181 
182 	cl_git_pass(git_buf_joinpath(&orig_head_path, git_repository_path(repo), "ORIG_HEAD"));
183 	cl_git_mkfile(git_buf_cstr(&orig_head_path), "0017bd4ab1ec30440b17bae1680cff124ab5f1f6");
184 
185 	cl_git_pass(git_revparse_single(&target, repo, "0017bd4"));
186 	cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
187 
188 	cl_assert(!git_path_exists(git_buf_cstr(&merge_head_path)));
189 	cl_assert(!git_path_exists(git_buf_cstr(&merge_msg_path)));
190 	cl_assert(!git_path_exists(git_buf_cstr(&merge_mode_path)));
191 
192 	cl_assert(git_path_exists(git_buf_cstr(&orig_head_path)));
193 	cl_git_pass(p_unlink(git_buf_cstr(&orig_head_path)));
194 
195 	git_buf_dispose(&merge_head_path);
196 	git_buf_dispose(&merge_msg_path);
197 	git_buf_dispose(&merge_mode_path);
198 	git_buf_dispose(&orig_head_path);
199 }
200 
test_reset_hard__reflog_is_correct(void)201 void test_reset_hard__reflog_is_correct(void)
202 {
203 	git_buf buf = GIT_BUF_INIT;
204 	git_annotated_commit *annotated;
205 	const char *exp_msg = "commit: Add a file which name should appear before the "
206 		"\"subdir/\" folder while being dealt with by the treewalker";
207 
208 	reflog_check(repo, "HEAD", 3, "emeric.fermas@gmail.com", exp_msg);
209 	reflog_check(repo, "refs/heads/master", 3, "emeric.fermas@gmail.com", exp_msg);
210 
211 	/* Branch not moving, no reflog entry */
212 	cl_git_pass(git_revparse_single(&target, repo, "HEAD^{commit}"));
213 	cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
214 	reflog_check(repo, "HEAD", 3, "emeric.fermas@gmail.com", exp_msg);
215 	reflog_check(repo, "refs/heads/master", 3, "emeric.fermas@gmail.com", exp_msg);
216 
217 	git_object_free(target);
218 
219 	/* Moved branch, expect id in message */
220 	cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
221 	cl_git_pass(git_buf_printf(&buf, "reset: moving to %s", git_oid_tostr_s(git_object_id(target))));
222 	cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
223 	reflog_check(repo, "HEAD", 4, NULL, git_buf_cstr(&buf));
224 	reflog_check(repo, "refs/heads/master", 4, NULL, git_buf_cstr(&buf));
225 
226 	git_buf_dispose(&buf);
227 
228 	/* Moved branch, expect revspec in message */
229 	exp_msg = "reset: moving to HEAD~^{commit}";
230 	cl_git_pass(git_annotated_commit_from_revspec(&annotated, repo, "HEAD~^{commit}"));
231 	cl_git_pass(git_reset_from_annotated(repo, annotated, GIT_RESET_HARD, NULL));
232 	reflog_check(repo, "HEAD", 5, NULL, exp_msg);
233 	reflog_check(repo, "refs/heads/master", 5, NULL, exp_msg);
234 
235 	git_annotated_commit_free(annotated);
236 
237 }
238 
test_reset_hard__switch_file_to_dir(void)239 void test_reset_hard__switch_file_to_dir(void)
240 {
241 	git_index_entry entry = {{ 0 }};
242 	git_index *idx;
243 	git_odb *odb;
244 	git_object *commit;
245 	git_tree *tree;
246 	git_signature *sig;
247 	git_oid src_tree_id, tgt_tree_id;
248 	git_oid src_id, tgt_id;
249 
250 	cl_git_pass(git_repository_odb(&odb, repo));
251 	cl_git_pass(git_odb_write(&entry.id, odb, "", 0, GIT_OBJECT_BLOB));
252 	git_odb_free(odb);
253 
254 	entry.mode = GIT_FILEMODE_BLOB;
255 	cl_git_pass(git_index_new(&idx));
256 	cl_git_pass(git_signature_now(&sig, "foo", "bar"));
257 
258 	/* Create the old tree */
259 	entry.path = "README";
260 	cl_git_pass(git_index_add(idx, &entry));
261 	entry.path = "dir";
262 	cl_git_pass(git_index_add(idx, &entry));
263 
264 	cl_git_pass(git_index_write_tree_to(&src_tree_id, idx, repo));
265 	cl_git_pass(git_index_clear(idx));
266 
267 	cl_git_pass(git_tree_lookup(&tree, repo, &src_tree_id));
268 	cl_git_pass(git_commit_create(&src_id, repo, NULL, sig, sig, NULL, "foo", tree, 0, NULL));
269 	git_tree_free(tree);
270 
271 	/* Create the new tree */
272 	entry.path = "README";
273 	cl_git_pass(git_index_add(idx, &entry));
274 	entry.path = "dir/FILE";
275 	cl_git_pass(git_index_add(idx, &entry));
276 
277 	cl_git_pass(git_index_write_tree_to(&tgt_tree_id, idx, repo));
278 	cl_git_pass(git_tree_lookup(&tree, repo, &tgt_tree_id));
279 	cl_git_pass(git_commit_create(&tgt_id, repo, NULL, sig, sig, NULL, "foo", tree, 0, NULL));
280 	git_tree_free(tree);
281 	git_index_free(idx);
282 	git_signature_free(sig);
283 
284 	/* Let's go to a known state of the src commit with the file named 'dir' */
285 	cl_git_pass(git_object_lookup(&commit, repo, &src_id, GIT_OBJECT_COMMIT));
286 	cl_git_pass(git_reset(repo, commit, GIT_RESET_HARD, NULL));
287 	git_object_free(commit);
288 
289 	/* And now we move over to the commit with the directory named 'dir' */
290 	cl_git_pass(git_object_lookup(&commit, repo, &tgt_id, GIT_OBJECT_COMMIT));
291 	cl_git_pass(git_reset(repo, commit, GIT_RESET_HARD, NULL));
292 	git_object_free(commit);
293 }
294