]> git.proxmox.com Git - libgit2.git/blob - tests/checkout/tree.c
Merge pull request #3155 from mgorny/userpass-const
[libgit2.git] / tests / checkout / tree.c
1 #include "clar_libgit2.h"
2 #include "checkout_helpers.h"
3
4 #include "git2/checkout.h"
5 #include "repository.h"
6 #include "buffer.h"
7 #include "fileops.h"
8
9 static git_repository *g_repo;
10 static git_checkout_options g_opts;
11 static git_object *g_object;
12
13 void test_checkout_tree__initialize(void)
14 {
15 g_repo = cl_git_sandbox_init("testrepo");
16
17 GIT_INIT_STRUCTURE(&g_opts, GIT_CHECKOUT_OPTIONS_VERSION);
18 g_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
19 }
20
21 void test_checkout_tree__cleanup(void)
22 {
23 git_object_free(g_object);
24 g_object = NULL;
25
26 cl_git_sandbox_cleanup();
27
28 if (git_path_isdir("alternative"))
29 git_futils_rmdir_r("alternative", NULL, GIT_RMDIR_REMOVE_FILES);
30 }
31
32 void test_checkout_tree__cannot_checkout_a_non_treeish(void)
33 {
34 /* blob */
35 cl_git_pass(git_revparse_single(&g_object, g_repo, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"));
36 cl_git_fail(git_checkout_tree(g_repo, g_object, NULL));
37 }
38
39 void test_checkout_tree__can_checkout_a_subdirectory_from_a_commit(void)
40 {
41 char *entries[] = { "ab/de/" };
42
43 g_opts.paths.strings = entries;
44 g_opts.paths.count = 1;
45
46 cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));
47
48 cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
49
50 cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
51
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"));
54 }
55
56 void test_checkout_tree__can_checkout_and_remove_directory(void)
57 {
58 cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
59
60 /* Checkout brach "subtrees" and update HEAD, so that HEAD matches the
61 * current working tree
62 */
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));
65
66 cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
67
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"));
71
72 git_object_free(g_object);
73 g_object = NULL;
74
75 /* Checkout brach "master" and update HEAD, so that HEAD matches the
76 * current working tree
77 */
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));
80
81 cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master"));
82
83 /* This directory should no longer exist */
84 cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
85 }
86
87 void test_checkout_tree__can_checkout_a_subdirectory_from_a_subtree(void)
88 {
89 char *entries[] = { "de/" };
90
91 g_opts.paths.strings = entries;
92 g_opts.paths.count = 1;
93
94 cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees:ab"));
95
96 cl_assert_equal_i(false, git_path_isdir("./testrepo/de/"));
97
98 cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
99
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"));
102 }
103
104 static void progress(const char *path, size_t cur, size_t tot, void *payload)
105 {
106 bool *was_called = (bool*)payload;
107 GIT_UNUSED(path); GIT_UNUSED(cur); GIT_UNUSED(tot);
108 *was_called = true;
109 }
110
111 void test_checkout_tree__calls_progress_callback(void)
112 {
113 bool was_called = 0;
114
115 g_opts.progress_cb = progress;
116 g_opts.progress_payload = &was_called;
117
118 cl_git_pass(git_revparse_single(&g_object, g_repo, "master"));
119
120 cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
121
122 cl_assert_equal_i(was_called, true);
123 }
124
125 void test_checkout_tree__doesnt_write_unrequested_files_to_worktree(void)
126 {
127 git_oid master_oid;
128 git_oid chomped_oid;
129 git_commit* p_master_commit;
130 git_commit* p_chomped_commit;
131 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
132
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));
137
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.
140 */
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"));
144
145 git_commit_free(p_master_commit);
146 git_commit_free(p_chomped_commit);
147 }
148
149 void test_checkout_tree__can_switch_branches(void)
150 {
151 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
152 git_oid oid;
153 git_object *obj = NULL;
154
155 assert_on_branch(g_repo, "master");
156
157 /* do first checkout with FORCE because we don't know if testrepo
158 * base data is clean for a checkout or not
159 */
160 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
161
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));
164
165 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
166 cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir"));
167
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"));
172
173 cl_assert(!git_path_isdir("testrepo/ab"));
174
175 assert_on_branch(g_repo, "dir");
176
177 git_object_free(obj);
178
179 /* do second checkout safe because we should be clean after first */
180 opts.checkout_strategy = GIT_CHECKOUT_SAFE;
181
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));
184
185 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
186 cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
187
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"));
195
196 cl_assert(!git_path_isdir("testrepo/a"));
197
198 assert_on_branch(g_repo, "subtrees");
199
200 git_object_free(obj);
201 }
202
203 void test_checkout_tree__can_remove_untracked(void)
204 {
205 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
206
207 opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_REMOVE_UNTRACKED;
208
209 cl_git_mkfile("testrepo/untracked_file", "as you wish");
210 cl_assert(git_path_isfile("testrepo/untracked_file"));
211
212 cl_git_pass(git_checkout_head(g_repo, &opts));
213
214 cl_assert(!git_path_isfile("testrepo/untracked_file"));
215 }
216
217 void test_checkout_tree__can_remove_ignored(void)
218 {
219 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
220 int ignored = 0;
221
222 opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_REMOVE_IGNORED;
223
224 cl_git_mkfile("testrepo/ignored_file", "as you wish");
225
226 cl_git_pass(git_ignore_add_rule(g_repo, "ignored_file\n"));
227
228 cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "ignored_file"));
229 cl_assert_equal_i(1, ignored);
230
231 cl_assert(git_path_isfile("testrepo/ignored_file"));
232
233 cl_git_pass(git_checkout_head(g_repo, &opts));
234
235 cl_assert(!git_path_isfile("testrepo/ignored_file"));
236 }
237
238 static int checkout_tree_with_blob_ignored_in_workdir(int strategy, bool isdir)
239 {
240 git_oid oid;
241 git_object *obj = NULL;
242 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
243 int ignored = 0, error;
244
245 assert_on_branch(g_repo, "master");
246
247 /* do first checkout with FORCE because we don't know if testrepo
248 * base data is clean for a checkout or not
249 */
250 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
251
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));
254
255 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
256 cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir"));
257
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"));
262
263 cl_assert(!git_path_isdir("testrepo/ab"));
264
265 assert_on_branch(g_repo, "dir");
266
267 git_object_free(obj);
268
269 opts.checkout_strategy = strategy;
270
271 if (isdir) {
272 cl_must_pass(p_mkdir("testrepo/ab", 0777));
273 cl_must_pass(p_mkdir("testrepo/ab/4.txt", 0777));
274
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");
278
279 cl_assert(git_path_isdir("testrepo/ab/4.txt"));
280 } else {
281 cl_must_pass(p_mkdir("testrepo/ab", 0777));
282 cl_git_mkfile("testrepo/ab/4.txt", "as you wish");
283
284 cl_assert(git_path_isfile("testrepo/ab/4.txt"));
285 }
286
287 cl_git_pass(git_ignore_add_rule(g_repo, "ab/4.txt\n"));
288
289 cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "ab/4.txt"));
290 cl_assert_equal_i(1, ignored);
291
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));
294
295 error = git_checkout_tree(g_repo, obj, &opts);
296
297 git_object_free(obj);
298
299 return error;
300 }
301
302 void test_checkout_tree__conflict_on_ignored_when_not_overwriting(void)
303 {
304 int error;
305
306 cl_git_fail(error = checkout_tree_with_blob_ignored_in_workdir(
307 GIT_CHECKOUT_SAFE | GIT_CHECKOUT_DONT_OVERWRITE_IGNORED, false));
308
309 cl_assert_equal_i(GIT_ECONFLICT, error);
310 }
311
312 void test_checkout_tree__can_overwrite_ignored_by_default(void)
313 {
314 cl_git_pass(checkout_tree_with_blob_ignored_in_workdir(GIT_CHECKOUT_SAFE, false));
315
316 cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
317
318 cl_assert(git_path_isfile("testrepo/ab/4.txt"));
319
320 assert_on_branch(g_repo, "subtrees");
321 }
322
323 void test_checkout_tree__conflict_on_ignored_folder_when_not_overwriting(void)
324 {
325 int error;
326
327 cl_git_fail(error = checkout_tree_with_blob_ignored_in_workdir(
328 GIT_CHECKOUT_SAFE | GIT_CHECKOUT_DONT_OVERWRITE_IGNORED, true));
329
330 cl_assert_equal_i(GIT_ECONFLICT, error);
331 }
332
333 void test_checkout_tree__can_overwrite_ignored_folder_by_default(void)
334 {
335 cl_git_pass(checkout_tree_with_blob_ignored_in_workdir(GIT_CHECKOUT_SAFE, true));
336
337 cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
338
339 cl_assert(git_path_isfile("testrepo/ab/4.txt"));
340
341 assert_on_branch(g_repo, "subtrees");
342
343 }
344
345 void test_checkout_tree__can_update_only(void)
346 {
347 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
348 git_oid oid;
349 git_object *obj = NULL;
350
351 /* first let's get things into a known state - by checkout out the HEAD */
352
353 assert_on_branch(g_repo, "master");
354
355 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
356 cl_git_pass(git_checkout_head(g_repo, &opts));
357
358 cl_assert(!git_path_isdir("testrepo/a"));
359
360 check_file_contents_nocr("testrepo/branch_file.txt", "hi\nbye!\n");
361
362 /* now checkout branch but with update only */
363
364 opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_UPDATE_ONLY;
365
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));
368
369 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
370 cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir"));
371
372 assert_on_branch(g_repo, "dir");
373
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.
377 */
378 cl_assert(!git_path_isdir("testrepo/a"));
379
380 /* but this file still should have been updated */
381 check_file_contents_nocr("testrepo/branch_file.txt", "hi\n");
382
383 git_object_free(obj);
384 }
385
386 void test_checkout_tree__can_checkout_with_pattern(void)
387 {
388 char *entries[] = { "[l-z]*.txt" };
389
390 /* reset to beginning of history (i.e. just a README file) */
391
392 g_opts.checkout_strategy =
393 GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED;
394
395 cl_git_pass(git_revparse_single(&g_object, g_repo, "8496071c1b46c854b31185ea97743be6a8774479"));
396
397 cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
398 cl_git_pass(
399 git_repository_set_head_detached(g_repo, git_object_id(g_object)));
400
401 git_object_free(g_object);
402 g_object = NULL;
403
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"));
408
409 /* now to a narrow patterned checkout */
410
411 g_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
412 g_opts.paths.strings = entries;
413 g_opts.paths.count = 1;
414
415 cl_git_pass(git_revparse_single(&g_object, g_repo, "refs/heads/master"));
416
417 cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
418
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"));
423 }
424
425 void test_checkout_tree__can_disable_pattern_match(void)
426 {
427 char *entries[] = { "b*.txt" };
428
429 /* reset to beginning of history (i.e. just a README file) */
430
431 g_opts.checkout_strategy =
432 GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED;
433
434 cl_git_pass(git_revparse_single(&g_object, g_repo, "8496071c1b46c854b31185ea97743be6a8774479"));
435
436 cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
437 cl_git_pass(
438 git_repository_set_head_detached(g_repo, git_object_id(g_object)));
439
440 git_object_free(g_object);
441 g_object = NULL;
442
443 cl_assert(!git_path_isfile("testrepo/branch_file.txt"));
444
445 /* now to a narrow patterned checkout, but disable pattern */
446
447 g_opts.checkout_strategy =
448 GIT_CHECKOUT_SAFE |
449 GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH;
450 g_opts.paths.strings = entries;
451 g_opts.paths.count = 1;
452
453 cl_git_pass(git_revparse_single(&g_object, g_repo, "refs/heads/master"));
454
455 cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
456
457 cl_assert(!git_path_isfile("testrepo/branch_file.txt"));
458
459 /* let's try that again, but allow the pattern match */
460
461 g_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
462
463 cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
464
465 cl_assert(git_path_isfile("testrepo/branch_file.txt"));
466 }
467
468 void assert_conflict(
469 const char *entry_path,
470 const char *new_content,
471 const char *parent_sha,
472 const char *commit_sha)
473 {
474 git_index *index;
475 git_object *hack_tree;
476 git_reference *branch, *head;
477 git_buf file_path = GIT_BUF_INIT;
478
479 cl_git_pass(git_repository_index(&index, g_repo));
480
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));
485
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);
491
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));
495
496 /* Hack-ishy workaound to ensure *all* the index entries
497 * match the content of the tree
498 */
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);
503 g_object = NULL;
504
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);
509
510 /* Trying to checkout the original commit */
511 cl_git_pass(git_revparse_single(&g_object, g_repo, commit_sha));
512
513 g_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
514 cl_assert_equal_i(
515 GIT_ECONFLICT, git_checkout_tree(g_repo, g_object, &g_opts));
516
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);
521
522 cl_assert_equal_i(
523 GIT_ECONFLICT, git_checkout_tree(g_repo, g_object, &g_opts));
524 }
525
526 void test_checkout_tree__checking_out_a_conflicting_type_change_returns_ECONFLICT(void)
527 {
528 /*
529 * 099faba adds a symlink named 'link_to_new.txt'
530 * a65fedf is the parent of 099faba
531 */
532
533 assert_conflict("link_to_new.txt", "old.txt", "a65fedf", "099faba");
534 }
535
536 void test_checkout_tree__checking_out_a_conflicting_type_change_returns_ECONFLICT_2(void)
537 {
538 /*
539 * cf80f8d adds a directory named 'a/'
540 * a4a7dce is the parent of cf80f8d
541 */
542
543 assert_conflict("a", "hello\n", "a4a7dce", "cf80f8d");
544 }
545
546 void test_checkout_tree__checking_out_a_conflicting_content_change_returns_ECONFLICT(void)
547 {
548 /*
549 * c47800c adds a symlink named 'branch_file.txt'
550 * 5b5b025 is the parent of 763d71a
551 */
552
553 assert_conflict("branch_file.txt", "hello\n", "5b5b025", "c47800c");
554 }
555
556 void test_checkout_tree__donot_update_deleted_file_by_default(void)
557 {
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;
562 checkout_counts ct;
563
564 opts.checkout_strategy = GIT_CHECKOUT_SAFE;
565
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;
570
571 cl_git_pass(git_repository_index(&index, g_repo));
572
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));
576
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));
580
581 cl_assert(!git_path_exists("testrepo/branch_file.txt"));
582
583 cl_git_pass(git_oid_fromstr(&new_id, "099fabac3a9ea935598528c27f866e34089c2eff"));
584 cl_git_pass(git_commit_lookup(&new_commit, g_repo, &new_id));
585
586
587 cl_git_fail(git_checkout_tree(g_repo, (git_object *)new_commit, &opts));
588
589 cl_assert_equal_i(1, ct.n_conflicts);
590 cl_assert_equal_i(1, ct.n_updates);
591
592 git_commit_free(old_commit);
593 git_commit_free(new_commit);
594 git_index_free(index);
595 }
596
597 struct checkout_cancel_at {
598 const char *filename;
599 int error;
600 int count;
601 };
602
603 static int checkout_cancel_cb(
604 git_checkout_notify_t why,
605 const char *path,
606 const git_diff_file *b,
607 const git_diff_file *t,
608 const git_diff_file *w,
609 void *payload)
610 {
611 struct checkout_cancel_at *ca = payload;
612
613 GIT_UNUSED(why); GIT_UNUSED(b); GIT_UNUSED(t); GIT_UNUSED(w);
614
615 ca->count++;
616
617 if (!strcmp(path, ca->filename))
618 return ca->error;
619
620 return 0;
621 }
622
623 void test_checkout_tree__can_cancel_checkout_from_notify(void)
624 {
625 struct checkout_cancel_at ca;
626 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
627 git_oid oid;
628 git_object *obj = NULL;
629
630 assert_on_branch(g_repo, "master");
631
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));
634
635 ca.filename = "new.txt";
636 ca.error = -5555;
637 ca.count = 0;
638
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;
643
644 cl_assert(!git_path_exists("testrepo/new.txt"));
645
646 cl_git_fail_with(git_checkout_tree(g_repo, obj, &opts), -5555);
647
648 cl_assert(!git_path_exists("testrepo/new.txt"));
649
650 /* on case-insensitive FS = a/b.txt, branch_file.txt, new.txt */
651 /* on case-sensitive FS = README, then above */
652
653 if (git_path_exists("testrepo/.git/CoNfIg")) /* case insensitive */
654 cl_assert_equal_i(3, ca.count);
655 else
656 cl_assert_equal_i(4, ca.count);
657
658 /* and again with a different stopping point and return code */
659 ca.filename = "README";
660 ca.error = 123;
661 ca.count = 0;
662
663 cl_git_fail_with(git_checkout_tree(g_repo, obj, &opts), 123);
664
665 cl_assert(!git_path_exists("testrepo/new.txt"));
666
667 if (git_path_exists("testrepo/.git/CoNfIg")) /* case insensitive */
668 cl_assert_equal_i(4, ca.count);
669 else
670 cl_assert_equal_i(1, ca.count);
671
672 git_object_free(obj);
673 }
674
675 void test_checkout_tree__can_checkout_with_last_workdir_item_missing(void)
676 {
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;
682
683 git_repository_index(&index, g_repo);
684
685 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
686
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));
689
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"));
692
693 cl_git_pass(p_mkdir("./testrepo/this-is-dir", 0777));
694 cl_git_mkfile("./testrepo/this-is-dir/contained_file", "content\n");
695
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));
699
700 cl_git_pass(p_unlink("./testrepo/this-is-dir/contained_file"));
701
702 opts.checkout_strategy = GIT_CHECKOUT_SAFE;
703
704 opts.checkout_strategy = 1;
705 git_checkout_tree(g_repo, (git_object *)tree, &opts);
706
707 git_tree_free(tree);
708 git_commit_free(commit);
709 git_index_free(index);
710 }
711
712 void test_checkout_tree__issue_1397(void)
713 {
714 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
715 const char *partial_oid = "8a7ef04";
716 git_object *tree = NULL;
717
718 test_checkout_tree__cleanup(); /* cleanup default checkout */
719
720 g_repo = cl_git_sandbox_init("issue_1397");
721
722 cl_repo_set_bool(g_repo, "core.autocrlf", true);
723
724 cl_git_pass(git_revparse_single(&tree, g_repo, partial_oid));
725
726 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
727
728 cl_git_pass(git_checkout_tree(g_repo, tree, &opts));
729
730 check_file_contents("./issue_1397/crlf_file.txt", "first line\r\nsecond line\r\nboth with crlf");
731
732 git_object_free(tree);
733 }
734
735 void test_checkout_tree__can_write_to_empty_dirs(void)
736 {
737 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
738 git_oid oid;
739 git_object *obj = NULL;
740
741 assert_on_branch(g_repo, "master");
742
743 cl_git_pass(p_mkdir("testrepo/a", 0777));
744
745 /* do first checkout with FORCE because we don't know if testrepo
746 * base data is clean for a checkout or not
747 */
748 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
749
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));
752
753 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
754
755 cl_assert(git_path_isfile("testrepo/a/b.txt"));
756
757 git_object_free(obj);
758 }
759
760 void test_checkout_tree__fails_when_dir_in_use(void)
761 {
762 #ifdef GIT_WIN32
763 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
764 git_oid oid;
765 git_object *obj = NULL;
766
767 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
768
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));
771
772 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
773
774 cl_assert(git_path_isfile("testrepo/a/b.txt"));
775
776 git_object_free(obj);
777
778 cl_git_pass(p_chdir("testrepo/a"));
779
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));
782
783 cl_git_fail(git_checkout_tree(g_repo, obj, &opts));
784
785 cl_git_pass(p_chdir("../.."));
786
787 cl_assert(git_path_is_empty_dir("testrepo/a"));
788
789 git_object_free(obj);
790 #endif
791 }
792
793 void test_checkout_tree__can_continue_when_dir_in_use(void)
794 {
795 #ifdef GIT_WIN32
796 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
797 git_oid oid;
798 git_object *obj = NULL;
799
800 opts.checkout_strategy = GIT_CHECKOUT_FORCE |
801 GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES;
802
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));
805
806 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
807
808 cl_assert(git_path_isfile("testrepo/a/b.txt"));
809
810 git_object_free(obj);
811
812 cl_git_pass(p_chdir("testrepo/a"));
813
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));
816
817 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
818
819 cl_git_pass(p_chdir("../.."));
820
821 cl_assert(git_path_is_empty_dir("testrepo/a"));
822
823 git_object_free(obj);
824 #endif
825 }
826
827 void test_checkout_tree__target_directory_from_bare(void)
828 {
829 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
830 git_oid oid;
831 checkout_counts cts;
832 memset(&cts, 0, sizeof(cts));
833
834 test_checkout_tree__cleanup(); /* cleanup default checkout */
835
836 g_repo = cl_git_sandbox_init("testrepo.git");
837 cl_assert(git_repository_is_bare(g_repo));
838
839 opts.checkout_strategy = GIT_CHECKOUT_SAFE |
840 GIT_CHECKOUT_RECREATE_MISSING;
841
842 opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL;
843 opts.notify_cb = checkout_count_callback;
844 opts.notify_payload = &cts;
845
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));
848
849 cl_git_fail(git_checkout_tree(g_repo, g_object, &opts));
850
851 opts.target_directory = "alternative";
852 cl_assert(!git_path_isdir("alternative"));
853
854 cl_git_pass(git_checkout_tree(g_repo, g_object, &opts));
855
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);
859
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");
863
864 cl_git_pass(git_futils_rmdir_r(
865 "alternative", NULL, GIT_RMDIR_REMOVE_FILES));
866 }
867
868 void test_checkout_tree__extremely_long_file_name(void)
869 {
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";
872 char path[1024];
873
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));
877
878 sprintf(path, "testrepo/%s.txt", longname);
879 cl_assert(git_path_exists(path));
880
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));
885 }
886
887 static void create_conflict(const char *path)
888 {
889 git_index *index;
890 git_index_entry entry;
891
892 cl_git_pass(git_repository_index(&index, g_repo));
893
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");
898 entry.path = path;
899 cl_git_pass(git_index_add(index, &entry));
900
901 GIT_IDXENTRY_STAGE_SET(&entry, 2);
902 git_oid_fromstr(&entry.id, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf");
903 cl_git_pass(git_index_add(index, &entry));
904
905 GIT_IDXENTRY_STAGE_SET(&entry, 3);
906 git_oid_fromstr(&entry.id, "2bd0a343aeef7a2cf0d158478966a6e587ff3863");
907 cl_git_pass(git_index_add(index, &entry));
908
909 git_index_write(index);
910 git_index_free(index);
911 }
912
913 void test_checkout_tree__fails_when_conflicts_exist_in_index(void)
914 {
915 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
916 git_oid oid;
917 git_object *obj = NULL;
918
919 opts.checkout_strategy = GIT_CHECKOUT_SAFE;
920
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));
923
924 create_conflict("conflicts.txt");
925
926 cl_git_fail(git_checkout_tree(g_repo, obj, &opts));
927
928 git_object_free(obj);
929 }
930
931 void test_checkout_tree__filemode_preserved_in_index(void)
932 {
933 git_oid executable_oid;
934 git_commit *commit;
935 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
936 git_index *index;
937 const git_index_entry *entry;
938
939 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
940
941 cl_git_pass(git_repository_index(&index, g_repo));
942
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));
946
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);
950
951 git_commit_free(commit);
952
953
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));
957
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);
961
962 git_commit_free(commit);
963
964
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));
968
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);
972
973 git_commit_free(commit);
974
975
976 git_index_free(index);
977 }
978
979 void test_checkout_tree__removes_conflicts(void)
980 {
981 git_oid commit_id;
982 git_commit *commit;
983 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
984 git_index *index;
985
986 cl_git_pass(git_oid_fromstr(&commit_id, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6"));
987 cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id));
988
989 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
990
991 cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
992
993 cl_git_pass(git_repository_index(&index, g_repo));
994 cl_git_pass(git_index_remove(index, "executable.txt", 0));
995
996 create_conflict("executable.txt");
997 cl_git_mkfile("testrepo/executable.txt", "This is the conflict file.\n");
998
999 create_conflict("other.txt");
1000 cl_git_mkfile("testrepo/other.txt", "This is another conflict file.\n");
1001
1002 git_index_write(index);
1003
1004 cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
1005
1006 cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 1));
1007 cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 2));
1008 cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 3));
1009
1010 cl_assert_equal_p(NULL, git_index_get_bypath(index, "other.txt", 1));
1011 cl_assert_equal_p(NULL, git_index_get_bypath(index, "other.txt", 2));
1012 cl_assert_equal_p(NULL, git_index_get_bypath(index, "other.txt", 3));
1013
1014 cl_assert(!git_path_exists("testrepo/other.txt"));
1015
1016 git_commit_free(commit);
1017 git_index_free(index);
1018 }
1019
1020
1021 void test_checkout_tree__removes_conflicts_only_by_pathscope(void)
1022 {
1023 git_oid commit_id;
1024 git_commit *commit;
1025 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1026 git_index *index;
1027 const char *path = "executable.txt";
1028
1029 cl_git_pass(git_oid_fromstr(&commit_id, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6"));
1030 cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id));
1031
1032 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
1033 opts.paths.count = 1;
1034 opts.paths.strings = (char **)&path;
1035
1036 cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
1037
1038 cl_git_pass(git_repository_index(&index, g_repo));
1039 cl_git_pass(git_index_remove(index, "executable.txt", 0));
1040
1041 create_conflict("executable.txt");
1042 cl_git_mkfile("testrepo/executable.txt", "This is the conflict file.\n");
1043
1044 create_conflict("other.txt");
1045 cl_git_mkfile("testrepo/other.txt", "This is another conflict file.\n");
1046
1047 git_index_write(index);
1048
1049 cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
1050
1051 cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 1));
1052 cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 2));
1053 cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 3));
1054
1055 cl_assert(git_index_get_bypath(index, "other.txt", 1) != NULL);
1056 cl_assert(git_index_get_bypath(index, "other.txt", 2) != NULL);
1057 cl_assert(git_index_get_bypath(index, "other.txt", 3) != NULL);
1058
1059 cl_assert(git_path_exists("testrepo/other.txt"));
1060
1061 git_commit_free(commit);
1062 git_index_free(index);
1063 }
1064
1065 void test_checkout_tree__case_changing_rename(void)
1066 {
1067 git_index *index;
1068 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1069 git_oid master_id, dir_commit_id, tree_id, commit_id;
1070 git_commit *master_commit, *dir_commit;
1071 git_tree *tree;
1072 git_signature *signature;
1073 const git_index_entry *index_entry;
1074 bool case_sensitive;
1075
1076 assert_on_branch(g_repo, "master");
1077
1078 cl_git_pass(git_repository_index(&index, g_repo));
1079
1080 /* Switch branches and perform a case-changing rename */
1081
1082 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
1083
1084 cl_git_pass(git_reference_name_to_id(&dir_commit_id, g_repo, "refs/heads/dir"));
1085 cl_git_pass(git_commit_lookup(&dir_commit, g_repo, &dir_commit_id));
1086
1087 cl_git_pass(git_checkout_tree(g_repo, (git_object *)dir_commit, &opts));
1088 cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir"));
1089
1090 cl_assert(git_path_isfile("testrepo/README"));
1091 case_sensitive = !git_path_isfile("testrepo/readme");
1092
1093 cl_assert(index_entry = git_index_get_bypath(index, "README", 0));
1094 cl_assert_equal_s("README", index_entry->path);
1095
1096 cl_git_pass(git_index_remove_bypath(index, "README"));
1097 cl_git_pass(p_rename("testrepo/README", "testrepo/__readme__"));
1098 cl_git_pass(p_rename("testrepo/__readme__", "testrepo/readme"));
1099 cl_git_append2file("testrepo/readme", "An addendum...");
1100 cl_git_pass(git_index_add_bypath(index, "readme"));
1101
1102 cl_git_pass(git_index_write(index));
1103
1104 cl_git_pass(git_index_write_tree(&tree_id, index));
1105 cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
1106
1107 cl_git_pass(git_signature_new(&signature, "Renamer", "rename@contoso.com", time(NULL), 0));
1108
1109 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));
1110
1111 cl_assert(git_path_isfile("testrepo/readme"));
1112 if (case_sensitive)
1113 cl_assert(!git_path_isfile("testrepo/README"));
1114
1115 cl_assert(index_entry = git_index_get_bypath(index, "readme", 0));
1116 cl_assert_equal_s("readme", index_entry->path);
1117
1118 /* Switching back to master should rename readme -> README */
1119 opts.checkout_strategy = GIT_CHECKOUT_SAFE;
1120
1121 cl_git_pass(git_reference_name_to_id(&master_id, g_repo, "refs/heads/master"));
1122 cl_git_pass(git_commit_lookup(&master_commit, g_repo, &master_id));
1123
1124 cl_git_pass(git_checkout_tree(g_repo, (git_object *)master_commit, &opts));
1125 cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master"));
1126
1127 assert_on_branch(g_repo, "master");
1128
1129 cl_assert(git_path_isfile("testrepo/README"));
1130 if (case_sensitive)
1131 cl_assert(!git_path_isfile("testrepo/readme"));
1132
1133 cl_assert(index_entry = git_index_get_bypath(index, "README", 0));
1134 cl_assert_equal_s("README", index_entry->path);
1135
1136 git_index_free(index);
1137 git_signature_free(signature);
1138 git_tree_free(tree);
1139 git_commit_free(dir_commit);
1140 git_commit_free(master_commit);
1141 }
1142
1143 void perfdata_cb(const git_checkout_perfdata *in, void *payload)
1144 {
1145 memcpy(payload, in, sizeof(git_checkout_perfdata));
1146 }
1147
1148 void test_checkout_tree__can_collect_perfdata(void)
1149 {
1150 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1151 git_oid oid;
1152 git_object *obj = NULL;
1153 git_checkout_perfdata perfdata = {0};
1154
1155 opts.perfdata_cb = perfdata_cb;
1156 opts.perfdata_payload = &perfdata;
1157
1158 assert_on_branch(g_repo, "master");
1159 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
1160
1161 cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
1162 cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY));
1163
1164 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
1165
1166 cl_assert(perfdata.mkdir_calls > 0);
1167 cl_assert(perfdata.stat_calls > 0);
1168
1169 git_object_free(obj);
1170 }
1171
1172 void update_attr_callback(
1173 const char *path,
1174 size_t completed_steps,
1175 size_t total_steps,
1176 void *payload)
1177 {
1178 GIT_UNUSED(completed_steps);
1179 GIT_UNUSED(total_steps);
1180 GIT_UNUSED(payload);
1181
1182 if (path && strcmp(path, "ident1.txt") == 0)
1183 cl_git_write2file("testrepo/.gitattributes",
1184 "*.txt ident\n", 12, O_RDWR|O_CREAT, 0666);
1185 }
1186
1187 void test_checkout_tree__caches_attributes_during_checkout(void)
1188 {
1189 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1190 git_oid oid;
1191 git_object *obj = NULL;
1192 git_buf ident1 = GIT_BUF_INIT, ident2 = GIT_BUF_INIT;
1193 char *ident_paths[] = { "ident1.txt", "ident2.txt" };
1194
1195 opts.progress_cb = update_attr_callback;
1196
1197 assert_on_branch(g_repo, "master");
1198 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
1199 opts.paths.strings = ident_paths;
1200 opts.paths.count = 2;
1201
1202 cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/ident"));
1203 cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY));
1204
1205 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
1206
1207 cl_git_pass(git_futils_readbuffer(&ident1, "testrepo/ident1.txt"));
1208 cl_git_pass(git_futils_readbuffer(&ident2, "testrepo/ident2.txt"));
1209
1210 cl_assert_equal_strn(ident1.ptr, "# $Id$", 6);
1211 cl_assert_equal_strn(ident2.ptr, "# $Id$", 6);
1212
1213 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
1214
1215 cl_git_pass(git_futils_readbuffer(&ident1, "testrepo/ident1.txt"));
1216 cl_git_pass(git_futils_readbuffer(&ident2, "testrepo/ident2.txt"));
1217
1218 cl_assert_equal_strn(ident1.ptr, "# $Id: ", 7);
1219 cl_assert_equal_strn(ident2.ptr, "# $Id: ", 7);
1220
1221 git_buf_free(&ident1);
1222 git_buf_free(&ident2);
1223 git_object_free(obj);
1224 }
1225
1226 void test_checkout_tree__can_not_update_index(void)
1227 {
1228 git_oid oid;
1229 git_object *head;
1230 unsigned int status;
1231 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1232 git_index *index;
1233
1234 opts.checkout_strategy |=
1235 GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DONT_UPDATE_INDEX;
1236
1237 cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD"));
1238 cl_git_pass(git_object_lookup(&head, g_repo, &oid, GIT_OBJ_ANY));
1239
1240 cl_git_pass(git_reset(g_repo, head, GIT_RESET_HARD, &g_opts));
1241
1242 cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
1243
1244 cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));
1245
1246 cl_git_pass(git_checkout_tree(g_repo, g_object, &opts));
1247
1248 cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt"));
1249 cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt"));
1250 cl_assert_equal_i(GIT_STATUS_WT_NEW, status);
1251
1252 cl_git_pass(git_repository_index(&index, g_repo));
1253 cl_git_pass(git_index_write(index));
1254
1255 cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt"));
1256 cl_assert_equal_i(GIT_STATUS_WT_NEW, status);
1257
1258 git_object_free(head);
1259 git_index_free(index);
1260 }
1261
1262 void test_checkout_tree__can_update_but_not_write_index(void)
1263 {
1264 git_oid oid;
1265 git_object *head;
1266 unsigned int status;
1267 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1268 git_index *index;
1269 git_repository *other;
1270
1271 opts.checkout_strategy |=
1272 GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DONT_WRITE_INDEX;
1273
1274 cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD"));
1275 cl_git_pass(git_object_lookup(&head, g_repo, &oid, GIT_OBJ_ANY));
1276
1277 cl_git_pass(git_reset(g_repo, head, GIT_RESET_HARD, &g_opts));
1278
1279 cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
1280
1281 cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));
1282
1283 cl_git_pass(git_checkout_tree(g_repo, g_object, &opts));
1284
1285 cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt"));
1286 cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt"));
1287 cl_assert_equal_i(GIT_STATUS_INDEX_NEW, status);
1288
1289 cl_git_pass(git_repository_open(&other, "testrepo"));
1290 cl_git_pass(git_status_file(&status, other, "ab/de/2.txt"));
1291 cl_assert_equal_i(GIT_STATUS_WT_NEW, status);
1292 git_repository_free(other);
1293
1294 cl_git_pass(git_repository_index(&index, g_repo));
1295 cl_git_pass(git_index_write(index));
1296
1297 cl_git_pass(git_repository_open(&other, "testrepo"));
1298 cl_git_pass(git_status_file(&status, other, "ab/de/2.txt"));
1299 cl_assert_equal_i(GIT_STATUS_INDEX_NEW, status);
1300 git_repository_free(other);
1301
1302 git_object_free(head);
1303 git_index_free(index);
1304 }
1305
1306 /* Emulate checking out in a repo created by clone --no-checkout,
1307 * which would not have written an index. */
1308 void test_checkout_tree__safe_proceeds_if_no_index(void)
1309 {
1310 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1311 git_oid oid;
1312 git_object *obj = NULL;
1313
1314 assert_on_branch(g_repo, "master");
1315 cl_must_pass(p_unlink("testrepo/.git/index"));
1316
1317 /* do second checkout safe because we should be clean after first */
1318 opts.checkout_strategy = GIT_CHECKOUT_SAFE;
1319
1320 cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/subtrees"));
1321 cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY));
1322
1323 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
1324 cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
1325
1326 cl_assert(git_path_isfile("testrepo/README"));
1327 cl_assert(git_path_isfile("testrepo/branch_file.txt"));
1328 cl_assert(git_path_isfile("testrepo/new.txt"));
1329 cl_assert(git_path_isfile("testrepo/ab/4.txt"));
1330 cl_assert(git_path_isfile("testrepo/ab/c/3.txt"));
1331 cl_assert(git_path_isfile("testrepo/ab/de/2.txt"));
1332 cl_assert(git_path_isfile("testrepo/ab/de/fgh/1.txt"));
1333
1334 cl_assert(!git_path_isdir("testrepo/a"));
1335
1336 assert_on_branch(g_repo, "subtrees");
1337
1338 git_object_free(obj);
1339 }
1340