1 #include "clar_libgit2.h"
2 #include "checkout_helpers.h"
4 #include "git2/checkout.h"
5 #include "repository.h"
9 static git_repository
*g_repo
;
10 static git_checkout_options g_opts
;
11 static git_object
*g_object
;
13 static void assert_status_entrycount(git_repository
*repo
, size_t count
)
15 git_status_list
*status
;
17 cl_git_pass(git_status_list_new(&status
, repo
, NULL
));
18 cl_assert_equal_i(count
, git_status_list_entrycount(status
));
20 git_status_list_free(status
);
23 void test_checkout_tree__initialize(void)
25 g_repo
= cl_git_sandbox_init("testrepo");
27 GIT_INIT_STRUCTURE(&g_opts
, GIT_CHECKOUT_OPTIONS_VERSION
);
28 g_opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
31 void test_checkout_tree__cleanup(void)
33 git_object_free(g_object
);
36 cl_git_sandbox_cleanup();
38 if (git_path_isdir("alternative"))
39 git_futils_rmdir_r("alternative", NULL
, GIT_RMDIR_REMOVE_FILES
);
42 void test_checkout_tree__cannot_checkout_a_non_treeish(void)
45 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"));
46 cl_git_fail(git_checkout_tree(g_repo
, g_object
, NULL
));
49 void test_checkout_tree__can_checkout_a_subdirectory_from_a_commit(void)
51 char *entries
[] = { "ab/de/" };
53 g_opts
.paths
.strings
= entries
;
54 g_opts
.paths
.count
= 1;
56 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "subtrees"));
58 cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
60 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
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"));
66 void test_checkout_tree__can_checkout_and_remove_directory(void)
68 cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
70 /* Checkout brach "subtrees" and update HEAD, so that HEAD matches the
71 * current working tree
73 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "subtrees"));
74 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
76 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/subtrees"));
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"));
82 git_object_free(g_object
);
85 /* Checkout brach "master" and update HEAD, so that HEAD matches the
86 * current working tree
88 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "master"));
89 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
91 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/master"));
93 /* This directory should no longer exist */
94 cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
97 void test_checkout_tree__can_checkout_a_subdirectory_from_a_subtree(void)
99 char *entries
[] = { "de/" };
101 g_opts
.paths
.strings
= entries
;
102 g_opts
.paths
.count
= 1;
104 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "subtrees:ab"));
106 cl_assert_equal_i(false, git_path_isdir("./testrepo/de/"));
108 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
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"));
114 static void progress(const char *path
, size_t cur
, size_t tot
, void *payload
)
116 bool *was_called
= (bool*)payload
;
117 GIT_UNUSED(path
); GIT_UNUSED(cur
); GIT_UNUSED(tot
);
121 void test_checkout_tree__calls_progress_callback(void)
125 g_opts
.progress_cb
= progress
;
126 g_opts
.progress_payload
= &was_called
;
128 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "master"));
130 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
132 cl_assert_equal_i(was_called
, true);
135 void test_checkout_tree__doesnt_write_unrequested_files_to_worktree(void)
139 git_commit
* p_master_commit
;
140 git_commit
* p_chomped_commit
;
141 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
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
));
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.
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"));
155 git_commit_free(p_master_commit
);
156 git_commit_free(p_chomped_commit
);
159 void test_checkout_tree__can_switch_branches(void)
161 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
163 git_object
*obj
= NULL
;
165 assert_on_branch(g_repo
, "master");
167 /* do first checkout with FORCE because we don't know if testrepo
168 * base data is clean for a checkout or not
170 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
172 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
173 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
175 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
176 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/dir"));
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"));
183 cl_assert(!git_path_isdir("testrepo/ab"));
185 assert_on_branch(g_repo
, "dir");
187 git_object_free(obj
);
189 /* do second checkout safe because we should be clean after first */
190 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
192 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/subtrees"));
193 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
195 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
196 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/subtrees"));
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"));
206 cl_assert(!git_path_isdir("testrepo/a"));
208 assert_on_branch(g_repo
, "subtrees");
210 git_object_free(obj
);
213 void test_checkout_tree__can_remove_untracked(void)
215 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
217 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
| GIT_CHECKOUT_REMOVE_UNTRACKED
;
219 cl_git_mkfile("testrepo/untracked_file", "as you wish");
220 cl_assert(git_path_isfile("testrepo/untracked_file"));
222 cl_git_pass(git_checkout_head(g_repo
, &opts
));
224 cl_assert(!git_path_isfile("testrepo/untracked_file"));
227 void test_checkout_tree__can_remove_ignored(void)
229 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
232 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
| GIT_CHECKOUT_REMOVE_IGNORED
;
234 cl_git_mkfile("testrepo/ignored_file", "as you wish");
236 cl_git_pass(git_ignore_add_rule(g_repo
, "ignored_file\n"));
238 cl_git_pass(git_ignore_path_is_ignored(&ignored
, g_repo
, "ignored_file"));
239 cl_assert_equal_i(1, ignored
);
241 cl_assert(git_path_isfile("testrepo/ignored_file"));
243 cl_git_pass(git_checkout_head(g_repo
, &opts
));
245 cl_assert(!git_path_isfile("testrepo/ignored_file"));
248 static int checkout_tree_with_blob_ignored_in_workdir(int strategy
, bool isdir
)
251 git_object
*obj
= NULL
;
252 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
253 int ignored
= 0, error
;
255 assert_on_branch(g_repo
, "master");
257 /* do first checkout with FORCE because we don't know if testrepo
258 * base data is clean for a checkout or not
260 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
262 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
263 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
265 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
266 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/dir"));
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"));
273 cl_assert(!git_path_isdir("testrepo/ab"));
275 assert_on_branch(g_repo
, "dir");
277 git_object_free(obj
);
279 opts
.checkout_strategy
= strategy
;
282 cl_must_pass(p_mkdir("testrepo/ab", 0777));
283 cl_must_pass(p_mkdir("testrepo/ab/4.txt", 0777));
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");
289 cl_assert(git_path_isdir("testrepo/ab/4.txt"));
291 cl_must_pass(p_mkdir("testrepo/ab", 0777));
292 cl_git_mkfile("testrepo/ab/4.txt", "as you wish");
294 cl_assert(git_path_isfile("testrepo/ab/4.txt"));
297 cl_git_pass(git_ignore_add_rule(g_repo
, "ab/4.txt\n"));
299 cl_git_pass(git_ignore_path_is_ignored(&ignored
, g_repo
, "ab/4.txt"));
300 cl_assert_equal_i(1, ignored
);
302 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/subtrees"));
303 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
305 error
= git_checkout_tree(g_repo
, obj
, &opts
);
307 git_object_free(obj
);
312 void test_checkout_tree__conflict_on_ignored_when_not_overwriting(void)
316 cl_git_fail(error
= checkout_tree_with_blob_ignored_in_workdir(
317 GIT_CHECKOUT_SAFE
| GIT_CHECKOUT_DONT_OVERWRITE_IGNORED
, false));
319 cl_assert_equal_i(GIT_ECONFLICT
, error
);
322 void test_checkout_tree__can_overwrite_ignored_by_default(void)
324 cl_git_pass(checkout_tree_with_blob_ignored_in_workdir(GIT_CHECKOUT_SAFE
, false));
326 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/subtrees"));
328 cl_assert(git_path_isfile("testrepo/ab/4.txt"));
330 assert_on_branch(g_repo
, "subtrees");
333 void test_checkout_tree__conflict_on_ignored_folder_when_not_overwriting(void)
337 cl_git_fail(error
= checkout_tree_with_blob_ignored_in_workdir(
338 GIT_CHECKOUT_SAFE
| GIT_CHECKOUT_DONT_OVERWRITE_IGNORED
, true));
340 cl_assert_equal_i(GIT_ECONFLICT
, error
);
343 void test_checkout_tree__can_overwrite_ignored_folder_by_default(void)
345 cl_git_pass(checkout_tree_with_blob_ignored_in_workdir(GIT_CHECKOUT_SAFE
, true));
347 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/subtrees"));
349 cl_assert(git_path_isfile("testrepo/ab/4.txt"));
351 assert_on_branch(g_repo
, "subtrees");
355 void test_checkout_tree__can_update_only(void)
357 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
359 git_object
*obj
= NULL
;
361 /* first let's get things into a known state - by checkout out the HEAD */
363 assert_on_branch(g_repo
, "master");
365 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
366 cl_git_pass(git_checkout_head(g_repo
, &opts
));
368 cl_assert(!git_path_isdir("testrepo/a"));
370 check_file_contents_nocr("testrepo/branch_file.txt", "hi\nbye!\n");
372 /* now checkout branch but with update only */
374 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
| GIT_CHECKOUT_UPDATE_ONLY
;
376 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
377 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
379 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
380 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/dir"));
382 assert_on_branch(g_repo
, "dir");
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.
388 cl_assert(!git_path_isdir("testrepo/a"));
390 /* but this file still should have been updated */
391 check_file_contents_nocr("testrepo/branch_file.txt", "hi\n");
393 git_object_free(obj
);
396 void test_checkout_tree__can_checkout_with_pattern(void)
398 char *entries
[] = { "[l-z]*.txt" };
400 /* reset to beginning of history (i.e. just a README file) */
402 g_opts
.checkout_strategy
=
403 GIT_CHECKOUT_FORCE
| GIT_CHECKOUT_REMOVE_UNTRACKED
;
405 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "8496071c1b46c854b31185ea97743be6a8774479"));
407 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
409 git_repository_set_head_detached(g_repo
, git_object_id(g_object
)));
411 git_object_free(g_object
);
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"));
419 /* now to a narrow patterned checkout */
421 g_opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
422 g_opts
.paths
.strings
= entries
;
423 g_opts
.paths
.count
= 1;
425 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "refs/heads/master"));
427 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
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"));
435 void test_checkout_tree__pathlist_checkout_ignores_non_matches(void)
437 char *entries
[] = { "branch_file.txt", "link_to_new.txt" };
439 /* reset to beginning of history (i.e. just a README file) */
441 g_opts
.checkout_strategy
=
442 GIT_CHECKOUT_FORCE
| GIT_CHECKOUT_REMOVE_UNTRACKED
;
444 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "refs/heads/master"));
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"));
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"));
454 git_object_free(g_object
);
455 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "8496071c1b46c854b31185ea97743be6a8774479"));
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;
462 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
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"));
470 void test_checkout_tree__can_disable_pattern_match(void)
472 char *entries
[] = { "b*.txt" };
474 /* reset to beginning of history (i.e. just a README file) */
476 g_opts
.checkout_strategy
=
477 GIT_CHECKOUT_FORCE
| GIT_CHECKOUT_REMOVE_UNTRACKED
;
479 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "8496071c1b46c854b31185ea97743be6a8774479"));
481 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
483 git_repository_set_head_detached(g_repo
, git_object_id(g_object
)));
485 git_object_free(g_object
);
488 cl_assert(!git_path_isfile("testrepo/branch_file.txt"));
490 /* now to a narrow patterned checkout, but disable pattern */
492 g_opts
.checkout_strategy
=
494 GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH
;
495 g_opts
.paths
.strings
= entries
;
496 g_opts
.paths
.count
= 1;
498 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "refs/heads/master"));
500 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
502 cl_assert(!git_path_isfile("testrepo/branch_file.txt"));
504 /* let's try that again, but allow the pattern match */
506 g_opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
508 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
510 cl_assert(git_path_isfile("testrepo/branch_file.txt"));
513 void assert_conflict(
514 const char *entry_path
,
515 const char *new_content
,
516 const char *parent_sha
,
517 const char *commit_sha
)
520 git_object
*hack_tree
;
521 git_reference
*branch
, *head
;
522 git_buf file_path
= GIT_BUF_INIT
;
524 cl_git_pass(git_repository_index(&index
, g_repo
));
526 /* Create a branch pointing at the parent */
527 cl_git_pass(git_revparse_single(&g_object
, g_repo
, parent_sha
));
528 cl_git_pass(git_branch_create(&branch
, g_repo
,
529 "potential_conflict", (git_commit
*)g_object
, 0));
531 /* Make HEAD point to this branch */
532 cl_git_pass(git_reference_symbolic_create(
533 &head
, g_repo
, "HEAD", git_reference_name(branch
), 1, NULL
));
534 git_reference_free(head
);
535 git_reference_free(branch
);
537 /* Checkout the parent */
538 g_opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
539 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
541 /* Hack-ishy workaound to ensure *all* the index entries
542 * match the content of the tree
544 cl_git_pass(git_object_peel(&hack_tree
, g_object
, GIT_OBJECT_TREE
));
545 cl_git_pass(git_index_read_tree(index
, (git_tree
*)hack_tree
));
546 cl_git_pass(git_index_write(index
));
547 git_object_free(hack_tree
);
548 git_object_free(g_object
);
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
);
554 git_buf_dispose(&file_path
);
556 /* Trying to checkout the original commit */
557 cl_git_pass(git_revparse_single(&g_object
, g_repo
, commit_sha
));
559 g_opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
561 GIT_ECONFLICT
, git_checkout_tree(g_repo
, g_object
, &g_opts
));
563 /* Stage the conflicting change */
564 cl_git_pass(git_index_add_bypath(index
, entry_path
));
565 cl_git_pass(git_index_write(index
));
566 git_index_free(index
);
569 GIT_ECONFLICT
, git_checkout_tree(g_repo
, g_object
, &g_opts
));
572 void test_checkout_tree__checking_out_a_conflicting_type_change_returns_ECONFLICT(void)
575 * 099faba adds a symlink named 'link_to_new.txt'
576 * a65fedf is the parent of 099faba
579 assert_conflict("link_to_new.txt", "old.txt", "a65fedf", "099faba");
582 void test_checkout_tree__checking_out_a_conflicting_type_change_returns_ECONFLICT_2(void)
585 * cf80f8d adds a directory named 'a/'
586 * a4a7dce is the parent of cf80f8d
589 assert_conflict("a", "hello\n", "a4a7dce", "cf80f8d");
592 void test_checkout_tree__checking_out_a_conflicting_content_change_returns_ECONFLICT(void)
595 * c47800c adds a symlink named 'branch_file.txt'
596 * 5b5b025 is the parent of 763d71a
599 assert_conflict("branch_file.txt", "hello\n", "5b5b025", "c47800c");
602 void test_checkout_tree__donot_update_deleted_file_by_default(void)
604 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
605 git_oid old_id
, new_id
;
606 git_commit
*old_commit
= NULL
, *new_commit
= NULL
;
607 git_index
*index
= NULL
;
610 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
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
;
617 cl_git_pass(git_repository_index(&index
, g_repo
));
619 cl_git_pass(git_oid_fromstr(&old_id
, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
620 cl_git_pass(git_commit_lookup(&old_commit
, g_repo
, &old_id
));
621 cl_git_pass(git_reset(g_repo
, (git_object
*)old_commit
, GIT_RESET_HARD
, NULL
));
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
));
627 cl_assert(!git_path_exists("testrepo/branch_file.txt"));
629 cl_git_pass(git_oid_fromstr(&new_id
, "099fabac3a9ea935598528c27f866e34089c2eff"));
630 cl_git_pass(git_commit_lookup(&new_commit
, g_repo
, &new_id
));
633 cl_git_fail(git_checkout_tree(g_repo
, (git_object
*)new_commit
, &opts
));
635 cl_assert_equal_i(1, ct
.n_conflicts
);
636 cl_assert_equal_i(1, ct
.n_updates
);
638 git_commit_free(old_commit
);
639 git_commit_free(new_commit
);
640 git_index_free(index
);
643 struct checkout_cancel_at
{
644 const char *filename
;
649 static int checkout_cancel_cb(
650 git_checkout_notify_t why
,
652 const git_diff_file
*b
,
653 const git_diff_file
*t
,
654 const git_diff_file
*w
,
657 struct checkout_cancel_at
*ca
= payload
;
659 GIT_UNUSED(why
); GIT_UNUSED(b
); GIT_UNUSED(t
); GIT_UNUSED(w
);
663 if (!strcmp(path
, ca
->filename
))
669 void test_checkout_tree__can_cancel_checkout_from_notify(void)
671 struct checkout_cancel_at ca
;
672 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
674 git_object
*obj
= NULL
;
676 assert_on_branch(g_repo
, "master");
678 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
679 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
681 ca
.filename
= "new.txt";
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
;
690 cl_assert(!git_path_exists("testrepo/new.txt"));
692 cl_git_fail_with(git_checkout_tree(g_repo
, obj
, &opts
), -5555);
694 cl_assert(!git_path_exists("testrepo/new.txt"));
696 /* on case-insensitive FS = a/b.txt, branch_file.txt, new.txt */
697 /* on case-sensitive FS = README, then above */
699 if (git_path_exists("testrepo/.git/CoNfIg")) /* case insensitive */
700 cl_assert_equal_i(3, ca
.count
);
702 cl_assert_equal_i(4, ca
.count
);
704 /* and again with a different stopping point and return code */
705 ca
.filename
= "README";
709 cl_git_fail_with(git_checkout_tree(g_repo
, obj
, &opts
), 123);
711 cl_assert(!git_path_exists("testrepo/new.txt"));
713 if (git_path_exists("testrepo/.git/CoNfIg")) /* case insensitive */
714 cl_assert_equal_i(4, ca
.count
);
716 cl_assert_equal_i(1, ca
.count
);
718 git_object_free(obj
);
721 void test_checkout_tree__can_checkout_with_last_workdir_item_missing(void)
723 git_index
*index
= NULL
;
724 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
725 git_oid tree_id
, commit_id
;
726 git_tree
*tree
= NULL
;
727 git_commit
*commit
= NULL
;
729 git_repository_index(&index
, g_repo
);
731 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
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
));
736 cl_git_pass(git_checkout_tree(g_repo
, (git_object
*)commit
, &opts
));
737 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/master"));
739 cl_git_pass(p_mkdir("./testrepo/this-is-dir", 0777));
740 cl_git_mkfile("./testrepo/this-is-dir/contained_file", "content\n");
742 cl_git_pass(git_index_add_bypath(index
, "this-is-dir/contained_file"));
743 cl_git_pass(git_index_write(index
));
745 cl_git_pass(git_index_write_tree(&tree_id
, index
));
746 cl_git_pass(git_tree_lookup(&tree
, g_repo
, &tree_id
));
748 cl_git_pass(p_unlink("./testrepo/this-is-dir/contained_file"));
750 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
752 opts
.checkout_strategy
= 1;
753 git_checkout_tree(g_repo
, (git_object
*)tree
, &opts
);
756 git_commit_free(commit
);
757 git_index_free(index
);
760 void test_checkout_tree__issue_1397(void)
762 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
763 const char *partial_oid
= "8a7ef04";
764 git_object
*tree
= NULL
;
766 test_checkout_tree__cleanup(); /* cleanup default checkout */
768 g_repo
= cl_git_sandbox_init("issue_1397");
770 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
772 cl_git_pass(git_revparse_single(&tree
, g_repo
, partial_oid
));
774 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
776 cl_git_pass(git_checkout_tree(g_repo
, tree
, &opts
));
778 check_file_contents("./issue_1397/crlf_file.txt", "first line\r\nsecond line\r\nboth with crlf");
780 git_object_free(tree
);
783 void test_checkout_tree__can_write_to_empty_dirs(void)
785 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
787 git_object
*obj
= NULL
;
789 assert_on_branch(g_repo
, "master");
791 cl_git_pass(p_mkdir("testrepo/a", 0777));
793 /* do first checkout with FORCE because we don't know if testrepo
794 * base data is clean for a checkout or not
796 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
798 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
799 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
801 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
803 cl_assert(git_path_isfile("testrepo/a/b.txt"));
805 git_object_free(obj
);
808 void test_checkout_tree__fails_when_dir_in_use(void)
811 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
813 git_object
*obj
= NULL
;
815 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
817 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
818 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
820 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
822 cl_assert(git_path_isfile("testrepo/a/b.txt"));
824 git_object_free(obj
);
826 cl_git_pass(p_chdir("testrepo/a"));
828 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/master"));
829 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
831 cl_git_fail(git_checkout_tree(g_repo
, obj
, &opts
));
833 cl_git_pass(p_chdir("../.."));
835 cl_assert(git_path_is_empty_dir("testrepo/a"));
837 git_object_free(obj
);
841 void test_checkout_tree__can_continue_when_dir_in_use(void)
844 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
846 git_object
*obj
= NULL
;
848 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
|
849 GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES
;
851 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
852 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
854 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
856 cl_assert(git_path_isfile("testrepo/a/b.txt"));
858 git_object_free(obj
);
860 cl_git_pass(p_chdir("testrepo/a"));
862 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/master"));
863 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
865 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
867 cl_git_pass(p_chdir("../.."));
869 cl_assert(git_path_is_empty_dir("testrepo/a"));
871 git_object_free(obj
);
875 void test_checkout_tree__target_directory_from_bare(void)
877 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
880 memset(&cts
, 0, sizeof(cts
));
882 test_checkout_tree__cleanup(); /* cleanup default checkout */
884 g_repo
= cl_git_sandbox_init("testrepo.git");
885 cl_assert(git_repository_is_bare(g_repo
));
887 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
|
888 GIT_CHECKOUT_RECREATE_MISSING
;
890 opts
.notify_flags
= GIT_CHECKOUT_NOTIFY_ALL
;
891 opts
.notify_cb
= checkout_count_callback
;
892 opts
.notify_payload
= &cts
;
894 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "HEAD"));
895 cl_git_pass(git_object_lookup(&g_object
, g_repo
, &oid
, GIT_OBJECT_ANY
));
897 cl_git_fail(git_checkout_tree(g_repo
, g_object
, &opts
));
899 opts
.target_directory
= "alternative";
900 cl_assert(!git_path_isdir("alternative"));
902 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &opts
));
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
);
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");
912 cl_git_pass(git_futils_rmdir_r(
913 "alternative", NULL
, GIT_RMDIR_REMOVE_FILES
));
916 void test_checkout_tree__extremely_long_file_name(void)
918 /* A utf-8 string with 83 characters, but 249 bytes. */
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] = {0};
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
));
926 sprintf(path
, "testrepo/%s.txt", longname
);
927 cl_assert(git_path_exists(path
));
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
));
935 static void create_conflict(const char *path
)
938 git_index_entry entry
;
940 cl_git_pass(git_repository_index(&index
, g_repo
));
942 memset(&entry
, 0x0, sizeof(git_index_entry
));
943 entry
.mode
= 0100644;
944 GIT_INDEX_ENTRY_STAGE_SET(&entry
, 1);
945 git_oid_fromstr(&entry
.id
, "d427e0b2e138501a3d15cc376077a3631e15bd46");
947 cl_git_pass(git_index_add(index
, &entry
));
949 GIT_INDEX_ENTRY_STAGE_SET(&entry
, 2);
950 git_oid_fromstr(&entry
.id
, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf");
951 cl_git_pass(git_index_add(index
, &entry
));
953 GIT_INDEX_ENTRY_STAGE_SET(&entry
, 3);
954 git_oid_fromstr(&entry
.id
, "2bd0a343aeef7a2cf0d158478966a6e587ff3863");
955 cl_git_pass(git_index_add(index
, &entry
));
957 cl_git_pass(git_index_write(index
));
958 git_index_free(index
);
961 void test_checkout_tree__fails_when_conflicts_exist_in_index(void)
963 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
965 git_object
*obj
= NULL
;
967 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
969 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "HEAD"));
970 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
972 create_conflict("conflicts.txt");
974 cl_git_fail(git_checkout_tree(g_repo
, obj
, &opts
));
976 git_object_free(obj
);
979 void test_checkout_tree__filemode_preserved_in_index(void)
981 git_oid executable_oid
;
983 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
985 const git_index_entry
*entry
;
987 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
989 cl_git_pass(git_repository_index(&index
, g_repo
));
991 /* test a freshly added executable */
992 cl_git_pass(git_oid_fromstr(&executable_oid
, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6"));
993 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &executable_oid
));
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));
997 cl_assert(GIT_PERMS_IS_EXEC(entry
->mode
));
999 git_commit_free(commit
);
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
));
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));
1008 cl_assert(!GIT_PERMS_IS_EXEC(entry
->mode
));
1010 git_commit_free(commit
);
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
));
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));
1019 cl_assert(GIT_PERMS_IS_EXEC(entry
->mode
));
1021 git_commit_free(commit
);
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
));
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));
1030 cl_assert(!GIT_PERMS_IS_EXEC(entry
->mode
));
1032 git_commit_free(commit
);
1035 git_index_free(index
);
1038 mode_t
read_filemode(const char *path
)
1040 git_buf fullpath
= GIT_BUF_INIT
;
1044 git_buf_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_buf_dispose(&fullpath
);
1055 void test_checkout_tree__filemode_preserved_in_workdir(void)
1058 git_oid executable_oid
;
1060 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1062 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
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
));
1068 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1069 cl_assert(GIT_PERMS_IS_EXEC(read_filemode("executable.txt")));
1071 git_commit_free(commit
);
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
));
1078 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1079 cl_assert(!GIT_PERMS_IS_EXEC(read_filemode("a/b.txt")));
1081 git_commit_free(commit
);
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
));
1088 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1089 cl_assert(GIT_PERMS_IS_EXEC(read_filemode("a/b.txt")));
1091 git_commit_free(commit
);
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
));
1098 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1099 cl_assert(!GIT_PERMS_IS_EXEC(read_filemode("a/b.txt")));
1101 git_commit_free(commit
);
1107 void test_checkout_tree__removes_conflicts(void)
1111 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1114 cl_git_pass(git_oid_fromstr(&commit_id
, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6"));
1115 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &commit_id
));
1117 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
1119 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1121 cl_git_pass(git_repository_index(&index
, g_repo
));
1122 cl_git_pass(git_index_remove(index
, "executable.txt", 0));
1124 create_conflict("executable.txt");
1125 cl_git_mkfile("testrepo/executable.txt", "This is the conflict file.\n");
1127 create_conflict("other.txt");
1128 cl_git_mkfile("testrepo/other.txt", "This is another conflict file.\n");
1130 cl_git_pass(git_index_write(index
));
1132 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
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));
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));
1142 cl_assert(!git_path_exists("testrepo/other.txt"));
1144 git_commit_free(commit
);
1145 git_index_free(index
);
1149 void test_checkout_tree__removes_conflicts_only_by_pathscope(void)
1153 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1155 const char *path
= "executable.txt";
1157 cl_git_pass(git_oid_fromstr(&commit_id
, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6"));
1158 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &commit_id
));
1160 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
1161 opts
.paths
.count
= 1;
1162 opts
.paths
.strings
= (char **)&path
;
1164 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1166 cl_git_pass(git_repository_index(&index
, g_repo
));
1167 cl_git_pass(git_index_remove(index
, "executable.txt", 0));
1169 create_conflict("executable.txt");
1170 cl_git_mkfile("testrepo/executable.txt", "This is the conflict file.\n");
1172 create_conflict("other.txt");
1173 cl_git_mkfile("testrepo/other.txt", "This is another conflict file.\n");
1175 cl_git_pass(git_index_write(index
));
1177 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
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));
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
);
1187 cl_assert(git_path_exists("testrepo/other.txt"));
1189 git_commit_free(commit
);
1190 git_index_free(index
);
1193 void test_checkout_tree__case_changing_rename(void)
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
;
1200 git_signature
*signature
;
1201 const git_index_entry
*index_entry
;
1202 bool case_sensitive
;
1204 assert_on_branch(g_repo
, "master");
1206 cl_git_pass(git_repository_index(&index
, g_repo
));
1208 /* Switch branches and perform a case-changing rename */
1210 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
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
));
1215 cl_git_pass(git_checkout_tree(g_repo
, (git_object
*)dir_commit
, &opts
));
1216 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/dir"));
1218 cl_assert(git_path_isfile("testrepo/README"));
1219 case_sensitive
= !git_path_isfile("testrepo/readme");
1221 cl_assert(index_entry
= git_index_get_bypath(index
, "README", 0));
1222 cl_assert_equal_s("README", index_entry
->path
);
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"));
1230 cl_git_pass(git_index_write(index
));
1232 cl_git_pass(git_index_write_tree(&tree_id
, index
));
1233 cl_git_pass(git_tree_lookup(&tree
, g_repo
, &tree_id
));
1235 cl_git_pass(git_signature_new(&signature
, "Renamer", "rename@contoso.com", time(NULL
), 0));
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
));
1239 cl_assert(git_path_isfile("testrepo/readme"));
1241 cl_assert(!git_path_isfile("testrepo/README"));
1243 cl_assert(index_entry
= git_index_get_bypath(index
, "readme", 0));
1244 cl_assert_equal_s("readme", index_entry
->path
);
1246 /* Switching back to master should rename readme -> README */
1247 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
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
));
1252 cl_git_pass(git_checkout_tree(g_repo
, (git_object
*)master_commit
, &opts
));
1253 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/master"));
1255 assert_on_branch(g_repo
, "master");
1257 cl_assert(git_path_isfile("testrepo/README"));
1259 cl_assert(!git_path_isfile("testrepo/readme"));
1261 cl_assert(index_entry
= git_index_get_bypath(index
, "README", 0));
1262 cl_assert_equal_s("README", index_entry
->path
);
1264 git_index_free(index
);
1265 git_signature_free(signature
);
1266 git_tree_free(tree
);
1267 git_commit_free(dir_commit
);
1268 git_commit_free(master_commit
);
1271 void perfdata_cb(const git_checkout_perfdata
*in
, void *payload
)
1273 memcpy(payload
, in
, sizeof(git_checkout_perfdata
));
1276 void test_checkout_tree__can_collect_perfdata(void)
1278 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1280 git_object
*obj
= NULL
;
1281 git_checkout_perfdata perfdata
= {0};
1283 opts
.perfdata_cb
= perfdata_cb
;
1284 opts
.perfdata_payload
= &perfdata
;
1286 assert_on_branch(g_repo
, "master");
1287 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
1289 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
1290 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
1292 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
1294 cl_assert(perfdata
.mkdir_calls
> 0);
1295 cl_assert(perfdata
.stat_calls
> 0);
1297 git_object_free(obj
);
1300 void update_attr_callback(
1302 size_t completed_steps
,
1306 GIT_UNUSED(completed_steps
);
1307 GIT_UNUSED(total_steps
);
1308 GIT_UNUSED(payload
);
1310 if (path
&& strcmp(path
, "ident1.txt") == 0)
1311 cl_git_write2file("testrepo/.gitattributes",
1312 "*.txt ident\n", 12, O_RDWR
|O_CREAT
, 0666);
1315 void test_checkout_tree__caches_attributes_during_checkout(void)
1317 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1319 git_object
*obj
= NULL
;
1320 git_buf ident1
= GIT_BUF_INIT
, ident2
= GIT_BUF_INIT
;
1321 char *ident_paths
[] = { "ident1.txt", "ident2.txt" };
1323 opts
.progress_cb
= update_attr_callback
;
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;
1330 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/ident"));
1331 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
1333 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
1335 cl_git_pass(git_futils_readbuffer(&ident1
, "testrepo/ident1.txt"));
1336 cl_git_pass(git_futils_readbuffer(&ident2
, "testrepo/ident2.txt"));
1338 cl_assert_equal_strn(ident1
.ptr
, "# $Id$", 6);
1339 cl_assert_equal_strn(ident2
.ptr
, "# $Id$", 6);
1341 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
1343 cl_git_pass(git_futils_readbuffer(&ident1
, "testrepo/ident1.txt"));
1344 cl_git_pass(git_futils_readbuffer(&ident2
, "testrepo/ident2.txt"));
1346 cl_assert_equal_strn(ident1
.ptr
, "# $Id: ", 7);
1347 cl_assert_equal_strn(ident2
.ptr
, "# $Id: ", 7);
1349 git_buf_dispose(&ident1
);
1350 git_buf_dispose(&ident2
);
1351 git_object_free(obj
);
1354 void test_checkout_tree__can_not_update_index(void)
1358 unsigned int status
;
1359 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1362 opts
.checkout_strategy
|=
1363 GIT_CHECKOUT_FORCE
| GIT_CHECKOUT_DONT_UPDATE_INDEX
;
1365 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "HEAD"));
1366 cl_git_pass(git_object_lookup(&head
, g_repo
, &oid
, GIT_OBJECT_ANY
));
1368 cl_git_pass(git_reset(g_repo
, head
, GIT_RESET_HARD
, &g_opts
));
1370 cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
1372 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "subtrees"));
1374 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &opts
));
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
);
1380 cl_git_pass(git_repository_index(&index
, g_repo
));
1381 cl_git_pass(git_index_write(index
));
1383 cl_git_pass(git_status_file(&status
, g_repo
, "ab/de/2.txt"));
1384 cl_assert_equal_i(GIT_STATUS_WT_NEW
, status
);
1386 git_object_free(head
);
1387 git_index_free(index
);
1390 void test_checkout_tree__can_update_but_not_write_index(void)
1394 unsigned int status
;
1395 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1397 git_repository
*other
;
1399 opts
.checkout_strategy
|=
1400 GIT_CHECKOUT_FORCE
| GIT_CHECKOUT_DONT_WRITE_INDEX
;
1402 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "HEAD"));
1403 cl_git_pass(git_object_lookup(&head
, g_repo
, &oid
, GIT_OBJECT_ANY
));
1405 cl_git_pass(git_reset(g_repo
, head
, GIT_RESET_HARD
, &g_opts
));
1407 cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
1409 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "subtrees"));
1411 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &opts
));
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
);
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
);
1422 cl_git_pass(git_repository_index(&index
, g_repo
));
1423 cl_git_pass(git_index_write(index
));
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
);
1430 git_object_free(head
);
1431 git_index_free(index
);
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)
1438 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1440 git_object
*obj
= NULL
;
1442 assert_on_branch(g_repo
, "master");
1443 cl_must_pass(p_unlink("testrepo/.git/index"));
1445 /* do second checkout safe because we should be clean after first */
1446 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
1448 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/subtrees"));
1449 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJECT_ANY
));
1451 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
1452 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/subtrees"));
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"));
1462 cl_assert(!git_path_isdir("testrepo/a"));
1464 assert_on_branch(g_repo
, "subtrees");
1466 git_object_free(obj
);
1469 static int checkout_conflict_count_cb(
1470 git_checkout_notify_t why
,
1472 const git_diff_file
*b
,
1473 const git_diff_file
*t
,
1474 const git_diff_file
*w
,
1477 size_t *n
= payload
;
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
1493 void test_checkout_tree__baseline_is_empty_when_no_index(void)
1495 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1496 git_reference
*head
;
1498 size_t conflicts
= 0;
1500 assert_on_branch(g_repo
, "master");
1502 cl_git_pass(git_repository_head(&head
, g_repo
));
1503 cl_git_pass(git_reference_peel(&obj
, head
, GIT_OBJECT_COMMIT
));
1505 cl_git_pass(git_reset(g_repo
, obj
, GIT_RESET_HARD
, NULL
));
1507 cl_must_pass(p_unlink("testrepo/.git/index"));
1509 /* for a safe checkout, we should have checkout conflicts with
1510 * the existing untracked files.
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
;
1517 cl_git_fail_with(GIT_ECONFLICT
, git_checkout_tree(g_repo
, obj
, &opts
));
1518 cl_assert_equal_i(4, conflicts
);
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
));
1524 assert_status_entrycount(g_repo
, 0);
1526 git_object_free(obj
);
1527 git_reference_free(head
);
1530 void test_checkout_tree__mode_change_is_force_updated(void)
1533 git_reference
*head
;
1536 if (!cl_is_chmod_supported())
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
));
1542 cl_git_pass(git_reference_peel(&obj
, head
, GIT_OBJECT_COMMIT
));
1544 cl_git_pass(git_reset(g_repo
, obj
, GIT_RESET_HARD
, NULL
));
1545 assert_status_entrycount(g_repo
, 0);
1547 /* update the mode on-disk */
1548 cl_must_pass(p_chmod("testrepo/README", 0755));
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);
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"));
1558 cl_git_pass(git_index_write(index
));
1559 assert_status_entrycount(g_repo
, 1);
1561 cl_git_pass(git_checkout_tree(g_repo
, obj
, &g_opts
));
1562 cl_git_pass(git_index_write(index
));
1564 assert_status_entrycount(g_repo
, 0);
1566 git_object_free(obj
);
1567 git_reference_free(head
);
1568 git_index_free(index
);
1571 void test_checkout_tree__nullopts(void)
1573 cl_git_pass(git_checkout_tree(g_repo
, NULL
, NULL
));
1576 static void modify_index_ondisk(void)
1578 git_repository
*other_repo
;
1579 git_index
*other_index
;
1580 git_index_entry entry
= {{0}};
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
));
1585 cl_git_pass(git_oid_fromstr(&entry
.id
, "1385f264afb75a56a5bec74243be9b367ba4ca08"));
1586 entry
.mode
= 0100644;
1587 entry
.path
= "README";
1589 cl_git_pass(git_index_add(other_index
, &entry
));
1590 cl_git_pass(git_index_write(other_index
));
1592 git_index_free(other_index
);
1593 git_repository_free(other_repo
);
1596 static void modify_index_and_checkout_tree(git_checkout_options
*opts
)
1599 git_reference
*head
;
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
));
1607 cl_git_pass(git_reset(g_repo
, obj
, GIT_RESET_HARD
, NULL
));
1608 assert_status_entrycount(g_repo
, 0);
1610 modify_index_ondisk();
1612 /* The file in the index remains modified */
1613 cl_git_pass(git_checkout_tree(g_repo
, obj
, opts
));
1615 git_object_free(obj
);
1616 git_reference_free(head
);
1617 git_index_free(index
);
1620 void test_checkout_tree__retains_external_index_changes(void)
1622 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1624 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
1626 modify_index_and_checkout_tree(&opts
);
1627 assert_status_entrycount(g_repo
, 1);
1630 void test_checkout_tree__no_index_refresh(void)
1632 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1634 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
| GIT_CHECKOUT_NO_REFRESH
;
1636 modify_index_and_checkout_tree(&opts
);
1637 assert_status_entrycount(g_repo
, 0);