]> git.proxmox.com Git - libgit2.git/blob - tests/checkout/tree.c
checkout test: ignore unstaged case-changing renames
[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_EMERGECONFLICT, 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_EMERGECONFLICT, 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_EMERGECONFLICT, 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_EMERGECONFLICT, git_checkout_tree(g_repo, g_object, &g_opts));
524 }
525
526 void test_checkout_tree__checking_out_a_conflicting_type_change_returns_EMERGECONFLICT(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_EMERGECONFLICT_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_EMERGECONFLICT(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 cl_assert_equal_i(4, ca.count);
650
651 /* and again with a different stopping point and return code */
652 ca.filename = "README";
653 ca.error = 123;
654 ca.count = 0;
655
656 cl_git_fail_with(git_checkout_tree(g_repo, obj, &opts), 123);
657
658 cl_assert(!git_path_exists("testrepo/new.txt"));
659 cl_assert_equal_i(1, ca.count);
660
661 git_object_free(obj);
662 }
663
664 void test_checkout_tree__can_checkout_with_last_workdir_item_missing(void)
665 {
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;
671
672 git_repository_index(&index, g_repo);
673
674 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
675
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));
678
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"));
681
682 cl_git_pass(p_mkdir("./testrepo/this-is-dir", 0777));
683 cl_git_mkfile("./testrepo/this-is-dir/contained_file", "content\n");
684
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));
688
689 cl_git_pass(p_unlink("./testrepo/this-is-dir/contained_file"));
690
691 opts.checkout_strategy = GIT_CHECKOUT_SAFE;
692
693 opts.checkout_strategy = 1;
694 git_checkout_tree(g_repo, (git_object *)tree, &opts);
695
696 git_tree_free(tree);
697 git_commit_free(commit);
698 git_index_free(index);
699 }
700
701 void test_checkout_tree__issue_1397(void)
702 {
703 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
704 const char *partial_oid = "8a7ef04";
705 git_object *tree = NULL;
706
707 test_checkout_tree__cleanup(); /* cleanup default checkout */
708
709 g_repo = cl_git_sandbox_init("issue_1397");
710
711 cl_repo_set_bool(g_repo, "core.autocrlf", true);
712
713 cl_git_pass(git_revparse_single(&tree, g_repo, partial_oid));
714
715 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
716
717 cl_git_pass(git_checkout_tree(g_repo, tree, &opts));
718
719 check_file_contents("./issue_1397/crlf_file.txt", "first line\r\nsecond line\r\nboth with crlf");
720
721 git_object_free(tree);
722 }
723
724 void test_checkout_tree__can_write_to_empty_dirs(void)
725 {
726 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
727 git_oid oid;
728 git_object *obj = NULL;
729
730 assert_on_branch(g_repo, "master");
731
732 cl_git_pass(p_mkdir("testrepo/a", 0777));
733
734 /* do first checkout with FORCE because we don't know if testrepo
735 * base data is clean for a checkout or not
736 */
737 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
738
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));
741
742 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
743
744 cl_assert(git_path_isfile("testrepo/a/b.txt"));
745
746 git_object_free(obj);
747 }
748
749 void test_checkout_tree__fails_when_dir_in_use(void)
750 {
751 #ifdef GIT_WIN32
752 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
753 git_oid oid;
754 git_object *obj = NULL;
755
756 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
757
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));
760
761 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
762
763 cl_assert(git_path_isfile("testrepo/a/b.txt"));
764
765 git_object_free(obj);
766
767 cl_git_pass(p_chdir("testrepo/a"));
768
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));
771
772 cl_git_fail(git_checkout_tree(g_repo, obj, &opts));
773
774 cl_git_pass(p_chdir("../.."));
775
776 cl_assert(git_path_is_empty_dir("testrepo/a"));
777
778 git_object_free(obj);
779 #endif
780 }
781
782 void test_checkout_tree__can_continue_when_dir_in_use(void)
783 {
784 #ifdef GIT_WIN32
785 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
786 git_oid oid;
787 git_object *obj = NULL;
788
789 opts.checkout_strategy = GIT_CHECKOUT_FORCE |
790 GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES;
791
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));
794
795 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
796
797 cl_assert(git_path_isfile("testrepo/a/b.txt"));
798
799 git_object_free(obj);
800
801 cl_git_pass(p_chdir("testrepo/a"));
802
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));
805
806 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
807
808 cl_git_pass(p_chdir("../.."));
809
810 cl_assert(git_path_is_empty_dir("testrepo/a"));
811
812 git_object_free(obj);
813 #endif
814 }
815
816 void test_checkout_tree__target_directory_from_bare(void)
817 {
818 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
819 git_oid oid;
820 checkout_counts cts;
821 memset(&cts, 0, sizeof(cts));
822
823 test_checkout_tree__cleanup(); /* cleanup default checkout */
824
825 g_repo = cl_git_sandbox_init("testrepo.git");
826 cl_assert(git_repository_is_bare(g_repo));
827
828 opts.checkout_strategy = GIT_CHECKOUT_SAFE |
829 GIT_CHECKOUT_RECREATE_MISSING;
830
831 opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL;
832 opts.notify_cb = checkout_count_callback;
833 opts.notify_payload = &cts;
834
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));
837
838 cl_git_fail(git_checkout_tree(g_repo, g_object, &opts));
839
840 opts.target_directory = "alternative";
841 cl_assert(!git_path_isdir("alternative"));
842
843 cl_git_pass(git_checkout_tree(g_repo, g_object, &opts));
844
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);
848
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");
852
853 cl_git_pass(git_futils_rmdir_r(
854 "alternative", NULL, GIT_RMDIR_REMOVE_FILES));
855 }
856
857 void test_checkout_tree__extremely_long_file_name(void)
858 {
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";
861 char path[1024];
862
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));
866
867 sprintf(path, "testrepo/%s.txt", longname);
868 cl_assert(git_path_exists(path));
869
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));
874 }
875
876 static void create_conflict(const char *path)
877 {
878 git_index *index;
879 git_index_entry entry;
880
881 cl_git_pass(git_repository_index(&index, g_repo));
882
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");
887 entry.path = path;
888 cl_git_pass(git_index_add(index, &entry));
889
890 entry.flags = 2 << GIT_IDXENTRY_STAGESHIFT;
891 git_oid_fromstr(&entry.id, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf");
892 cl_git_pass(git_index_add(index, &entry));
893
894 entry.flags = 3 << GIT_IDXENTRY_STAGESHIFT;
895 git_oid_fromstr(&entry.id, "2bd0a343aeef7a2cf0d158478966a6e587ff3863");
896 cl_git_pass(git_index_add(index, &entry));
897
898 git_index_write(index);
899 git_index_free(index);
900 }
901
902 void test_checkout_tree__fails_when_conflicts_exist_in_index(void)
903 {
904 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
905 git_oid oid;
906 git_object *obj = NULL;
907
908 opts.checkout_strategy = GIT_CHECKOUT_SAFE;
909
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));
912
913 create_conflict("conflicts.txt");
914
915 cl_git_fail(git_checkout_tree(g_repo, obj, &opts));
916
917 git_object_free(obj);
918 }
919
920 void test_checkout_tree__filemode_preserved_in_index(void)
921 {
922 git_oid executable_oid;
923 git_commit *commit;
924 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
925 git_index *index;
926 const git_index_entry *entry;
927
928 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
929
930 cl_git_pass(git_repository_index(&index, g_repo));
931
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));
935
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);
939
940 git_commit_free(commit);
941
942
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));
946
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);
950
951 git_commit_free(commit);
952
953
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));
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(0100755, entry->mode);
961
962 git_commit_free(commit);
963
964
965 git_index_free(index);
966 }
967
968 void test_checkout_tree__removes_conflicts(void)
969 {
970 git_oid commit_id;
971 git_commit *commit;
972 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
973 git_index *index;
974
975 cl_git_pass(git_oid_fromstr(&commit_id, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6"));
976 cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id));
977
978 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
979
980 cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
981
982 cl_git_pass(git_repository_index(&index, g_repo));
983 cl_git_pass(git_index_remove(index, "executable.txt", 0));
984
985 create_conflict("executable.txt");
986 cl_git_mkfile("testrepo/executable.txt", "This is the conflict file.\n");
987
988 create_conflict("other.txt");
989 cl_git_mkfile("testrepo/other.txt", "This is another conflict file.\n");
990
991 git_index_write(index);
992
993 cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
994
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));
998
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));
1002
1003 cl_assert(!git_path_exists("testrepo/other.txt"));
1004
1005 git_commit_free(commit);
1006 git_index_free(index);
1007 }
1008
1009
1010 void test_checkout_tree__removes_conflicts_only_by_pathscope(void)
1011 {
1012 git_oid commit_id;
1013 git_commit *commit;
1014 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1015 git_index *index;
1016 const char *path = "executable.txt";
1017
1018 cl_git_pass(git_oid_fromstr(&commit_id, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6"));
1019 cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id));
1020
1021 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
1022 opts.paths.count = 1;
1023 opts.paths.strings = (char **)&path;
1024
1025 cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
1026
1027 cl_git_pass(git_repository_index(&index, g_repo));
1028 cl_git_pass(git_index_remove(index, "executable.txt", 0));
1029
1030 create_conflict("executable.txt");
1031 cl_git_mkfile("testrepo/executable.txt", "This is the conflict file.\n");
1032
1033 create_conflict("other.txt");
1034 cl_git_mkfile("testrepo/other.txt", "This is another conflict file.\n");
1035
1036 git_index_write(index);
1037
1038 cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
1039
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));
1043
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);
1047
1048 cl_assert(git_path_exists("testrepo/other.txt"));
1049
1050 git_commit_free(commit);
1051 git_index_free(index);
1052 }
1053
1054 void test_checkout_tree__case_changing_rename(void)
1055 {
1056 git_index *index;
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;
1060 git_tree *tree;
1061 git_signature *signature;
1062 const git_index_entry *index_entry;
1063 bool case_sensitive;
1064
1065 assert_on_branch(g_repo, "master");
1066
1067 cl_git_pass(git_repository_index(&index, g_repo));
1068
1069 /* Switch branches and perform a case-changing rename */
1070
1071 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
1072
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));
1075
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"));
1078
1079 cl_assert(git_path_isfile("testrepo/README"));
1080 case_sensitive = !git_path_isfile("testrepo/readme");
1081
1082 cl_assert(index_entry = git_index_get_bypath(index, "README", 0));
1083 cl_assert_equal_s("README", index_entry->path);
1084
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"));
1090
1091 cl_git_pass(git_index_write(index));
1092
1093 cl_git_pass(git_index_write_tree(&tree_id, index));
1094 cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
1095
1096 cl_git_pass(git_signature_new(&signature, "Renamer", "rename@contoso.com", time(NULL), 0));
1097
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));
1099
1100 cl_assert(git_path_isfile("testrepo/readme"));
1101 if (case_sensitive)
1102 cl_assert(!git_path_isfile("testrepo/README"));
1103
1104 cl_assert(index_entry = git_index_get_bypath(index, "readme", 0));
1105 cl_assert_equal_s("readme", index_entry->path);
1106
1107 /* Switching back to master should rename readme -> README */
1108 opts.checkout_strategy = GIT_CHECKOUT_SAFE;
1109
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));
1112
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"));
1115
1116 assert_on_branch(g_repo, "master");
1117
1118 cl_assert(git_path_isfile("testrepo/README"));
1119 if (case_sensitive)
1120 cl_assert(!git_path_isfile("testrepo/readme"));
1121
1122 cl_assert(index_entry = git_index_get_bypath(index, "README", 0));
1123 cl_assert_equal_s("README", index_entry->path);
1124
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);
1130 }
1131
1132 void perfdata_cb(const git_checkout_perfdata *in, void *payload)
1133 {
1134 memcpy(payload, in, sizeof(git_checkout_perfdata));
1135 }
1136
1137 void test_checkout_tree__can_collect_perfdata(void)
1138 {
1139 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1140 git_oid oid;
1141 git_object *obj = NULL;
1142 git_checkout_perfdata perfdata = {0};
1143
1144 opts.perfdata_cb = perfdata_cb;
1145 opts.perfdata_payload = &perfdata;
1146
1147 assert_on_branch(g_repo, "master");
1148 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
1149
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));
1152
1153 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
1154
1155 cl_assert(perfdata.mkdir_calls > 0);
1156 cl_assert(perfdata.stat_calls > 0);
1157
1158 git_object_free(obj);
1159 }
1160
1161 void update_attr_callback(
1162 const char *path,
1163 size_t completed_steps,
1164 size_t total_steps,
1165 void *payload)
1166 {
1167 GIT_UNUSED(completed_steps);
1168 GIT_UNUSED(total_steps);
1169 GIT_UNUSED(payload);
1170
1171 if (path && strcmp(path, "ident1.txt") == 0)
1172 cl_git_write2file("testrepo/.gitattributes",
1173 "*.txt ident\n", 12, O_RDWR|O_CREAT, 0666);
1174 }
1175
1176 void test_checkout_tree__caches_attributes_during_checkout(void)
1177 {
1178 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1179 git_oid oid;
1180 git_object *obj = NULL;
1181 git_buf ident1 = GIT_BUF_INIT, ident2 = GIT_BUF_INIT;
1182 char *ident_paths[] = { "ident1.txt", "ident2.txt" };
1183
1184 opts.progress_cb = update_attr_callback;
1185
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;
1190
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));
1193
1194 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
1195
1196 cl_git_pass(git_futils_readbuffer(&ident1, "testrepo/ident1.txt"));
1197 cl_git_pass(git_futils_readbuffer(&ident2, "testrepo/ident2.txt"));
1198
1199 cl_assert_equal_strn(ident1.ptr, "# $Id$", 6);
1200 cl_assert_equal_strn(ident2.ptr, "# $Id$", 6);
1201
1202 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
1203
1204 cl_git_pass(git_futils_readbuffer(&ident1, "testrepo/ident1.txt"));
1205 cl_git_pass(git_futils_readbuffer(&ident2, "testrepo/ident2.txt"));
1206
1207 cl_assert_equal_strn(ident1.ptr, "# $Id: ", 7);
1208 cl_assert_equal_strn(ident2.ptr, "# $Id: ", 7);
1209
1210 git_buf_free(&ident1);
1211 git_buf_free(&ident2);
1212 git_object_free(obj);
1213 }
1214
1215 void test_checkout_tree__can_not_update_index(void)
1216 {
1217 git_oid oid;
1218 git_object *head;
1219 unsigned int status;
1220 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1221 git_index *index;
1222
1223 opts.checkout_strategy |=
1224 GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DONT_UPDATE_INDEX;
1225
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));
1228
1229 cl_git_pass(git_reset(g_repo, head, GIT_RESET_HARD, &g_opts));
1230
1231 cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
1232
1233 cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));
1234
1235 cl_git_pass(git_checkout_tree(g_repo, g_object, &opts));
1236
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);
1240
1241 cl_git_pass(git_repository_index(&index, g_repo));
1242 cl_git_pass(git_index_write(index));
1243
1244 cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt"));
1245 cl_assert_equal_i(GIT_STATUS_WT_NEW, status);
1246
1247 git_object_free(head);
1248 git_index_free(index);
1249 }
1250
1251 void test_checkout_tree__can_update_but_not_write_index(void)
1252 {
1253 git_oid oid;
1254 git_object *head;
1255 unsigned int status;
1256 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1257 git_index *index;
1258 git_repository *other;
1259
1260 opts.checkout_strategy |=
1261 GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DONT_WRITE_INDEX;
1262
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));
1265
1266 cl_git_pass(git_reset(g_repo, head, GIT_RESET_HARD, &g_opts));
1267
1268 cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
1269
1270 cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));
1271
1272 cl_git_pass(git_checkout_tree(g_repo, g_object, &opts));
1273
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);
1277
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);
1282
1283 cl_git_pass(git_repository_index(&index, g_repo));
1284 cl_git_pass(git_index_write(index));
1285
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);
1290
1291 git_object_free(head);
1292 git_index_free(index);
1293 }
1294
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)
1298 {
1299 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1300 git_oid oid;
1301 git_object *obj = NULL;
1302
1303 assert_on_branch(g_repo, "master");
1304 cl_must_pass(p_unlink("testrepo/.git/index"));
1305
1306 /* do second checkout safe because we should be clean after first */
1307 opts.checkout_strategy = GIT_CHECKOUT_SAFE;
1308
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));
1311
1312 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
1313 cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
1314
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"));
1322
1323 cl_assert(!git_path_isdir("testrepo/a"));
1324
1325 assert_on_branch(g_repo, "subtrees");
1326
1327 git_object_free(obj);
1328 }
1329
1330 void test_checkout_tree__ignores_unstaged_casechange(void)
1331 {
1332 git_reference *orig_ref, *br2_ref;
1333 git_commit *orig, *br2;
1334 git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
1335
1336 checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
1337
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));
1341
1342 cl_rename("testrepo/branch_file.txt", "testrepo/Branch_File.txt");
1343
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)));
1346
1347 cl_git_pass(git_checkout_tree(g_repo, (const git_object *)br2, &checkout_opts));
1348
1349 git_commit_free(orig);
1350 git_reference_free(orig_ref);
1351 }