]> git.proxmox.com Git - libgit2.git/blob - tests/checkout/tree.c
Merge pull request #3818 from meatcoder/fix_odb_read_error
[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(GIT_PERMS_IS_EXEC(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(!GIT_PERMS_IS_EXEC(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(GIT_PERMS_IS_EXEC(entry->mode));
972
973 git_commit_free(commit);
974
975
976 /* Finally, check out the text file again and check that the exec bit is cleared */
977 cl_git_pass(git_oid_fromstr(&executable_oid, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9"));
978 cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid));
979
980 cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
981 cl_assert(entry = git_index_get_bypath(index, "a/b.txt", 0));
982 cl_assert(!GIT_PERMS_IS_EXEC(entry->mode));
983
984 git_commit_free(commit);
985
986
987 git_index_free(index);
988 }
989
990 mode_t read_filemode(const char *path)
991 {
992 git_buf fullpath = GIT_BUF_INIT;
993 struct stat st;
994 mode_t result;
995
996 git_buf_joinpath(&fullpath, "testrepo", path);
997 cl_must_pass(p_stat(fullpath.ptr, &st));
998
999 result = GIT_PERMS_IS_EXEC(st.st_mode) ?
1000 GIT_FILEMODE_BLOB_EXECUTABLE : GIT_FILEMODE_BLOB;
1001
1002 git_buf_free(&fullpath);
1003
1004 return result;
1005 }
1006
1007 void test_checkout_tree__filemode_preserved_in_workdir(void)
1008 {
1009 #ifndef GIT_WIN32
1010 git_oid executable_oid;
1011 git_commit *commit;
1012 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1013
1014 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
1015
1016 /* test a freshly added executable */
1017 cl_git_pass(git_oid_fromstr(&executable_oid, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6"));
1018 cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid));
1019
1020 cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
1021 cl_assert(GIT_PERMS_IS_EXEC(read_filemode("executable.txt")));
1022
1023 git_commit_free(commit);
1024
1025
1026 /* Now start with a commit which has a text file */
1027 cl_git_pass(git_oid_fromstr(&executable_oid, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9"));
1028 cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid));
1029
1030 cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
1031 cl_assert(!GIT_PERMS_IS_EXEC(read_filemode("a/b.txt")));
1032
1033 git_commit_free(commit);
1034
1035
1036 /* And then check out to a commit which converts the text file to an executable */
1037 cl_git_pass(git_oid_fromstr(&executable_oid, "144344043ba4d4a405da03de3844aa829ae8be0e"));
1038 cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid));
1039
1040 cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
1041 cl_assert(GIT_PERMS_IS_EXEC(read_filemode("a/b.txt")));
1042
1043 git_commit_free(commit);
1044
1045
1046 /* Finally, check out the text file again and check that the exec bit is cleared */
1047 cl_git_pass(git_oid_fromstr(&executable_oid, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9"));
1048 cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid));
1049
1050 cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
1051 cl_assert(!GIT_PERMS_IS_EXEC(read_filemode("a/b.txt")));
1052
1053 git_commit_free(commit);
1054 #endif
1055 }
1056
1057 void test_checkout_tree__removes_conflicts(void)
1058 {
1059 git_oid commit_id;
1060 git_commit *commit;
1061 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1062 git_index *index;
1063
1064 cl_git_pass(git_oid_fromstr(&commit_id, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6"));
1065 cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id));
1066
1067 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
1068
1069 cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
1070
1071 cl_git_pass(git_repository_index(&index, g_repo));
1072 cl_git_pass(git_index_remove(index, "executable.txt", 0));
1073
1074 create_conflict("executable.txt");
1075 cl_git_mkfile("testrepo/executable.txt", "This is the conflict file.\n");
1076
1077 create_conflict("other.txt");
1078 cl_git_mkfile("testrepo/other.txt", "This is another conflict file.\n");
1079
1080 git_index_write(index);
1081
1082 cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
1083
1084 cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 1));
1085 cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 2));
1086 cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 3));
1087
1088 cl_assert_equal_p(NULL, git_index_get_bypath(index, "other.txt", 1));
1089 cl_assert_equal_p(NULL, git_index_get_bypath(index, "other.txt", 2));
1090 cl_assert_equal_p(NULL, git_index_get_bypath(index, "other.txt", 3));
1091
1092 cl_assert(!git_path_exists("testrepo/other.txt"));
1093
1094 git_commit_free(commit);
1095 git_index_free(index);
1096 }
1097
1098
1099 void test_checkout_tree__removes_conflicts_only_by_pathscope(void)
1100 {
1101 git_oid commit_id;
1102 git_commit *commit;
1103 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1104 git_index *index;
1105 const char *path = "executable.txt";
1106
1107 cl_git_pass(git_oid_fromstr(&commit_id, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6"));
1108 cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id));
1109
1110 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
1111 opts.paths.count = 1;
1112 opts.paths.strings = (char **)&path;
1113
1114 cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
1115
1116 cl_git_pass(git_repository_index(&index, g_repo));
1117 cl_git_pass(git_index_remove(index, "executable.txt", 0));
1118
1119 create_conflict("executable.txt");
1120 cl_git_mkfile("testrepo/executable.txt", "This is the conflict file.\n");
1121
1122 create_conflict("other.txt");
1123 cl_git_mkfile("testrepo/other.txt", "This is another conflict file.\n");
1124
1125 git_index_write(index);
1126
1127 cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
1128
1129 cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 1));
1130 cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 2));
1131 cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 3));
1132
1133 cl_assert(git_index_get_bypath(index, "other.txt", 1) != NULL);
1134 cl_assert(git_index_get_bypath(index, "other.txt", 2) != NULL);
1135 cl_assert(git_index_get_bypath(index, "other.txt", 3) != NULL);
1136
1137 cl_assert(git_path_exists("testrepo/other.txt"));
1138
1139 git_commit_free(commit);
1140 git_index_free(index);
1141 }
1142
1143 void test_checkout_tree__case_changing_rename(void)
1144 {
1145 git_index *index;
1146 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1147 git_oid master_id, dir_commit_id, tree_id, commit_id;
1148 git_commit *master_commit, *dir_commit;
1149 git_tree *tree;
1150 git_signature *signature;
1151 const git_index_entry *index_entry;
1152 bool case_sensitive;
1153
1154 assert_on_branch(g_repo, "master");
1155
1156 cl_git_pass(git_repository_index(&index, g_repo));
1157
1158 /* Switch branches and perform a case-changing rename */
1159
1160 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
1161
1162 cl_git_pass(git_reference_name_to_id(&dir_commit_id, g_repo, "refs/heads/dir"));
1163 cl_git_pass(git_commit_lookup(&dir_commit, g_repo, &dir_commit_id));
1164
1165 cl_git_pass(git_checkout_tree(g_repo, (git_object *)dir_commit, &opts));
1166 cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir"));
1167
1168 cl_assert(git_path_isfile("testrepo/README"));
1169 case_sensitive = !git_path_isfile("testrepo/readme");
1170
1171 cl_assert(index_entry = git_index_get_bypath(index, "README", 0));
1172 cl_assert_equal_s("README", index_entry->path);
1173
1174 cl_git_pass(git_index_remove_bypath(index, "README"));
1175 cl_git_pass(p_rename("testrepo/README", "testrepo/__readme__"));
1176 cl_git_pass(p_rename("testrepo/__readme__", "testrepo/readme"));
1177 cl_git_append2file("testrepo/readme", "An addendum...");
1178 cl_git_pass(git_index_add_bypath(index, "readme"));
1179
1180 cl_git_pass(git_index_write(index));
1181
1182 cl_git_pass(git_index_write_tree(&tree_id, index));
1183 cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
1184
1185 cl_git_pass(git_signature_new(&signature, "Renamer", "rename@contoso.com", time(NULL), 0));
1186
1187 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));
1188
1189 cl_assert(git_path_isfile("testrepo/readme"));
1190 if (case_sensitive)
1191 cl_assert(!git_path_isfile("testrepo/README"));
1192
1193 cl_assert(index_entry = git_index_get_bypath(index, "readme", 0));
1194 cl_assert_equal_s("readme", index_entry->path);
1195
1196 /* Switching back to master should rename readme -> README */
1197 opts.checkout_strategy = GIT_CHECKOUT_SAFE;
1198
1199 cl_git_pass(git_reference_name_to_id(&master_id, g_repo, "refs/heads/master"));
1200 cl_git_pass(git_commit_lookup(&master_commit, g_repo, &master_id));
1201
1202 cl_git_pass(git_checkout_tree(g_repo, (git_object *)master_commit, &opts));
1203 cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master"));
1204
1205 assert_on_branch(g_repo, "master");
1206
1207 cl_assert(git_path_isfile("testrepo/README"));
1208 if (case_sensitive)
1209 cl_assert(!git_path_isfile("testrepo/readme"));
1210
1211 cl_assert(index_entry = git_index_get_bypath(index, "README", 0));
1212 cl_assert_equal_s("README", index_entry->path);
1213
1214 git_index_free(index);
1215 git_signature_free(signature);
1216 git_tree_free(tree);
1217 git_commit_free(dir_commit);
1218 git_commit_free(master_commit);
1219 }
1220
1221 void perfdata_cb(const git_checkout_perfdata *in, void *payload)
1222 {
1223 memcpy(payload, in, sizeof(git_checkout_perfdata));
1224 }
1225
1226 void test_checkout_tree__can_collect_perfdata(void)
1227 {
1228 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1229 git_oid oid;
1230 git_object *obj = NULL;
1231 git_checkout_perfdata perfdata = {0};
1232
1233 opts.perfdata_cb = perfdata_cb;
1234 opts.perfdata_payload = &perfdata;
1235
1236 assert_on_branch(g_repo, "master");
1237 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
1238
1239 cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
1240 cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY));
1241
1242 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
1243
1244 cl_assert(perfdata.mkdir_calls > 0);
1245 cl_assert(perfdata.stat_calls > 0);
1246
1247 git_object_free(obj);
1248 }
1249
1250 void update_attr_callback(
1251 const char *path,
1252 size_t completed_steps,
1253 size_t total_steps,
1254 void *payload)
1255 {
1256 GIT_UNUSED(completed_steps);
1257 GIT_UNUSED(total_steps);
1258 GIT_UNUSED(payload);
1259
1260 if (path && strcmp(path, "ident1.txt") == 0)
1261 cl_git_write2file("testrepo/.gitattributes",
1262 "*.txt ident\n", 12, O_RDWR|O_CREAT, 0666);
1263 }
1264
1265 void test_checkout_tree__caches_attributes_during_checkout(void)
1266 {
1267 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1268 git_oid oid;
1269 git_object *obj = NULL;
1270 git_buf ident1 = GIT_BUF_INIT, ident2 = GIT_BUF_INIT;
1271 char *ident_paths[] = { "ident1.txt", "ident2.txt" };
1272
1273 opts.progress_cb = update_attr_callback;
1274
1275 assert_on_branch(g_repo, "master");
1276 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
1277 opts.paths.strings = ident_paths;
1278 opts.paths.count = 2;
1279
1280 cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/ident"));
1281 cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY));
1282
1283 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
1284
1285 cl_git_pass(git_futils_readbuffer(&ident1, "testrepo/ident1.txt"));
1286 cl_git_pass(git_futils_readbuffer(&ident2, "testrepo/ident2.txt"));
1287
1288 cl_assert_equal_strn(ident1.ptr, "# $Id$", 6);
1289 cl_assert_equal_strn(ident2.ptr, "# $Id$", 6);
1290
1291 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
1292
1293 cl_git_pass(git_futils_readbuffer(&ident1, "testrepo/ident1.txt"));
1294 cl_git_pass(git_futils_readbuffer(&ident2, "testrepo/ident2.txt"));
1295
1296 cl_assert_equal_strn(ident1.ptr, "# $Id: ", 7);
1297 cl_assert_equal_strn(ident2.ptr, "# $Id: ", 7);
1298
1299 git_buf_free(&ident1);
1300 git_buf_free(&ident2);
1301 git_object_free(obj);
1302 }
1303
1304 void test_checkout_tree__can_not_update_index(void)
1305 {
1306 git_oid oid;
1307 git_object *head;
1308 unsigned int status;
1309 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1310 git_index *index;
1311
1312 opts.checkout_strategy |=
1313 GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DONT_UPDATE_INDEX;
1314
1315 cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD"));
1316 cl_git_pass(git_object_lookup(&head, g_repo, &oid, GIT_OBJ_ANY));
1317
1318 cl_git_pass(git_reset(g_repo, head, GIT_RESET_HARD, &g_opts));
1319
1320 cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
1321
1322 cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));
1323
1324 cl_git_pass(git_checkout_tree(g_repo, g_object, &opts));
1325
1326 cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt"));
1327 cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt"));
1328 cl_assert_equal_i(GIT_STATUS_WT_NEW, status);
1329
1330 cl_git_pass(git_repository_index(&index, g_repo));
1331 cl_git_pass(git_index_write(index));
1332
1333 cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt"));
1334 cl_assert_equal_i(GIT_STATUS_WT_NEW, status);
1335
1336 git_object_free(head);
1337 git_index_free(index);
1338 }
1339
1340 void test_checkout_tree__can_update_but_not_write_index(void)
1341 {
1342 git_oid oid;
1343 git_object *head;
1344 unsigned int status;
1345 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1346 git_index *index;
1347 git_repository *other;
1348
1349 opts.checkout_strategy |=
1350 GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DONT_WRITE_INDEX;
1351
1352 cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD"));
1353 cl_git_pass(git_object_lookup(&head, g_repo, &oid, GIT_OBJ_ANY));
1354
1355 cl_git_pass(git_reset(g_repo, head, GIT_RESET_HARD, &g_opts));
1356
1357 cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
1358
1359 cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));
1360
1361 cl_git_pass(git_checkout_tree(g_repo, g_object, &opts));
1362
1363 cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt"));
1364 cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt"));
1365 cl_assert_equal_i(GIT_STATUS_INDEX_NEW, status);
1366
1367 cl_git_pass(git_repository_open(&other, "testrepo"));
1368 cl_git_pass(git_status_file(&status, other, "ab/de/2.txt"));
1369 cl_assert_equal_i(GIT_STATUS_WT_NEW, status);
1370 git_repository_free(other);
1371
1372 cl_git_pass(git_repository_index(&index, g_repo));
1373 cl_git_pass(git_index_write(index));
1374
1375 cl_git_pass(git_repository_open(&other, "testrepo"));
1376 cl_git_pass(git_status_file(&status, other, "ab/de/2.txt"));
1377 cl_assert_equal_i(GIT_STATUS_INDEX_NEW, status);
1378 git_repository_free(other);
1379
1380 git_object_free(head);
1381 git_index_free(index);
1382 }
1383
1384 /* Emulate checking out in a repo created by clone --no-checkout,
1385 * which would not have written an index. */
1386 void test_checkout_tree__safe_proceeds_if_no_index(void)
1387 {
1388 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1389 git_oid oid;
1390 git_object *obj = NULL;
1391
1392 assert_on_branch(g_repo, "master");
1393 cl_must_pass(p_unlink("testrepo/.git/index"));
1394
1395 /* do second checkout safe because we should be clean after first */
1396 opts.checkout_strategy = GIT_CHECKOUT_SAFE;
1397
1398 cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/subtrees"));
1399 cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY));
1400
1401 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
1402 cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
1403
1404 cl_assert(git_path_isfile("testrepo/README"));
1405 cl_assert(git_path_isfile("testrepo/branch_file.txt"));
1406 cl_assert(git_path_isfile("testrepo/new.txt"));
1407 cl_assert(git_path_isfile("testrepo/ab/4.txt"));
1408 cl_assert(git_path_isfile("testrepo/ab/c/3.txt"));
1409 cl_assert(git_path_isfile("testrepo/ab/de/2.txt"));
1410 cl_assert(git_path_isfile("testrepo/ab/de/fgh/1.txt"));
1411
1412 cl_assert(!git_path_isdir("testrepo/a"));
1413
1414 assert_on_branch(g_repo, "subtrees");
1415
1416 git_object_free(obj);
1417 }
1418
1419 static int checkout_conflict_count_cb(
1420 git_checkout_notify_t why,
1421 const char *path,
1422 const git_diff_file *b,
1423 const git_diff_file *t,
1424 const git_diff_file *w,
1425 void *payload)
1426 {
1427 size_t *n = payload;
1428
1429 GIT_UNUSED(why);
1430 GIT_UNUSED(path);
1431 GIT_UNUSED(b);
1432 GIT_UNUSED(t);
1433 GIT_UNUSED(w);
1434
1435 (*n)++;
1436
1437 return 0;
1438 }
1439
1440 /* A repo that has a HEAD (even a properly born HEAD that peels to
1441 * a commit) but no index should be treated as if it's an empty baseline
1442 */
1443 void test_checkout_tree__baseline_is_empty_when_no_index(void)
1444 {
1445 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1446 git_reference *head;
1447 git_object *obj;
1448 git_status_list *status;
1449 size_t conflicts = 0;
1450
1451 assert_on_branch(g_repo, "master");
1452 cl_git_pass(git_repository_head(&head, g_repo));
1453 cl_git_pass(git_reference_peel(&obj, head, GIT_OBJ_COMMIT));
1454
1455 cl_git_pass(git_reset(g_repo, obj, GIT_RESET_HARD, NULL));
1456
1457 cl_must_pass(p_unlink("testrepo/.git/index"));
1458
1459 /* for a safe checkout, we should have checkout conflicts with
1460 * the existing untracked files.
1461 */
1462 opts.checkout_strategy &= ~GIT_CHECKOUT_FORCE;
1463 opts.notify_flags = GIT_CHECKOUT_NOTIFY_CONFLICT;
1464 opts.notify_cb = checkout_conflict_count_cb;
1465 opts.notify_payload = &conflicts;
1466
1467 cl_git_fail_with(GIT_ECONFLICT, git_checkout_tree(g_repo, obj, &opts));
1468 cl_assert_equal_i(4, conflicts);
1469
1470 /* but force should succeed and update the index */
1471 opts.checkout_strategy |= GIT_CHECKOUT_FORCE;
1472 cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
1473
1474 cl_git_pass(git_status_list_new(&status, g_repo, NULL));
1475 cl_assert_equal_i(0, git_status_list_entrycount(status));
1476 git_status_list_free(status);
1477
1478 git_object_free(obj);
1479 git_reference_free(head);
1480 }
1481