1 #include "clar_libgit2.h"
2 #include "checkout_helpers.h"
4 #include "git2/checkout.h"
5 #include "repository.h"
8 static git_repository
*g_repo
;
9 static git_checkout_options g_opts
;
10 static git_object
*g_object
;
12 static void assert_status_entrycount(git_repository
*repo
, size_t count
)
14 git_status_list
*status
;
16 cl_git_pass(git_status_list_new(&status
, repo
, NULL
));
17 cl_assert_equal_i(count
, git_status_list_entrycount(status
));
19 git_status_list_free(status
);
22 void test_checkout_tree__initialize(void)
24 g_repo
= cl_git_sandbox_init("testrepo");
26 GIT_INIT_STRUCTURE(&g_opts
, GIT_CHECKOUT_OPTIONS_VERSION
);
27 g_opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
30 void test_checkout_tree__cleanup(void)
32 git_object_free(g_object
);
35 cl_git_sandbox_cleanup();
37 if (git_fs_path_isdir("alternative"))
38 git_futils_rmdir_r("alternative", NULL
, GIT_RMDIR_REMOVE_FILES
);
41 void test_checkout_tree__cannot_checkout_a_non_treeish(void)
44 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"));
45 cl_git_fail(git_checkout_tree(g_repo
, g_object
, NULL
));
48 void test_checkout_tree__can_checkout_a_subdirectory_from_a_commit(void)
50 char *entries
[] = { "ab/de/" };
52 g_opts
.paths
.strings
= entries
;
53 g_opts
.paths
.count
= 1;
55 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "subtrees"));
57 cl_assert_equal_i(false, git_fs_path_isdir("./testrepo/ab/"));
59 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
61 cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/ab/de/2.txt"));
62 cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/ab/de/fgh/1.txt"));
65 void test_checkout_tree__can_checkout_and_remove_directory(void)
67 cl_assert_equal_i(false, git_fs_path_isdir("./testrepo/ab/"));
69 /* Checkout branch "subtrees" and update HEAD, so that HEAD matches the
70 * current working tree
72 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "subtrees"));
73 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
75 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/subtrees"));
77 cl_assert_equal_i(true, git_fs_path_isdir("./testrepo/ab/"));
78 cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/ab/de/2.txt"));
79 cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/ab/de/fgh/1.txt"));
81 git_object_free(g_object
);
84 /* Checkout branch "master" and update HEAD, so that HEAD matches the
85 * current working tree
87 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "master"));
88 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
90 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/master"));
92 /* This directory should no longer exist */
93 cl_assert_equal_i(false, git_fs_path_isdir("./testrepo/ab/"));
96 void test_checkout_tree__can_checkout_a_subdirectory_from_a_subtree(void)
98 char *entries
[] = { "de/" };
100 g_opts
.paths
.strings
= entries
;
101 g_opts
.paths
.count
= 1;
103 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "subtrees:ab"));
105 cl_assert_equal_i(false, git_fs_path_isdir("./testrepo/de/"));
107 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
109 cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/de/2.txt"));
110 cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/de/fgh/1.txt"));
113 static void progress(const char *path
, size_t cur
, size_t tot
, void *payload
)
115 bool *was_called
= (bool*)payload
;
116 GIT_UNUSED(path
); GIT_UNUSED(cur
); GIT_UNUSED(tot
);
120 void test_checkout_tree__calls_progress_callback(void)
124 g_opts
.progress_cb
= progress
;
125 g_opts
.progress_payload
= &was_called
;
127 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "master"));
129 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
131 cl_assert_equal_i(was_called
, true);
134 void test_checkout_tree__doesnt_write_unrequested_files_to_worktree(void)
138 git_commit
* p_master_commit
;
139 git_commit
* p_chomped_commit
;
140 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
142 git_oid_fromstr(&master_oid
, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
143 git_oid_fromstr(&chomped_oid
, "e90810b8df3e80c413d903f631643c716887138d");
144 cl_git_pass(git_commit_lookup(&p_master_commit
, g_repo
, &master_oid
));
145 cl_git_pass(git_commit_lookup(&p_chomped_commit
, g_repo
, &chomped_oid
));
147 /* GIT_CHECKOUT_NONE should not add any file to the working tree from the
148 * index as it is supposed to be a dry run.
150 opts
.checkout_strategy
= GIT_CHECKOUT_NONE
;
151 git_checkout_tree(g_repo
, (git_object
*)p_chomped_commit
, &opts
);
152 cl_assert_equal_i(false, git_fs_path_isfile("testrepo/readme.txt"));
154 git_commit_free(p_master_commit
);
155 git_commit_free(p_chomped_commit
);
158 void test_checkout_tree__can_switch_branches(void)
160 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
162 git_object
*obj
= NULL
;
164 assert_on_branch(g_repo
, "master");
166 /* do first checkout with FORCE because we don't know if testrepo
167 * base data is clean for a checkout or not
169 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
171 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
172 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
174 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
175 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/dir"));
177 cl_assert(git_fs_path_isfile("testrepo/README"));
178 cl_assert(git_fs_path_isfile("testrepo/branch_file.txt"));
179 cl_assert(git_fs_path_isfile("testrepo/new.txt"));
180 cl_assert(git_fs_path_isfile("testrepo/a/b.txt"));
182 cl_assert(!git_fs_path_isdir("testrepo/ab"));
184 assert_on_branch(g_repo
, "dir");
186 git_object_free(obj
);
188 /* do second checkout safe because we should be clean after first */
189 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
191 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/subtrees"));
192 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
194 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
195 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/subtrees"));
197 cl_assert(git_fs_path_isfile("testrepo/README"));
198 cl_assert(git_fs_path_isfile("testrepo/branch_file.txt"));
199 cl_assert(git_fs_path_isfile("testrepo/new.txt"));
200 cl_assert(git_fs_path_isfile("testrepo/ab/4.txt"));
201 cl_assert(git_fs_path_isfile("testrepo/ab/c/3.txt"));
202 cl_assert(git_fs_path_isfile("testrepo/ab/de/2.txt"));
203 cl_assert(git_fs_path_isfile("testrepo/ab/de/fgh/1.txt"));
205 cl_assert(!git_fs_path_isdir("testrepo/a"));
207 assert_on_branch(g_repo
, "subtrees");
209 git_object_free(obj
);
212 void test_checkout_tree__can_remove_untracked(void)
214 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
216 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
| GIT_CHECKOUT_REMOVE_UNTRACKED
;
218 cl_git_mkfile("testrepo/untracked_file", "as you wish");
219 cl_assert(git_fs_path_isfile("testrepo/untracked_file"));
221 cl_git_pass(git_checkout_head(g_repo
, &opts
));
223 cl_assert(!git_fs_path_isfile("testrepo/untracked_file"));
226 void test_checkout_tree__can_remove_ignored(void)
228 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
231 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
| GIT_CHECKOUT_REMOVE_IGNORED
;
233 cl_git_mkfile("testrepo/ignored_file", "as you wish");
235 cl_git_pass(git_ignore_add_rule(g_repo
, "ignored_file\n"));
237 cl_git_pass(git_ignore_path_is_ignored(&ignored
, g_repo
, "ignored_file"));
238 cl_assert_equal_i(1, ignored
);
240 cl_assert(git_fs_path_isfile("testrepo/ignored_file"));
242 cl_git_pass(git_checkout_head(g_repo
, &opts
));
244 cl_assert(!git_fs_path_isfile("testrepo/ignored_file"));
247 static int checkout_tree_with_blob_ignored_in_workdir(int strategy
, bool isdir
)
250 git_object
*obj
= NULL
;
251 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
252 int ignored
= 0, error
;
254 assert_on_branch(g_repo
, "master");
256 /* do first checkout with FORCE because we don't know if testrepo
257 * base data is clean for a checkout or not
259 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
261 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
262 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
264 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
265 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/dir"));
267 cl_assert(git_fs_path_isfile("testrepo/README"));
268 cl_assert(git_fs_path_isfile("testrepo/branch_file.txt"));
269 cl_assert(git_fs_path_isfile("testrepo/new.txt"));
270 cl_assert(git_fs_path_isfile("testrepo/a/b.txt"));
272 cl_assert(!git_fs_path_isdir("testrepo/ab"));
274 assert_on_branch(g_repo
, "dir");
276 git_object_free(obj
);
278 opts
.checkout_strategy
= strategy
;
281 cl_must_pass(p_mkdir("testrepo/ab", 0777));
282 cl_must_pass(p_mkdir("testrepo/ab/4.txt", 0777));
284 cl_git_mkfile("testrepo/ab/4.txt/file1.txt", "as you wish");
285 cl_git_mkfile("testrepo/ab/4.txt/file2.txt", "foo bar foo");
286 cl_git_mkfile("testrepo/ab/4.txt/file3.txt", "inky blinky pinky clyde");
288 cl_assert(git_fs_path_isdir("testrepo/ab/4.txt"));
290 cl_must_pass(p_mkdir("testrepo/ab", 0777));
291 cl_git_mkfile("testrepo/ab/4.txt", "as you wish");
293 cl_assert(git_fs_path_isfile("testrepo/ab/4.txt"));
296 cl_git_pass(git_ignore_add_rule(g_repo
, "ab/4.txt\n"));
298 cl_git_pass(git_ignore_path_is_ignored(&ignored
, g_repo
, "ab/4.txt"));
299 cl_assert_equal_i(1, ignored
);
301 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/subtrees"));
302 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
304 error
= git_checkout_tree(g_repo
, obj
, &opts
);
306 git_object_free(obj
);
311 void test_checkout_tree__conflict_on_ignored_when_not_overwriting(void)
315 cl_git_fail(error
= checkout_tree_with_blob_ignored_in_workdir(
316 GIT_CHECKOUT_SAFE
| GIT_CHECKOUT_DONT_OVERWRITE_IGNORED
, false));
318 cl_assert_equal_i(GIT_ECONFLICT
, error
);
321 void test_checkout_tree__can_overwrite_ignored_by_default(void)
323 cl_git_pass(checkout_tree_with_blob_ignored_in_workdir(GIT_CHECKOUT_SAFE
, false));
325 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/subtrees"));
327 cl_assert(git_fs_path_isfile("testrepo/ab/4.txt"));
329 assert_on_branch(g_repo
, "subtrees");
332 void test_checkout_tree__conflict_on_ignored_folder_when_not_overwriting(void)
336 cl_git_fail(error
= checkout_tree_with_blob_ignored_in_workdir(
337 GIT_CHECKOUT_SAFE
| GIT_CHECKOUT_DONT_OVERWRITE_IGNORED
, true));
339 cl_assert_equal_i(GIT_ECONFLICT
, error
);
342 void test_checkout_tree__can_overwrite_ignored_folder_by_default(void)
344 cl_git_pass(checkout_tree_with_blob_ignored_in_workdir(GIT_CHECKOUT_SAFE
, true));
346 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/subtrees"));
348 cl_assert(git_fs_path_isfile("testrepo/ab/4.txt"));
350 assert_on_branch(g_repo
, "subtrees");
354 void test_checkout_tree__can_update_only(void)
356 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
358 git_object
*obj
= NULL
;
360 /* first let's get things into a known state - by checkout out the HEAD */
362 assert_on_branch(g_repo
, "master");
364 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
365 cl_git_pass(git_checkout_head(g_repo
, &opts
));
367 cl_assert(!git_fs_path_isdir("testrepo/a"));
369 check_file_contents_nocr("testrepo/branch_file.txt", "hi\nbye!\n");
371 /* now checkout branch but with update only */
373 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
| GIT_CHECKOUT_UPDATE_ONLY
;
375 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
376 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
378 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
379 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/dir"));
381 assert_on_branch(g_repo
, "dir");
383 /* this normally would have been created (which was tested separately in
384 * the test_checkout_tree__can_switch_branches test), but with
385 * UPDATE_ONLY it will not have been created.
387 cl_assert(!git_fs_path_isdir("testrepo/a"));
389 /* but this file still should have been updated */
390 check_file_contents_nocr("testrepo/branch_file.txt", "hi\n");
392 git_object_free(obj
);
395 void test_checkout_tree__can_checkout_with_pattern(void)
397 char *entries
[] = { "[l-z]*.txt" };
399 /* reset to beginning of history (i.e. just a README file) */
401 g_opts
.checkout_strategy
=
402 GIT_CHECKOUT_FORCE
| GIT_CHECKOUT_REMOVE_UNTRACKED
;
404 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "8496071c1b46c854b31185ea97743be6a8774479"));
406 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
408 git_repository_set_head_detached(g_repo
, git_object_id(g_object
)));
410 git_object_free(g_object
);
413 cl_assert(git_fs_path_exists("testrepo/README"));
414 cl_assert(!git_fs_path_exists("testrepo/branch_file.txt"));
415 cl_assert(!git_fs_path_exists("testrepo/link_to_new.txt"));
416 cl_assert(!git_fs_path_exists("testrepo/new.txt"));
418 /* now to a narrow patterned checkout */
420 g_opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
421 g_opts
.paths
.strings
= entries
;
422 g_opts
.paths
.count
= 1;
424 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "refs/heads/master"));
426 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
428 cl_assert(git_fs_path_exists("testrepo/README"));
429 cl_assert(!git_fs_path_exists("testrepo/branch_file.txt"));
430 cl_assert(git_fs_path_exists("testrepo/link_to_new.txt"));
431 cl_assert(git_fs_path_exists("testrepo/new.txt"));
434 void test_checkout_tree__pathlist_checkout_ignores_non_matches(void)
436 char *entries
[] = { "branch_file.txt", "link_to_new.txt" };
438 /* reset to beginning of history (i.e. just a README file) */
440 g_opts
.checkout_strategy
=
441 GIT_CHECKOUT_FORCE
| GIT_CHECKOUT_REMOVE_UNTRACKED
;
443 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "refs/heads/master"));
445 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
446 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/master"));
448 cl_assert(git_fs_path_exists("testrepo/README"));
449 cl_assert(git_fs_path_exists("testrepo/branch_file.txt"));
450 cl_assert(git_fs_path_exists("testrepo/link_to_new.txt"));
451 cl_assert(git_fs_path_exists("testrepo/new.txt"));
453 git_object_free(g_object
);
454 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "8496071c1b46c854b31185ea97743be6a8774479"));
456 g_opts
.checkout_strategy
=
457 GIT_CHECKOUT_FORCE
| GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH
;
458 g_opts
.paths
.strings
= entries
;
459 g_opts
.paths
.count
= 2;
461 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
463 cl_assert(git_fs_path_exists("testrepo/README"));
464 cl_assert(!git_fs_path_exists("testrepo/branch_file.txt"));
465 cl_assert(!git_fs_path_exists("testrepo/link_to_new.txt"));
466 cl_assert(git_fs_path_exists("testrepo/new.txt"));
469 void test_checkout_tree__can_disable_pattern_match(void)
471 char *entries
[] = { "b*.txt" };
473 /* reset to beginning of history (i.e. just a README file) */
475 g_opts
.checkout_strategy
=
476 GIT_CHECKOUT_FORCE
| GIT_CHECKOUT_REMOVE_UNTRACKED
;
478 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "8496071c1b46c854b31185ea97743be6a8774479"));
480 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
482 git_repository_set_head_detached(g_repo
, git_object_id(g_object
)));
484 git_object_free(g_object
);
487 cl_assert(!git_fs_path_isfile("testrepo/branch_file.txt"));
489 /* now to a narrow patterned checkout, but disable pattern */
491 g_opts
.checkout_strategy
=
493 GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH
;
494 g_opts
.paths
.strings
= entries
;
495 g_opts
.paths
.count
= 1;
497 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "refs/heads/master"));
499 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
501 cl_assert(!git_fs_path_isfile("testrepo/branch_file.txt"));
503 /* let's try that again, but allow the pattern match */
505 g_opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
507 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
509 cl_assert(git_fs_path_isfile("testrepo/branch_file.txt"));
512 static void assert_conflict(
513 const char *entry_path
,
514 const char *new_content
,
515 const char *parent_sha
,
516 const char *commit_sha
)
519 git_object
*hack_tree
;
520 git_reference
*branch
, *head
;
521 git_str file_path
= GIT_STR_INIT
;
523 cl_git_pass(git_repository_index(&index
, g_repo
));
525 /* Create a branch pointing at the parent */
526 cl_git_pass(git_revparse_single(&g_object
, g_repo
, parent_sha
));
527 cl_git_pass(git_branch_create(&branch
, g_repo
,
528 "potential_conflict", (git_commit
*)g_object
, 0));
530 /* Make HEAD point to this branch */
531 cl_git_pass(git_reference_symbolic_create(
532 &head
, g_repo
, "HEAD", git_reference_name(branch
), 1, NULL
));
533 git_reference_free(head
);
534 git_reference_free(branch
);
536 /* Checkout the parent */
537 g_opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
538 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
540 /* Hack-ishy workaround to ensure *all* the index entries
541 * match the content of the tree
543 cl_git_pass(git_object_peel(&hack_tree
, g_object
, GIT_OBJECT_TREE
));
544 cl_git_pass(git_index_read_tree(index
, (git_tree
*)hack_tree
));
545 cl_git_pass(git_index_write(index
));
546 git_object_free(hack_tree
);
547 git_object_free(g_object
);
550 /* Create a conflicting file */
551 cl_git_pass(git_str_joinpath(&file_path
, "./testrepo", entry_path
));
552 cl_git_mkfile(git_str_cstr(&file_path
), new_content
);
553 git_str_dispose(&file_path
);
555 /* Trying to checkout the original commit */
556 cl_git_pass(git_revparse_single(&g_object
, g_repo
, commit_sha
));
558 g_opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
560 GIT_ECONFLICT
, git_checkout_tree(g_repo
, g_object
, &g_opts
));
562 /* Stage the conflicting change */
563 cl_git_pass(git_index_add_bypath(index
, entry_path
));
564 cl_git_pass(git_index_write(index
));
565 git_index_free(index
);
568 GIT_ECONFLICT
, git_checkout_tree(g_repo
, g_object
, &g_opts
));
571 void test_checkout_tree__checking_out_a_conflicting_type_change_returns_ECONFLICT(void)
574 * 099faba adds a symlink named 'link_to_new.txt'
575 * a65fedf is the parent of 099faba
578 assert_conflict("link_to_new.txt", "old.txt", "a65fedf", "099faba");
581 void test_checkout_tree__checking_out_a_conflicting_type_change_returns_ECONFLICT_2(void)
584 * cf80f8d adds a directory named 'a/'
585 * a4a7dce is the parent of cf80f8d
588 assert_conflict("a", "hello\n", "a4a7dce", "cf80f8d");
591 void test_checkout_tree__checking_out_a_conflicting_content_change_returns_ECONFLICT(void)
594 * c47800c adds a symlink named 'branch_file.txt'
595 * 5b5b025 is the parent of 763d71a
598 assert_conflict("branch_file.txt", "hello\n", "5b5b025", "c47800c");
601 void test_checkout_tree__donot_update_deleted_file_by_default(void)
603 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
604 git_oid old_id
, new_id
;
605 git_commit
*old_commit
= NULL
, *new_commit
= NULL
;
606 git_index
*index
= NULL
;
609 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
611 memset(&ct
, 0, sizeof(ct
));
612 opts
.notify_flags
= GIT_CHECKOUT_NOTIFY_ALL
;
613 opts
.notify_cb
= checkout_count_callback
;
614 opts
.notify_payload
= &ct
;
616 cl_git_pass(git_repository_index(&index
, g_repo
));
618 cl_git_pass(git_oid_fromstr(&old_id
, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
619 cl_git_pass(git_commit_lookup(&old_commit
, g_repo
, &old_id
));
620 cl_git_pass(git_reset(g_repo
, (git_object
*)old_commit
, GIT_RESET_HARD
, NULL
));
622 cl_git_pass(p_unlink("testrepo/branch_file.txt"));
623 cl_git_pass(git_index_remove_bypath(index
,"branch_file.txt"));
624 cl_git_pass(git_index_write(index
));
626 cl_assert(!git_fs_path_exists("testrepo/branch_file.txt"));
628 cl_git_pass(git_oid_fromstr(&new_id
, "099fabac3a9ea935598528c27f866e34089c2eff"));
629 cl_git_pass(git_commit_lookup(&new_commit
, g_repo
, &new_id
));
632 cl_git_fail(git_checkout_tree(g_repo
, (git_object
*)new_commit
, &opts
));
634 cl_assert_equal_i(1, ct
.n_conflicts
);
635 cl_assert_equal_i(1, ct
.n_updates
);
637 git_commit_free(old_commit
);
638 git_commit_free(new_commit
);
639 git_index_free(index
);
642 struct checkout_cancel_at
{
643 const char *filename
;
648 static int checkout_cancel_cb(
649 git_checkout_notify_t why
,
651 const git_diff_file
*b
,
652 const git_diff_file
*t
,
653 const git_diff_file
*w
,
656 struct checkout_cancel_at
*ca
= payload
;
658 GIT_UNUSED(why
); GIT_UNUSED(b
); GIT_UNUSED(t
); GIT_UNUSED(w
);
662 if (!strcmp(path
, ca
->filename
))
668 void test_checkout_tree__can_cancel_checkout_from_notify(void)
670 struct checkout_cancel_at ca
;
671 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
673 git_object
*obj
= NULL
;
675 assert_on_branch(g_repo
, "master");
677 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
678 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
680 ca
.filename
= "new.txt";
684 opts
.notify_flags
= GIT_CHECKOUT_NOTIFY_UPDATED
;
685 opts
.notify_cb
= checkout_cancel_cb
;
686 opts
.notify_payload
= &ca
;
687 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
689 cl_assert(!git_fs_path_exists("testrepo/new.txt"));
691 cl_git_fail_with(git_checkout_tree(g_repo
, obj
, &opts
), -5555);
693 cl_assert(!git_fs_path_exists("testrepo/new.txt"));
695 /* on case-insensitive FS = a/b.txt, branch_file.txt, new.txt */
696 /* on case-sensitive FS = README, then above */
698 if (git_fs_path_exists("testrepo/.git/CoNfIg")) /* case insensitive */
699 cl_assert_equal_i(3, ca
.count
);
701 cl_assert_equal_i(4, ca
.count
);
703 /* and again with a different stopping point and return code */
704 ca
.filename
= "README";
708 cl_git_fail_with(git_checkout_tree(g_repo
, obj
, &opts
), 123);
710 cl_assert(!git_fs_path_exists("testrepo/new.txt"));
712 if (git_fs_path_exists("testrepo/.git/CoNfIg")) /* case insensitive */
713 cl_assert_equal_i(4, ca
.count
);
715 cl_assert_equal_i(1, ca
.count
);
717 git_object_free(obj
);
720 void test_checkout_tree__can_checkout_with_last_workdir_item_missing(void)
722 git_index
*index
= NULL
;
723 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
724 git_oid tree_id
, commit_id
;
725 git_tree
*tree
= NULL
;
726 git_commit
*commit
= NULL
;
728 git_repository_index(&index
, g_repo
);
730 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
732 cl_git_pass(git_reference_name_to_id(&commit_id
, g_repo
, "refs/heads/master"));
733 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &commit_id
));
735 cl_git_pass(git_checkout_tree(g_repo
, (git_object
*)commit
, &opts
));
736 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/master"));
738 cl_git_pass(p_mkdir("./testrepo/this-is-dir", 0777));
739 cl_git_mkfile("./testrepo/this-is-dir/contained_file", "content\n");
741 cl_git_pass(git_index_add_bypath(index
, "this-is-dir/contained_file"));
742 cl_git_pass(git_index_write(index
));
744 cl_git_pass(git_index_write_tree(&tree_id
, index
));
745 cl_git_pass(git_tree_lookup(&tree
, g_repo
, &tree_id
));
747 cl_git_pass(p_unlink("./testrepo/this-is-dir/contained_file"));
749 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
751 opts
.checkout_strategy
= 1;
752 git_checkout_tree(g_repo
, (git_object
*)tree
, &opts
);
755 git_commit_free(commit
);
756 git_index_free(index
);
759 void test_checkout_tree__issue_1397(void)
761 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
762 const char *partial_oid
= "8a7ef04";
763 git_object
*tree
= NULL
;
765 test_checkout_tree__cleanup(); /* cleanup default checkout */
767 g_repo
= cl_git_sandbox_init("issue_1397");
769 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
771 cl_git_pass(git_revparse_single(&tree
, g_repo
, partial_oid
));
773 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
775 cl_git_pass(git_checkout_tree(g_repo
, tree
, &opts
));
777 check_file_contents("./issue_1397/crlf_file.txt", "first line\r\nsecond line\r\nboth with crlf");
779 git_object_free(tree
);
782 void test_checkout_tree__can_write_to_empty_dirs(void)
784 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
786 git_object
*obj
= NULL
;
788 assert_on_branch(g_repo
, "master");
790 cl_git_pass(p_mkdir("testrepo/a", 0777));
792 /* do first checkout with FORCE because we don't know if testrepo
793 * base data is clean for a checkout or not
795 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
797 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
798 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
800 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
802 cl_assert(git_fs_path_isfile("testrepo/a/b.txt"));
804 git_object_free(obj
);
807 void test_checkout_tree__fails_when_dir_in_use(void)
810 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
812 git_object
*obj
= NULL
;
814 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
816 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
817 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
819 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
821 cl_assert(git_fs_path_isfile("testrepo/a/b.txt"));
823 git_object_free(obj
);
825 cl_git_pass(p_chdir("testrepo/a"));
827 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/master"));
828 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
830 cl_git_fail(git_checkout_tree(g_repo
, obj
, &opts
));
832 cl_git_pass(p_chdir("../.."));
834 cl_assert(git_fs_path_is_empty_dir("testrepo/a"));
836 git_object_free(obj
);
840 void test_checkout_tree__can_continue_when_dir_in_use(void)
843 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
845 git_object
*obj
= NULL
;
847 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
|
848 GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES
;
850 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
851 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
853 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
855 cl_assert(git_fs_path_isfile("testrepo/a/b.txt"));
857 git_object_free(obj
);
859 cl_git_pass(p_chdir("testrepo/a"));
861 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/master"));
862 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
864 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
866 cl_git_pass(p_chdir("../.."));
868 cl_assert(git_fs_path_is_empty_dir("testrepo/a"));
870 git_object_free(obj
);
874 void test_checkout_tree__target_directory_from_bare(void)
876 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
879 memset(&cts
, 0, sizeof(cts
));
881 test_checkout_tree__cleanup(); /* cleanup default checkout */
883 g_repo
= cl_git_sandbox_init("testrepo.git");
884 cl_assert(git_repository_is_bare(g_repo
));
886 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
|
887 GIT_CHECKOUT_RECREATE_MISSING
;
889 opts
.notify_flags
= GIT_CHECKOUT_NOTIFY_ALL
;
890 opts
.notify_cb
= checkout_count_callback
;
891 opts
.notify_payload
= &cts
;
893 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "HEAD"));
894 cl_git_pass(git_object_lookup(&g_object
, g_repo
, &oid
, GIT_OBJECT_ANY
));
896 cl_git_fail(git_checkout_tree(g_repo
, g_object
, &opts
));
898 opts
.target_directory
= "alternative";
899 cl_assert(!git_fs_path_isdir("alternative"));
901 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &opts
));
903 cl_assert_equal_i(0, cts
.n_untracked
);
904 cl_assert_equal_i(0, cts
.n_ignored
);
905 cl_assert_equal_i(3, cts
.n_updates
);
907 check_file_contents_nocr("./alternative/README", "hey there\n");
908 check_file_contents_nocr("./alternative/branch_file.txt", "hi\nbye!\n");
909 check_file_contents_nocr("./alternative/new.txt", "my new file\n");
911 cl_git_pass(git_futils_rmdir_r(
912 "alternative", NULL
, GIT_RMDIR_REMOVE_FILES
));
915 void test_checkout_tree__extremely_long_file_name(void)
917 /* A utf-8 string with 83 characters, but 249 bytes. */
918 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";
919 char path
[1024] = {0};
921 g_opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
922 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "long-file-name"));
923 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
925 sprintf(path
, "testrepo/%s.txt", longname
);
926 cl_assert(git_fs_path_exists(path
));
928 git_object_free(g_object
);
929 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "master"));
930 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
931 cl_assert(!git_fs_path_exists(path
));
934 static void create_conflict(const char *path
)
937 git_index_entry entry
;
939 cl_git_pass(git_repository_index(&index
, g_repo
));
941 memset(&entry
, 0x0, sizeof(git_index_entry
));
942 entry
.mode
= 0100644;
943 GIT_INDEX_ENTRY_STAGE_SET(&entry
, 1);
944 git_oid_fromstr(&entry
.id
, "d427e0b2e138501a3d15cc376077a3631e15bd46");
946 cl_git_pass(git_index_add(index
, &entry
));
948 GIT_INDEX_ENTRY_STAGE_SET(&entry
, 2);
949 git_oid_fromstr(&entry
.id
, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf");
950 cl_git_pass(git_index_add(index
, &entry
));
952 GIT_INDEX_ENTRY_STAGE_SET(&entry
, 3);
953 git_oid_fromstr(&entry
.id
, "2bd0a343aeef7a2cf0d158478966a6e587ff3863");
954 cl_git_pass(git_index_add(index
, &entry
));
956 cl_git_pass(git_index_write(index
));
957 git_index_free(index
);
960 void test_checkout_tree__fails_when_conflicts_exist_in_index(void)
962 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
964 git_object
*obj
= NULL
;
966 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
968 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "HEAD"));
969 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
971 create_conflict("conflicts.txt");
973 cl_git_fail(git_checkout_tree(g_repo
, obj
, &opts
));
975 git_object_free(obj
);
978 void test_checkout_tree__filemode_preserved_in_index(void)
980 git_oid executable_oid
;
982 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
984 const git_index_entry
*entry
;
986 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
988 cl_git_pass(git_repository_index(&index
, g_repo
));
990 /* test a freshly added executable */
991 cl_git_pass(git_oid_fromstr(&executable_oid
, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6"));
992 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &executable_oid
));
994 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
995 cl_assert(entry
= git_index_get_bypath(index
, "executable.txt", 0));
996 cl_assert(GIT_PERMS_IS_EXEC(entry
->mode
));
998 git_commit_free(commit
);
1001 /* Now start with a commit which has a text file */
1002 cl_git_pass(git_oid_fromstr(&executable_oid
, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9"));
1003 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &executable_oid
));
1005 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1006 cl_assert(entry
= git_index_get_bypath(index
, "a/b.txt", 0));
1007 cl_assert(!GIT_PERMS_IS_EXEC(entry
->mode
));
1009 git_commit_free(commit
);
1012 /* And then check out to a commit which converts the text file to an executable */
1013 cl_git_pass(git_oid_fromstr(&executable_oid
, "144344043ba4d4a405da03de3844aa829ae8be0e"));
1014 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &executable_oid
));
1016 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1017 cl_assert(entry
= git_index_get_bypath(index
, "a/b.txt", 0));
1018 cl_assert(GIT_PERMS_IS_EXEC(entry
->mode
));
1020 git_commit_free(commit
);
1023 /* Finally, check out the text file again and check that the exec bit is cleared */
1024 cl_git_pass(git_oid_fromstr(&executable_oid
, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9"));
1025 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &executable_oid
));
1027 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1028 cl_assert(entry
= git_index_get_bypath(index
, "a/b.txt", 0));
1029 cl_assert(!GIT_PERMS_IS_EXEC(entry
->mode
));
1031 git_commit_free(commit
);
1034 git_index_free(index
);
1038 static mode_t
read_filemode(const char *path
)
1040 git_str fullpath
= GIT_STR_INIT
;
1044 git_str_joinpath(&fullpath
, "testrepo", path
);
1045 cl_must_pass(p_stat(fullpath
.ptr
, &st
));
1047 result
= GIT_PERMS_IS_EXEC(st
.st_mode
) ?
1048 GIT_FILEMODE_BLOB_EXECUTABLE
: GIT_FILEMODE_BLOB
;
1050 git_str_dispose(&fullpath
);
1056 void test_checkout_tree__filemode_preserved_in_workdir(void)
1059 git_oid executable_oid
;
1061 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1063 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
1065 /* test a freshly added executable */
1066 cl_git_pass(git_oid_fromstr(&executable_oid
, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6"));
1067 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &executable_oid
));
1069 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1070 cl_assert(GIT_PERMS_IS_EXEC(read_filemode("executable.txt")));
1072 git_commit_free(commit
);
1075 /* Now start with a commit which has a text file */
1076 cl_git_pass(git_oid_fromstr(&executable_oid
, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9"));
1077 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &executable_oid
));
1079 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1080 cl_assert(!GIT_PERMS_IS_EXEC(read_filemode("a/b.txt")));
1082 git_commit_free(commit
);
1085 /* And then check out to a commit which converts the text file to an executable */
1086 cl_git_pass(git_oid_fromstr(&executable_oid
, "144344043ba4d4a405da03de3844aa829ae8be0e"));
1087 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &executable_oid
));
1089 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1090 cl_assert(GIT_PERMS_IS_EXEC(read_filemode("a/b.txt")));
1092 git_commit_free(commit
);
1095 /* Finally, check out the text file again and check that the exec bit is cleared */
1096 cl_git_pass(git_oid_fromstr(&executable_oid
, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9"));
1097 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &executable_oid
));
1099 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1100 cl_assert(!GIT_PERMS_IS_EXEC(read_filemode("a/b.txt")));
1102 git_commit_free(commit
);
1108 void test_checkout_tree__removes_conflicts(void)
1112 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1115 cl_git_pass(git_oid_fromstr(&commit_id
, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6"));
1116 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &commit_id
));
1118 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
1120 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1122 cl_git_pass(git_repository_index(&index
, g_repo
));
1123 cl_git_pass(git_index_remove(index
, "executable.txt", 0));
1125 create_conflict("executable.txt");
1126 cl_git_mkfile("testrepo/executable.txt", "This is the conflict file.\n");
1128 create_conflict("other.txt");
1129 cl_git_mkfile("testrepo/other.txt", "This is another conflict file.\n");
1131 cl_git_pass(git_index_write(index
));
1133 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1135 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "executable.txt", 1));
1136 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "executable.txt", 2));
1137 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "executable.txt", 3));
1139 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "other.txt", 1));
1140 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "other.txt", 2));
1141 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "other.txt", 3));
1143 cl_assert(!git_fs_path_exists("testrepo/other.txt"));
1145 git_commit_free(commit
);
1146 git_index_free(index
);
1150 void test_checkout_tree__removes_conflicts_only_by_pathscope(void)
1154 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1156 const char *path
= "executable.txt";
1158 cl_git_pass(git_oid_fromstr(&commit_id
, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6"));
1159 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &commit_id
));
1161 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
1162 opts
.paths
.count
= 1;
1163 opts
.paths
.strings
= (char **)&path
;
1165 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1167 cl_git_pass(git_repository_index(&index
, g_repo
));
1168 cl_git_pass(git_index_remove(index
, "executable.txt", 0));
1170 create_conflict("executable.txt");
1171 cl_git_mkfile("testrepo/executable.txt", "This is the conflict file.\n");
1173 create_conflict("other.txt");
1174 cl_git_mkfile("testrepo/other.txt", "This is another conflict file.\n");
1176 cl_git_pass(git_index_write(index
));
1178 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1180 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "executable.txt", 1));
1181 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "executable.txt", 2));
1182 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "executable.txt", 3));
1184 cl_assert(git_index_get_bypath(index
, "other.txt", 1) != NULL
);
1185 cl_assert(git_index_get_bypath(index
, "other.txt", 2) != NULL
);
1186 cl_assert(git_index_get_bypath(index
, "other.txt", 3) != NULL
);
1188 cl_assert(git_fs_path_exists("testrepo/other.txt"));
1190 git_commit_free(commit
);
1191 git_index_free(index
);
1194 void test_checkout_tree__case_changing_rename(void)
1197 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1198 git_oid master_id
, dir_commit_id
, tree_id
, commit_id
;
1199 git_commit
*master_commit
, *dir_commit
;
1201 git_signature
*signature
;
1202 const git_index_entry
*index_entry
;
1203 bool case_sensitive
;
1205 assert_on_branch(g_repo
, "master");
1207 cl_git_pass(git_repository_index(&index
, g_repo
));
1209 /* Switch branches and perform a case-changing rename */
1211 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
1213 cl_git_pass(git_reference_name_to_id(&dir_commit_id
, g_repo
, "refs/heads/dir"));
1214 cl_git_pass(git_commit_lookup(&dir_commit
, g_repo
, &dir_commit_id
));
1216 cl_git_pass(git_checkout_tree(g_repo
, (git_object
*)dir_commit
, &opts
));
1217 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/dir"));
1219 cl_assert(git_fs_path_isfile("testrepo/README"));
1220 case_sensitive
= !git_fs_path_isfile("testrepo/readme");
1222 cl_assert(index_entry
= git_index_get_bypath(index
, "README", 0));
1223 cl_assert_equal_s("README", index_entry
->path
);
1225 cl_git_pass(git_index_remove_bypath(index
, "README"));
1226 cl_git_pass(p_rename("testrepo/README", "testrepo/__readme__"));
1227 cl_git_pass(p_rename("testrepo/__readme__", "testrepo/readme"));
1228 cl_git_append2file("testrepo/readme", "An addendum...");
1229 cl_git_pass(git_index_add_bypath(index
, "readme"));
1231 cl_git_pass(git_index_write(index
));
1233 cl_git_pass(git_index_write_tree(&tree_id
, index
));
1234 cl_git_pass(git_tree_lookup(&tree
, g_repo
, &tree_id
));
1236 cl_git_pass(git_signature_new(&signature
, "Renamer", "rename@contoso.com", time(NULL
), 0));
1238 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
));
1240 cl_assert(git_fs_path_isfile("testrepo/readme"));
1242 cl_assert(!git_fs_path_isfile("testrepo/README"));
1244 cl_assert(index_entry
= git_index_get_bypath(index
, "readme", 0));
1245 cl_assert_equal_s("readme", index_entry
->path
);
1247 /* Switching back to master should rename readme -> README */
1248 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
1250 cl_git_pass(git_reference_name_to_id(&master_id
, g_repo
, "refs/heads/master"));
1251 cl_git_pass(git_commit_lookup(&master_commit
, g_repo
, &master_id
));
1253 cl_git_pass(git_checkout_tree(g_repo
, (git_object
*)master_commit
, &opts
));
1254 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/master"));
1256 assert_on_branch(g_repo
, "master");
1258 cl_assert(git_fs_path_isfile("testrepo/README"));
1260 cl_assert(!git_fs_path_isfile("testrepo/readme"));
1262 cl_assert(index_entry
= git_index_get_bypath(index
, "README", 0));
1263 cl_assert_equal_s("README", index_entry
->path
);
1265 git_index_free(index
);
1266 git_signature_free(signature
);
1267 git_tree_free(tree
);
1268 git_commit_free(dir_commit
);
1269 git_commit_free(master_commit
);
1272 static void perfdata_cb(const git_checkout_perfdata
*in
, void *payload
)
1274 memcpy(payload
, in
, sizeof(git_checkout_perfdata
));
1277 void test_checkout_tree__can_collect_perfdata(void)
1279 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1281 git_object
*obj
= NULL
;
1282 git_checkout_perfdata perfdata
= {0};
1284 opts
.perfdata_cb
= perfdata_cb
;
1285 opts
.perfdata_payload
= &perfdata
;
1287 assert_on_branch(g_repo
, "master");
1288 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
1290 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
1291 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
1293 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
1295 cl_assert(perfdata
.mkdir_calls
> 0);
1296 cl_assert(perfdata
.stat_calls
> 0);
1298 git_object_free(obj
);
1301 static void update_attr_callback(
1303 size_t completed_steps
,
1307 GIT_UNUSED(completed_steps
);
1308 GIT_UNUSED(total_steps
);
1309 GIT_UNUSED(payload
);
1311 if (path
&& strcmp(path
, "ident1.txt") == 0)
1312 cl_git_write2file("testrepo/.gitattributes",
1313 "*.txt ident\n", 12, O_RDWR
|O_CREAT
, 0666);
1316 void test_checkout_tree__caches_attributes_during_checkout(void)
1318 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1320 git_object
*obj
= NULL
;
1321 git_str ident1
= GIT_STR_INIT
, ident2
= GIT_STR_INIT
;
1322 char *ident_paths
[] = { "ident1.txt", "ident2.txt" };
1324 opts
.progress_cb
= update_attr_callback
;
1326 assert_on_branch(g_repo
, "master");
1327 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
1328 opts
.paths
.strings
= ident_paths
;
1329 opts
.paths
.count
= 2;
1331 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/ident"));
1332 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
1334 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
1336 cl_git_pass(git_futils_readbuffer(&ident1
, "testrepo/ident1.txt"));
1337 cl_git_pass(git_futils_readbuffer(&ident2
, "testrepo/ident2.txt"));
1339 cl_assert_equal_strn(ident1
.ptr
, "# $Id$", 6);
1340 cl_assert_equal_strn(ident2
.ptr
, "# $Id$", 6);
1342 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
1344 cl_git_pass(git_futils_readbuffer(&ident1
, "testrepo/ident1.txt"));
1345 cl_git_pass(git_futils_readbuffer(&ident2
, "testrepo/ident2.txt"));
1347 cl_assert_equal_strn(ident1
.ptr
, "# $Id: ", 7);
1348 cl_assert_equal_strn(ident2
.ptr
, "# $Id: ", 7);
1350 git_str_dispose(&ident1
);
1351 git_str_dispose(&ident2
);
1352 git_object_free(obj
);
1355 void test_checkout_tree__can_not_update_index(void)
1359 unsigned int status
;
1360 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1363 opts
.checkout_strategy
|=
1364 GIT_CHECKOUT_FORCE
| GIT_CHECKOUT_DONT_UPDATE_INDEX
;
1366 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "HEAD"));
1367 cl_git_pass(git_object_lookup(&head
, g_repo
, &oid
, GIT_OBJECT_ANY
));
1369 cl_git_pass(git_reset(g_repo
, head
, GIT_RESET_HARD
, &g_opts
));
1371 cl_assert_equal_i(false, git_fs_path_isdir("./testrepo/ab/"));
1373 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "subtrees"));
1375 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &opts
));
1377 cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/ab/de/2.txt"));
1378 cl_git_pass(git_status_file(&status
, g_repo
, "ab/de/2.txt"));
1379 cl_assert_equal_i(GIT_STATUS_WT_NEW
, status
);
1381 cl_git_pass(git_repository_index(&index
, g_repo
));
1382 cl_git_pass(git_index_write(index
));
1384 cl_git_pass(git_status_file(&status
, g_repo
, "ab/de/2.txt"));
1385 cl_assert_equal_i(GIT_STATUS_WT_NEW
, status
);
1387 git_object_free(head
);
1388 git_index_free(index
);
1391 void test_checkout_tree__can_update_but_not_write_index(void)
1395 unsigned int status
;
1396 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1398 git_repository
*other
;
1400 opts
.checkout_strategy
|=
1401 GIT_CHECKOUT_FORCE
| GIT_CHECKOUT_DONT_WRITE_INDEX
;
1403 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "HEAD"));
1404 cl_git_pass(git_object_lookup(&head
, g_repo
, &oid
, GIT_OBJECT_ANY
));
1406 cl_git_pass(git_reset(g_repo
, head
, GIT_RESET_HARD
, &g_opts
));
1408 cl_assert_equal_i(false, git_fs_path_isdir("./testrepo/ab/"));
1410 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "subtrees"));
1412 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &opts
));
1414 cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/ab/de/2.txt"));
1415 cl_git_pass(git_status_file(&status
, g_repo
, "ab/de/2.txt"));
1416 cl_assert_equal_i(GIT_STATUS_INDEX_NEW
, status
);
1418 cl_git_pass(git_repository_open(&other
, "testrepo"));
1419 cl_git_pass(git_status_file(&status
, other
, "ab/de/2.txt"));
1420 cl_assert_equal_i(GIT_STATUS_WT_NEW
, status
);
1421 git_repository_free(other
);
1423 cl_git_pass(git_repository_index(&index
, g_repo
));
1424 cl_git_pass(git_index_write(index
));
1426 cl_git_pass(git_repository_open(&other
, "testrepo"));
1427 cl_git_pass(git_status_file(&status
, other
, "ab/de/2.txt"));
1428 cl_assert_equal_i(GIT_STATUS_INDEX_NEW
, status
);
1429 git_repository_free(other
);
1431 git_object_free(head
);
1432 git_index_free(index
);
1435 /* Emulate checking out in a repo created by clone --no-checkout,
1436 * which would not have written an index. */
1437 void test_checkout_tree__safe_proceeds_if_no_index(void)
1439 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1441 git_object
*obj
= NULL
;
1443 assert_on_branch(g_repo
, "master");
1444 cl_must_pass(p_unlink("testrepo/.git/index"));
1446 /* do second checkout safe because we should be clean after first */
1447 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
1449 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/subtrees"));
1450 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
1452 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
1453 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/subtrees"));
1455 cl_assert(git_fs_path_isfile("testrepo/README"));
1456 cl_assert(git_fs_path_isfile("testrepo/branch_file.txt"));
1457 cl_assert(git_fs_path_isfile("testrepo/new.txt"));
1458 cl_assert(git_fs_path_isfile("testrepo/ab/4.txt"));
1459 cl_assert(git_fs_path_isfile("testrepo/ab/c/3.txt"));
1460 cl_assert(git_fs_path_isfile("testrepo/ab/de/2.txt"));
1461 cl_assert(git_fs_path_isfile("testrepo/ab/de/fgh/1.txt"));
1463 cl_assert(!git_fs_path_isdir("testrepo/a"));
1465 assert_on_branch(g_repo
, "subtrees");
1467 git_object_free(obj
);
1470 static int checkout_conflict_count_cb(
1471 git_checkout_notify_t why
,
1473 const git_diff_file
*b
,
1474 const git_diff_file
*t
,
1475 const git_diff_file
*w
,
1478 size_t *n
= payload
;
1491 /* A repo that has a HEAD (even a properly born HEAD that peels to
1492 * a commit) but no index should be treated as if it's an empty baseline
1494 void test_checkout_tree__baseline_is_empty_when_no_index(void)
1496 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1497 git_reference
*head
;
1499 size_t conflicts
= 0;
1501 assert_on_branch(g_repo
, "master");
1503 cl_git_pass(git_repository_head(&head
, g_repo
));
1504 cl_git_pass(git_reference_peel(&obj
, head
, GIT_OBJECT_COMMIT
));
1506 cl_git_pass(git_reset(g_repo
, obj
, GIT_RESET_HARD
, NULL
));
1508 cl_must_pass(p_unlink("testrepo/.git/index"));
1510 /* for a safe checkout, we should have checkout conflicts with
1511 * the existing untracked files.
1513 opts
.checkout_strategy
&= ~GIT_CHECKOUT_FORCE
;
1514 opts
.notify_flags
= GIT_CHECKOUT_NOTIFY_CONFLICT
;
1515 opts
.notify_cb
= checkout_conflict_count_cb
;
1516 opts
.notify_payload
= &conflicts
;
1518 cl_git_fail_with(GIT_ECONFLICT
, git_checkout_tree(g_repo
, obj
, &opts
));
1519 cl_assert_equal_i(4, conflicts
);
1521 /* but force should succeed and update the index */
1522 opts
.checkout_strategy
|= GIT_CHECKOUT_FORCE
;
1523 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
1525 assert_status_entrycount(g_repo
, 0);
1527 git_object_free(obj
);
1528 git_reference_free(head
);
1531 void test_checkout_tree__mode_change_is_force_updated(void)
1534 git_reference
*head
;
1537 if (!cl_is_chmod_supported())
1540 assert_on_branch(g_repo
, "master");
1541 cl_git_pass(git_repository_index(&index
, g_repo
));
1542 cl_git_pass(git_repository_head(&head
, g_repo
));
1543 cl_git_pass(git_reference_peel(&obj
, head
, GIT_OBJECT_COMMIT
));
1545 cl_git_pass(git_reset(g_repo
, obj
, GIT_RESET_HARD
, NULL
));
1546 assert_status_entrycount(g_repo
, 0);
1548 /* update the mode on-disk */
1549 cl_must_pass(p_chmod("testrepo/README", 0755));
1551 assert_status_entrycount(g_repo
, 1);
1552 cl_git_pass(git_checkout_tree(g_repo
, obj
, &g_opts
));
1553 assert_status_entrycount(g_repo
, 0);
1555 /* update the mode on-disk and in the index */
1556 cl_must_pass(p_chmod("testrepo/README", 0755));
1557 cl_must_pass(git_index_add_bypath(index
, "README"));
1559 cl_git_pass(git_index_write(index
));
1560 assert_status_entrycount(g_repo
, 1);
1562 cl_git_pass(git_checkout_tree(g_repo
, obj
, &g_opts
));
1563 cl_git_pass(git_index_write(index
));
1565 assert_status_entrycount(g_repo
, 0);
1567 git_object_free(obj
);
1568 git_reference_free(head
);
1569 git_index_free(index
);
1572 void test_checkout_tree__nullopts(void)
1574 cl_git_pass(git_checkout_tree(g_repo
, NULL
, NULL
));
1577 static void modify_index_ondisk(void)
1579 git_repository
*other_repo
;
1580 git_index
*other_index
;
1581 git_index_entry entry
= {{0}};
1583 cl_git_pass(git_repository_open(&other_repo
, git_repository_workdir(g_repo
)));
1584 cl_git_pass(git_repository_index(&other_index
, other_repo
));
1586 cl_git_pass(git_oid_fromstr(&entry
.id
, "1385f264afb75a56a5bec74243be9b367ba4ca08"));
1587 entry
.mode
= 0100644;
1588 entry
.path
= "README";
1590 cl_git_pass(git_index_add(other_index
, &entry
));
1591 cl_git_pass(git_index_write(other_index
));
1593 git_index_free(other_index
);
1594 git_repository_free(other_repo
);
1597 static void modify_index_and_checkout_tree(git_checkout_options
*opts
)
1600 git_reference
*head
;
1603 /* External changes to the index are maintained by default */
1604 cl_git_pass(git_repository_index(&index
, g_repo
));
1605 cl_git_pass(git_repository_head(&head
, g_repo
));
1606 cl_git_pass(git_reference_peel(&obj
, head
, GIT_OBJECT_COMMIT
));
1608 cl_git_pass(git_reset(g_repo
, obj
, GIT_RESET_HARD
, NULL
));
1609 assert_status_entrycount(g_repo
, 0);
1611 modify_index_ondisk();
1613 /* The file in the index remains modified */
1614 cl_git_pass(git_checkout_tree(g_repo
, obj
, opts
));
1616 git_object_free(obj
);
1617 git_reference_free(head
);
1618 git_index_free(index
);
1621 void test_checkout_tree__retains_external_index_changes(void)
1623 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1625 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
1627 modify_index_and_checkout_tree(&opts
);
1628 assert_status_entrycount(g_repo
, 1);
1631 void test_checkout_tree__no_index_refresh(void)
1633 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1635 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
| GIT_CHECKOUT_NO_REFRESH
;
1637 modify_index_and_checkout_tree(&opts
);
1638 assert_status_entrycount(g_repo
, 0);
1641 void test_checkout_tree__dry_run(void)
1643 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1645 git_object
*obj
= NULL
;
1648 /* first let's get things into a known state - by checkout out the HEAD */
1650 assert_on_branch(g_repo
, "master");
1652 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
1653 cl_git_pass(git_checkout_head(g_repo
, &opts
));
1655 cl_assert(!git_fs_path_isdir("testrepo/a"));
1657 check_file_contents_nocr("testrepo/branch_file.txt", "hi\nbye!\n");
1659 /* now checkout branch but with dry run enabled */
1661 memset(&ct
, 0, sizeof(ct
));
1662 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
| GIT_CHECKOUT_DRY_RUN
;
1663 opts
.notify_flags
= GIT_CHECKOUT_NOTIFY_ALL
;
1664 opts
.notify_cb
= checkout_count_callback
;
1665 opts
.notify_payload
= &ct
;
1667 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
1668 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
1670 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
1671 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/dir"));
1673 assert_on_branch(g_repo
, "dir");
1675 /* these normally would have been created and updated, but with
1676 * DRY_RUN they will be unchanged.
1678 cl_assert(!git_fs_path_isdir("testrepo/a"));
1679 check_file_contents_nocr("testrepo/branch_file.txt", "hi\nbye!\n");
1681 /* check that notify callback was invoked */
1682 cl_assert_equal_i(ct
.n_updates
, 2);
1684 git_object_free(obj
);