3 # Copyright (c) 2012 Avery Pennaraum
4 # Copyright (c) 2015 Alexey Shumkin
6 test_description='Basic porcelain support for subtrees
8 This test verifies the basic operation of the add, pull, merge
9 and split subcommands of git subtree.
12 TEST_DIRECTORY=$(pwd)/../../../t
15 . ../../../t/test-lib.sh
17 subtree_test_create_repo()
19 test_create_repo "$1" &&
22 git config log.date relative
35 test_debug "echo \"check a:\" \"{$1}\""
36 test_debug "echo \" b:\" \"{$2}\""
37 if [ "$1" = "$2" ]; then
46 git reset --hard HEAD~
49 # Make sure no patch changes more than one file.
50 # The original set of commits changed only one file each.
51 # A multi-file change would imply that we pruned commits
60 elif [ "$x" = "commit:" ]; then
61 if [ -n "$commit" ]; then
73 test_create_commit() (
77 mkdir -p "$(dirname "$commit")" \
78 || error "Could not create directory for commit"
79 echo "$commit" >"$commit" &&
80 git add "$commit" || error "Could not add commit"
81 git commit -m "$commit" || error "Could not commit"
86 git log --pretty=format:%s -1
90 # Tests for 'git subtree add'
93 test_expect_success 'no merge from non-existent subtree' '
94 subtree_test_create_repo "$test_count" &&
95 subtree_test_create_repo "$test_count/sub proj" &&
96 test_create_commit "$test_count" main1 &&
97 test_create_commit "$test_count/sub proj" sub1 &&
100 git fetch ./"sub proj" HEAD &&
101 test_must_fail git subtree merge --prefix="sub dir" FETCH_HEAD
105 test_expect_success 'no pull from non-existent subtree' '
106 subtree_test_create_repo "$test_count" &&
107 subtree_test_create_repo "$test_count/sub proj" &&
108 test_create_commit "$test_count" main1 &&
109 test_create_commit "$test_count/sub proj" sub1 &&
112 git fetch ./"sub proj" HEAD &&
113 test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" HEAD
116 test_expect_success 'add subproj as subtree into sub dir/ with --prefix' '
117 subtree_test_create_repo "$test_count" &&
118 subtree_test_create_repo "$test_count/sub proj" &&
119 test_create_commit "$test_count" main1 &&
120 test_create_commit "$test_count/sub proj" sub1 &&
123 git fetch ./"sub proj" HEAD &&
124 git subtree add --prefix="sub dir" FETCH_HEAD &&
125 check_equal "$(last_commit_message)" "Add '\''sub dir/'\'' from commit '\''$(git rev-parse FETCH_HEAD)'\''"
129 test_expect_success 'add subproj as subtree into sub dir/ with --prefix and --message' '
130 subtree_test_create_repo "$test_count" &&
131 subtree_test_create_repo "$test_count/sub proj" &&
132 test_create_commit "$test_count" main1 &&
133 test_create_commit "$test_count/sub proj" sub1 &&
136 git fetch ./"sub proj" HEAD &&
137 git subtree add --prefix="sub dir" --message="Added subproject" FETCH_HEAD &&
138 check_equal "$(last_commit_message)" "Added subproject"
142 test_expect_success 'add subproj as subtree into sub dir/ with --prefix as -P and --message as -m' '
143 subtree_test_create_repo "$test_count" &&
144 subtree_test_create_repo "$test_count/sub proj" &&
145 test_create_commit "$test_count" main1 &&
146 test_create_commit "$test_count/sub proj" sub1 &&
149 git fetch ./"sub proj" HEAD &&
150 git subtree add -P "sub dir" -m "Added subproject" FETCH_HEAD &&
151 check_equal "$(last_commit_message)" "Added subproject"
155 test_expect_success 'add subproj as subtree into sub dir/ with --squash and --prefix and --message' '
156 subtree_test_create_repo "$test_count" &&
157 subtree_test_create_repo "$test_count/sub proj" &&
158 test_create_commit "$test_count" main1 &&
159 test_create_commit "$test_count/sub proj" sub1 &&
162 git fetch ./"sub proj" HEAD &&
163 git subtree add --prefix="sub dir" --message="Added subproject with squash" --squash FETCH_HEAD &&
164 check_equal "$(last_commit_message)" "Added subproject with squash"
169 # Tests for 'git subtree merge'
172 test_expect_success 'merge new subproj history into sub dir/ with --prefix' '
173 subtree_test_create_repo "$test_count" &&
174 subtree_test_create_repo "$test_count/sub proj" &&
175 test_create_commit "$test_count" main1 &&
176 test_create_commit "$test_count/sub proj" sub1 &&
179 git fetch ./"sub proj" HEAD &&
180 git subtree add --prefix="sub dir" FETCH_HEAD
182 test_create_commit "$test_count/sub proj" sub2 &&
185 git fetch ./"sub proj" HEAD &&
186 git subtree merge --prefix="sub dir" FETCH_HEAD &&
187 check_equal "$(last_commit_message)" "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''"
191 test_expect_success 'merge new subproj history into sub dir/ with --prefix and --message' '
192 subtree_test_create_repo "$test_count" &&
193 subtree_test_create_repo "$test_count/sub proj" &&
194 test_create_commit "$test_count" main1 &&
195 test_create_commit "$test_count/sub proj" sub1 &&
198 git fetch ./"sub proj" HEAD &&
199 git subtree add --prefix="sub dir" FETCH_HEAD
201 test_create_commit "$test_count/sub proj" sub2 &&
204 git fetch ./"sub proj" HEAD &&
205 git subtree merge --prefix="sub dir" --message="Merged changes from subproject" FETCH_HEAD &&
206 check_equal "$(last_commit_message)" "Merged changes from subproject"
210 test_expect_success 'merge new subproj history into sub dir/ with --squash and --prefix and --message' '
211 subtree_test_create_repo "$test_count/sub proj" &&
212 subtree_test_create_repo "$test_count" &&
213 test_create_commit "$test_count" main1 &&
214 test_create_commit "$test_count/sub proj" sub1 &&
217 git fetch ./"sub proj" HEAD &&
218 git subtree add --prefix="sub dir" FETCH_HEAD
220 test_create_commit "$test_count/sub proj" sub2 &&
223 git fetch ./"sub proj" HEAD &&
224 git subtree merge --prefix="sub dir" --message="Merged changes from subproject using squash" --squash FETCH_HEAD &&
225 check_equal "$(last_commit_message)" "Merged changes from subproject using squash"
229 test_expect_success 'merge the added subproj again, should do nothing' '
230 subtree_test_create_repo "$test_count" &&
231 subtree_test_create_repo "$test_count/sub proj" &&
232 test_create_commit "$test_count" main1 &&
233 test_create_commit "$test_count/sub proj" sub1 &&
236 git fetch ./"sub proj" HEAD &&
237 git subtree add --prefix="sub dir" FETCH_HEAD &&
238 # this shouldn not actually do anything, since FETCH_HEAD
239 # is already a parent
240 result=$(git merge -s ours -m "merge -s -ours" FETCH_HEAD) &&
241 check_equal "${result}" "Already up to date."
245 test_expect_success 'merge new subproj history into subdir/ with a slash appended to the argument of --prefix' '
246 test_create_repo "$test_count" &&
247 test_create_repo "$test_count/subproj" &&
248 test_create_commit "$test_count" main1 &&
249 test_create_commit "$test_count/subproj" sub1 &&
252 git fetch ./subproj HEAD &&
253 git subtree add --prefix=subdir/ FETCH_HEAD
255 test_create_commit "$test_count/subproj" sub2 &&
258 git fetch ./subproj HEAD &&
259 git subtree merge --prefix=subdir/ FETCH_HEAD &&
260 check_equal "$(last_commit_message)" "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''"
265 # Tests for 'git subtree split'
268 test_expect_success 'split requires option --prefix' '
269 subtree_test_create_repo "$test_count" &&
270 subtree_test_create_repo "$test_count/sub proj" &&
271 test_create_commit "$test_count" main1 &&
272 test_create_commit "$test_count/sub proj" sub1 &&
275 git fetch ./"sub proj" HEAD &&
276 git subtree add --prefix="sub dir" FETCH_HEAD &&
277 echo "You must provide the --prefix option." > expected &&
278 test_must_fail git subtree split > actual 2>&1 &&
279 test_debug "printf '"expected: "'" &&
280 test_debug "cat expected" &&
281 test_debug "printf '"actual: "'" &&
282 test_debug "cat actual" &&
283 test_cmp expected actual
287 test_expect_success 'split requires path given by option --prefix must exist' '
288 subtree_test_create_repo "$test_count" &&
289 subtree_test_create_repo "$test_count/sub proj" &&
290 test_create_commit "$test_count" main1 &&
291 test_create_commit "$test_count/sub proj" sub1 &&
294 git fetch ./"sub proj" HEAD &&
295 git subtree add --prefix="sub dir" FETCH_HEAD &&
296 echo "'\''non-existent-directory'\'' does not exist; use '\''git subtree add'\''" > expected &&
297 test_must_fail git subtree split --prefix=non-existent-directory > actual 2>&1 &&
298 test_debug "printf '"expected: "'" &&
299 test_debug "cat expected" &&
300 test_debug "printf '"actual: "'" &&
301 test_debug "cat actual" &&
302 test_cmp expected actual
306 test_expect_success 'split sub dir/ with --rejoin' '
307 subtree_test_create_repo "$test_count" &&
308 subtree_test_create_repo "$test_count/sub proj" &&
309 test_create_commit "$test_count" main1 &&
310 test_create_commit "$test_count/sub proj" sub1 &&
313 git fetch ./"sub proj" HEAD &&
314 git subtree add --prefix="sub dir" FETCH_HEAD
316 test_create_commit "$test_count" "sub dir"/main-sub1 &&
317 test_create_commit "$test_count" main2 &&
318 test_create_commit "$test_count/sub proj" sub2 &&
319 test_create_commit "$test_count" "sub dir"/main-sub2 &&
322 git fetch ./"sub proj" HEAD &&
323 git subtree merge --prefix="sub dir" FETCH_HEAD &&
324 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
325 git subtree split --prefix="sub dir" --annotate="*" --rejoin &&
326 check_equal "$(last_commit_message)" "Split '\''sub dir/'\'' into commit '\''$split_hash'\''"
330 test_expect_success 'split sub dir/ with --rejoin from scratch' '
331 subtree_test_create_repo "$test_count" &&
332 test_create_commit "$test_count" main1 &&
336 echo file >"sub dir"/file &&
337 git add "sub dir/file" &&
338 git commit -m"sub dir file" &&
339 split_hash=$(git subtree split --prefix="sub dir" --rejoin) &&
340 git subtree split --prefix="sub dir" --rejoin &&
341 check_equal "$(last_commit_message)" "Split '\''sub dir/'\'' into commit '\''$split_hash'\''"
345 test_expect_success 'split sub dir/ with --rejoin and --message' '
346 subtree_test_create_repo "$test_count" &&
347 subtree_test_create_repo "$test_count/sub proj" &&
348 test_create_commit "$test_count" main1 &&
349 test_create_commit "$test_count/sub proj" sub1 &&
352 git fetch ./"sub proj" HEAD &&
353 git subtree add --prefix="sub dir" FETCH_HEAD
355 test_create_commit "$test_count" "sub dir"/main-sub1 &&
356 test_create_commit "$test_count" main2 &&
357 test_create_commit "$test_count/sub proj" sub2 &&
358 test_create_commit "$test_count" "sub dir"/main-sub2 &&
361 git fetch ./"sub proj" HEAD &&
362 git subtree merge --prefix="sub dir" FETCH_HEAD &&
363 git subtree split --prefix="sub dir" --message="Split & rejoin" --annotate="*" --rejoin &&
364 check_equal "$(last_commit_message)" "Split & rejoin"
368 test_expect_success 'split "sub dir"/ with --branch' '
369 subtree_test_create_repo "$test_count" &&
370 subtree_test_create_repo "$test_count/sub proj" &&
371 test_create_commit "$test_count" main1 &&
372 test_create_commit "$test_count/sub proj" sub1 &&
375 git fetch ./"sub proj" HEAD &&
376 git subtree add --prefix="sub dir" FETCH_HEAD
378 test_create_commit "$test_count" "sub dir"/main-sub1 &&
379 test_create_commit "$test_count" main2 &&
380 test_create_commit "$test_count/sub proj" sub2 &&
381 test_create_commit "$test_count" "sub dir"/main-sub2 &&
384 git fetch ./"sub proj" HEAD &&
385 git subtree merge --prefix="sub dir" FETCH_HEAD &&
386 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
387 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
388 check_equal "$(git rev-parse subproj-br)" "$split_hash"
392 test_expect_success 'check hash of split' '
393 subtree_test_create_repo "$test_count" &&
394 subtree_test_create_repo "$test_count/sub proj" &&
395 test_create_commit "$test_count" main1 &&
396 test_create_commit "$test_count/sub proj" sub1 &&
399 git fetch ./"sub proj" HEAD &&
400 git subtree add --prefix="sub dir" FETCH_HEAD
402 test_create_commit "$test_count" "sub dir"/main-sub1 &&
403 test_create_commit "$test_count" main2 &&
404 test_create_commit "$test_count/sub proj" sub2 &&
405 test_create_commit "$test_count" "sub dir"/main-sub2 &&
408 git fetch ./"sub proj" HEAD &&
409 git subtree merge --prefix="sub dir" FETCH_HEAD &&
410 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
411 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
412 check_equal "$(git rev-parse subproj-br)" "$split_hash" &&
413 # Check hash of split
414 new_hash=$(git rev-parse subproj-br^2) &&
417 subdir_hash=$(git rev-parse HEAD) &&
418 check_equal ''"$new_hash"'' "$subdir_hash"
423 test_expect_success 'split "sub dir"/ with --branch for an existing branch' '
424 subtree_test_create_repo "$test_count" &&
425 subtree_test_create_repo "$test_count/sub proj" &&
426 test_create_commit "$test_count" main1 &&
427 test_create_commit "$test_count/sub proj" sub1 &&
430 git fetch ./"sub proj" HEAD &&
431 git branch subproj-br FETCH_HEAD &&
432 git subtree add --prefix="sub dir" FETCH_HEAD
434 test_create_commit "$test_count" "sub dir"/main-sub1 &&
435 test_create_commit "$test_count" main2 &&
436 test_create_commit "$test_count/sub proj" sub2 &&
437 test_create_commit "$test_count" "sub dir"/main-sub2 &&
440 git fetch ./"sub proj" HEAD &&
441 git subtree merge --prefix="sub dir" FETCH_HEAD &&
442 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
443 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
444 check_equal "$(git rev-parse subproj-br)" "$split_hash"
448 test_expect_success 'split "sub dir"/ with --branch for an incompatible branch' '
449 subtree_test_create_repo "$test_count" &&
450 subtree_test_create_repo "$test_count/sub proj" &&
451 test_create_commit "$test_count" main1 &&
452 test_create_commit "$test_count/sub proj" sub1 &&
455 git branch init HEAD &&
456 git fetch ./"sub proj" HEAD &&
457 git subtree add --prefix="sub dir" FETCH_HEAD
459 test_create_commit "$test_count" "sub dir"/main-sub1 &&
460 test_create_commit "$test_count" main2 &&
461 test_create_commit "$test_count/sub proj" sub2 &&
462 test_create_commit "$test_count" "sub dir"/main-sub2 &&
465 git fetch ./"sub proj" HEAD &&
466 git subtree merge --prefix="sub dir" FETCH_HEAD &&
467 test_must_fail git subtree split --prefix="sub dir" --branch init
475 test_expect_success 'make sure exactly the right set of files ends up in the subproj' '
476 subtree_test_create_repo "$test_count" &&
477 subtree_test_create_repo "$test_count/sub proj" &&
478 test_create_commit "$test_count" main1 &&
479 test_create_commit "$test_count/sub proj" sub1 &&
482 git fetch ./"sub proj" HEAD &&
483 git subtree add --prefix="sub dir" FETCH_HEAD
485 test_create_commit "$test_count" "sub dir"/main-sub1 &&
486 test_create_commit "$test_count" main2 &&
487 test_create_commit "$test_count/sub proj" sub2 &&
488 test_create_commit "$test_count" "sub dir"/main-sub2 &&
491 git fetch ./"sub proj" HEAD &&
492 git subtree merge --prefix="sub dir" FETCH_HEAD &&
493 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
495 test_create_commit "$test_count/sub proj" sub3 &&
496 test_create_commit "$test_count" "sub dir"/main-sub3 &&
498 cd "$test_count/sub proj" &&
499 git fetch .. subproj-br &&
502 test_create_commit "$test_count/sub proj" sub4 &&
505 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
507 test_create_commit "$test_count" "sub dir"/main-sub4 &&
510 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
513 cd "$test_count/sub proj" &&
514 git fetch .. subproj-br &&
515 git merge FETCH_HEAD &&
517 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 \
518 sub1 sub2 sub3 sub4 >expect &&
519 git ls-files >actual &&
520 test_cmp expect actual
524 test_expect_success 'make sure the subproj *only* contains commits that affect the "sub dir"' '
525 subtree_test_create_repo "$test_count" &&
526 subtree_test_create_repo "$test_count/sub proj" &&
527 test_create_commit "$test_count" main1 &&
528 test_create_commit "$test_count/sub proj" sub1 &&
531 git fetch ./"sub proj" HEAD &&
532 git subtree add --prefix="sub dir" FETCH_HEAD
534 test_create_commit "$test_count" "sub dir"/main-sub1 &&
535 test_create_commit "$test_count" main2 &&
536 test_create_commit "$test_count/sub proj" sub2 &&
537 test_create_commit "$test_count" "sub dir"/main-sub2 &&
540 git fetch ./"sub proj" HEAD &&
541 git subtree merge --prefix="sub dir" FETCH_HEAD &&
542 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
544 test_create_commit "$test_count/sub proj" sub3 &&
545 test_create_commit "$test_count" "sub dir"/main-sub3 &&
547 cd "$test_count/sub proj" &&
548 git fetch .. subproj-br &&
551 test_create_commit "$test_count/sub proj" sub4 &&
554 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
556 test_create_commit "$test_count" "sub dir"/main-sub4 &&
559 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
562 cd "$test_count/sub proj" &&
563 git fetch .. subproj-br &&
564 git merge FETCH_HEAD &&
566 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 \
567 sub1 sub2 sub3 sub4 >expect &&
568 git log --name-only --pretty=format:"" >log &&
569 sort <log | sed "/^\$/ d" >actual &&
570 test_cmp expect actual
574 test_expect_success 'make sure exactly the right set of files ends up in the mainline' '
575 subtree_test_create_repo "$test_count" &&
576 subtree_test_create_repo "$test_count/sub proj" &&
577 test_create_commit "$test_count" main1 &&
578 test_create_commit "$test_count/sub proj" sub1 &&
581 git fetch ./"sub proj" HEAD &&
582 git subtree add --prefix="sub dir" FETCH_HEAD
584 test_create_commit "$test_count" "sub dir"/main-sub1 &&
585 test_create_commit "$test_count" main2 &&
586 test_create_commit "$test_count/sub proj" sub2 &&
587 test_create_commit "$test_count" "sub dir"/main-sub2 &&
590 git fetch ./"sub proj" HEAD &&
591 git subtree merge --prefix="sub dir" FETCH_HEAD &&
592 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
594 test_create_commit "$test_count/sub proj" sub3 &&
595 test_create_commit "$test_count" "sub dir"/main-sub3 &&
597 cd "$test_count/sub proj" &&
598 git fetch .. subproj-br &&
601 test_create_commit "$test_count/sub proj" sub4 &&
604 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
606 test_create_commit "$test_count" "sub dir"/main-sub4 &&
609 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
612 cd "$test_count/sub proj" &&
613 git fetch .. subproj-br &&
618 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
620 test_write_lines main1 main2 >chkm &&
621 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 >chkms &&
622 sed "s,^,sub dir/," chkms >chkms_sub &&
623 test_write_lines sub1 sub2 sub3 sub4 >chks &&
624 sed "s,^,sub dir/," chks >chks_sub &&
626 cat chkm chkms_sub chks_sub >expect &&
627 git ls-files >actual &&
628 test_cmp expect actual
632 test_expect_success 'make sure each filename changed exactly once in the entire history' '
633 subtree_test_create_repo "$test_count" &&
634 subtree_test_create_repo "$test_count/sub proj" &&
635 test_create_commit "$test_count" main1 &&
636 test_create_commit "$test_count/sub proj" sub1 &&
639 git config log.date relative &&
640 git fetch ./"sub proj" HEAD &&
641 git subtree add --prefix="sub dir" FETCH_HEAD
643 test_create_commit "$test_count" "sub dir"/main-sub1 &&
644 test_create_commit "$test_count" main2 &&
645 test_create_commit "$test_count/sub proj" sub2 &&
646 test_create_commit "$test_count" "sub dir"/main-sub2 &&
649 git fetch ./"sub proj" HEAD &&
650 git subtree merge --prefix="sub dir" FETCH_HEAD &&
651 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
653 test_create_commit "$test_count/sub proj" sub3 &&
654 test_create_commit "$test_count" "sub dir"/main-sub3 &&
656 cd "$test_count/sub proj" &&
657 git fetch .. subproj-br &&
660 test_create_commit "$test_count/sub proj" sub4 &&
663 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
665 test_create_commit "$test_count" "sub dir"/main-sub4 &&
668 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
671 cd "$test_count/sub proj" &&
672 git fetch .. subproj-br &&
677 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
679 test_write_lines main1 main2 >chkm &&
680 test_write_lines sub1 sub2 sub3 sub4 >chks &&
681 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 >chkms &&
682 sed "s,^,sub dir/," chkms >chkms_sub &&
684 # main-sub?? and /"sub dir"/main-sub?? both change, because those are the
685 # changes that were split into their own history. And "sub dir"/sub?? never
686 # change, since they were *only* changed in the subtree branch.
687 git log --name-only --pretty=format:"" >log &&
688 sort <log >sorted-log &&
689 sed "/^$/ d" sorted-log >actual &&
691 cat chkms chkm chks chkms_sub >expect-unsorted &&
692 sort expect-unsorted >expect &&
693 test_cmp expect actual
697 test_expect_success 'make sure the --rejoin commits never make it into subproj' '
698 subtree_test_create_repo "$test_count" &&
699 subtree_test_create_repo "$test_count/sub proj" &&
700 test_create_commit "$test_count" main1 &&
701 test_create_commit "$test_count/sub proj" sub1 &&
704 git fetch ./"sub proj" HEAD &&
705 git subtree add --prefix="sub dir" FETCH_HEAD
707 test_create_commit "$test_count" "sub dir"/main-sub1 &&
708 test_create_commit "$test_count" main2 &&
709 test_create_commit "$test_count/sub proj" sub2 &&
710 test_create_commit "$test_count" "sub dir"/main-sub2 &&
713 git fetch ./"sub proj" HEAD &&
714 git subtree merge --prefix="sub dir" FETCH_HEAD &&
715 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
717 test_create_commit "$test_count/sub proj" sub3 &&
718 test_create_commit "$test_count" "sub dir"/main-sub3 &&
720 cd "$test_count/sub proj" &&
721 git fetch .. subproj-br &&
724 test_create_commit "$test_count/sub proj" sub4 &&
727 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
729 test_create_commit "$test_count" "sub dir"/main-sub4 &&
732 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
735 cd "$test_count/sub proj" &&
736 git fetch .. subproj-br &&
741 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
742 check_equal "$(git log --pretty=format:"%s" HEAD^2 | grep -i split)" ""
746 test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' '
747 subtree_test_create_repo "$test_count" &&
748 subtree_test_create_repo "$test_count/sub proj" &&
749 test_create_commit "$test_count" main1 &&
750 test_create_commit "$test_count/sub proj" sub1 &&
753 git fetch ./"sub proj" HEAD &&
754 git subtree add --prefix="sub dir" FETCH_HEAD
756 test_create_commit "$test_count" "sub dir"/main-sub1 &&
757 test_create_commit "$test_count" main2 &&
758 test_create_commit "$test_count/sub proj" sub2 &&
759 test_create_commit "$test_count" "sub dir"/main-sub2 &&
762 git fetch ./"sub proj" HEAD &&
763 git subtree merge --prefix="sub dir" FETCH_HEAD &&
764 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
766 test_create_commit "$test_count/sub proj" sub3 &&
767 test_create_commit "$test_count" "sub dir"/main-sub3 &&
769 cd "$test_count/sub proj" &&
770 git fetch .. subproj-br &&
773 test_create_commit "$test_count/sub proj" sub4 &&
776 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
778 test_create_commit "$test_count" "sub dir"/main-sub4 &&
781 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
784 cd "$test_count/sub proj" &&
785 git fetch .. subproj-br &&
790 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
792 # They are meaningless to subproj since one side of the merge refers to the mainline
793 check_equal "$(git log --pretty=format:"%s%n%b" HEAD^2 | grep "git-subtree.*:")" ""
801 test_expect_success 'make sure "git subtree split" find the correct parent' '
802 subtree_test_create_repo "$test_count" &&
803 subtree_test_create_repo "$test_count/sub proj" &&
804 test_create_commit "$test_count" main1 &&
805 test_create_commit "$test_count/sub proj" sub1 &&
808 git fetch ./"sub proj" HEAD &&
809 git subtree add --prefix="sub dir" FETCH_HEAD
811 test_create_commit "$test_count/sub proj" sub2 &&
814 git fetch ./"sub proj" HEAD &&
815 git branch subproj-ref FETCH_HEAD &&
816 git subtree merge --prefix="sub dir" FETCH_HEAD
818 test_create_commit "$test_count" "sub dir"/main-sub1 &&
821 git subtree split --prefix="sub dir" --branch subproj-br &&
823 # at this point, the new commit parent should be subproj-ref, if it is
824 # not, something went wrong (the "newparent" of "HEAD~" commit should
825 # have been sub2, but it was not, because its cache was not set to
827 check_equal "$(git log --pretty=format:%P -1 subproj-br)" "$(git rev-parse subproj-ref)"
831 test_expect_success 'split a new subtree without --onto option' '
832 subtree_test_create_repo "$test_count" &&
833 subtree_test_create_repo "$test_count/sub proj" &&
834 test_create_commit "$test_count" main1 &&
835 test_create_commit "$test_count/sub proj" sub1 &&
838 git fetch ./"sub proj" HEAD &&
839 git subtree add --prefix="sub dir" FETCH_HEAD
841 test_create_commit "$test_count/sub proj" sub2 &&
844 git fetch ./"sub proj" HEAD &&
845 git subtree merge --prefix="sub dir" FETCH_HEAD
847 test_create_commit "$test_count" "sub dir"/main-sub1 &&
850 git subtree split --prefix="sub dir" --branch subproj-br
852 mkdir "$test_count"/"sub dir2" &&
853 test_create_commit "$test_count" "sub dir2"/main-sub2 &&
857 # also test that we still can split out an entirely new subtree
858 # if the parent of the first commit in the tree is not empty,
859 # then the new subtree has accidentally been attached to something
860 git subtree split --prefix="sub dir2" --branch subproj2-br &&
861 check_equal "$(git log --pretty=format:%P -1 subproj2-br)" ""
865 test_expect_success 'verify one file change per commit' '
866 subtree_test_create_repo "$test_count" &&
867 subtree_test_create_repo "$test_count/sub proj" &&
868 test_create_commit "$test_count" main1 &&
869 test_create_commit "$test_count/sub proj" sub1 &&
872 git fetch ./"sub proj" HEAD &&
873 git branch sub1 FETCH_HEAD &&
874 git subtree add --prefix="sub dir" sub1
876 test_create_commit "$test_count/sub proj" sub2 &&
879 git fetch ./"sub proj" HEAD &&
880 git subtree merge --prefix="sub dir" FETCH_HEAD
882 test_create_commit "$test_count" "sub dir"/main-sub1 &&
885 git subtree split --prefix="sub dir" --branch subproj-br
887 mkdir "$test_count"/"sub dir2" &&
888 test_create_commit "$test_count" "sub dir2"/main-sub2 &&
891 git subtree split --prefix="sub dir2" --branch subproj2-br &&
894 git log --pretty=format:"commit: %H" | join_commits |
896 while read commit a b; do
897 test_debug "echo Verifying commit $commit"
898 test_debug "echo a: $a"
899 test_debug "echo b: $b"
908 test_expect_success 'push split to subproj' '
909 subtree_test_create_repo "$test_count" &&
910 subtree_test_create_repo "$test_count/sub proj" &&
911 test_create_commit "$test_count" main1 &&
912 test_create_commit "$test_count/sub proj" sub1 &&
915 git fetch ./"sub proj" HEAD &&
916 git subtree add --prefix="sub dir" FETCH_HEAD
918 test_create_commit "$test_count" "sub dir"/main-sub1 &&
919 test_create_commit "$test_count" main2 &&
920 test_create_commit "$test_count/sub proj" sub2 &&
921 test_create_commit "$test_count" "sub dir"/main-sub2 &&
923 cd $test_count/"sub proj" &&
924 git branch sub-branch-1 &&
926 git fetch ./"sub proj" HEAD &&
927 git subtree merge --prefix="sub dir" FETCH_HEAD
929 test_create_commit "$test_count" "sub dir"/main-sub3 &&
932 git subtree push ./"sub proj" --prefix "sub dir" sub-branch-1 &&
934 git checkout sub-branch-1 &&
935 check_equal "$(last_commit_message)" "sub dir/main-sub3"
940 # This test covers 2 cases in subtree split copy_or_skip code
941 # 1) Merges where one parent is a superset of the changes of the other
942 # parent regarding changes to the subtree, in this case the merge
943 # commit should be copied
944 # 2) Merges where only one parent operate on the subtree, and the merge
945 # commit should be skipped
947 # (1) is checked by ensuring subtree_tip is a descendent of subtree_branch
948 # (2) should have a check added (not_a_subtree_change shouldn't be present
949 # on the produced subtree)
951 # Other related cases which are not tested (or currently handled correctly)
952 # - Case (1) where there are more than 2 parents, it will sometimes correctly copy
953 # the merge, and sometimes not
954 # - Merge commit where both parents have same tree as the merge, currently
955 # will always be skipped, even if they reached that state via different
959 test_expect_success 'subtree descendant check' '
960 subtree_test_create_repo "$test_count" &&
961 defaultBranch=$(sed "s,ref: refs/heads/,," "$test_count/.git/HEAD") &&
962 test_create_commit "$test_count" folder_subtree/a &&
967 test_create_commit "$test_count" folder_subtree/0 &&
968 test_create_commit "$test_count" folder_subtree/b &&
969 cherry=$(cd "$test_count"; git rev-parse HEAD) &&
974 test_create_commit "$test_count" commit_on_branch &&
977 git cherry-pick $cherry &&
978 git checkout $defaultBranch &&
979 git merge -m "merge should be kept on subtree" branch &&
980 git branch no_subtree_work_branch
982 test_create_commit "$test_count" folder_subtree/d &&
985 git checkout no_subtree_work_branch
987 test_create_commit "$test_count" not_a_subtree_change &&
990 git checkout $defaultBranch &&
991 git merge -m "merge should be skipped on subtree" no_subtree_work_branch &&
993 git subtree split --prefix folder_subtree/ --branch subtree_tip $defaultBranch &&
994 git subtree split --prefix folder_subtree/ --branch subtree_branch branch &&
995 check_equal $(git rev-list --count subtree_tip..subtree_branch) 0