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_ECONFLICT
, 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_ECONFLICT
, 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_ECONFLICT
, 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_ECONFLICT
, git_checkout_tree(g_repo
, g_object
, &g_opts
));
526 void test_checkout_tree__checking_out_a_conflicting_type_change_returns_ECONFLICT(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_ECONFLICT_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_ECONFLICT(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"));
650 /* on case-insensitive FS = a/b.txt, branch_file.txt, new.txt */
651 /* on case-sensitive FS = README, then above */
653 if (git_path_exists("testrepo/.git/CoNfIg")) /* case insensitive */
654 cl_assert_equal_i(3, ca
.count
);
656 cl_assert_equal_i(4, ca
.count
);
658 /* and again with a different stopping point and return code */
659 ca
.filename
= "README";
663 cl_git_fail_with(git_checkout_tree(g_repo
, obj
, &opts
), 123);
665 cl_assert(!git_path_exists("testrepo/new.txt"));
667 if (git_path_exists("testrepo/.git/CoNfIg")) /* case insensitive */
668 cl_assert_equal_i(4, ca
.count
);
670 cl_assert_equal_i(1, ca
.count
);
672 git_object_free(obj
);
675 void test_checkout_tree__can_checkout_with_last_workdir_item_missing(void)
677 git_index
*index
= NULL
;
678 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
679 git_oid tree_id
, commit_id
;
680 git_tree
*tree
= NULL
;
681 git_commit
*commit
= NULL
;
683 git_repository_index(&index
, g_repo
);
685 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
687 cl_git_pass(git_reference_name_to_id(&commit_id
, g_repo
, "refs/heads/master"));
688 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &commit_id
));
690 cl_git_pass(git_checkout_tree(g_repo
, (git_object
*)commit
, &opts
));
691 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/master"));
693 cl_git_pass(p_mkdir("./testrepo/this-is-dir", 0777));
694 cl_git_mkfile("./testrepo/this-is-dir/contained_file", "content\n");
696 cl_git_pass(git_index_add_bypath(index
, "this-is-dir/contained_file"));
697 git_index_write_tree(&tree_id
, index
);
698 cl_git_pass(git_tree_lookup(&tree
, g_repo
, &tree_id
));
700 cl_git_pass(p_unlink("./testrepo/this-is-dir/contained_file"));
702 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
704 opts
.checkout_strategy
= 1;
705 git_checkout_tree(g_repo
, (git_object
*)tree
, &opts
);
708 git_commit_free(commit
);
709 git_index_free(index
);
712 void test_checkout_tree__issue_1397(void)
714 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
715 const char *partial_oid
= "8a7ef04";
716 git_object
*tree
= NULL
;
718 test_checkout_tree__cleanup(); /* cleanup default checkout */
720 g_repo
= cl_git_sandbox_init("issue_1397");
722 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
724 cl_git_pass(git_revparse_single(&tree
, g_repo
, partial_oid
));
726 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
728 cl_git_pass(git_checkout_tree(g_repo
, tree
, &opts
));
730 check_file_contents("./issue_1397/crlf_file.txt", "first line\r\nsecond line\r\nboth with crlf");
732 git_object_free(tree
);
735 void test_checkout_tree__can_write_to_empty_dirs(void)
737 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
739 git_object
*obj
= NULL
;
741 assert_on_branch(g_repo
, "master");
743 cl_git_pass(p_mkdir("testrepo/a", 0777));
745 /* do first checkout with FORCE because we don't know if testrepo
746 * base data is clean for a checkout or not
748 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
750 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
751 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
753 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
755 cl_assert(git_path_isfile("testrepo/a/b.txt"));
757 git_object_free(obj
);
760 void test_checkout_tree__fails_when_dir_in_use(void)
763 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
765 git_object
*obj
= NULL
;
767 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
769 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
770 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
772 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
774 cl_assert(git_path_isfile("testrepo/a/b.txt"));
776 git_object_free(obj
);
778 cl_git_pass(p_chdir("testrepo/a"));
780 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/master"));
781 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
783 cl_git_fail(git_checkout_tree(g_repo
, obj
, &opts
));
785 cl_git_pass(p_chdir("../.."));
787 cl_assert(git_path_is_empty_dir("testrepo/a"));
789 git_object_free(obj
);
793 void test_checkout_tree__can_continue_when_dir_in_use(void)
796 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
798 git_object
*obj
= NULL
;
800 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
|
801 GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES
;
803 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
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_assert(git_path_isfile("testrepo/a/b.txt"));
810 git_object_free(obj
);
812 cl_git_pass(p_chdir("testrepo/a"));
814 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/master"));
815 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
817 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
819 cl_git_pass(p_chdir("../.."));
821 cl_assert(git_path_is_empty_dir("testrepo/a"));
823 git_object_free(obj
);
827 void test_checkout_tree__target_directory_from_bare(void)
829 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
832 memset(&cts
, 0, sizeof(cts
));
834 test_checkout_tree__cleanup(); /* cleanup default checkout */
836 g_repo
= cl_git_sandbox_init("testrepo.git");
837 cl_assert(git_repository_is_bare(g_repo
));
839 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
|
840 GIT_CHECKOUT_RECREATE_MISSING
;
842 opts
.notify_flags
= GIT_CHECKOUT_NOTIFY_ALL
;
843 opts
.notify_cb
= checkout_count_callback
;
844 opts
.notify_payload
= &cts
;
846 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "HEAD"));
847 cl_git_pass(git_object_lookup(&g_object
, g_repo
, &oid
, GIT_OBJ_ANY
));
849 cl_git_fail(git_checkout_tree(g_repo
, g_object
, &opts
));
851 opts
.target_directory
= "alternative";
852 cl_assert(!git_path_isdir("alternative"));
854 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &opts
));
856 cl_assert_equal_i(0, cts
.n_untracked
);
857 cl_assert_equal_i(0, cts
.n_ignored
);
858 cl_assert_equal_i(3, cts
.n_updates
);
860 check_file_contents_nocr("./alternative/README", "hey there\n");
861 check_file_contents_nocr("./alternative/branch_file.txt", "hi\nbye!\n");
862 check_file_contents_nocr("./alternative/new.txt", "my new file\n");
864 cl_git_pass(git_futils_rmdir_r(
865 "alternative", NULL
, GIT_RMDIR_REMOVE_FILES
));
868 void test_checkout_tree__extremely_long_file_name(void)
870 // A utf-8 string with 83 characters, but 249 bytes.
871 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";
874 g_opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
875 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "long-file-name"));
876 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
878 sprintf(path
, "testrepo/%s.txt", longname
);
879 cl_assert(git_path_exists(path
));
881 git_object_free(g_object
);
882 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "master"));
883 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &g_opts
));
884 cl_assert(!git_path_exists(path
));
887 static void create_conflict(const char *path
)
890 git_index_entry entry
;
892 cl_git_pass(git_repository_index(&index
, g_repo
));
894 memset(&entry
, 0x0, sizeof(git_index_entry
));
895 entry
.mode
= 0100644;
896 GIT_IDXENTRY_STAGE_SET(&entry
, 1);
897 git_oid_fromstr(&entry
.id
, "d427e0b2e138501a3d15cc376077a3631e15bd46");
899 cl_git_pass(git_index_add(index
, &entry
));
901 GIT_IDXENTRY_STAGE_SET(&entry
, 2);
902 git_oid_fromstr(&entry
.id
, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf");
903 cl_git_pass(git_index_add(index
, &entry
));
905 GIT_IDXENTRY_STAGE_SET(&entry
, 3);
906 git_oid_fromstr(&entry
.id
, "2bd0a343aeef7a2cf0d158478966a6e587ff3863");
907 cl_git_pass(git_index_add(index
, &entry
));
909 git_index_write(index
);
910 git_index_free(index
);
913 void test_checkout_tree__fails_when_conflicts_exist_in_index(void)
915 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
917 git_object
*obj
= NULL
;
919 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
921 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "HEAD"));
922 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
924 create_conflict("conflicts.txt");
926 cl_git_fail(git_checkout_tree(g_repo
, obj
, &opts
));
928 git_object_free(obj
);
931 void test_checkout_tree__filemode_preserved_in_index(void)
933 git_oid executable_oid
;
935 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
937 const git_index_entry
*entry
;
939 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
941 cl_git_pass(git_repository_index(&index
, g_repo
));
943 /* test a freshly added executable */
944 cl_git_pass(git_oid_fromstr(&executable_oid
, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6"));
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
, "executable.txt", 0));
949 cl_assert_equal_i(0100755, entry
->mode
);
951 git_commit_free(commit
);
954 /* Now start with a commit which has a text file */
955 cl_git_pass(git_oid_fromstr(&executable_oid
, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9"));
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(0100644, entry
->mode
);
962 git_commit_free(commit
);
965 /* And then check out to a commit which converts the text file to an executable */
966 cl_git_pass(git_oid_fromstr(&executable_oid
, "144344043ba4d4a405da03de3844aa829ae8be0e"));
967 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &executable_oid
));
969 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
970 cl_assert(entry
= git_index_get_bypath(index
, "a/b.txt", 0));
971 cl_assert_equal_i(0100755, entry
->mode
);
973 git_commit_free(commit
);
976 /* Finally, check out the text file again and check that the exec bit is cleared */
977 cl_git_pass(git_oid_fromstr(&executable_oid
, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9"));
978 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &executable_oid
));
980 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
981 cl_assert(entry
= git_index_get_bypath(index
, "a/b.txt", 0));
982 cl_assert_equal_i(0100644, entry
->mode
);
984 git_commit_free(commit
);
987 git_index_free(index
);
990 mode_t
read_filemode(const char *path
)
993 char pathabs
[256] = {0};
995 strcat(pathabs
, clar_sandbox_path());
996 strcat(pathabs
, "/testrepo/");
997 strcat(pathabs
, path
);
998 cl_must_pass(p_stat(pathabs
, &st
));
1003 void test_checkout_tree__filemode_preserved_in_workdir(void)
1006 git_oid executable_oid
;
1008 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1010 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
1012 /* test a freshly added executable */
1013 cl_git_pass(git_oid_fromstr(&executable_oid
, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6"));
1014 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &executable_oid
));
1016 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1017 cl_assert_equal_i(0100755, read_filemode("executable.txt"));
1019 git_commit_free(commit
);
1022 /* Now start with a commit which has a text file */
1023 cl_git_pass(git_oid_fromstr(&executable_oid
, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9"));
1024 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &executable_oid
));
1026 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1027 cl_assert_equal_i(0100644, read_filemode("a/b.txt"));
1029 git_commit_free(commit
);
1032 /* And then check out to a commit which converts the text file to an executable */
1033 cl_git_pass(git_oid_fromstr(&executable_oid
, "144344043ba4d4a405da03de3844aa829ae8be0e"));
1034 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &executable_oid
));
1036 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1037 cl_assert_equal_i(0100755, read_filemode("a/b.txt"));
1039 git_commit_free(commit
);
1042 /* Finally, check out the text file again and check that the exec bit is cleared */
1043 cl_git_pass(git_oid_fromstr(&executable_oid
, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9"));
1044 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &executable_oid
));
1046 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1047 cl_assert_equal_i(0100644, read_filemode("a/b.txt"));
1049 git_commit_free(commit
);
1053 void test_checkout_tree__removes_conflicts(void)
1057 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1060 cl_git_pass(git_oid_fromstr(&commit_id
, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6"));
1061 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &commit_id
));
1063 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
1065 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1067 cl_git_pass(git_repository_index(&index
, g_repo
));
1068 cl_git_pass(git_index_remove(index
, "executable.txt", 0));
1070 create_conflict("executable.txt");
1071 cl_git_mkfile("testrepo/executable.txt", "This is the conflict file.\n");
1073 create_conflict("other.txt");
1074 cl_git_mkfile("testrepo/other.txt", "This is another conflict file.\n");
1076 git_index_write(index
);
1078 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1080 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "executable.txt", 1));
1081 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "executable.txt", 2));
1082 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "executable.txt", 3));
1084 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "other.txt", 1));
1085 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "other.txt", 2));
1086 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "other.txt", 3));
1088 cl_assert(!git_path_exists("testrepo/other.txt"));
1090 git_commit_free(commit
);
1091 git_index_free(index
);
1095 void test_checkout_tree__removes_conflicts_only_by_pathscope(void)
1099 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1101 const char *path
= "executable.txt";
1103 cl_git_pass(git_oid_fromstr(&commit_id
, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6"));
1104 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &commit_id
));
1106 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
1107 opts
.paths
.count
= 1;
1108 opts
.paths
.strings
= (char **)&path
;
1110 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1112 cl_git_pass(git_repository_index(&index
, g_repo
));
1113 cl_git_pass(git_index_remove(index
, "executable.txt", 0));
1115 create_conflict("executable.txt");
1116 cl_git_mkfile("testrepo/executable.txt", "This is the conflict file.\n");
1118 create_conflict("other.txt");
1119 cl_git_mkfile("testrepo/other.txt", "This is another conflict file.\n");
1121 git_index_write(index
);
1123 cl_git_pass(git_checkout_tree(g_repo
, (const git_object
*)commit
, &opts
));
1125 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "executable.txt", 1));
1126 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "executable.txt", 2));
1127 cl_assert_equal_p(NULL
, git_index_get_bypath(index
, "executable.txt", 3));
1129 cl_assert(git_index_get_bypath(index
, "other.txt", 1) != NULL
);
1130 cl_assert(git_index_get_bypath(index
, "other.txt", 2) != NULL
);
1131 cl_assert(git_index_get_bypath(index
, "other.txt", 3) != NULL
);
1133 cl_assert(git_path_exists("testrepo/other.txt"));
1135 git_commit_free(commit
);
1136 git_index_free(index
);
1139 void test_checkout_tree__case_changing_rename(void)
1142 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1143 git_oid master_id
, dir_commit_id
, tree_id
, commit_id
;
1144 git_commit
*master_commit
, *dir_commit
;
1146 git_signature
*signature
;
1147 const git_index_entry
*index_entry
;
1148 bool case_sensitive
;
1150 assert_on_branch(g_repo
, "master");
1152 cl_git_pass(git_repository_index(&index
, g_repo
));
1154 /* Switch branches and perform a case-changing rename */
1156 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
1158 cl_git_pass(git_reference_name_to_id(&dir_commit_id
, g_repo
, "refs/heads/dir"));
1159 cl_git_pass(git_commit_lookup(&dir_commit
, g_repo
, &dir_commit_id
));
1161 cl_git_pass(git_checkout_tree(g_repo
, (git_object
*)dir_commit
, &opts
));
1162 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/dir"));
1164 cl_assert(git_path_isfile("testrepo/README"));
1165 case_sensitive
= !git_path_isfile("testrepo/readme");
1167 cl_assert(index_entry
= git_index_get_bypath(index
, "README", 0));
1168 cl_assert_equal_s("README", index_entry
->path
);
1170 cl_git_pass(git_index_remove_bypath(index
, "README"));
1171 cl_git_pass(p_rename("testrepo/README", "testrepo/__readme__"));
1172 cl_git_pass(p_rename("testrepo/__readme__", "testrepo/readme"));
1173 cl_git_append2file("testrepo/readme", "An addendum...");
1174 cl_git_pass(git_index_add_bypath(index
, "readme"));
1176 cl_git_pass(git_index_write(index
));
1178 cl_git_pass(git_index_write_tree(&tree_id
, index
));
1179 cl_git_pass(git_tree_lookup(&tree
, g_repo
, &tree_id
));
1181 cl_git_pass(git_signature_new(&signature
, "Renamer", "rename@contoso.com", time(NULL
), 0));
1183 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
));
1185 cl_assert(git_path_isfile("testrepo/readme"));
1187 cl_assert(!git_path_isfile("testrepo/README"));
1189 cl_assert(index_entry
= git_index_get_bypath(index
, "readme", 0));
1190 cl_assert_equal_s("readme", index_entry
->path
);
1192 /* Switching back to master should rename readme -> README */
1193 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
1195 cl_git_pass(git_reference_name_to_id(&master_id
, g_repo
, "refs/heads/master"));
1196 cl_git_pass(git_commit_lookup(&master_commit
, g_repo
, &master_id
));
1198 cl_git_pass(git_checkout_tree(g_repo
, (git_object
*)master_commit
, &opts
));
1199 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/master"));
1201 assert_on_branch(g_repo
, "master");
1203 cl_assert(git_path_isfile("testrepo/README"));
1205 cl_assert(!git_path_isfile("testrepo/readme"));
1207 cl_assert(index_entry
= git_index_get_bypath(index
, "README", 0));
1208 cl_assert_equal_s("README", index_entry
->path
);
1210 git_index_free(index
);
1211 git_signature_free(signature
);
1212 git_tree_free(tree
);
1213 git_commit_free(dir_commit
);
1214 git_commit_free(master_commit
);
1217 void perfdata_cb(const git_checkout_perfdata
*in
, void *payload
)
1219 memcpy(payload
, in
, sizeof(git_checkout_perfdata
));
1222 void test_checkout_tree__can_collect_perfdata(void)
1224 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1226 git_object
*obj
= NULL
;
1227 git_checkout_perfdata perfdata
= {0};
1229 opts
.perfdata_cb
= perfdata_cb
;
1230 opts
.perfdata_payload
= &perfdata
;
1232 assert_on_branch(g_repo
, "master");
1233 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
1235 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/dir"));
1236 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
1238 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
1240 cl_assert(perfdata
.mkdir_calls
> 0);
1241 cl_assert(perfdata
.stat_calls
> 0);
1243 git_object_free(obj
);
1246 void update_attr_callback(
1248 size_t completed_steps
,
1252 GIT_UNUSED(completed_steps
);
1253 GIT_UNUSED(total_steps
);
1254 GIT_UNUSED(payload
);
1256 if (path
&& strcmp(path
, "ident1.txt") == 0)
1257 cl_git_write2file("testrepo/.gitattributes",
1258 "*.txt ident\n", 12, O_RDWR
|O_CREAT
, 0666);
1261 void test_checkout_tree__caches_attributes_during_checkout(void)
1263 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1265 git_object
*obj
= NULL
;
1266 git_buf ident1
= GIT_BUF_INIT
, ident2
= GIT_BUF_INIT
;
1267 char *ident_paths
[] = { "ident1.txt", "ident2.txt" };
1269 opts
.progress_cb
= update_attr_callback
;
1271 assert_on_branch(g_repo
, "master");
1272 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
1273 opts
.paths
.strings
= ident_paths
;
1274 opts
.paths
.count
= 2;
1276 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/ident"));
1277 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
1279 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
1281 cl_git_pass(git_futils_readbuffer(&ident1
, "testrepo/ident1.txt"));
1282 cl_git_pass(git_futils_readbuffer(&ident2
, "testrepo/ident2.txt"));
1284 cl_assert_equal_strn(ident1
.ptr
, "# $Id$", 6);
1285 cl_assert_equal_strn(ident2
.ptr
, "# $Id$", 6);
1287 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
1289 cl_git_pass(git_futils_readbuffer(&ident1
, "testrepo/ident1.txt"));
1290 cl_git_pass(git_futils_readbuffer(&ident2
, "testrepo/ident2.txt"));
1292 cl_assert_equal_strn(ident1
.ptr
, "# $Id: ", 7);
1293 cl_assert_equal_strn(ident2
.ptr
, "# $Id: ", 7);
1295 git_buf_free(&ident1
);
1296 git_buf_free(&ident2
);
1297 git_object_free(obj
);
1300 void test_checkout_tree__can_not_update_index(void)
1304 unsigned int status
;
1305 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1308 opts
.checkout_strategy
|=
1309 GIT_CHECKOUT_FORCE
| GIT_CHECKOUT_DONT_UPDATE_INDEX
;
1311 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "HEAD"));
1312 cl_git_pass(git_object_lookup(&head
, g_repo
, &oid
, GIT_OBJ_ANY
));
1314 cl_git_pass(git_reset(g_repo
, head
, GIT_RESET_HARD
, &g_opts
));
1316 cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
1318 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "subtrees"));
1320 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &opts
));
1322 cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt"));
1323 cl_git_pass(git_status_file(&status
, g_repo
, "ab/de/2.txt"));
1324 cl_assert_equal_i(GIT_STATUS_WT_NEW
, status
);
1326 cl_git_pass(git_repository_index(&index
, g_repo
));
1327 cl_git_pass(git_index_write(index
));
1329 cl_git_pass(git_status_file(&status
, g_repo
, "ab/de/2.txt"));
1330 cl_assert_equal_i(GIT_STATUS_WT_NEW
, status
);
1332 git_object_free(head
);
1333 git_index_free(index
);
1336 void test_checkout_tree__can_update_but_not_write_index(void)
1340 unsigned int status
;
1341 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1343 git_repository
*other
;
1345 opts
.checkout_strategy
|=
1346 GIT_CHECKOUT_FORCE
| GIT_CHECKOUT_DONT_WRITE_INDEX
;
1348 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "HEAD"));
1349 cl_git_pass(git_object_lookup(&head
, g_repo
, &oid
, GIT_OBJ_ANY
));
1351 cl_git_pass(git_reset(g_repo
, head
, GIT_RESET_HARD
, &g_opts
));
1353 cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
1355 cl_git_pass(git_revparse_single(&g_object
, g_repo
, "subtrees"));
1357 cl_git_pass(git_checkout_tree(g_repo
, g_object
, &opts
));
1359 cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt"));
1360 cl_git_pass(git_status_file(&status
, g_repo
, "ab/de/2.txt"));
1361 cl_assert_equal_i(GIT_STATUS_INDEX_NEW
, status
);
1363 cl_git_pass(git_repository_open(&other
, "testrepo"));
1364 cl_git_pass(git_status_file(&status
, other
, "ab/de/2.txt"));
1365 cl_assert_equal_i(GIT_STATUS_WT_NEW
, status
);
1366 git_repository_free(other
);
1368 cl_git_pass(git_repository_index(&index
, g_repo
));
1369 cl_git_pass(git_index_write(index
));
1371 cl_git_pass(git_repository_open(&other
, "testrepo"));
1372 cl_git_pass(git_status_file(&status
, other
, "ab/de/2.txt"));
1373 cl_assert_equal_i(GIT_STATUS_INDEX_NEW
, status
);
1374 git_repository_free(other
);
1376 git_object_free(head
);
1377 git_index_free(index
);
1380 /* Emulate checking out in a repo created by clone --no-checkout,
1381 * which would not have written an index. */
1382 void test_checkout_tree__safe_proceeds_if_no_index(void)
1384 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
1386 git_object
*obj
= NULL
;
1388 assert_on_branch(g_repo
, "master");
1389 cl_must_pass(p_unlink("testrepo/.git/index"));
1391 /* do second checkout safe because we should be clean after first */
1392 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
1394 cl_git_pass(git_reference_name_to_id(&oid
, g_repo
, "refs/heads/subtrees"));
1395 cl_git_pass(git_object_lookup(&obj
, g_repo
, &oid
, GIT_OBJ_ANY
));
1397 cl_git_pass(git_checkout_tree(g_repo
, obj
, &opts
));
1398 cl_git_pass(git_repository_set_head(g_repo
, "refs/heads/subtrees"));
1400 cl_assert(git_path_isfile("testrepo/README"));
1401 cl_assert(git_path_isfile("testrepo/branch_file.txt"));
1402 cl_assert(git_path_isfile("testrepo/new.txt"));
1403 cl_assert(git_path_isfile("testrepo/ab/4.txt"));
1404 cl_assert(git_path_isfile("testrepo/ab/c/3.txt"));
1405 cl_assert(git_path_isfile("testrepo/ab/de/2.txt"));
1406 cl_assert(git_path_isfile("testrepo/ab/de/fgh/1.txt"));
1408 cl_assert(!git_path_isdir("testrepo/a"));
1410 assert_on_branch(g_repo
, "subtrees");
1412 git_object_free(obj
);