]>
Commit | Line | Data |
---|---|---|
3aa443a9 | 1 | #include "clar_libgit2.h" |
bebdbcd4 | 2 | #include "checkout_helpers.h" |
3aa443a9 | 3 | |
4 | #include "git2/checkout.h" | |
5 | #include "repository.h" | |
0d70f650 RB |
6 | #include "buffer.h" |
7 | #include "fileops.h" | |
3aa443a9 | 8 | |
3aa443a9 | 9 | static git_repository *g_repo; |
6affd71f | 10 | static git_checkout_options g_opts; |
10df95c3 | 11 | static git_object *g_object; |
3aa443a9 | 12 | |
eae0bfdc PP |
13 | static void assert_status_entrycount(git_repository *repo, size_t count) |
14 | { | |
15 | git_status_list *status; | |
16 | ||
17 | cl_git_pass(git_status_list_new(&status, repo, NULL)); | |
18 | cl_assert_equal_i(count, git_status_list_entrycount(status)); | |
19 | ||
20 | git_status_list_free(status); | |
21 | } | |
22 | ||
3aa443a9 | 23 | void test_checkout_tree__initialize(void) |
24 | { | |
3aa443a9 | 25 | g_repo = cl_git_sandbox_init("testrepo"); |
10df95c3 | 26 | |
6affd71f | 27 | GIT_INIT_STRUCTURE(&g_opts, GIT_CHECKOUT_OPTIONS_VERSION); |
496b76d4 | 28 | g_opts.checkout_strategy = GIT_CHECKOUT_FORCE; |
3aa443a9 | 29 | } |
30 | ||
31 | void test_checkout_tree__cleanup(void) | |
32 | { | |
10df95c3 | 33 | git_object_free(g_object); |
9094d30b | 34 | g_object = NULL; |
10df95c3 | 35 | |
3aa443a9 | 36 | cl_git_sandbox_cleanup(); |
f3f4c6b5 RB |
37 | |
38 | if (git_path_isdir("alternative")) | |
39 | git_futils_rmdir_r("alternative", NULL, GIT_RMDIR_REMOVE_FILES); | |
3aa443a9 | 40 | } |
41 | ||
3aa443a9 | 42 | void test_checkout_tree__cannot_checkout_a_non_treeish(void) |
43 | { | |
10df95c3 | 44 | /* blob */ |
2ebc3c66 | 45 | cl_git_pass(git_revparse_single(&g_object, g_repo, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd")); |
80642656 | 46 | cl_git_fail(git_checkout_tree(g_repo, g_object, NULL)); |
10df95c3 | 47 | } |
48 | ||
49 | void test_checkout_tree__can_checkout_a_subdirectory_from_a_commit(void) | |
50 | { | |
10df95c3 | 51 | char *entries[] = { "ab/de/" }; |
52 | ||
39783719 | 53 | g_opts.paths.strings = entries; |
54 | g_opts.paths.count = 1; | |
10df95c3 | 55 | |
2ebc3c66 | 56 | cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees")); |
10df95c3 | 57 | |
58 | cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/")); | |
59 | ||
80642656 | 60 | cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); |
10df95c3 | 61 | |
62 | cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt")); | |
63 | cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/fgh/1.txt")); | |
64 | } | |
65 | ||
c5df10f4 JM |
66 | void test_checkout_tree__can_checkout_and_remove_directory(void) |
67 | { | |
c5df10f4 JM |
68 | cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/")); |
69 | ||
5cf9875a RB |
70 | /* Checkout brach "subtrees" and update HEAD, so that HEAD matches the |
71 | * current working tree | |
72 | */ | |
2ebc3c66 | 73 | cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees")); |
c5df10f4 | 74 | cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); |
5cf9875a | 75 | |
4e498646 | 76 | cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees")); |
5cf9875a | 77 | |
c5df10f4 JM |
78 | cl_assert_equal_i(true, git_path_isdir("./testrepo/ab/")); |
79 | cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt")); | |
80 | cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/fgh/1.txt")); | |
81 | ||
b97fabfa | 82 | git_object_free(g_object); |
83 | g_object = NULL; | |
84 | ||
5cf9875a RB |
85 | /* Checkout brach "master" and update HEAD, so that HEAD matches the |
86 | * current working tree | |
87 | */ | |
2ebc3c66 | 88 | cl_git_pass(git_revparse_single(&g_object, g_repo, "master")); |
c5df10f4 | 89 | cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); |
c5df10f4 | 90 | |
4e498646 | 91 | cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master")); |
5cf9875a RB |
92 | |
93 | /* This directory should no longer exist */ | |
c5df10f4 JM |
94 | cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/")); |
95 | } | |
96 | ||
10df95c3 | 97 | void test_checkout_tree__can_checkout_a_subdirectory_from_a_subtree(void) |
98 | { | |
10df95c3 | 99 | char *entries[] = { "de/" }; |
100 | ||
39783719 | 101 | g_opts.paths.strings = entries; |
102 | g_opts.paths.count = 1; | |
10df95c3 | 103 | |
2ebc3c66 | 104 | cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees:ab")); |
3aa443a9 | 105 | |
10df95c3 | 106 | cl_assert_equal_i(false, git_path_isdir("./testrepo/de/")); |
3aa443a9 | 107 | |
80642656 | 108 | cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); |
3aa443a9 | 109 | |
10df95c3 | 110 | cl_assert_equal_i(true, git_path_isfile("./testrepo/de/2.txt")); |
111 | cl_assert_equal_i(true, git_path_isfile("./testrepo/de/fgh/1.txt")); | |
3aa443a9 | 112 | } |
80642656 | 113 | |
9c05c17b | 114 | static void progress(const char *path, size_t cur, size_t tot, void *payload) |
80642656 | 115 | { |
25e8b201 | 116 | bool *was_called = (bool*)payload; |
1fc375e6 | 117 | GIT_UNUSED(path); GIT_UNUSED(cur); GIT_UNUSED(tot); |
25e8b201 | 118 | *was_called = true; |
80642656 BS |
119 | } |
120 | ||
121 | void test_checkout_tree__calls_progress_callback(void) | |
122 | { | |
25e8b201 | 123 | bool was_called = 0; |
ad9a921b | 124 | |
80642656 | 125 | g_opts.progress_cb = progress; |
25e8b201 | 126 | g_opts.progress_payload = &was_called; |
80642656 | 127 | |
2ebc3c66 | 128 | cl_git_pass(git_revparse_single(&g_object, g_repo, "master")); |
ad9a921b | 129 | |
80642656 | 130 | cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); |
ad9a921b | 131 | |
25e8b201 | 132 | cl_assert_equal_i(was_called, true); |
80642656 | 133 | } |
d0951175 MG |
134 | |
135 | void test_checkout_tree__doesnt_write_unrequested_files_to_worktree(void) | |
136 | { | |
cce2f16b | 137 | git_oid master_oid; |
138 | git_oid chomped_oid; | |
139 | git_commit* p_master_commit; | |
140 | git_commit* p_chomped_commit; | |
6affd71f | 141 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; |
cce2f16b | 142 | |
143 | git_oid_fromstr(&master_oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); | |
144 | git_oid_fromstr(&chomped_oid, "e90810b8df3e80c413d903f631643c716887138d"); | |
145 | cl_git_pass(git_commit_lookup(&p_master_commit, g_repo, &master_oid)); | |
146 | cl_git_pass(git_commit_lookup(&p_chomped_commit, g_repo, &chomped_oid)); | |
147 | ||
148 | /* GIT_CHECKOUT_NONE should not add any file to the working tree from the | |
149 | * index as it is supposed to be a dry run. | |
150 | */ | |
151 | opts.checkout_strategy = GIT_CHECKOUT_NONE; | |
152 | git_checkout_tree(g_repo, (git_object*)p_chomped_commit, &opts); | |
153 | cl_assert_equal_i(false, git_path_isfile("testrepo/readme.txt")); | |
154 | ||
155 | git_commit_free(p_master_commit); | |
156 | git_commit_free(p_chomped_commit); | |
d0951175 | 157 | } |
0d70f650 | 158 | |
0d70f650 RB |
159 | void test_checkout_tree__can_switch_branches(void) |
160 | { | |
6affd71f | 161 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; |
0d70f650 RB |
162 | git_oid oid; |
163 | git_object *obj = NULL; | |
164 | ||
165 | assert_on_branch(g_repo, "master"); | |
166 | ||
167 | /* do first checkout with FORCE because we don't know if testrepo | |
168 | * base data is clean for a checkout or not | |
169 | */ | |
170 | opts.checkout_strategy = GIT_CHECKOUT_FORCE; | |
171 | ||
172 | cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir")); | |
ac3d33df | 173 | cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY)); |
0d70f650 RB |
174 | |
175 | cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); | |
4e498646 | 176 | cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir")); |
0d70f650 RB |
177 | |
178 | cl_assert(git_path_isfile("testrepo/README")); | |
179 | cl_assert(git_path_isfile("testrepo/branch_file.txt")); | |
180 | cl_assert(git_path_isfile("testrepo/new.txt")); | |
181 | cl_assert(git_path_isfile("testrepo/a/b.txt")); | |
182 | ||
183 | cl_assert(!git_path_isdir("testrepo/ab")); | |
184 | ||
185 | assert_on_branch(g_repo, "dir"); | |
186 | ||
187 | git_object_free(obj); | |
188 | ||
189 | /* do second checkout safe because we should be clean after first */ | |
190 | opts.checkout_strategy = GIT_CHECKOUT_SAFE; | |
191 | ||
192 | cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/subtrees")); | |
ac3d33df | 193 | cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY)); |
0d70f650 RB |
194 | |
195 | cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); | |
4e498646 | 196 | cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees")); |
0d70f650 RB |
197 | |
198 | cl_assert(git_path_isfile("testrepo/README")); | |
199 | cl_assert(git_path_isfile("testrepo/branch_file.txt")); | |
200 | cl_assert(git_path_isfile("testrepo/new.txt")); | |
201 | cl_assert(git_path_isfile("testrepo/ab/4.txt")); | |
202 | cl_assert(git_path_isfile("testrepo/ab/c/3.txt")); | |
203 | cl_assert(git_path_isfile("testrepo/ab/de/2.txt")); | |
204 | cl_assert(git_path_isfile("testrepo/ab/de/fgh/1.txt")); | |
205 | ||
206 | cl_assert(!git_path_isdir("testrepo/a")); | |
207 | ||
208 | assert_on_branch(g_repo, "subtrees"); | |
209 | ||
210 | git_object_free(obj); | |
211 | } | |
212 | ||
213 | void test_checkout_tree__can_remove_untracked(void) | |
214 | { | |
6affd71f | 215 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; |
0d70f650 RB |
216 | |
217 | opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_REMOVE_UNTRACKED; | |
218 | ||
219 | cl_git_mkfile("testrepo/untracked_file", "as you wish"); | |
220 | cl_assert(git_path_isfile("testrepo/untracked_file")); | |
221 | ||
222 | cl_git_pass(git_checkout_head(g_repo, &opts)); | |
223 | ||
224 | cl_assert(!git_path_isfile("testrepo/untracked_file")); | |
225 | } | |
226 | ||
227 | void test_checkout_tree__can_remove_ignored(void) | |
228 | { | |
6affd71f | 229 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; |
0d70f650 RB |
230 | int ignored = 0; |
231 | ||
232 | opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_REMOVE_IGNORED; | |
233 | ||
234 | cl_git_mkfile("testrepo/ignored_file", "as you wish"); | |
235 | ||
236 | cl_git_pass(git_ignore_add_rule(g_repo, "ignored_file\n")); | |
237 | ||
238 | cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "ignored_file")); | |
239 | cl_assert_equal_i(1, ignored); | |
240 | ||
241 | cl_assert(git_path_isfile("testrepo/ignored_file")); | |
242 | ||
243 | cl_git_pass(git_checkout_head(g_repo, &opts)); | |
244 | ||
245 | cl_assert(!git_path_isfile("testrepo/ignored_file")); | |
246 | } | |
247 | ||
bf4a577c | 248 | static int checkout_tree_with_blob_ignored_in_workdir(int strategy, bool isdir) |
81a2012d ET |
249 | { |
250 | git_oid oid; | |
251 | git_object *obj = NULL; | |
6affd71f | 252 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; |
81a2012d ET |
253 | int ignored = 0, error; |
254 | ||
255 | assert_on_branch(g_repo, "master"); | |
256 | ||
257 | /* do first checkout with FORCE because we don't know if testrepo | |
258 | * base data is clean for a checkout or not | |
259 | */ | |
260 | opts.checkout_strategy = GIT_CHECKOUT_FORCE; | |
261 | ||
262 | cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir")); | |
ac3d33df | 263 | cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY)); |
81a2012d ET |
264 | |
265 | cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); | |
4e498646 | 266 | cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir")); |
81a2012d ET |
267 | |
268 | cl_assert(git_path_isfile("testrepo/README")); | |
269 | cl_assert(git_path_isfile("testrepo/branch_file.txt")); | |
270 | cl_assert(git_path_isfile("testrepo/new.txt")); | |
271 | cl_assert(git_path_isfile("testrepo/a/b.txt")); | |
272 | ||
273 | cl_assert(!git_path_isdir("testrepo/ab")); | |
274 | ||
275 | assert_on_branch(g_repo, "dir"); | |
276 | ||
277 | git_object_free(obj); | |
278 | ||
279 | opts.checkout_strategy = strategy; | |
280 | ||
bf4a577c ET |
281 | if (isdir) { |
282 | cl_must_pass(p_mkdir("testrepo/ab", 0777)); | |
283 | cl_must_pass(p_mkdir("testrepo/ab/4.txt", 0777)); | |
284 | ||
285 | cl_git_mkfile("testrepo/ab/4.txt/file1.txt", "as you wish"); | |
286 | cl_git_mkfile("testrepo/ab/4.txt/file2.txt", "foo bar foo"); | |
287 | cl_git_mkfile("testrepo/ab/4.txt/file3.txt", "inky blinky pinky clyde"); | |
288 | ||
289 | cl_assert(git_path_isdir("testrepo/ab/4.txt")); | |
290 | } else { | |
291 | cl_must_pass(p_mkdir("testrepo/ab", 0777)); | |
292 | cl_git_mkfile("testrepo/ab/4.txt", "as you wish"); | |
293 | ||
294 | cl_assert(git_path_isfile("testrepo/ab/4.txt")); | |
295 | } | |
81a2012d ET |
296 | |
297 | cl_git_pass(git_ignore_add_rule(g_repo, "ab/4.txt\n")); | |
298 | ||
299 | cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "ab/4.txt")); | |
300 | cl_assert_equal_i(1, ignored); | |
301 | ||
81a2012d | 302 | cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/subtrees")); |
ac3d33df | 303 | cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY)); |
81a2012d ET |
304 | |
305 | error = git_checkout_tree(g_repo, obj, &opts); | |
306 | ||
307 | git_object_free(obj); | |
308 | ||
309 | return error; | |
310 | } | |
311 | ||
312 | void test_checkout_tree__conflict_on_ignored_when_not_overwriting(void) | |
313 | { | |
314 | int error; | |
315 | ||
316 | cl_git_fail(error = checkout_tree_with_blob_ignored_in_workdir( | |
bf4a577c | 317 | GIT_CHECKOUT_SAFE | GIT_CHECKOUT_DONT_OVERWRITE_IGNORED, false)); |
81a2012d | 318 | |
885b94aa | 319 | cl_assert_equal_i(GIT_ECONFLICT, error); |
81a2012d ET |
320 | } |
321 | ||
322 | void test_checkout_tree__can_overwrite_ignored_by_default(void) | |
323 | { | |
bf4a577c | 324 | cl_git_pass(checkout_tree_with_blob_ignored_in_workdir(GIT_CHECKOUT_SAFE, false)); |
81a2012d | 325 | |
4e498646 | 326 | cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees")); |
81a2012d ET |
327 | |
328 | cl_assert(git_path_isfile("testrepo/ab/4.txt")); | |
329 | ||
330 | assert_on_branch(g_repo, "subtrees"); | |
331 | } | |
332 | ||
bf4a577c ET |
333 | void test_checkout_tree__conflict_on_ignored_folder_when_not_overwriting(void) |
334 | { | |
335 | int error; | |
336 | ||
337 | cl_git_fail(error = checkout_tree_with_blob_ignored_in_workdir( | |
338 | GIT_CHECKOUT_SAFE | GIT_CHECKOUT_DONT_OVERWRITE_IGNORED, true)); | |
339 | ||
885b94aa | 340 | cl_assert_equal_i(GIT_ECONFLICT, error); |
bf4a577c ET |
341 | } |
342 | ||
343 | void test_checkout_tree__can_overwrite_ignored_folder_by_default(void) | |
344 | { | |
345 | cl_git_pass(checkout_tree_with_blob_ignored_in_workdir(GIT_CHECKOUT_SAFE, true)); | |
346 | ||
4e498646 | 347 | cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees")); |
bf4a577c ET |
348 | |
349 | cl_assert(git_path_isfile("testrepo/ab/4.txt")); | |
350 | ||
351 | assert_on_branch(g_repo, "subtrees"); | |
352 | ||
353 | } | |
354 | ||
0d70f650 RB |
355 | void test_checkout_tree__can_update_only(void) |
356 | { | |
6affd71f | 357 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; |
0d70f650 RB |
358 | git_oid oid; |
359 | git_object *obj = NULL; | |
0d70f650 RB |
360 | |
361 | /* first let's get things into a known state - by checkout out the HEAD */ | |
362 | ||
363 | assert_on_branch(g_repo, "master"); | |
364 | ||
365 | opts.checkout_strategy = GIT_CHECKOUT_FORCE; | |
366 | cl_git_pass(git_checkout_head(g_repo, &opts)); | |
367 | ||
368 | cl_assert(!git_path_isdir("testrepo/a")); | |
369 | ||
050ab995 | 370 | check_file_contents_nocr("testrepo/branch_file.txt", "hi\nbye!\n"); |
0d70f650 RB |
371 | |
372 | /* now checkout branch but with update only */ | |
373 | ||
374 | opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_UPDATE_ONLY; | |
375 | ||
376 | cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir")); | |
ac3d33df | 377 | cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY)); |
0d70f650 RB |
378 | |
379 | cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); | |
4e498646 | 380 | cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir")); |
0d70f650 RB |
381 | |
382 | assert_on_branch(g_repo, "dir"); | |
383 | ||
384 | /* this normally would have been created (which was tested separately in | |
385 | * the test_checkout_tree__can_switch_branches test), but with | |
386 | * UPDATE_ONLY it will not have been created. | |
387 | */ | |
388 | cl_assert(!git_path_isdir("testrepo/a")); | |
389 | ||
390 | /* but this file still should have been updated */ | |
050ab995 | 391 | check_file_contents_nocr("testrepo/branch_file.txt", "hi\n"); |
0d70f650 RB |
392 | |
393 | git_object_free(obj); | |
394 | } | |
40342bd2 RB |
395 | |
396 | void test_checkout_tree__can_checkout_with_pattern(void) | |
397 | { | |
398 | char *entries[] = { "[l-z]*.txt" }; | |
399 | ||
400 | /* reset to beginning of history (i.e. just a README file) */ | |
401 | ||
402 | g_opts.checkout_strategy = | |
403 | GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED; | |
404 | ||
2ebc3c66 | 405 | cl_git_pass(git_revparse_single(&g_object, g_repo, "8496071c1b46c854b31185ea97743be6a8774479")); |
40342bd2 RB |
406 | |
407 | cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); | |
408 | cl_git_pass( | |
4e498646 | 409 | git_repository_set_head_detached(g_repo, git_object_id(g_object))); |
40342bd2 RB |
410 | |
411 | git_object_free(g_object); | |
412 | g_object = NULL; | |
413 | ||
414 | cl_assert(git_path_exists("testrepo/README")); | |
415 | cl_assert(!git_path_exists("testrepo/branch_file.txt")); | |
416 | cl_assert(!git_path_exists("testrepo/link_to_new.txt")); | |
417 | cl_assert(!git_path_exists("testrepo/new.txt")); | |
418 | ||
419 | /* now to a narrow patterned checkout */ | |
420 | ||
496b76d4 | 421 | g_opts.checkout_strategy = GIT_CHECKOUT_SAFE; |
40342bd2 RB |
422 | g_opts.paths.strings = entries; |
423 | g_opts.paths.count = 1; | |
424 | ||
2ebc3c66 | 425 | cl_git_pass(git_revparse_single(&g_object, g_repo, "refs/heads/master")); |
40342bd2 RB |
426 | |
427 | cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); | |
428 | ||
429 | cl_assert(git_path_exists("testrepo/README")); | |
430 | cl_assert(!git_path_exists("testrepo/branch_file.txt")); | |
431 | cl_assert(git_path_exists("testrepo/link_to_new.txt")); | |
432 | cl_assert(git_path_exists("testrepo/new.txt")); | |
433 | } | |
434 | ||
5f959dca JF |
435 | void test_checkout_tree__pathlist_checkout_ignores_non_matches(void) |
436 | { | |
437 | char *entries[] = { "branch_file.txt", "link_to_new.txt" }; | |
438 | ||
439 | /* reset to beginning of history (i.e. just a README file) */ | |
440 | ||
441 | g_opts.checkout_strategy = | |
442 | GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED; | |
443 | ||
444 | cl_git_pass(git_revparse_single(&g_object, g_repo, "refs/heads/master")); | |
445 | ||
446 | cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); | |
447 | cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master")); | |
448 | ||
449 | cl_assert(git_path_exists("testrepo/README")); | |
450 | cl_assert(git_path_exists("testrepo/branch_file.txt")); | |
451 | cl_assert(git_path_exists("testrepo/link_to_new.txt")); | |
452 | cl_assert(git_path_exists("testrepo/new.txt")); | |
453 | ||
1f813cf2 | 454 | git_object_free(g_object); |
5f959dca JF |
455 | cl_git_pass(git_revparse_single(&g_object, g_repo, "8496071c1b46c854b31185ea97743be6a8774479")); |
456 | ||
457 | g_opts.checkout_strategy = | |
458 | GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH; | |
459 | g_opts.paths.strings = entries; | |
460 | g_opts.paths.count = 2; | |
461 | ||
462 | cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); | |
463 | ||
464 | cl_assert(git_path_exists("testrepo/README")); | |
465 | cl_assert(!git_path_exists("testrepo/branch_file.txt")); | |
466 | cl_assert(!git_path_exists("testrepo/link_to_new.txt")); | |
467 | cl_assert(git_path_exists("testrepo/new.txt")); | |
5f959dca JF |
468 | } |
469 | ||
40342bd2 RB |
470 | void test_checkout_tree__can_disable_pattern_match(void) |
471 | { | |
472 | char *entries[] = { "b*.txt" }; | |
473 | ||
474 | /* reset to beginning of history (i.e. just a README file) */ | |
475 | ||
476 | g_opts.checkout_strategy = | |
477 | GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED; | |
478 | ||
2ebc3c66 | 479 | cl_git_pass(git_revparse_single(&g_object, g_repo, "8496071c1b46c854b31185ea97743be6a8774479")); |
40342bd2 RB |
480 | |
481 | cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); | |
482 | cl_git_pass( | |
4e498646 | 483 | git_repository_set_head_detached(g_repo, git_object_id(g_object))); |
40342bd2 RB |
484 | |
485 | git_object_free(g_object); | |
486 | g_object = NULL; | |
487 | ||
488 | cl_assert(!git_path_isfile("testrepo/branch_file.txt")); | |
489 | ||
490 | /* now to a narrow patterned checkout, but disable pattern */ | |
491 | ||
492 | g_opts.checkout_strategy = | |
96b82b11 | 493 | GIT_CHECKOUT_SAFE | |
96b82b11 | 494 | GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH; |
40342bd2 RB |
495 | g_opts.paths.strings = entries; |
496 | g_opts.paths.count = 1; | |
497 | ||
2ebc3c66 | 498 | cl_git_pass(git_revparse_single(&g_object, g_repo, "refs/heads/master")); |
40342bd2 RB |
499 | |
500 | cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); | |
501 | ||
502 | cl_assert(!git_path_isfile("testrepo/branch_file.txt")); | |
503 | ||
504 | /* let's try that again, but allow the pattern match */ | |
505 | ||
496b76d4 | 506 | g_opts.checkout_strategy = GIT_CHECKOUT_SAFE; |
40342bd2 RB |
507 | |
508 | cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); | |
509 | ||
510 | cl_assert(git_path_isfile("testrepo/branch_file.txt")); | |
511 | } | |
4a0ac175 | 512 | |
513 | void assert_conflict( | |
514 | const char *entry_path, | |
515 | const char *new_content, | |
516 | const char *parent_sha, | |
517 | const char *commit_sha) | |
518 | { | |
519 | git_index *index; | |
520 | git_object *hack_tree; | |
521 | git_reference *branch, *head; | |
1fed6b07 | 522 | git_buf file_path = GIT_BUF_INIT; |
4a0ac175 | 523 | |
524 | cl_git_pass(git_repository_index(&index, g_repo)); | |
525 | ||
526 | /* Create a branch pointing at the parent */ | |
2ebc3c66 | 527 | cl_git_pass(git_revparse_single(&g_object, g_repo, parent_sha)); |
4a0ac175 | 528 | cl_git_pass(git_branch_create(&branch, g_repo, |
6bfb990d | 529 | "potential_conflict", (git_commit *)g_object, 0)); |
4a0ac175 | 530 | |
531 | /* Make HEAD point to this branch */ | |
532 | cl_git_pass(git_reference_symbolic_create( | |
659cf202 | 533 | &head, g_repo, "HEAD", git_reference_name(branch), 1, NULL)); |
0b3aa7be CMN |
534 | git_reference_free(head); |
535 | git_reference_free(branch); | |
4a0ac175 | 536 | |
537 | /* Checkout the parent */ | |
5b524d69 | 538 | g_opts.checkout_strategy = GIT_CHECKOUT_FORCE; |
4a0ac175 | 539 | cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); |
540 | ||
541 | /* Hack-ishy workaound to ensure *all* the index entries | |
542 | * match the content of the tree | |
543 | */ | |
ac3d33df | 544 | cl_git_pass(git_object_peel(&hack_tree, g_object, GIT_OBJECT_TREE)); |
4a0ac175 | 545 | cl_git_pass(git_index_read_tree(index, (git_tree *)hack_tree)); |
ac3d33df | 546 | cl_git_pass(git_index_write(index)); |
4a0ac175 | 547 | git_object_free(hack_tree); |
548 | git_object_free(g_object); | |
549 | g_object = NULL; | |
550 | ||
551 | /* Create a conflicting file */ | |
552 | cl_git_pass(git_buf_joinpath(&file_path, "./testrepo", entry_path)); | |
553 | cl_git_mkfile(git_buf_cstr(&file_path), new_content); | |
ac3d33df | 554 | git_buf_dispose(&file_path); |
4a0ac175 | 555 | |
556 | /* Trying to checkout the original commit */ | |
2ebc3c66 | 557 | cl_git_pass(git_revparse_single(&g_object, g_repo, commit_sha)); |
4a0ac175 | 558 | |
5b524d69 | 559 | g_opts.checkout_strategy = GIT_CHECKOUT_SAFE; |
4a0ac175 | 560 | cl_assert_equal_i( |
885b94aa | 561 | GIT_ECONFLICT, git_checkout_tree(g_repo, g_object, &g_opts)); |
4a0ac175 | 562 | |
563 | /* Stage the conflicting change */ | |
25743bd7 | 564 | cl_git_pass(git_index_add_bypath(index, entry_path)); |
4a0ac175 | 565 | cl_git_pass(git_index_write(index)); |
0b3aa7be | 566 | git_index_free(index); |
4a0ac175 | 567 | |
568 | cl_assert_equal_i( | |
885b94aa | 569 | GIT_ECONFLICT, git_checkout_tree(g_repo, g_object, &g_opts)); |
4a0ac175 | 570 | } |
571 | ||
885b94aa | 572 | void test_checkout_tree__checking_out_a_conflicting_type_change_returns_ECONFLICT(void) |
4a0ac175 | 573 | { |
574 | /* | |
575 | * 099faba adds a symlink named 'link_to_new.txt' | |
576 | * a65fedf is the parent of 099faba | |
577 | */ | |
578 | ||
579 | assert_conflict("link_to_new.txt", "old.txt", "a65fedf", "099faba"); | |
580 | } | |
581 | ||
885b94aa | 582 | void test_checkout_tree__checking_out_a_conflicting_type_change_returns_ECONFLICT_2(void) |
4a0ac175 | 583 | { |
584 | /* | |
585 | * cf80f8d adds a directory named 'a/' | |
586 | * a4a7dce is the parent of cf80f8d | |
587 | */ | |
588 | ||
589 | assert_conflict("a", "hello\n", "a4a7dce", "cf80f8d"); | |
590 | } | |
591 | ||
885b94aa | 592 | void test_checkout_tree__checking_out_a_conflicting_content_change_returns_ECONFLICT(void) |
4a0ac175 | 593 | { |
594 | /* | |
595 | * c47800c adds a symlink named 'branch_file.txt' | |
596 | * 5b5b025 is the parent of 763d71a | |
597 | */ | |
598 | ||
599 | assert_conflict("branch_file.txt", "hello\n", "5b5b025", "c47800c"); | |
600 | } | |
395509ff | 601 | |
dacce80b ET |
602 | void test_checkout_tree__donot_update_deleted_file_by_default(void) |
603 | { | |
6affd71f | 604 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; |
dacce80b ET |
605 | git_oid old_id, new_id; |
606 | git_commit *old_commit = NULL, *new_commit = NULL; | |
607 | git_index *index = NULL; | |
36fd9e30 | 608 | checkout_counts ct; |
dacce80b ET |
609 | |
610 | opts.checkout_strategy = GIT_CHECKOUT_SAFE; | |
611 | ||
36fd9e30 RB |
612 | memset(&ct, 0, sizeof(ct)); |
613 | opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL; | |
614 | opts.notify_cb = checkout_count_callback; | |
615 | opts.notify_payload = &ct; | |
616 | ||
dacce80b ET |
617 | cl_git_pass(git_repository_index(&index, g_repo)); |
618 | ||
619 | cl_git_pass(git_oid_fromstr(&old_id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644")); | |
620 | cl_git_pass(git_commit_lookup(&old_commit, g_repo, &old_id)); | |
23a17803 | 621 | cl_git_pass(git_reset(g_repo, (git_object *)old_commit, GIT_RESET_HARD, NULL)); |
dacce80b ET |
622 | |
623 | cl_git_pass(p_unlink("testrepo/branch_file.txt")); | |
624 | cl_git_pass(git_index_remove_bypath(index ,"branch_file.txt")); | |
625 | cl_git_pass(git_index_write(index)); | |
626 | ||
627 | cl_assert(!git_path_exists("testrepo/branch_file.txt")); | |
628 | ||
629 | cl_git_pass(git_oid_fromstr(&new_id, "099fabac3a9ea935598528c27f866e34089c2eff")); | |
630 | cl_git_pass(git_commit_lookup(&new_commit, g_repo, &new_id)); | |
36fd9e30 RB |
631 | |
632 | ||
dacce80b ET |
633 | cl_git_fail(git_checkout_tree(g_repo, (git_object *)new_commit, &opts)); |
634 | ||
36fd9e30 RB |
635 | cl_assert_equal_i(1, ct.n_conflicts); |
636 | cl_assert_equal_i(1, ct.n_updates); | |
637 | ||
dacce80b ET |
638 | git_commit_free(old_commit); |
639 | git_commit_free(new_commit); | |
640 | git_index_free(index); | |
641 | } | |
642 | ||
cbd04896 RB |
643 | struct checkout_cancel_at { |
644 | const char *filename; | |
645 | int error; | |
646 | int count; | |
647 | }; | |
648 | ||
649 | static int checkout_cancel_cb( | |
650 | git_checkout_notify_t why, | |
651 | const char *path, | |
652 | const git_diff_file *b, | |
653 | const git_diff_file *t, | |
654 | const git_diff_file *w, | |
655 | void *payload) | |
656 | { | |
657 | struct checkout_cancel_at *ca = payload; | |
658 | ||
659 | GIT_UNUSED(why); GIT_UNUSED(b); GIT_UNUSED(t); GIT_UNUSED(w); | |
660 | ||
661 | ca->count++; | |
662 | ||
663 | if (!strcmp(path, ca->filename)) | |
664 | return ca->error; | |
665 | ||
666 | return 0; | |
667 | } | |
668 | ||
669 | void test_checkout_tree__can_cancel_checkout_from_notify(void) | |
670 | { | |
671 | struct checkout_cancel_at ca; | |
6affd71f | 672 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; |
cbd04896 RB |
673 | git_oid oid; |
674 | git_object *obj = NULL; | |
675 | ||
676 | assert_on_branch(g_repo, "master"); | |
677 | ||
678 | cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir")); | |
ac3d33df | 679 | cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY)); |
cbd04896 RB |
680 | |
681 | ca.filename = "new.txt"; | |
682 | ca.error = -5555; | |
683 | ca.count = 0; | |
684 | ||
685 | opts.notify_flags = GIT_CHECKOUT_NOTIFY_UPDATED; | |
686 | opts.notify_cb = checkout_cancel_cb; | |
687 | opts.notify_payload = &ca; | |
688 | opts.checkout_strategy = GIT_CHECKOUT_FORCE; | |
689 | ||
690 | cl_assert(!git_path_exists("testrepo/new.txt")); | |
691 | ||
692 | cl_git_fail_with(git_checkout_tree(g_repo, obj, &opts), -5555); | |
693 | ||
694 | cl_assert(!git_path_exists("testrepo/new.txt")); | |
3520c970 ET |
695 | |
696 | /* on case-insensitive FS = a/b.txt, branch_file.txt, new.txt */ | |
697 | /* on case-sensitive FS = README, then above */ | |
698 | ||
699 | if (git_path_exists("testrepo/.git/CoNfIg")) /* case insensitive */ | |
700 | cl_assert_equal_i(3, ca.count); | |
701 | else | |
702 | cl_assert_equal_i(4, ca.count); | |
cbd04896 RB |
703 | |
704 | /* and again with a different stopping point and return code */ | |
705 | ca.filename = "README"; | |
706 | ca.error = 123; | |
707 | ca.count = 0; | |
708 | ||
709 | cl_git_fail_with(git_checkout_tree(g_repo, obj, &opts), 123); | |
710 | ||
711 | cl_assert(!git_path_exists("testrepo/new.txt")); | |
3520c970 ET |
712 | |
713 | if (git_path_exists("testrepo/.git/CoNfIg")) /* case insensitive */ | |
714 | cl_assert_equal_i(4, ca.count); | |
715 | else | |
716 | cl_assert_equal_i(1, ca.count); | |
cbd04896 RB |
717 | |
718 | git_object_free(obj); | |
719 | } | |
720 | ||
395509ff ET |
721 | void test_checkout_tree__can_checkout_with_last_workdir_item_missing(void) |
722 | { | |
723 | git_index *index = NULL; | |
6affd71f | 724 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; |
395509ff ET |
725 | git_oid tree_id, commit_id; |
726 | git_tree *tree = NULL; | |
727 | git_commit *commit = NULL; | |
1323c6d1 | 728 | |
395509ff | 729 | git_repository_index(&index, g_repo); |
1323c6d1 | 730 | |
395509ff | 731 | opts.checkout_strategy = GIT_CHECKOUT_FORCE; |
1323c6d1 | 732 | |
395509ff ET |
733 | cl_git_pass(git_reference_name_to_id(&commit_id, g_repo, "refs/heads/master")); |
734 | cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id)); | |
1323c6d1 | 735 | |
395509ff | 736 | cl_git_pass(git_checkout_tree(g_repo, (git_object *)commit, &opts)); |
4e498646 | 737 | cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master")); |
1323c6d1 | 738 | |
395509ff ET |
739 | cl_git_pass(p_mkdir("./testrepo/this-is-dir", 0777)); |
740 | cl_git_mkfile("./testrepo/this-is-dir/contained_file", "content\n"); | |
1323c6d1 | 741 | |
395509ff | 742 | cl_git_pass(git_index_add_bypath(index, "this-is-dir/contained_file")); |
ac3d33df JK |
743 | cl_git_pass(git_index_write(index)); |
744 | ||
745 | cl_git_pass(git_index_write_tree(&tree_id, index)); | |
395509ff | 746 | cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id)); |
1323c6d1 | 747 | |
395509ff | 748 | cl_git_pass(p_unlink("./testrepo/this-is-dir/contained_file")); |
1323c6d1 | 749 | |
395509ff | 750 | opts.checkout_strategy = GIT_CHECKOUT_SAFE; |
1323c6d1 | 751 | |
395509ff ET |
752 | opts.checkout_strategy = 1; |
753 | git_checkout_tree(g_repo, (git_object *)tree, &opts); | |
1323c6d1 | 754 | |
395509ff ET |
755 | git_tree_free(tree); |
756 | git_commit_free(commit); | |
757 | git_index_free(index); | |
758 | } | |
b8acb775 SS |
759 | |
760 | void test_checkout_tree__issue_1397(void) | |
761 | { | |
6affd71f | 762 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; |
b8acb775 | 763 | const char *partial_oid = "8a7ef04"; |
1098cfae RB |
764 | git_object *tree = NULL; |
765 | ||
766 | test_checkout_tree__cleanup(); /* cleanup default checkout */ | |
b8acb775 SS |
767 | |
768 | g_repo = cl_git_sandbox_init("issue_1397"); | |
769 | ||
1098cfae | 770 | cl_repo_set_bool(g_repo, "core.autocrlf", true); |
b8acb775 | 771 | |
2ebc3c66 | 772 | cl_git_pass(git_revparse_single(&tree, g_repo, partial_oid)); |
b8acb775 SS |
773 | |
774 | opts.checkout_strategy = GIT_CHECKOUT_FORCE; | |
775 | ||
1098cfae | 776 | cl_git_pass(git_checkout_tree(g_repo, tree, &opts)); |
b8acb775 | 777 | |
050ab995 | 778 | check_file_contents("./issue_1397/crlf_file.txt", "first line\r\nsecond line\r\nboth with crlf"); |
b8acb775 | 779 | |
1098cfae | 780 | git_object_free(tree); |
b8acb775 | 781 | } |
0cc7d8df ET |
782 | |
783 | void test_checkout_tree__can_write_to_empty_dirs(void) | |
784 | { | |
6affd71f | 785 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; |
0cc7d8df ET |
786 | git_oid oid; |
787 | git_object *obj = NULL; | |
788 | ||
789 | assert_on_branch(g_repo, "master"); | |
790 | ||
791 | cl_git_pass(p_mkdir("testrepo/a", 0777)); | |
792 | ||
793 | /* do first checkout with FORCE because we don't know if testrepo | |
794 | * base data is clean for a checkout or not | |
795 | */ | |
796 | opts.checkout_strategy = GIT_CHECKOUT_FORCE; | |
797 | ||
798 | cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir")); | |
ac3d33df | 799 | cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY)); |
0cc7d8df ET |
800 | |
801 | cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); | |
802 | ||
803 | cl_assert(git_path_isfile("testrepo/a/b.txt")); | |
804 | ||
805 | git_object_free(obj); | |
806 | } | |
e09d18ee ET |
807 | |
808 | void test_checkout_tree__fails_when_dir_in_use(void) | |
809 | { | |
810 | #ifdef GIT_WIN32 | |
6affd71f | 811 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; |
e09d18ee ET |
812 | git_oid oid; |
813 | git_object *obj = NULL; | |
814 | ||
815 | opts.checkout_strategy = GIT_CHECKOUT_FORCE; | |
816 | ||
817 | cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir")); | |
ac3d33df | 818 | cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY)); |
e09d18ee ET |
819 | |
820 | cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); | |
821 | ||
822 | cl_assert(git_path_isfile("testrepo/a/b.txt")); | |
823 | ||
824 | git_object_free(obj); | |
825 | ||
826 | cl_git_pass(p_chdir("testrepo/a")); | |
827 | ||
828 | cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/master")); | |
ac3d33df | 829 | cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY)); |
e09d18ee ET |
830 | |
831 | cl_git_fail(git_checkout_tree(g_repo, obj, &opts)); | |
832 | ||
833 | cl_git_pass(p_chdir("../..")); | |
834 | ||
835 | cl_assert(git_path_is_empty_dir("testrepo/a")); | |
d4f98ba4 RB |
836 | |
837 | git_object_free(obj); | |
e09d18ee ET |
838 | #endif |
839 | } | |
840 | ||
841 | void test_checkout_tree__can_continue_when_dir_in_use(void) | |
842 | { | |
843 | #ifdef GIT_WIN32 | |
6affd71f | 844 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; |
e09d18ee ET |
845 | git_oid oid; |
846 | git_object *obj = NULL; | |
847 | ||
848 | opts.checkout_strategy = GIT_CHECKOUT_FORCE | | |
849 | GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES; | |
850 | ||
851 | cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir")); | |
ac3d33df | 852 | cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY)); |
e09d18ee ET |
853 | |
854 | cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); | |
855 | ||
856 | cl_assert(git_path_isfile("testrepo/a/b.txt")); | |
857 | ||
858 | git_object_free(obj); | |
859 | ||
860 | cl_git_pass(p_chdir("testrepo/a")); | |
861 | ||
862 | cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/master")); | |
ac3d33df | 863 | cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY)); |
e09d18ee ET |
864 | |
865 | cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); | |
866 | ||
867 | cl_git_pass(p_chdir("../..")); | |
868 | ||
869 | cl_assert(git_path_is_empty_dir("testrepo/a")); | |
d4f98ba4 RB |
870 | |
871 | git_object_free(obj); | |
e09d18ee ET |
872 | #endif |
873 | } | |
d4f98ba4 RB |
874 | |
875 | void test_checkout_tree__target_directory_from_bare(void) | |
876 | { | |
6affd71f | 877 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; |
d4f98ba4 RB |
878 | git_oid oid; |
879 | checkout_counts cts; | |
880 | memset(&cts, 0, sizeof(cts)); | |
881 | ||
882 | test_checkout_tree__cleanup(); /* cleanup default checkout */ | |
883 | ||
884 | g_repo = cl_git_sandbox_init("testrepo.git"); | |
885 | cl_assert(git_repository_is_bare(g_repo)); | |
886 | ||
96b82b11 ET |
887 | opts.checkout_strategy = GIT_CHECKOUT_SAFE | |
888 | GIT_CHECKOUT_RECREATE_MISSING; | |
d4f98ba4 RB |
889 | |
890 | opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL; | |
891 | opts.notify_cb = checkout_count_callback; | |
892 | opts.notify_payload = &cts; | |
893 | ||
894 | cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD")); | |
ac3d33df | 895 | cl_git_pass(git_object_lookup(&g_object, g_repo, &oid, GIT_OBJECT_ANY)); |
d4f98ba4 RB |
896 | |
897 | cl_git_fail(git_checkout_tree(g_repo, g_object, &opts)); | |
898 | ||
899 | opts.target_directory = "alternative"; | |
900 | cl_assert(!git_path_isdir("alternative")); | |
901 | ||
902 | cl_git_pass(git_checkout_tree(g_repo, g_object, &opts)); | |
903 | ||
904 | cl_assert_equal_i(0, cts.n_untracked); | |
905 | cl_assert_equal_i(0, cts.n_ignored); | |
906 | cl_assert_equal_i(3, cts.n_updates); | |
907 | ||
f3f4c6b5 RB |
908 | check_file_contents_nocr("./alternative/README", "hey there\n"); |
909 | check_file_contents_nocr("./alternative/branch_file.txt", "hi\nbye!\n"); | |
910 | check_file_contents_nocr("./alternative/new.txt", "my new file\n"); | |
d4f98ba4 RB |
911 | |
912 | cl_git_pass(git_futils_rmdir_r( | |
913 | "alternative", NULL, GIT_RMDIR_REMOVE_FILES)); | |
914 | } | |
75f98a95 BS |
915 | |
916 | void test_checkout_tree__extremely_long_file_name(void) | |
917 | { | |
ac3d33df | 918 | /* A utf-8 string with 83 characters, but 249 bytes. */ |
75f98a95 BS |
919 | const char *longname = "\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97"; |
920 | char path[1024]; | |
921 | ||
922 | g_opts.checkout_strategy = GIT_CHECKOUT_FORCE; | |
923 | cl_git_pass(git_revparse_single(&g_object, g_repo, "long-file-name")); | |
924 | cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); | |
925 | ||
926 | sprintf(path, "testrepo/%s.txt", longname); | |
927 | cl_assert(git_path_exists(path)); | |
928 | ||
929 | git_object_free(g_object); | |
930 | cl_git_pass(git_revparse_single(&g_object, g_repo, "master")); | |
931 | cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); | |
932 | cl_assert(!git_path_exists(path)); | |
933 | } | |
4f7897ab | 934 | |
9f664347 | 935 | static void create_conflict(const char *path) |
4f7897ab ET |
936 | { |
937 | git_index *index; | |
938 | git_index_entry entry; | |
939 | ||
940 | cl_git_pass(git_repository_index(&index, g_repo)); | |
941 | ||
942 | memset(&entry, 0x0, sizeof(git_index_entry)); | |
943 | entry.mode = 0100644; | |
ac3d33df | 944 | GIT_INDEX_ENTRY_STAGE_SET(&entry, 1); |
d541170c | 945 | git_oid_fromstr(&entry.id, "d427e0b2e138501a3d15cc376077a3631e15bd46"); |
9f664347 | 946 | entry.path = path; |
4f7897ab ET |
947 | cl_git_pass(git_index_add(index, &entry)); |
948 | ||
ac3d33df | 949 | GIT_INDEX_ENTRY_STAGE_SET(&entry, 2); |
d541170c | 950 | git_oid_fromstr(&entry.id, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf"); |
4f7897ab ET |
951 | cl_git_pass(git_index_add(index, &entry)); |
952 | ||
ac3d33df | 953 | GIT_INDEX_ENTRY_STAGE_SET(&entry, 3); |
d541170c | 954 | git_oid_fromstr(&entry.id, "2bd0a343aeef7a2cf0d158478966a6e587ff3863"); |
4f7897ab ET |
955 | cl_git_pass(git_index_add(index, &entry)); |
956 | ||
ac3d33df | 957 | cl_git_pass(git_index_write(index)); |
4f7897ab ET |
958 | git_index_free(index); |
959 | } | |
960 | ||
961 | void test_checkout_tree__fails_when_conflicts_exist_in_index(void) | |
962 | { | |
6affd71f | 963 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; |
4f7897ab ET |
964 | git_oid oid; |
965 | git_object *obj = NULL; | |
966 | ||
967 | opts.checkout_strategy = GIT_CHECKOUT_SAFE; | |
968 | ||
969 | cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD")); | |
ac3d33df | 970 | cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY)); |
4f7897ab | 971 | |
9f664347 | 972 | create_conflict("conflicts.txt"); |
4f7897ab ET |
973 | |
974 | cl_git_fail(git_checkout_tree(g_repo, obj, &opts)); | |
975 | ||
976 | git_object_free(obj); | |
977 | } | |
e8b81c69 ET |
978 | |
979 | void test_checkout_tree__filemode_preserved_in_index(void) | |
980 | { | |
981 | git_oid executable_oid; | |
982 | git_commit *commit; | |
6affd71f | 983 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; |
e8b81c69 ET |
984 | git_index *index; |
985 | const git_index_entry *entry; | |
986 | ||
67db2bde JF |
987 | opts.checkout_strategy = GIT_CHECKOUT_FORCE; |
988 | ||
e8b81c69 ET |
989 | cl_git_pass(git_repository_index(&index, g_repo)); |
990 | ||
67db2bde | 991 | /* test a freshly added executable */ |
e8b81c69 ET |
992 | cl_git_pass(git_oid_fromstr(&executable_oid, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6")); |
993 | cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid)); | |
994 | ||
e8b81c69 ET |
995 | cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts)); |
996 | cl_assert(entry = git_index_get_bypath(index, "executable.txt", 0)); | |
6fe32284 | 997 | cl_assert(GIT_PERMS_IS_EXEC(entry->mode)); |
e8b81c69 ET |
998 | |
999 | git_commit_free(commit); | |
67db2bde JF |
1000 | |
1001 | ||
1002 | /* Now start with a commit which has a text file */ | |
1003 | cl_git_pass(git_oid_fromstr(&executable_oid, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9")); | |
1004 | cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid)); | |
1005 | ||
1006 | cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts)); | |
1007 | cl_assert(entry = git_index_get_bypath(index, "a/b.txt", 0)); | |
6fe32284 | 1008 | cl_assert(!GIT_PERMS_IS_EXEC(entry->mode)); |
67db2bde JF |
1009 | |
1010 | git_commit_free(commit); | |
1011 | ||
1012 | ||
1013 | /* And then check out to a commit which converts the text file to an executable */ | |
1014 | cl_git_pass(git_oid_fromstr(&executable_oid, "144344043ba4d4a405da03de3844aa829ae8be0e")); | |
1015 | cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid)); | |
1016 | ||
1017 | cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts)); | |
1018 | cl_assert(entry = git_index_get_bypath(index, "a/b.txt", 0)); | |
6fe32284 | 1019 | cl_assert(GIT_PERMS_IS_EXEC(entry->mode)); |
67db2bde JF |
1020 | |
1021 | git_commit_free(commit); | |
1022 | ||
1023 | ||
6124d983 MV |
1024 | /* Finally, check out the text file again and check that the exec bit is cleared */ |
1025 | cl_git_pass(git_oid_fromstr(&executable_oid, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9")); | |
1026 | cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid)); | |
1027 | ||
1028 | cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts)); | |
1029 | cl_assert(entry = git_index_get_bypath(index, "a/b.txt", 0)); | |
6fe32284 | 1030 | cl_assert(!GIT_PERMS_IS_EXEC(entry->mode)); |
6124d983 MV |
1031 | |
1032 | git_commit_free(commit); | |
1033 | ||
1034 | ||
e8b81c69 ET |
1035 | git_index_free(index); |
1036 | } | |
9f664347 | 1037 | |
33cad995 MV |
1038 | mode_t read_filemode(const char *path) |
1039 | { | |
b4d183a7 | 1040 | git_buf fullpath = GIT_BUF_INIT; |
33cad995 | 1041 | struct stat st; |
b4d183a7 | 1042 | mode_t result; |
33cad995 | 1043 | |
b4d183a7 ET |
1044 | git_buf_joinpath(&fullpath, "testrepo", path); |
1045 | cl_must_pass(p_stat(fullpath.ptr, &st)); | |
33cad995 | 1046 | |
eea7c850 ET |
1047 | result = GIT_PERMS_IS_EXEC(st.st_mode) ? |
1048 | GIT_FILEMODE_BLOB_EXECUTABLE : GIT_FILEMODE_BLOB; | |
b4d183a7 | 1049 | |
ac3d33df | 1050 | git_buf_dispose(&fullpath); |
b4d183a7 ET |
1051 | |
1052 | return result; | |
33cad995 MV |
1053 | } |
1054 | ||
1055 | void test_checkout_tree__filemode_preserved_in_workdir(void) | |
1056 | { | |
1057 | #ifndef GIT_WIN32 | |
1058 | git_oid executable_oid; | |
1059 | git_commit *commit; | |
1060 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; | |
1061 | ||
1062 | opts.checkout_strategy = GIT_CHECKOUT_FORCE; | |
1063 | ||
1064 | /* test a freshly added executable */ | |
1065 | cl_git_pass(git_oid_fromstr(&executable_oid, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6")); | |
1066 | cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid)); | |
1067 | ||
1068 | cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts)); | |
6fe32284 | 1069 | cl_assert(GIT_PERMS_IS_EXEC(read_filemode("executable.txt"))); |
33cad995 MV |
1070 | |
1071 | git_commit_free(commit); | |
1072 | ||
1073 | ||
1074 | /* Now start with a commit which has a text file */ | |
1075 | cl_git_pass(git_oid_fromstr(&executable_oid, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9")); | |
1076 | cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid)); | |
1077 | ||
1078 | cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts)); | |
6fe32284 | 1079 | cl_assert(!GIT_PERMS_IS_EXEC(read_filemode("a/b.txt"))); |
33cad995 MV |
1080 | |
1081 | git_commit_free(commit); | |
1082 | ||
1083 | ||
1084 | /* And then check out to a commit which converts the text file to an executable */ | |
1085 | cl_git_pass(git_oid_fromstr(&executable_oid, "144344043ba4d4a405da03de3844aa829ae8be0e")); | |
1086 | cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid)); | |
1087 | ||
1088 | cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts)); | |
6fe32284 | 1089 | cl_assert(GIT_PERMS_IS_EXEC(read_filemode("a/b.txt"))); |
33cad995 MV |
1090 | |
1091 | git_commit_free(commit); | |
1092 | ||
1093 | ||
1094 | /* Finally, check out the text file again and check that the exec bit is cleared */ | |
1095 | cl_git_pass(git_oid_fromstr(&executable_oid, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9")); | |
1096 | cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid)); | |
1097 | ||
1098 | cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts)); | |
6fe32284 | 1099 | cl_assert(!GIT_PERMS_IS_EXEC(read_filemode("a/b.txt"))); |
33cad995 MV |
1100 | |
1101 | git_commit_free(commit); | |
eae0bfdc PP |
1102 | #else |
1103 | cl_skip(); | |
33cad995 MV |
1104 | #endif |
1105 | } | |
1106 | ||
9f664347 ET |
1107 | void test_checkout_tree__removes_conflicts(void) |
1108 | { | |
1109 | git_oid commit_id; | |
1110 | git_commit *commit; | |
1111 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; | |
1112 | git_index *index; | |
ac3d33df | 1113 | |
9f664347 ET |
1114 | cl_git_pass(git_oid_fromstr(&commit_id, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6")); |
1115 | cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id)); | |
1116 | ||
1117 | opts.checkout_strategy = GIT_CHECKOUT_FORCE; | |
1118 | ||
1119 | cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts)); | |
1120 | ||
1121 | cl_git_pass(git_repository_index(&index, g_repo)); | |
1122 | cl_git_pass(git_index_remove(index, "executable.txt", 0)); | |
1123 | ||
1124 | create_conflict("executable.txt"); | |
1125 | cl_git_mkfile("testrepo/executable.txt", "This is the conflict file.\n"); | |
1126 | ||
1127 | create_conflict("other.txt"); | |
1128 | cl_git_mkfile("testrepo/other.txt", "This is another conflict file.\n"); | |
1129 | ||
ac3d33df | 1130 | cl_git_pass(git_index_write(index)); |
9f664347 ET |
1131 | |
1132 | cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts)); | |
1133 | ||
1134 | cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 1)); | |
1135 | cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 2)); | |
1136 | cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 3)); | |
1137 | ||
1138 | cl_assert_equal_p(NULL, git_index_get_bypath(index, "other.txt", 1)); | |
1139 | cl_assert_equal_p(NULL, git_index_get_bypath(index, "other.txt", 2)); | |
1140 | cl_assert_equal_p(NULL, git_index_get_bypath(index, "other.txt", 3)); | |
1141 | ||
1142 | cl_assert(!git_path_exists("testrepo/other.txt")); | |
1143 | ||
f7fcb18f | 1144 | git_commit_free(commit); |
9f664347 ET |
1145 | git_index_free(index); |
1146 | } | |
1147 | ||
1148 | ||
1149 | void test_checkout_tree__removes_conflicts_only_by_pathscope(void) | |
1150 | { | |
1151 | git_oid commit_id; | |
1152 | git_commit *commit; | |
1153 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; | |
1154 | git_index *index; | |
1155 | const char *path = "executable.txt"; | |
ac3d33df | 1156 | |
9f664347 ET |
1157 | cl_git_pass(git_oid_fromstr(&commit_id, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6")); |
1158 | cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id)); | |
1159 | ||
1160 | opts.checkout_strategy = GIT_CHECKOUT_FORCE; | |
1161 | opts.paths.count = 1; | |
1162 | opts.paths.strings = (char **)&path; | |
1163 | ||
1164 | cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts)); | |
1165 | ||
1166 | cl_git_pass(git_repository_index(&index, g_repo)); | |
1167 | cl_git_pass(git_index_remove(index, "executable.txt", 0)); | |
1168 | ||
1169 | create_conflict("executable.txt"); | |
1170 | cl_git_mkfile("testrepo/executable.txt", "This is the conflict file.\n"); | |
1171 | ||
1172 | create_conflict("other.txt"); | |
1173 | cl_git_mkfile("testrepo/other.txt", "This is another conflict file.\n"); | |
1174 | ||
ac3d33df | 1175 | cl_git_pass(git_index_write(index)); |
9f664347 ET |
1176 | |
1177 | cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts)); | |
1178 | ||
1179 | cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 1)); | |
1180 | cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 2)); | |
1181 | cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 3)); | |
1182 | ||
1183 | cl_assert(git_index_get_bypath(index, "other.txt", 1) != NULL); | |
1184 | cl_assert(git_index_get_bypath(index, "other.txt", 2) != NULL); | |
1185 | cl_assert(git_index_get_bypath(index, "other.txt", 3) != NULL); | |
1186 | ||
1187 | cl_assert(git_path_exists("testrepo/other.txt")); | |
1188 | ||
f7fcb18f | 1189 | git_commit_free(commit); |
9f664347 ET |
1190 | git_index_free(index); |
1191 | } | |
61ee5b0e ET |
1192 | |
1193 | void test_checkout_tree__case_changing_rename(void) | |
1194 | { | |
1195 | git_index *index; | |
1196 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; | |
1197 | git_oid master_id, dir_commit_id, tree_id, commit_id; | |
1198 | git_commit *master_commit, *dir_commit; | |
1199 | git_tree *tree; | |
1200 | git_signature *signature; | |
1201 | const git_index_entry *index_entry; | |
1202 | bool case_sensitive; | |
1203 | ||
1204 | assert_on_branch(g_repo, "master"); | |
1205 | ||
1206 | cl_git_pass(git_repository_index(&index, g_repo)); | |
1207 | ||
1208 | /* Switch branches and perform a case-changing rename */ | |
1209 | ||
1210 | opts.checkout_strategy = GIT_CHECKOUT_FORCE; | |
1211 | ||
1212 | cl_git_pass(git_reference_name_to_id(&dir_commit_id, g_repo, "refs/heads/dir")); | |
1213 | cl_git_pass(git_commit_lookup(&dir_commit, g_repo, &dir_commit_id)); | |
1214 | ||
1215 | cl_git_pass(git_checkout_tree(g_repo, (git_object *)dir_commit, &opts)); | |
4e498646 | 1216 | cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir")); |
61ee5b0e ET |
1217 | |
1218 | cl_assert(git_path_isfile("testrepo/README")); | |
1219 | case_sensitive = !git_path_isfile("testrepo/readme"); | |
1220 | ||
1221 | cl_assert(index_entry = git_index_get_bypath(index, "README", 0)); | |
1222 | cl_assert_equal_s("README", index_entry->path); | |
1223 | ||
1224 | cl_git_pass(git_index_remove_bypath(index, "README")); | |
1225 | cl_git_pass(p_rename("testrepo/README", "testrepo/__readme__")); | |
1226 | cl_git_pass(p_rename("testrepo/__readme__", "testrepo/readme")); | |
1227 | cl_git_append2file("testrepo/readme", "An addendum..."); | |
1228 | cl_git_pass(git_index_add_bypath(index, "readme")); | |
1229 | ||
1230 | cl_git_pass(git_index_write(index)); | |
1231 | ||
1232 | cl_git_pass(git_index_write_tree(&tree_id, index)); | |
1233 | cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id)); | |
1234 | ||
1235 | cl_git_pass(git_signature_new(&signature, "Renamer", "rename@contoso.com", time(NULL), 0)); | |
1236 | ||
1237 | cl_git_pass(git_commit_create(&commit_id, g_repo, "refs/heads/dir", signature, signature, NULL, "case-changing rename", tree, 1, (const git_commit **)&dir_commit)); | |
1238 | ||
1239 | cl_assert(git_path_isfile("testrepo/readme")); | |
1240 | if (case_sensitive) | |
1241 | cl_assert(!git_path_isfile("testrepo/README")); | |
1242 | ||
1243 | cl_assert(index_entry = git_index_get_bypath(index, "readme", 0)); | |
1244 | cl_assert_equal_s("readme", index_entry->path); | |
1245 | ||
1246 | /* Switching back to master should rename readme -> README */ | |
1247 | opts.checkout_strategy = GIT_CHECKOUT_SAFE; | |
1248 | ||
1249 | cl_git_pass(git_reference_name_to_id(&master_id, g_repo, "refs/heads/master")); | |
1250 | cl_git_pass(git_commit_lookup(&master_commit, g_repo, &master_id)); | |
1251 | ||
1252 | cl_git_pass(git_checkout_tree(g_repo, (git_object *)master_commit, &opts)); | |
4e498646 | 1253 | cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master")); |
ac3d33df | 1254 | |
61ee5b0e ET |
1255 | assert_on_branch(g_repo, "master"); |
1256 | ||
1257 | cl_assert(git_path_isfile("testrepo/README")); | |
1258 | if (case_sensitive) | |
1259 | cl_assert(!git_path_isfile("testrepo/readme")); | |
1260 | ||
1261 | cl_assert(index_entry = git_index_get_bypath(index, "README", 0)); | |
1262 | cl_assert_equal_s("README", index_entry->path); | |
1263 | ||
c4a2fd5c | 1264 | git_index_free(index); |
61ee5b0e ET |
1265 | git_signature_free(signature); |
1266 | git_tree_free(tree); | |
1267 | git_commit_free(dir_commit); | |
1268 | git_commit_free(master_commit); | |
1269 | } | |
1d50b364 ET |
1270 | |
1271 | void perfdata_cb(const git_checkout_perfdata *in, void *payload) | |
1272 | { | |
1273 | memcpy(payload, in, sizeof(git_checkout_perfdata)); | |
1274 | } | |
1275 | ||
1276 | void test_checkout_tree__can_collect_perfdata(void) | |
1277 | { | |
1278 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; | |
1279 | git_oid oid; | |
1280 | git_object *obj = NULL; | |
1281 | git_checkout_perfdata perfdata = {0}; | |
1282 | ||
1283 | opts.perfdata_cb = perfdata_cb; | |
1284 | opts.perfdata_payload = &perfdata; | |
1285 | ||
1286 | assert_on_branch(g_repo, "master"); | |
1287 | opts.checkout_strategy = GIT_CHECKOUT_FORCE; | |
1288 | ||
1289 | cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir")); | |
ac3d33df | 1290 | cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY)); |
1d50b364 ET |
1291 | |
1292 | cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); | |
1293 | ||
1294 | cl_assert(perfdata.mkdir_calls > 0); | |
1295 | cl_assert(perfdata.stat_calls > 0); | |
1296 | ||
1297 | git_object_free(obj); | |
1298 | } | |
2fbce0bf ET |
1299 | |
1300 | void update_attr_callback( | |
1301 | const char *path, | |
1302 | size_t completed_steps, | |
1303 | size_t total_steps, | |
1304 | void *payload) | |
1305 | { | |
fa89ff20 ET |
1306 | GIT_UNUSED(completed_steps); |
1307 | GIT_UNUSED(total_steps); | |
1308 | GIT_UNUSED(payload); | |
1309 | ||
2fbce0bf ET |
1310 | if (path && strcmp(path, "ident1.txt") == 0) |
1311 | cl_git_write2file("testrepo/.gitattributes", | |
1312 | "*.txt ident\n", 12, O_RDWR|O_CREAT, 0666); | |
1313 | } | |
1314 | ||
1315 | void test_checkout_tree__caches_attributes_during_checkout(void) | |
1316 | { | |
1317 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; | |
1318 | git_oid oid; | |
1319 | git_object *obj = NULL; | |
1320 | git_buf ident1 = GIT_BUF_INIT, ident2 = GIT_BUF_INIT; | |
1321 | char *ident_paths[] = { "ident1.txt", "ident2.txt" }; | |
1322 | ||
1323 | opts.progress_cb = update_attr_callback; | |
1324 | ||
1325 | assert_on_branch(g_repo, "master"); | |
1326 | opts.checkout_strategy = GIT_CHECKOUT_FORCE; | |
1327 | opts.paths.strings = ident_paths; | |
1328 | opts.paths.count = 2; | |
1329 | ||
1330 | cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/ident")); | |
ac3d33df | 1331 | cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY)); |
2fbce0bf ET |
1332 | |
1333 | cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); | |
1334 | ||
1335 | cl_git_pass(git_futils_readbuffer(&ident1, "testrepo/ident1.txt")); | |
1336 | cl_git_pass(git_futils_readbuffer(&ident2, "testrepo/ident2.txt")); | |
1337 | ||
1338 | cl_assert_equal_strn(ident1.ptr, "# $Id$", 6); | |
1339 | cl_assert_equal_strn(ident2.ptr, "# $Id$", 6); | |
1340 | ||
1341 | cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); | |
1342 | ||
1343 | cl_git_pass(git_futils_readbuffer(&ident1, "testrepo/ident1.txt")); | |
1344 | cl_git_pass(git_futils_readbuffer(&ident2, "testrepo/ident2.txt")); | |
1345 | ||
1346 | cl_assert_equal_strn(ident1.ptr, "# $Id: ", 7); | |
1347 | cl_assert_equal_strn(ident2.ptr, "# $Id: ", 7); | |
1348 | ||
ac3d33df JK |
1349 | git_buf_dispose(&ident1); |
1350 | git_buf_dispose(&ident2); | |
2fbce0bf ET |
1351 | git_object_free(obj); |
1352 | } | |
8639ea5f ET |
1353 | |
1354 | void test_checkout_tree__can_not_update_index(void) | |
1355 | { | |
1356 | git_oid oid; | |
1357 | git_object *head; | |
1358 | unsigned int status; | |
1359 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; | |
1360 | git_index *index; | |
1361 | ||
1362 | opts.checkout_strategy |= | |
1363 | GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DONT_UPDATE_INDEX; | |
1364 | ||
1365 | cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD")); | |
ac3d33df | 1366 | cl_git_pass(git_object_lookup(&head, g_repo, &oid, GIT_OBJECT_ANY)); |
8639ea5f | 1367 | |
6bfb990d | 1368 | cl_git_pass(git_reset(g_repo, head, GIT_RESET_HARD, &g_opts)); |
8639ea5f ET |
1369 | |
1370 | cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/")); | |
1371 | ||
1372 | cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees")); | |
1373 | ||
1374 | cl_git_pass(git_checkout_tree(g_repo, g_object, &opts)); | |
1375 | ||
1376 | cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt")); | |
1377 | cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt")); | |
1378 | cl_assert_equal_i(GIT_STATUS_WT_NEW, status); | |
1379 | ||
1380 | cl_git_pass(git_repository_index(&index, g_repo)); | |
1381 | cl_git_pass(git_index_write(index)); | |
1382 | ||
1383 | cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt")); | |
1384 | cl_assert_equal_i(GIT_STATUS_WT_NEW, status); | |
1385 | ||
1386 | git_object_free(head); | |
1387 | git_index_free(index); | |
1388 | } | |
1389 | ||
1390 | void test_checkout_tree__can_update_but_not_write_index(void) | |
1391 | { | |
1392 | git_oid oid; | |
1393 | git_object *head; | |
1394 | unsigned int status; | |
1395 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; | |
1396 | git_index *index; | |
1397 | git_repository *other; | |
1398 | ||
1399 | opts.checkout_strategy |= | |
1400 | GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DONT_WRITE_INDEX; | |
1401 | ||
1402 | cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD")); | |
ac3d33df | 1403 | cl_git_pass(git_object_lookup(&head, g_repo, &oid, GIT_OBJECT_ANY)); |
8639ea5f | 1404 | |
6bfb990d | 1405 | cl_git_pass(git_reset(g_repo, head, GIT_RESET_HARD, &g_opts)); |
8639ea5f ET |
1406 | |
1407 | cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/")); | |
1408 | ||
1409 | cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees")); | |
1410 | ||
1411 | cl_git_pass(git_checkout_tree(g_repo, g_object, &opts)); | |
1412 | ||
1413 | cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt")); | |
1414 | cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt")); | |
1415 | cl_assert_equal_i(GIT_STATUS_INDEX_NEW, status); | |
1416 | ||
1417 | cl_git_pass(git_repository_open(&other, "testrepo")); | |
1418 | cl_git_pass(git_status_file(&status, other, "ab/de/2.txt")); | |
1419 | cl_assert_equal_i(GIT_STATUS_WT_NEW, status); | |
1420 | git_repository_free(other); | |
1421 | ||
1422 | cl_git_pass(git_repository_index(&index, g_repo)); | |
1423 | cl_git_pass(git_index_write(index)); | |
1424 | ||
1425 | cl_git_pass(git_repository_open(&other, "testrepo")); | |
1426 | cl_git_pass(git_status_file(&status, other, "ab/de/2.txt")); | |
1427 | cl_assert_equal_i(GIT_STATUS_INDEX_NEW, status); | |
1428 | git_repository_free(other); | |
1429 | ||
1430 | git_object_free(head); | |
1431 | git_index_free(index); | |
1432 | } | |
e6da3e44 ET |
1433 | |
1434 | /* Emulate checking out in a repo created by clone --no-checkout, | |
1435 | * which would not have written an index. */ | |
1436 | void test_checkout_tree__safe_proceeds_if_no_index(void) | |
1437 | { | |
1438 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; | |
1439 | git_oid oid; | |
1440 | git_object *obj = NULL; | |
1441 | ||
1442 | assert_on_branch(g_repo, "master"); | |
1443 | cl_must_pass(p_unlink("testrepo/.git/index")); | |
1444 | ||
1445 | /* do second checkout safe because we should be clean after first */ | |
1446 | opts.checkout_strategy = GIT_CHECKOUT_SAFE; | |
1447 | ||
1448 | cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/subtrees")); | |
ac3d33df | 1449 | cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY)); |
e6da3e44 ET |
1450 | |
1451 | cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); | |
4e498646 | 1452 | cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees")); |
e6da3e44 ET |
1453 | |
1454 | cl_assert(git_path_isfile("testrepo/README")); | |
1455 | cl_assert(git_path_isfile("testrepo/branch_file.txt")); | |
1456 | cl_assert(git_path_isfile("testrepo/new.txt")); | |
1457 | cl_assert(git_path_isfile("testrepo/ab/4.txt")); | |
1458 | cl_assert(git_path_isfile("testrepo/ab/c/3.txt")); | |
1459 | cl_assert(git_path_isfile("testrepo/ab/de/2.txt")); | |
1460 | cl_assert(git_path_isfile("testrepo/ab/de/fgh/1.txt")); | |
1461 | ||
1462 | cl_assert(!git_path_isdir("testrepo/a")); | |
1463 | ||
1464 | assert_on_branch(g_repo, "subtrees"); | |
1465 | ||
1466 | git_object_free(obj); | |
1467 | } | |
1468 | ||
bb0bd71a ET |
1469 | static int checkout_conflict_count_cb( |
1470 | git_checkout_notify_t why, | |
1471 | const char *path, | |
1472 | const git_diff_file *b, | |
1473 | const git_diff_file *t, | |
1474 | const git_diff_file *w, | |
1475 | void *payload) | |
1476 | { | |
1477 | size_t *n = payload; | |
1478 | ||
1479 | GIT_UNUSED(why); | |
1480 | GIT_UNUSED(path); | |
1481 | GIT_UNUSED(b); | |
1482 | GIT_UNUSED(t); | |
1483 | GIT_UNUSED(w); | |
1484 | ||
1485 | (*n)++; | |
1486 | ||
1487 | return 0; | |
1488 | } | |
1489 | ||
1490 | /* A repo that has a HEAD (even a properly born HEAD that peels to | |
1491 | * a commit) but no index should be treated as if it's an empty baseline | |
1492 | */ | |
1493 | void test_checkout_tree__baseline_is_empty_when_no_index(void) | |
1494 | { | |
1495 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; | |
1496 | git_reference *head; | |
1497 | git_object *obj; | |
bb0bd71a ET |
1498 | size_t conflicts = 0; |
1499 | ||
1500 | assert_on_branch(g_repo, "master"); | |
ac3d33df | 1501 | |
bb0bd71a | 1502 | cl_git_pass(git_repository_head(&head, g_repo)); |
ac3d33df | 1503 | cl_git_pass(git_reference_peel(&obj, head, GIT_OBJECT_COMMIT)); |
bb0bd71a ET |
1504 | |
1505 | cl_git_pass(git_reset(g_repo, obj, GIT_RESET_HARD, NULL)); | |
1506 | ||
1507 | cl_must_pass(p_unlink("testrepo/.git/index")); | |
1508 | ||
1509 | /* for a safe checkout, we should have checkout conflicts with | |
1510 | * the existing untracked files. | |
1511 | */ | |
1512 | opts.checkout_strategy &= ~GIT_CHECKOUT_FORCE; | |
1513 | opts.notify_flags = GIT_CHECKOUT_NOTIFY_CONFLICT; | |
1514 | opts.notify_cb = checkout_conflict_count_cb; | |
1515 | opts.notify_payload = &conflicts; | |
1516 | ||
1517 | cl_git_fail_with(GIT_ECONFLICT, git_checkout_tree(g_repo, obj, &opts)); | |
1518 | cl_assert_equal_i(4, conflicts); | |
1519 | ||
1520 | /* but force should succeed and update the index */ | |
1521 | opts.checkout_strategy |= GIT_CHECKOUT_FORCE; | |
1522 | cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); | |
1523 | ||
eae0bfdc | 1524 | assert_status_entrycount(g_repo, 0); |
bb0bd71a ET |
1525 | |
1526 | git_object_free(obj); | |
1527 | git_reference_free(head); | |
1528 | } | |
1529 | ||
eae0bfdc PP |
1530 | void test_checkout_tree__mode_change_is_force_updated(void) |
1531 | { | |
1532 | git_index *index; | |
1533 | git_reference *head; | |
1534 | git_object *obj; | |
1535 | ||
1536 | if (!cl_is_chmod_supported()) | |
1537 | clar__skip(); | |
1538 | ||
1539 | assert_on_branch(g_repo, "master"); | |
1540 | cl_git_pass(git_repository_index(&index, g_repo)); | |
1541 | cl_git_pass(git_repository_head(&head, g_repo)); | |
ac3d33df | 1542 | cl_git_pass(git_reference_peel(&obj, head, GIT_OBJECT_COMMIT)); |
eae0bfdc PP |
1543 | |
1544 | cl_git_pass(git_reset(g_repo, obj, GIT_RESET_HARD, NULL)); | |
1545 | assert_status_entrycount(g_repo, 0); | |
1546 | ||
1547 | /* update the mode on-disk */ | |
1548 | cl_must_pass(p_chmod("testrepo/README", 0755)); | |
1549 | ||
1550 | assert_status_entrycount(g_repo, 1); | |
1551 | cl_git_pass(git_checkout_tree(g_repo, obj, &g_opts)); | |
1552 | assert_status_entrycount(g_repo, 0); | |
1553 | ||
1554 | /* update the mode on-disk and in the index */ | |
1555 | cl_must_pass(p_chmod("testrepo/README", 0755)); | |
1556 | cl_must_pass(git_index_add_bypath(index, "README")); | |
1557 | ||
ac3d33df | 1558 | cl_git_pass(git_index_write(index)); |
eae0bfdc | 1559 | assert_status_entrycount(g_repo, 1); |
ac3d33df | 1560 | |
eae0bfdc | 1561 | cl_git_pass(git_checkout_tree(g_repo, obj, &g_opts)); |
ac3d33df JK |
1562 | cl_git_pass(git_index_write(index)); |
1563 | ||
eae0bfdc PP |
1564 | assert_status_entrycount(g_repo, 0); |
1565 | ||
1566 | git_object_free(obj); | |
1567 | git_reference_free(head); | |
1568 | git_index_free(index); | |
1569 | } | |
1570 | ||
88cfe614 SH |
1571 | void test_checkout_tree__nullopts(void) |
1572 | { | |
1573 | cl_git_pass(git_checkout_tree(g_repo, NULL, NULL)); | |
1574 | } | |
ac3d33df JK |
1575 | |
1576 | static void modify_index_ondisk(void) | |
1577 | { | |
1578 | git_repository *other_repo; | |
1579 | git_index *other_index; | |
1580 | git_index_entry entry = {{0}}; | |
1581 | ||
1582 | cl_git_pass(git_repository_open(&other_repo, git_repository_workdir(g_repo))); | |
1583 | cl_git_pass(git_repository_index(&other_index, other_repo)); | |
1584 | ||
1585 | cl_git_pass(git_oid_fromstr(&entry.id, "1385f264afb75a56a5bec74243be9b367ba4ca08")); | |
1586 | entry.mode = 0100644; | |
1587 | entry.path = "README"; | |
1588 | ||
1589 | cl_git_pass(git_index_add(other_index, &entry)); | |
1590 | cl_git_pass(git_index_write(other_index)); | |
1591 | ||
1592 | git_index_free(other_index); | |
1593 | git_repository_free(other_repo); | |
1594 | } | |
1595 | ||
1596 | static void modify_index_and_checkout_tree(git_checkout_options *opts) | |
1597 | { | |
1598 | git_index *index; | |
1599 | git_reference *head; | |
1600 | git_object *obj; | |
1601 | ||
1602 | /* External changes to the index are maintained by default */ | |
1603 | cl_git_pass(git_repository_index(&index, g_repo)); | |
1604 | cl_git_pass(git_repository_head(&head, g_repo)); | |
1605 | cl_git_pass(git_reference_peel(&obj, head, GIT_OBJECT_COMMIT)); | |
1606 | ||
1607 | cl_git_pass(git_reset(g_repo, obj, GIT_RESET_HARD, NULL)); | |
1608 | assert_status_entrycount(g_repo, 0); | |
1609 | ||
1610 | modify_index_ondisk(); | |
1611 | ||
1612 | /* The file in the index remains modified */ | |
1613 | cl_git_pass(git_checkout_tree(g_repo, obj, opts)); | |
1614 | ||
1615 | git_object_free(obj); | |
1616 | git_reference_free(head); | |
1617 | git_index_free(index); | |
1618 | } | |
1619 | ||
1620 | void test_checkout_tree__retains_external_index_changes(void) | |
1621 | { | |
1622 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; | |
1623 | ||
1624 | opts.checkout_strategy = GIT_CHECKOUT_SAFE; | |
1625 | ||
1626 | modify_index_and_checkout_tree(&opts); | |
1627 | assert_status_entrycount(g_repo, 1); | |
1628 | } | |
1629 | ||
1630 | void test_checkout_tree__no_index_refresh(void) | |
1631 | { | |
1632 | git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; | |
1633 | ||
1634 | opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_NO_REFRESH; | |
1635 | ||
1636 | modify_index_and_checkout_tree(&opts); | |
1637 | assert_status_entrycount(g_repo, 0); | |
1638 | } |