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 void test_checkout_tree__initialize(void)
15 g_repo
= cl_git_sandbox_init("testrepo");
17 GIT_INIT_STRUCTURE(&g_opts
, GIT_CHECKOUT_OPTIONS_VERSION
);
18 g_opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
21 void test_checkout_tree__cleanup(void)
23 git_object_free(g_object
);
26 cl_git_sandbox_cleanup();
28 if (git_path_isdir("alternative"))
29 git_futils_rmdir_r("alternative", NULL
, GIT_RMDIR_REMOVE_FILES
);
32 void test_checkout_tree__cannot_checkout_a_non_treeish(void)
35 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"));
36 cl_git_fail(git_checkout_tree(g_repo
, g_object
, NULL
));
39 void test_checkout_tree__can_checkout_a_subdirectory_from_a_commit(void)
41 char *entries
[] = { "ab/de/" };
43 g_opts
.paths
.strings
= entries
;
44 g_opts
.paths
.count
= 1;
46 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "subtrees"));
48 cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
50 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
52 cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt"));
53 cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/fgh/1.txt"));
56 void test_checkout_tree__can_checkout_and_remove_directory(void)
58 cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
60 /* Checkout brach "subtrees" and update HEAD, so that HEAD matches the
61 * current working tree
63 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "subtrees"));
64 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
66 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/subtrees"));
68 cl_assert_equal_i(true, git_path_isdir("./testrepo/ab/"));
69 cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt"));
70 cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/fgh/1.txt"));
72 git_object_free(g_object
);
75 /* Checkout brach "master" and update HEAD, so that HEAD matches the
76 * current working tree
78 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "master"));
79 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
81 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/master"));
83 /* This directory should no longer exist */
84 cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
87 void test_checkout_tree__can_checkout_a_subdirectory_from_a_subtree(void)
89 char *entries
[] = { "de/" };
91 g_opts
.paths
.strings
= entries
;
92 g_opts
.paths
.count
= 1;
94 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "subtrees:ab"));
96 cl_assert_equal_i(false, git_path_isdir("./testrepo/de/"));
98 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
100 cl_assert_equal_i(true, git_path_isfile("./testrepo/de/2.txt"));
101 cl_assert_equal_i(true, git_path_isfile("./testrepo/de/fgh/1.txt"));
104 static void progress(const char *path
, size_t cur
, size_t tot
, void *payload
)
106 bool *was_called
= (bool*)payload
;
107 GIT_UNUSED(path
); GIT_UNUSED(cur
); GIT_UNUSED(tot
);
111 void test_checkout_tree__calls_progress_callback(void)
115 g_opts
.progress_cb
= progress
;
116 g_opts
.progress_payload
= &was_called
;
118 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "master"));
120 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
122 cl_assert_equal_i(was_called
, true);
125 void test_checkout_tree__doesnt_write_unrequested_files_to_worktree(void)
129 git_commit
* p_master_commit
;
130 git_commit
* p_chomped_commit
;
131 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
133 git_oid_fromstr(&master_oid
, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
134 git_oid_fromstr(&chomped_oid
, "e90810b8df3e80c413d903f631643c716887138d");
135 cl_git_pass(git_commit_lookup(&p_master_commit
, g_repo
, &master_oid
));
136 cl_git_pass(git_commit_lookup(&p_chomped_commit
, g_repo
, &chomped_oid
));
138 /* GIT_CHECKOUT_NONE should not add any file to the working tree from the
139 * index as it is supposed to be a dry run.
141 opts
.checkout_strategy
= GIT_CHECKOUT_NONE
;
142 git_checkout_tree(g_repo
, (git_object
*)p_chomped_commit
, &opts
);
143 cl_assert_equal_i(false, git_path_isfile("testrepo/readme.txt"));
145 git_commit_free(p_master_commit
);
146 git_commit_free(p_chomped_commit
);
149 void test_checkout_tree__can_switch_branches(void)
151 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
153 git_object
*obj
= NULL
;
155 assert_on_branch(g_repo
, "master");
157 /* do first checkout with FORCE because we don't know if testrepo
158 * base data is clean for a checkout or not
160 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
162 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
163 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
165 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
166 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/dir"));
168 cl_assert(git_path_isfile("testrepo/README"));
169 cl_assert(git_path_isfile("testrepo/branch_file.txt"));
170 cl_assert(git_path_isfile("testrepo/new.txt"));
171 cl_assert(git_path_isfile("testrepo/a/b.txt"));
173 cl_assert(!git_path_isdir("testrepo/ab"));
175 assert_on_branch(g_repo
, "dir");
177 git_object_free(obj
);
179 /* do second checkout safe because we should be clean after first */
180 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
182 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/subtrees"));
183 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
185 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
186 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/subtrees"));
188 cl_assert(git_path_isfile("testrepo/README"));
189 cl_assert(git_path_isfile("testrepo/branch_file.txt"));
190 cl_assert(git_path_isfile("testrepo/new.txt"));
191 cl_assert(git_path_isfile("testrepo/ab/4.txt"));
192 cl_assert(git_path_isfile("testrepo/ab/c/3.txt"));
193 cl_assert(git_path_isfile("testrepo/ab/de/2.txt"));
194 cl_assert(git_path_isfile("testrepo/ab/de/fgh/1.txt"));
196 cl_assert(!git_path_isdir("testrepo/a"));
198 assert_on_branch(g_repo
, "subtrees");
200 git_object_free(obj
);
203 void test_checkout_tree__can_remove_untracked(void)
205 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
207 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
| GIT_CHECKOUT_REMOVE_UNTRACKED
;
209 cl_git_mkfile("testrepo/untracked_file", "as you wish");
210 cl_assert(git_path_isfile("testrepo/untracked_file"));
212 cl_git_pass(git_checkout_head(g_repo
, &opts
));
214 cl_assert(!git_path_isfile("testrepo/untracked_file"));
217 void test_checkout_tree__can_remove_ignored(void)
219 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
222 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
| GIT_CHECKOUT_REMOVE_IGNORED
;
224 cl_git_mkfile("testrepo/ignored_file", "as you wish");
226 cl_git_pass(git_ignore_add_rule(g_repo
, "ignored_file\n"));
228 cl_git_pass(git_ignore_path_is_ignored(&ignored
, g_repo
, "ignored_file"));
229 cl_assert_equal_i(1, ignored
);
231 cl_assert(git_path_isfile("testrepo/ignored_file"));
233 cl_git_pass(git_checkout_head(g_repo
, &opts
));
235 cl_assert(!git_path_isfile("testrepo/ignored_file"));
238 static int checkout_tree_with_blob_ignored_in_workdir(int strategy
, bool isdir
)
241 git_object
*obj
= NULL
;
242 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
243 int ignored
= 0, error
;
245 assert_on_branch(g_repo
, "master");
247 /* do first checkout with FORCE because we don't know if testrepo
248 * base data is clean for a checkout or not
250 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
252 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
253 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
255 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
256 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/dir"));
258 cl_assert(git_path_isfile("testrepo/README"));
259 cl_assert(git_path_isfile("testrepo/branch_file.txt"));
260 cl_assert(git_path_isfile("testrepo/new.txt"));
261 cl_assert(git_path_isfile("testrepo/a/b.txt"));
263 cl_assert(!git_path_isdir("testrepo/ab"));
265 assert_on_branch(g_repo
, "dir");
267 git_object_free(obj
);
269 opts
.checkout_strategy
= strategy
;
272 cl_must_pass(p_mkdir("testrepo/ab", 0777));
273 cl_must_pass(p_mkdir("testrepo/ab/4.txt", 0777));
275 cl_git_mkfile("testrepo/ab/4.txt/file1.txt", "as you wish");
276 cl_git_mkfile("testrepo/ab/4.txt/file2.txt", "foo bar foo");
277 cl_git_mkfile("testrepo/ab/4.txt/file3.txt", "inky blinky pinky clyde");
279 cl_assert(git_path_isdir("testrepo/ab/4.txt"));
281 cl_must_pass(p_mkdir("testrepo/ab", 0777));
282 cl_git_mkfile("testrepo/ab/4.txt", "as you wish");
284 cl_assert(git_path_isfile("testrepo/ab/4.txt"));
287 cl_git_pass(git_ignore_add_rule(g_repo
, "ab/4.txt\n"));
289 cl_git_pass(git_ignore_path_is_ignored(&ignored
, g_repo
, "ab/4.txt"));
290 cl_assert_equal_i(1, ignored
);
292 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/subtrees"));
293 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
295 error
= git_checkout_tree(g_repo
, obj
, &opts
);
297 git_object_free(obj
);
302 void test_checkout_tree__conflict_on_ignored_when_not_overwriting(void)
306 cl_git_fail(error
= checkout_tree_with_blob_ignored_in_workdir(
307 GIT_CHECKOUT_SAFE
| GIT_CHECKOUT_DONT_OVERWRITE_IGNORED
, false));
309 cl_assert_equal_i(GIT_EMERGECONFLICT
, error
);
312 void test_checkout_tree__can_overwrite_ignored_by_default(void)
314 cl_git_pass(checkout_tree_with_blob_ignored_in_workdir(GIT_CHECKOUT_SAFE
, false));
316 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/subtrees"));
318 cl_assert(git_path_isfile("testrepo/ab/4.txt"));
320 assert_on_branch(g_repo
, "subtrees");
323 void test_checkout_tree__conflict_on_ignored_folder_when_not_overwriting(void)
327 cl_git_fail(error
= checkout_tree_with_blob_ignored_in_workdir(
328 GIT_CHECKOUT_SAFE
| GIT_CHECKOUT_DONT_OVERWRITE_IGNORED
, true));
330 cl_assert_equal_i(GIT_EMERGECONFLICT
, error
);
333 void test_checkout_tree__can_overwrite_ignored_folder_by_default(void)
335 cl_git_pass(checkout_tree_with_blob_ignored_in_workdir(GIT_CHECKOUT_SAFE
, true));
337 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/subtrees"));
339 cl_assert(git_path_isfile("testrepo/ab/4.txt"));
341 assert_on_branch(g_repo
, "subtrees");
345 void test_checkout_tree__can_update_only(void)
347 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
349 git_object
*obj
= NULL
;
351 /* first let's get things into a known state - by checkout out the HEAD */
353 assert_on_branch(g_repo
, "master");
355 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
356 cl_git_pass(git_checkout_head(g_repo
, &opts
));
358 cl_assert(!git_path_isdir("testrepo/a"));
360 check_file_contents_nocr("testrepo/branch_file.txt", "hi\nbye!\n");
362 /* now checkout branch but with update only */
364 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
| GIT_CHECKOUT_UPDATE_ONLY
;
366 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
367 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
369 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
370 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/dir"));
372 assert_on_branch(g_repo
, "dir");
374 /* this normally would have been created (which was tested separately in
375 * the test_checkout_tree__can_switch_branches test), but with
376 * UPDATE_ONLY it will not have been created.
378 cl_assert(!git_path_isdir("testrepo/a"));
380 /* but this file still should have been updated */
381 check_file_contents_nocr("testrepo/branch_file.txt", "hi\n");
383 git_object_free(obj
);
386 void test_checkout_tree__can_checkout_with_pattern(void)
388 char *entries
[] = { "[l-z]*.txt" };
390 /* reset to beginning of history (i.e. just a README file) */
392 g_opts
.checkout_strategy
=
393 GIT_CHECKOUT_FORCE
| GIT_CHECKOUT_REMOVE_UNTRACKED
;
395 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "8496071c1b46c854b31185ea97743be6a8774479"));
397 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
399 git_repository_set_head_detached(g_repo
, git_object_id(g_object
)));
401 git_object_free(g_object
);
404 cl_assert(git_path_exists("testrepo/README"));
405 cl_assert(!git_path_exists("testrepo/branch_file.txt"));
406 cl_assert(!git_path_exists("testrepo/link_to_new.txt"));
407 cl_assert(!git_path_exists("testrepo/new.txt"));
409 /* now to a narrow patterned checkout */
411 g_opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
412 g_opts
.paths
.strings
= entries
;
413 g_opts
.paths
.count
= 1;
415 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "refs/heads/master"));
417 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
419 cl_assert(git_path_exists("testrepo/README"));
420 cl_assert(!git_path_exists("testrepo/branch_file.txt"));
421 cl_assert(git_path_exists("testrepo/link_to_new.txt"));
422 cl_assert(git_path_exists("testrepo/new.txt"));
425 void test_checkout_tree__can_disable_pattern_match(void)
427 char *entries
[] = { "b*.txt" };
429 /* reset to beginning of history (i.e. just a README file) */
431 g_opts
.checkout_strategy
=
432 GIT_CHECKOUT_FORCE
| GIT_CHECKOUT_REMOVE_UNTRACKED
;
434 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "8496071c1b46c854b31185ea97743be6a8774479"));
436 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
438 git_repository_set_head_detached(g_repo
, git_object_id(g_object
)));
440 git_object_free(g_object
);
443 cl_assert(!git_path_isfile("testrepo/branch_file.txt"));
445 /* now to a narrow patterned checkout, but disable pattern */
447 g_opts
.checkout_strategy
=
449 GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH
;
450 g_opts
.paths
.strings
= entries
;
451 g_opts
.paths
.count
= 1;
453 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "refs/heads/master"));
455 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
457 cl_assert(!git_path_isfile("testrepo/branch_file.txt"));
459 /* let's try that again, but allow the pattern match */
461 g_opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
463 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
465 cl_assert(git_path_isfile("testrepo/branch_file.txt"));
468 void assert_conflict(
469 const char *entry_path
,
470 const char *new_content
,
471 const char *parent_sha
,
472 const char *commit_sha
)
475 git_object
*hack_tree
;
476 git_reference
*branch
, *head
;
477 git_buf file_path
= GIT_BUF_INIT
;
479 cl_git_pass(git_repository_index(&index
, g_repo
));
481 /* Create a branch pointing at the parent */
482 cl_git_pass(git_revparse_single(&g_object
, g_repo
, parent_sha
));
483 cl_git_pass(git_branch_create(&branch
, g_repo
,
484 "potential_conflict", (git_commit
*)g_object
, 0));
486 /* Make HEAD point to this branch */
487 cl_git_pass(git_reference_symbolic_create(
488 &head
, g_repo
, "HEAD", git_reference_name(branch
), 1, NULL
));
489 git_reference_free(head
);
490 git_reference_free(branch
);
492 /* Checkout the parent */
493 g_opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
494 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
496 /* Hack-ishy workaound to ensure *all* the index entries
497 * match the content of the tree
499 cl_git_pass(git_object_peel(&hack_tree
, g_object
, GIT_OBJ_TREE
));
500 cl_git_pass(git_index_read_tree(index
, (git_tree
*)hack_tree
));
501 git_object_free(hack_tree
);
502 git_object_free(g_object
);
505 /* Create a conflicting file */
506 cl_git_pass(git_buf_joinpath(&file_path
, "./testrepo", entry_path
));
507 cl_git_mkfile(git_buf_cstr(&file_path
), new_content
);
508 git_buf_free(&file_path
);
510 /* Trying to checkout the original commit */
511 cl_git_pass(git_revparse_single(&g_object
, g_repo
, commit_sha
));
513 g_opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
515 GIT_EMERGECONFLICT
, git_checkout_tree(g_repo
, g_object
, &g_opts
));
517 /* Stage the conflicting change */
518 cl_git_pass(git_index_add_bypath(index
, entry_path
));
519 cl_git_pass(git_index_write(index
));
520 git_index_free(index
);
523 GIT_EMERGECONFLICT
, git_checkout_tree(g_repo
, g_object
, &g_opts
));
526 void test_checkout_tree__checking_out_a_conflicting_type_change_returns_EMERGECONFLICT(void)
529 * 099faba adds a symlink named 'link_to_new.txt'
530 * a65fedf is the parent of 099faba
533 assert_conflict("link_to_new.txt", "old.txt", "a65fedf", "099faba");
536 void test_checkout_tree__checking_out_a_conflicting_type_change_returns_EMERGECONFLICT_2(void)
539 * cf80f8d adds a directory named 'a/'
540 * a4a7dce is the parent of cf80f8d
543 assert_conflict("a", "hello\n", "a4a7dce", "cf80f8d");
546 void test_checkout_tree__checking_out_a_conflicting_content_change_returns_EMERGECONFLICT(void)
549 * c47800c adds a symlink named 'branch_file.txt'
550 * 5b5b025 is the parent of 763d71a
553 assert_conflict("branch_file.txt", "hello\n", "5b5b025", "c47800c");
556 void test_checkout_tree__donot_update_deleted_file_by_default(void)
558 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
559 git_oid old_id
, new_id
;
560 git_commit
*old_commit
= NULL
, *new_commit
= NULL
;
561 git_index
*index
= NULL
;
564 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
566 memset(&ct
, 0, sizeof(ct
));
567 opts
.notify_flags
= GIT_CHECKOUT_NOTIFY_ALL
;
568 opts
.notify_cb
= checkout_count_callback
;
569 opts
.notify_payload
= &ct
;
571 cl_git_pass(git_repository_index(&index
, g_repo
));
573 cl_git_pass(git_oid_fromstr(&old_id
, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
574 cl_git_pass(git_commit_lookup(&old_commit
, g_repo
, &old_id
));
575 cl_git_pass(git_reset(g_repo
, (git_object
*)old_commit
, GIT_RESET_HARD
, NULL
));
577 cl_git_pass(p_unlink("testrepo/branch_file.txt"));
578 cl_git_pass(git_index_remove_bypath(index
,"branch_file.txt"));
579 cl_git_pass(git_index_write(index
));
581 cl_assert(!git_path_exists("testrepo/branch_file.txt"));
583 cl_git_pass(git_oid_fromstr(&new_id
, "099fabac3a9ea935598528c27f866e34089c2eff"));
584 cl_git_pass(git_commit_lookup(&new_commit
, g_repo
, &new_id
));
587 cl_git_fail(git_checkout_tree(g_repo
, (git_object
*)new_commit
, &opts
));
589 cl_assert_equal_i(1, ct
.n_conflicts
);
590 cl_assert_equal_i(1, ct
.n_updates
);
592 git_commit_free(old_commit
);
593 git_commit_free(new_commit
);
594 git_index_free(index
);
597 struct checkout_cancel_at
{
598 const char *filename
;
603 static int checkout_cancel_cb(
604 git_checkout_notify_t why
,
606 const git_diff_file
*b
,
607 const git_diff_file
*t
,
608 const git_diff_file
*w
,
611 struct checkout_cancel_at
*ca
= payload
;
613 GIT_UNUSED(why
); GIT_UNUSED(b
); GIT_UNUSED(t
); GIT_UNUSED(w
);
617 if (!strcmp(path
, ca
->filename
))
623 void test_checkout_tree__can_cancel_checkout_from_notify(void)
625 struct checkout_cancel_at ca
;
626 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
628 git_object
*obj
= NULL
;
630 assert_on_branch(g_repo
, "master");
632 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
633 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
635 ca
.filename
= "new.txt";
639 opts
.notify_flags
= GIT_CHECKOUT_NOTIFY_UPDATED
;
640 opts
.notify_cb
= checkout_cancel_cb
;
641 opts
.notify_payload
= &ca
;
642 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
644 cl_assert(!git_path_exists("testrepo/new.txt"));
646 cl_git_fail_with(git_checkout_tree(g_repo
, obj
, &opts
), -5555);
648 cl_assert(!git_path_exists("testrepo/new.txt"));
649 cl_assert_equal_i(4, ca
.count
);
651 /* and again with a different stopping point and return code */
652 ca
.filename
= "README";
656 cl_git_fail_with(git_checkout_tree(g_repo
, obj
, &opts
), 123);
658 cl_assert(!git_path_exists("testrepo/new.txt"));
659 cl_assert_equal_i(1, ca
.count
);
661 git_object_free(obj
);
664 void test_checkout_tree__can_checkout_with_last_workdir_item_missing(void)
666 git_index
*index
= NULL
;
667 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
668 git_oid tree_id
, commit_id
;
669 git_tree
*tree
= NULL
;
670 git_commit
*commit
= NULL
;
672 git_repository_index(&index
, g_repo
);
674 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
676 cl_git_pass(git_reference_name_to_id(&commit_id
, g_repo
, "refs/heads/master"));
677 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &commit_id
));
679 cl_git_pass(git_checkout_tree(g_repo
, (git_object
*)commit
, &opts
));
680 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/master"));
682 cl_git_pass(p_mkdir("./testrepo/this-is-dir", 0777));
683 cl_git_mkfile("./testrepo/this-is-dir/contained_file", "content\n");
685 cl_git_pass(git_index_add_bypath(index
, "this-is-dir/contained_file"));
686 git_index_write_tree(&tree_id
, index
);
687 cl_git_pass(git_tree_lookup(&tree
, g_repo
, &tree_id
));
689 cl_git_pass(p_unlink("./testrepo/this-is-dir/contained_file"));
691 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
693 opts
.checkout_strategy
= 1;
694 git_checkout_tree(g_repo
, (git_object
*)tree
, &opts
);
697 git_commit_free(commit
);
698 git_index_free(index
);
701 void test_checkout_tree__issue_1397(void)
703 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
704 const char *partial_oid
= "8a7ef04";
705 git_object
*tree
= NULL
;
707 test_checkout_tree__cleanup(); /* cleanup default checkout */
709 g_repo
= cl_git_sandbox_init("issue_1397");
711 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
713 cl_git_pass(git_revparse_single(&tree
, g_repo
, partial_oid
));
715 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
717 cl_git_pass(git_checkout_tree(g_repo
, tree
, &opts
));
719 check_file_contents("./issue_1397/crlf_file.txt", "first line\r\nsecond line\r\nboth with crlf");
721 git_object_free(tree
);
724 void test_checkout_tree__can_write_to_empty_dirs(void)
726 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
728 git_object
*obj
= NULL
;
730 assert_on_branch(g_repo
, "master");
732 cl_git_pass(p_mkdir("testrepo/a", 0777));
734 /* do first checkout with FORCE because we don't know if testrepo
735 * base data is clean for a checkout or not
737 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
739 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
740 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
742 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
744 cl_assert(git_path_isfile("testrepo/a/b.txt"));
746 git_object_free(obj
);
749 void test_checkout_tree__fails_when_dir_in_use(void)
752 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
754 git_object
*obj
= NULL
;
756 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
758 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
759 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
761 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
763 cl_assert(git_path_isfile("testrepo/a/b.txt"));
765 git_object_free(obj
);
767 cl_git_pass(p_chdir("testrepo/a"));
769 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/master"));
770 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
772 cl_git_fail(git_checkout_tree(g_repo
, obj
, &opts
));
774 cl_git_pass(p_chdir("../.."));
776 cl_assert(git_path_is_empty_dir("testrepo/a"));
778 git_object_free(obj
);
782 void test_checkout_tree__can_continue_when_dir_in_use(void)
785 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
787 git_object
*obj
= NULL
;
789 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
|
790 GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES
;
792 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
793 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
795 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
797 cl_assert(git_path_isfile("testrepo/a/b.txt"));
799 git_object_free(obj
);
801 cl_git_pass(p_chdir("testrepo/a"));
803 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/master"));
804 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
806 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
808 cl_git_pass(p_chdir("../.."));
810 cl_assert(git_path_is_empty_dir("testrepo/a"));
812 git_object_free(obj
);
816 void test_checkout_tree__target_directory_from_bare(void)
818 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
821 memset(&cts
, 0, sizeof(cts
));
823 test_checkout_tree__cleanup(); /* cleanup default checkout */
825 g_repo
= cl_git_sandbox_init("testrepo.git");
826 cl_assert(git_repository_is_bare(g_repo
));
828 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
|
829 GIT_CHECKOUT_RECREATE_MISSING
;
831 opts
.notify_flags
= GIT_CHECKOUT_NOTIFY_ALL
;
832 opts
.notify_cb
= checkout_count_callback
;
833 opts
.notify_payload
= &cts
;
835 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "HEAD"));
836 cl_git_pass(git_object_lookup(&g_object
, g_repo
, &oid
, GIT_OBJ_ANY
));
838 cl_git_fail(git_checkout_tree(g_repo
, g_object
, &opts
));
840 opts
.target_directory
= "alternative";
841 cl_assert(!git_path_isdir("alternative"));
843 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &opts
));
845 cl_assert_equal_i(0, cts
.n_untracked
);
846 cl_assert_equal_i(0, cts
.n_ignored
);
847 cl_assert_equal_i(3, cts
.n_updates
);
849 check_file_contents_nocr("./alternative/README", "hey there\n");
850 check_file_contents_nocr("./alternative/branch_file.txt", "hi\nbye!\n");
851 check_file_contents_nocr("./alternative/new.txt", "my new file\n");
853 cl_git_pass(git_futils_rmdir_r(
854 "alternative", NULL
, GIT_RMDIR_REMOVE_FILES
));
857 void test_checkout_tree__extremely_long_file_name(void)
859 // A utf-8 string with 83 characters, but 249 bytes.
860 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";
863 g_opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
864 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "long-file-name"));
865 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
867 sprintf(path
, "testrepo/%s.txt", longname
);
868 cl_assert(git_path_exists(path
));
870 git_object_free(g_object
);
871 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "master"));
872 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
873 cl_assert(!git_path_exists(path
));
876 static void create_conflict(const char *path
)
879 git_index_entry entry
;
881 cl_git_pass(git_repository_index(&index
, g_repo
));
883 memset(&entry
, 0x0, sizeof(git_index_entry
));
884 entry
.mode
= 0100644;
885 entry
.flags
= 1 << GIT_IDXENTRY_STAGESHIFT
;
886 git_oid_fromstr(&entry
.id
, "d427e0b2e138501a3d15cc376077a3631e15bd46");
888 cl_git_pass(git_index_add(index
, &entry
));
890 entry
.flags
= 2 << GIT_IDXENTRY_STAGESHIFT
;
891 git_oid_fromstr(&entry
.id
, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf");
892 cl_git_pass(git_index_add(index
, &entry
));
894 entry
.flags
= 3 << GIT_IDXENTRY_STAGESHIFT
;
895 git_oid_fromstr(&entry
.id
, "2bd0a343aeef7a2cf0d158478966a6e587ff3863");
896 cl_git_pass(git_index_add(index
, &entry
));
898 git_index_write(index
);
899 git_index_free(index
);
902 void test_checkout_tree__fails_when_conflicts_exist_in_index(void)
904 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
906 git_object
*obj
= NULL
;
908 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
910 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "HEAD"));
911 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
913 create_conflict("conflicts.txt");
915 cl_git_fail(git_checkout_tree(g_repo
, obj
, &opts
));
917 git_object_free(obj
);
920 void test_checkout_tree__filemode_preserved_in_index(void)
922 git_oid executable_oid
;
924 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
926 const git_index_entry
*entry
;
928 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
930 cl_git_pass(git_repository_index(&index
, g_repo
));
932 /* test a freshly added executable */
933 cl_git_pass(git_oid_fromstr(&executable_oid
, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6"));
934 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &executable_oid
));
936 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
937 cl_assert(entry
= git_index_get_bypath(index
, "executable.txt", 0));
938 cl_assert_equal_i(0100755, entry
->mode
);
940 git_commit_free(commit
);
943 /* Now start with a commit which has a text file */
944 cl_git_pass(git_oid_fromstr(&executable_oid
, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9"));
945 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &executable_oid
));
947 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
948 cl_assert(entry
= git_index_get_bypath(index
, "a/b.txt", 0));
949 cl_assert_equal_i(0100644, entry
->mode
);
951 git_commit_free(commit
);
954 /* And then check out to a commit which converts the text file to an executable */
955 cl_git_pass(git_oid_fromstr(&executable_oid
, "144344043ba4d4a405da03de3844aa829ae8be0e"));
956 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &executable_oid
));
958 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
959 cl_assert(entry
= git_index_get_bypath(index
, "a/b.txt", 0));
960 cl_assert_equal_i(0100755, entry
->mode
);
962 git_commit_free(commit
);
965 git_index_free(index
);
968 void test_checkout_tree__removes_conflicts(void)
972 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
975 cl_git_pass(git_oid_fromstr(&commit_id
, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6"));
976 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &commit_id
));
978 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
980 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
982 cl_git_pass(git_repository_index(&index
, g_repo
));
983 cl_git_pass(git_index_remove(index
, "executable.txt", 0));
985 create_conflict("executable.txt");
986 cl_git_mkfile("testrepo/executable.txt", "This is the conflict file.\n");
988 create_conflict("other.txt");
989 cl_git_mkfile("testrepo/other.txt", "This is another conflict file.\n");
991 git_index_write(index
);
993 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
995 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "executable.txt", 1));
996 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "executable.txt", 2));
997 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "executable.txt", 3));
999 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "other.txt", 1));
1000 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "other.txt", 2));
1001 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "other.txt", 3));
1003 cl_assert(!git_path_exists("testrepo/other.txt"));
1005 git_commit_free(commit
);
1006 git_index_free(index
);
1010 void test_checkout_tree__removes_conflicts_only_by_pathscope(void)
1014 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1016 const char *path
= "executable.txt";
1018 cl_git_pass(git_oid_fromstr(&commit_id
, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6"));
1019 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &commit_id
));
1021 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
1022 opts
.paths
.count
= 1;
1023 opts
.paths
.strings
= (char **)&path
;
1025 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1027 cl_git_pass(git_repository_index(&index
, g_repo
));
1028 cl_git_pass(git_index_remove(index
, "executable.txt", 0));
1030 create_conflict("executable.txt");
1031 cl_git_mkfile("testrepo/executable.txt", "This is the conflict file.\n");
1033 create_conflict("other.txt");
1034 cl_git_mkfile("testrepo/other.txt", "This is another conflict file.\n");
1036 git_index_write(index
);
1038 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1040 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "executable.txt", 1));
1041 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "executable.txt", 2));
1042 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "executable.txt", 3));
1044 cl_assert(git_index_get_bypath(index
, "other.txt", 1) != NULL
);
1045 cl_assert(git_index_get_bypath(index
, "other.txt", 2) != NULL
);
1046 cl_assert(git_index_get_bypath(index
, "other.txt", 3) != NULL
);
1048 cl_assert(git_path_exists("testrepo/other.txt"));
1050 git_commit_free(commit
);
1051 git_index_free(index
);
1054 void test_checkout_tree__case_changing_rename(void)
1057 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1058 git_oid master_id
, dir_commit_id
, tree_id
, commit_id
;
1059 git_commit
*master_commit
, *dir_commit
;
1061 git_signature
*signature
;
1062 const git_index_entry
*index_entry
;
1063 bool case_sensitive
;
1065 assert_on_branch(g_repo
, "master");
1067 cl_git_pass(git_repository_index(&index
, g_repo
));
1069 /* Switch branches and perform a case-changing rename */
1071 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
1073 cl_git_pass(git_reference_name_to_id(&dir_commit_id
, g_repo
, "refs/heads/dir"));
1074 cl_git_pass(git_commit_lookup(&dir_commit
, g_repo
, &dir_commit_id
));
1076 cl_git_pass(git_checkout_tree(g_repo
, (git_object
*)dir_commit
, &opts
));
1077 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/dir"));
1079 cl_assert(git_path_isfile("testrepo/README"));
1080 case_sensitive
= !git_path_isfile("testrepo/readme");
1082 cl_assert(index_entry
= git_index_get_bypath(index
, "README", 0));
1083 cl_assert_equal_s("README", index_entry
->path
);
1085 cl_git_pass(git_index_remove_bypath(index
, "README"));
1086 cl_git_pass(p_rename("testrepo/README", "testrepo/__readme__"));
1087 cl_git_pass(p_rename("testrepo/__readme__", "testrepo/readme"));
1088 cl_git_append2file("testrepo/readme", "An addendum...");
1089 cl_git_pass(git_index_add_bypath(index
, "readme"));
1091 cl_git_pass(git_index_write(index
));
1093 cl_git_pass(git_index_write_tree(&tree_id
, index
));
1094 cl_git_pass(git_tree_lookup(&tree
, g_repo
, &tree_id
));
1096 cl_git_pass(git_signature_new(&signature
, "Renamer", "rename@contoso.com", time(NULL
), 0));
1098 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
));
1100 cl_assert(git_path_isfile("testrepo/readme"));
1102 cl_assert(!git_path_isfile("testrepo/README"));
1104 cl_assert(index_entry
= git_index_get_bypath(index
, "readme", 0));
1105 cl_assert_equal_s("readme", index_entry
->path
);
1107 /* Switching back to master should rename readme -> README */
1108 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
1110 cl_git_pass(git_reference_name_to_id(&master_id
, g_repo
, "refs/heads/master"));
1111 cl_git_pass(git_commit_lookup(&master_commit
, g_repo
, &master_id
));
1113 cl_git_pass(git_checkout_tree(g_repo
, (git_object
*)master_commit
, &opts
));
1114 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/master"));
1116 assert_on_branch(g_repo
, "master");
1118 cl_assert(git_path_isfile("testrepo/README"));
1120 cl_assert(!git_path_isfile("testrepo/readme"));
1122 cl_assert(index_entry
= git_index_get_bypath(index
, "README", 0));
1123 cl_assert_equal_s("README", index_entry
->path
);
1125 git_index_free(index
);
1126 git_signature_free(signature
);
1127 git_tree_free(tree
);
1128 git_commit_free(dir_commit
);
1129 git_commit_free(master_commit
);
1132 void perfdata_cb(const git_checkout_perfdata
*in
, void *payload
)
1134 memcpy(payload
, in
, sizeof(git_checkout_perfdata
));
1137 void test_checkout_tree__can_collect_perfdata(void)
1139 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1141 git_object
*obj
= NULL
;
1142 git_checkout_perfdata perfdata
= {0};
1144 opts
.perfdata_cb
= perfdata_cb
;
1145 opts
.perfdata_payload
= &perfdata
;
1147 assert_on_branch(g_repo
, "master");
1148 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
1150 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
1151 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
1153 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
1155 cl_assert(perfdata
.mkdir_calls
> 0);
1156 cl_assert(perfdata
.stat_calls
> 0);
1158 git_object_free(obj
);
1161 void update_attr_callback(
1163 size_t completed_steps
,
1167 GIT_UNUSED(completed_steps
);
1168 GIT_UNUSED(total_steps
);
1169 GIT_UNUSED(payload
);
1171 if (path
&& strcmp(path
, "ident1.txt") == 0)
1172 cl_git_write2file("testrepo/.gitattributes",
1173 "*.txt ident\n", 12, O_RDWR
|O_CREAT
, 0666);
1176 void test_checkout_tree__caches_attributes_during_checkout(void)
1178 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1180 git_object
*obj
= NULL
;
1181 git_buf ident1
= GIT_BUF_INIT
, ident2
= GIT_BUF_INIT
;
1182 char *ident_paths
[] = { "ident1.txt", "ident2.txt" };
1184 opts
.progress_cb
= update_attr_callback
;
1186 assert_on_branch(g_repo
, "master");
1187 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
1188 opts
.paths
.strings
= ident_paths
;
1189 opts
.paths
.count
= 2;
1191 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/ident"));
1192 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
1194 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
1196 cl_git_pass(git_futils_readbuffer(&ident1
, "testrepo/ident1.txt"));
1197 cl_git_pass(git_futils_readbuffer(&ident2
, "testrepo/ident2.txt"));
1199 cl_assert_equal_strn(ident1
.ptr
, "# $Id$", 6);
1200 cl_assert_equal_strn(ident2
.ptr
, "# $Id$", 6);
1202 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
1204 cl_git_pass(git_futils_readbuffer(&ident1
, "testrepo/ident1.txt"));
1205 cl_git_pass(git_futils_readbuffer(&ident2
, "testrepo/ident2.txt"));
1207 cl_assert_equal_strn(ident1
.ptr
, "# $Id: ", 7);
1208 cl_assert_equal_strn(ident2
.ptr
, "# $Id: ", 7);
1210 git_buf_free(&ident1
);
1211 git_buf_free(&ident2
);
1212 git_object_free(obj
);
1215 void test_checkout_tree__can_not_update_index(void)
1219 unsigned int status
;
1220 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1223 opts
.checkout_strategy
|=
1224 GIT_CHECKOUT_FORCE
| GIT_CHECKOUT_DONT_UPDATE_INDEX
;
1226 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "HEAD"));
1227 cl_git_pass(git_object_lookup(&head
, g_repo
, &oid
, GIT_OBJ_ANY
));
1229 cl_git_pass(git_reset(g_repo
, head
, GIT_RESET_HARD
, &g_opts
));
1231 cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
1233 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "subtrees"));
1235 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &opts
));
1237 cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt"));
1238 cl_git_pass(git_status_file(&status
, g_repo
, "ab/de/2.txt"));
1239 cl_assert_equal_i(GIT_STATUS_WT_NEW
, status
);
1241 cl_git_pass(git_repository_index(&index
, g_repo
));
1242 cl_git_pass(git_index_write(index
));
1244 cl_git_pass(git_status_file(&status
, g_repo
, "ab/de/2.txt"));
1245 cl_assert_equal_i(GIT_STATUS_WT_NEW
, status
);
1247 git_object_free(head
);
1248 git_index_free(index
);
1251 void test_checkout_tree__can_update_but_not_write_index(void)
1255 unsigned int status
;
1256 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1258 git_repository
*other
;
1260 opts
.checkout_strategy
|=
1261 GIT_CHECKOUT_FORCE
| GIT_CHECKOUT_DONT_WRITE_INDEX
;
1263 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "HEAD"));
1264 cl_git_pass(git_object_lookup(&head
, g_repo
, &oid
, GIT_OBJ_ANY
));
1266 cl_git_pass(git_reset(g_repo
, head
, GIT_RESET_HARD
, &g_opts
));
1268 cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
1270 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "subtrees"));
1272 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &opts
));
1274 cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt"));
1275 cl_git_pass(git_status_file(&status
, g_repo
, "ab/de/2.txt"));
1276 cl_assert_equal_i(GIT_STATUS_INDEX_NEW
, status
);
1278 cl_git_pass(git_repository_open(&other
, "testrepo"));
1279 cl_git_pass(git_status_file(&status
, other
, "ab/de/2.txt"));
1280 cl_assert_equal_i(GIT_STATUS_WT_NEW
, status
);
1281 git_repository_free(other
);
1283 cl_git_pass(git_repository_index(&index
, g_repo
));
1284 cl_git_pass(git_index_write(index
));
1286 cl_git_pass(git_repository_open(&other
, "testrepo"));
1287 cl_git_pass(git_status_file(&status
, other
, "ab/de/2.txt"));
1288 cl_assert_equal_i(GIT_STATUS_INDEX_NEW
, status
);
1289 git_repository_free(other
);
1291 git_object_free(head
);
1292 git_index_free(index
);
1295 /* Emulate checking out in a repo created by clone --no-checkout,
1296 * which would not have written an index. */
1297 void test_checkout_tree__safe_proceeds_if_no_index(void)
1299 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1301 git_object
*obj
= NULL
;
1303 assert_on_branch(g_repo
, "master");
1304 cl_must_pass(p_unlink("testrepo/.git/index"));
1306 /* do second checkout safe because we should be clean after first */
1307 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
1309 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/subtrees"));
1310 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
1312 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
1313 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/subtrees"));
1315 cl_assert(git_path_isfile("testrepo/README"));
1316 cl_assert(git_path_isfile("testrepo/branch_file.txt"));
1317 cl_assert(git_path_isfile("testrepo/new.txt"));
1318 cl_assert(git_path_isfile("testrepo/ab/4.txt"));
1319 cl_assert(git_path_isfile("testrepo/ab/c/3.txt"));
1320 cl_assert(git_path_isfile("testrepo/ab/de/2.txt"));
1321 cl_assert(git_path_isfile("testrepo/ab/de/fgh/1.txt"));
1323 cl_assert(!git_path_isdir("testrepo/a"));
1325 assert_on_branch(g_repo
, "subtrees");
1327 git_object_free(obj
);
1330 void test_checkout_tree__ignores_unstaged_casechange(void)
1332 git_reference
*orig_ref
, *br2_ref
;
1333 git_commit
*orig
, *br2
;
1334 git_checkout_options checkout_opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1336 checkout_opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
1338 cl_git_pass(git_reference_lookup_resolved(&orig_ref
, g_repo
, "HEAD", 100));
1339 cl_git_pass(git_commit_lookup(&orig
, g_repo
, git_reference_target(orig_ref
)));
1340 cl_git_pass(git_reset(g_repo
, (git_object
*)orig
, GIT_RESET_HARD
, NULL
));
1342 cl_rename("testrepo/branch_file.txt", "testrepo/Branch_File.txt");
1344 cl_git_pass(git_reference_lookup_resolved(&br2_ref
, g_repo
, "refs/heads/br2", 100));
1345 cl_git_pass(git_commit_lookup(&br2
, g_repo
, git_reference_target(br2_ref
)));
1347 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)br2
, &checkout_opts
));
1349 git_commit_free(orig
);
1350 git_reference_free(orig_ref
);