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
13 . "$TEST_DIRECTORY"/test-lib.sh
15 # Use our own wrapper around test-lib.sh's test_create_repo, in order
16 # to set log.date=relative. `git subtree` parses the output of `git
17 # log`, and so it must be careful to not be affected by settings that
18 # change the `git log` output. We test this by setting
19 # log.date=relative for every repo in the tests.
20 subtree_test_create_repo () {
21 test_create_repo "$1" &&
22 git -C "$1" config log.date relative
25 test_create_commit () (
29 mkdir -p "$(dirname "$commit")" \
30 || error "Could not create directory for commit"
31 echo "$commit" >"$commit" &&
32 git add "$commit" || error "Could not add commit"
33 git commit -m "$commit" || error "Could not commit"
36 last_commit_subject () {
37 git log --pretty=format:%s -1
41 # Tests for 'git subtree add'
44 test_expect_success 'no merge from non-existent subtree' '
45 subtree_test_create_repo "$test_count" &&
46 subtree_test_create_repo "$test_count/sub proj" &&
47 test_create_commit "$test_count" main1 &&
48 test_create_commit "$test_count/sub proj" sub1 &&
51 git fetch ./"sub proj" HEAD &&
52 test_must_fail git subtree merge --prefix="sub dir" FETCH_HEAD
56 test_expect_success 'no pull from non-existent subtree' '
57 subtree_test_create_repo "$test_count" &&
58 subtree_test_create_repo "$test_count/sub proj" &&
59 test_create_commit "$test_count" main1 &&
60 test_create_commit "$test_count/sub proj" sub1 &&
63 git fetch ./"sub proj" HEAD &&
64 test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" HEAD
68 test_expect_success 'add subproj as subtree into sub dir/ with --prefix' '
69 subtree_test_create_repo "$test_count" &&
70 subtree_test_create_repo "$test_count/sub proj" &&
71 test_create_commit "$test_count" main1 &&
72 test_create_commit "$test_count/sub proj" sub1 &&
75 git fetch ./"sub proj" HEAD &&
76 git subtree add --prefix="sub dir" FETCH_HEAD &&
77 test "$(last_commit_subject)" = "Add '\''sub dir/'\'' from commit '\''$(git rev-parse FETCH_HEAD)'\''"
81 test_expect_success 'add subproj as subtree into sub dir/ with --prefix and --message' '
82 subtree_test_create_repo "$test_count" &&
83 subtree_test_create_repo "$test_count/sub proj" &&
84 test_create_commit "$test_count" main1 &&
85 test_create_commit "$test_count/sub proj" sub1 &&
88 git fetch ./"sub proj" HEAD &&
89 git subtree add --prefix="sub dir" --message="Added subproject" FETCH_HEAD &&
90 test "$(last_commit_subject)" = "Added subproject"
94 test_expect_success 'add subproj as subtree into sub dir/ with --prefix as -P and --message as -m' '
95 subtree_test_create_repo "$test_count" &&
96 subtree_test_create_repo "$test_count/sub proj" &&
97 test_create_commit "$test_count" main1 &&
98 test_create_commit "$test_count/sub proj" sub1 &&
101 git fetch ./"sub proj" HEAD &&
102 git subtree add -P "sub dir" -m "Added subproject" FETCH_HEAD &&
103 test "$(last_commit_subject)" = "Added subproject"
107 test_expect_success 'add subproj as subtree into sub dir/ with --squash and --prefix and --message' '
108 subtree_test_create_repo "$test_count" &&
109 subtree_test_create_repo "$test_count/sub proj" &&
110 test_create_commit "$test_count" main1 &&
111 test_create_commit "$test_count/sub proj" sub1 &&
114 git fetch ./"sub proj" HEAD &&
115 git subtree add --prefix="sub dir" --message="Added subproject with squash" --squash FETCH_HEAD &&
116 test "$(last_commit_subject)" = "Added subproject with squash"
121 # Tests for 'git subtree merge'
124 test_expect_success 'merge new subproj history into sub dir/ with --prefix' '
125 subtree_test_create_repo "$test_count" &&
126 subtree_test_create_repo "$test_count/sub proj" &&
127 test_create_commit "$test_count" main1 &&
128 test_create_commit "$test_count/sub proj" sub1 &&
131 git fetch ./"sub proj" HEAD &&
132 git subtree add --prefix="sub dir" FETCH_HEAD
134 test_create_commit "$test_count/sub proj" sub2 &&
137 git fetch ./"sub proj" HEAD &&
138 git subtree merge --prefix="sub dir" FETCH_HEAD &&
139 test "$(last_commit_subject)" = "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''"
143 test_expect_success 'merge new subproj history into sub dir/ with --prefix and --message' '
144 subtree_test_create_repo "$test_count" &&
145 subtree_test_create_repo "$test_count/sub proj" &&
146 test_create_commit "$test_count" main1 &&
147 test_create_commit "$test_count/sub proj" sub1 &&
150 git fetch ./"sub proj" HEAD &&
151 git subtree add --prefix="sub dir" FETCH_HEAD
153 test_create_commit "$test_count/sub proj" sub2 &&
156 git fetch ./"sub proj" HEAD &&
157 git subtree merge --prefix="sub dir" --message="Merged changes from subproject" FETCH_HEAD &&
158 test "$(last_commit_subject)" = "Merged changes from subproject"
162 test_expect_success 'merge new subproj history into sub dir/ with --squash and --prefix and --message' '
163 subtree_test_create_repo "$test_count/sub proj" &&
164 subtree_test_create_repo "$test_count" &&
165 test_create_commit "$test_count" main1 &&
166 test_create_commit "$test_count/sub proj" sub1 &&
169 git fetch ./"sub proj" HEAD &&
170 git subtree add --prefix="sub dir" FETCH_HEAD
172 test_create_commit "$test_count/sub proj" sub2 &&
175 git fetch ./"sub proj" HEAD &&
176 git subtree merge --prefix="sub dir" --message="Merged changes from subproject using squash" --squash FETCH_HEAD &&
177 test "$(last_commit_subject)" = "Merged changes from subproject using squash"
181 test_expect_success 'merge the added subproj again, should do nothing' '
182 subtree_test_create_repo "$test_count" &&
183 subtree_test_create_repo "$test_count/sub proj" &&
184 test_create_commit "$test_count" main1 &&
185 test_create_commit "$test_count/sub proj" sub1 &&
188 git fetch ./"sub proj" HEAD &&
189 git subtree add --prefix="sub dir" FETCH_HEAD &&
190 # this shouldn not actually do anything, since FETCH_HEAD
191 # is already a parent
192 result=$(git merge -s ours -m "merge -s -ours" FETCH_HEAD) &&
193 test "${result}" = "Already up to date."
197 test_expect_success 'merge new subproj history into subdir/ with a slash appended to the argument of --prefix' '
198 subtree_test_create_repo "$test_count" &&
199 subtree_test_create_repo "$test_count/subproj" &&
200 test_create_commit "$test_count" main1 &&
201 test_create_commit "$test_count/subproj" sub1 &&
204 git fetch ./subproj HEAD &&
205 git subtree add --prefix=subdir/ FETCH_HEAD
207 test_create_commit "$test_count/subproj" sub2 &&
210 git fetch ./subproj HEAD &&
211 git subtree merge --prefix=subdir/ FETCH_HEAD &&
212 test "$(last_commit_subject)" = "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''"
217 # Tests for 'git subtree split'
220 test_expect_success 'split requires option --prefix' '
221 subtree_test_create_repo "$test_count" &&
222 subtree_test_create_repo "$test_count/sub proj" &&
223 test_create_commit "$test_count" main1 &&
224 test_create_commit "$test_count/sub proj" sub1 &&
227 git fetch ./"sub proj" HEAD &&
228 git subtree add --prefix="sub dir" FETCH_HEAD &&
229 echo "You must provide the --prefix option." >expected &&
230 test_must_fail git subtree split >actual 2>&1 &&
231 test_debug "printf '"expected: "'" &&
232 test_debug "cat expected" &&
233 test_debug "printf '"actual: "'" &&
234 test_debug "cat actual" &&
235 test_cmp expected actual
239 test_expect_success 'split requires path given by option --prefix must exist' '
240 subtree_test_create_repo "$test_count" &&
241 subtree_test_create_repo "$test_count/sub proj" &&
242 test_create_commit "$test_count" main1 &&
243 test_create_commit "$test_count/sub proj" sub1 &&
246 git fetch ./"sub proj" HEAD &&
247 git subtree add --prefix="sub dir" FETCH_HEAD &&
248 echo "'\''non-existent-directory'\'' does not exist; use '\''git subtree add'\''" >expected &&
249 test_must_fail git subtree split --prefix=non-existent-directory >actual 2>&1 &&
250 test_debug "printf '"expected: "'" &&
251 test_debug "cat expected" &&
252 test_debug "printf '"actual: "'" &&
253 test_debug "cat actual" &&
254 test_cmp expected actual
258 test_expect_success 'split sub dir/ with --rejoin' '
259 subtree_test_create_repo "$test_count" &&
260 subtree_test_create_repo "$test_count/sub proj" &&
261 test_create_commit "$test_count" main1 &&
262 test_create_commit "$test_count/sub proj" sub1 &&
265 git fetch ./"sub proj" HEAD &&
266 git subtree add --prefix="sub dir" FETCH_HEAD
268 test_create_commit "$test_count" "sub dir"/main-sub1 &&
269 test_create_commit "$test_count" main2 &&
270 test_create_commit "$test_count/sub proj" sub2 &&
271 test_create_commit "$test_count" "sub dir"/main-sub2 &&
274 git fetch ./"sub proj" HEAD &&
275 git subtree merge --prefix="sub dir" FETCH_HEAD &&
276 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
277 git subtree split --prefix="sub dir" --annotate="*" --rejoin &&
278 test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''"
282 test_expect_success 'split sub dir/ with --rejoin from scratch' '
283 subtree_test_create_repo "$test_count" &&
284 test_create_commit "$test_count" main1 &&
288 echo file >"sub dir"/file &&
289 git add "sub dir/file" &&
290 git commit -m"sub dir file" &&
291 split_hash=$(git subtree split --prefix="sub dir" --rejoin) &&
292 git subtree split --prefix="sub dir" --rejoin &&
293 test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''"
297 test_expect_success 'split sub dir/ with --rejoin and --message' '
298 subtree_test_create_repo "$test_count" &&
299 subtree_test_create_repo "$test_count/sub proj" &&
300 test_create_commit "$test_count" main1 &&
301 test_create_commit "$test_count/sub proj" sub1 &&
304 git fetch ./"sub proj" HEAD &&
305 git subtree add --prefix="sub dir" FETCH_HEAD
307 test_create_commit "$test_count" "sub dir"/main-sub1 &&
308 test_create_commit "$test_count" main2 &&
309 test_create_commit "$test_count/sub proj" sub2 &&
310 test_create_commit "$test_count" "sub dir"/main-sub2 &&
313 git fetch ./"sub proj" HEAD &&
314 git subtree merge --prefix="sub dir" FETCH_HEAD &&
315 git subtree split --prefix="sub dir" --message="Split & rejoin" --annotate="*" --rejoin &&
316 test "$(last_commit_subject)" = "Split & rejoin"
320 test_expect_success 'split "sub dir"/ with --branch' '
321 subtree_test_create_repo "$test_count" &&
322 subtree_test_create_repo "$test_count/sub proj" &&
323 test_create_commit "$test_count" main1 &&
324 test_create_commit "$test_count/sub proj" sub1 &&
327 git fetch ./"sub proj" HEAD &&
328 git subtree add --prefix="sub dir" FETCH_HEAD
330 test_create_commit "$test_count" "sub dir"/main-sub1 &&
331 test_create_commit "$test_count" main2 &&
332 test_create_commit "$test_count/sub proj" sub2 &&
333 test_create_commit "$test_count" "sub dir"/main-sub2 &&
336 git fetch ./"sub proj" HEAD &&
337 git subtree merge --prefix="sub dir" FETCH_HEAD &&
338 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
339 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
340 test "$(git rev-parse subproj-br)" = "$split_hash"
344 test_expect_success 'check hash of split' '
345 subtree_test_create_repo "$test_count" &&
346 subtree_test_create_repo "$test_count/sub proj" &&
347 test_create_commit "$test_count" main1 &&
348 test_create_commit "$test_count/sub proj" sub1 &&
351 git fetch ./"sub proj" HEAD &&
352 git subtree add --prefix="sub dir" FETCH_HEAD
354 test_create_commit "$test_count" "sub dir"/main-sub1 &&
355 test_create_commit "$test_count" main2 &&
356 test_create_commit "$test_count/sub proj" sub2 &&
357 test_create_commit "$test_count" "sub dir"/main-sub2 &&
360 git fetch ./"sub proj" HEAD &&
361 git subtree merge --prefix="sub dir" FETCH_HEAD &&
362 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
363 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
364 test "$(git rev-parse subproj-br)" = "$split_hash" &&
365 # Check hash of split
366 new_hash=$(git rev-parse subproj-br^2) &&
369 subdir_hash=$(git rev-parse HEAD) &&
370 test "$new_hash" = "$subdir_hash"
375 test_expect_success 'split "sub dir"/ with --branch for an existing branch' '
376 subtree_test_create_repo "$test_count" &&
377 subtree_test_create_repo "$test_count/sub proj" &&
378 test_create_commit "$test_count" main1 &&
379 test_create_commit "$test_count/sub proj" sub1 &&
382 git fetch ./"sub proj" HEAD &&
383 git branch subproj-br FETCH_HEAD &&
384 git subtree add --prefix="sub dir" FETCH_HEAD
386 test_create_commit "$test_count" "sub dir"/main-sub1 &&
387 test_create_commit "$test_count" main2 &&
388 test_create_commit "$test_count/sub proj" sub2 &&
389 test_create_commit "$test_count" "sub dir"/main-sub2 &&
392 git fetch ./"sub proj" HEAD &&
393 git subtree merge --prefix="sub dir" FETCH_HEAD &&
394 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
395 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
396 test "$(git rev-parse subproj-br)" = "$split_hash"
400 test_expect_success 'split "sub dir"/ with --branch for an incompatible branch' '
401 subtree_test_create_repo "$test_count" &&
402 subtree_test_create_repo "$test_count/sub proj" &&
403 test_create_commit "$test_count" main1 &&
404 test_create_commit "$test_count/sub proj" sub1 &&
407 git branch init HEAD &&
408 git fetch ./"sub proj" HEAD &&
409 git subtree add --prefix="sub dir" FETCH_HEAD
411 test_create_commit "$test_count" "sub dir"/main-sub1 &&
412 test_create_commit "$test_count" main2 &&
413 test_create_commit "$test_count/sub proj" sub2 &&
414 test_create_commit "$test_count" "sub dir"/main-sub2 &&
417 git fetch ./"sub proj" HEAD &&
418 git subtree merge --prefix="sub dir" FETCH_HEAD &&
419 test_must_fail git subtree split --prefix="sub dir" --branch init
427 test_expect_success 'make sure exactly the right set of files ends up in the subproj' '
428 subtree_test_create_repo "$test_count" &&
429 subtree_test_create_repo "$test_count/sub proj" &&
430 test_create_commit "$test_count" main1 &&
431 test_create_commit "$test_count/sub proj" sub1 &&
434 git fetch ./"sub proj" HEAD &&
435 git subtree add --prefix="sub dir" FETCH_HEAD
437 test_create_commit "$test_count" "sub dir"/main-sub1 &&
438 test_create_commit "$test_count" main2 &&
439 test_create_commit "$test_count/sub proj" sub2 &&
440 test_create_commit "$test_count" "sub dir"/main-sub2 &&
443 git fetch ./"sub proj" HEAD &&
444 git subtree merge --prefix="sub dir" FETCH_HEAD &&
445 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
447 test_create_commit "$test_count/sub proj" sub3 &&
448 test_create_commit "$test_count" "sub dir"/main-sub3 &&
450 cd "$test_count/sub proj" &&
451 git fetch .. subproj-br &&
454 test_create_commit "$test_count/sub proj" sub4 &&
457 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
459 test_create_commit "$test_count" "sub dir"/main-sub4 &&
462 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
465 cd "$test_count/sub proj" &&
466 git fetch .. subproj-br &&
467 git merge FETCH_HEAD &&
469 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 \
470 sub1 sub2 sub3 sub4 >expect &&
471 git ls-files >actual &&
472 test_cmp expect actual
476 test_expect_success 'make sure the subproj *only* contains commits that affect the "sub dir"' '
477 subtree_test_create_repo "$test_count" &&
478 subtree_test_create_repo "$test_count/sub proj" &&
479 test_create_commit "$test_count" main1 &&
480 test_create_commit "$test_count/sub proj" sub1 &&
483 git fetch ./"sub proj" HEAD &&
484 git subtree add --prefix="sub dir" FETCH_HEAD
486 test_create_commit "$test_count" "sub dir"/main-sub1 &&
487 test_create_commit "$test_count" main2 &&
488 test_create_commit "$test_count/sub proj" sub2 &&
489 test_create_commit "$test_count" "sub dir"/main-sub2 &&
492 git fetch ./"sub proj" HEAD &&
493 git subtree merge --prefix="sub dir" FETCH_HEAD &&
494 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
496 test_create_commit "$test_count/sub proj" sub3 &&
497 test_create_commit "$test_count" "sub dir"/main-sub3 &&
499 cd "$test_count/sub proj" &&
500 git fetch .. subproj-br &&
503 test_create_commit "$test_count/sub proj" sub4 &&
506 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
508 test_create_commit "$test_count" "sub dir"/main-sub4 &&
511 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
514 cd "$test_count/sub proj" &&
515 git fetch .. subproj-br &&
516 git merge FETCH_HEAD &&
518 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 \
519 sub1 sub2 sub3 sub4 >expect &&
520 git log --name-only --pretty=format:"" >log &&
521 sort <log | sed "/^\$/ d" >actual &&
522 test_cmp expect actual
526 test_expect_success 'make sure exactly the right set of files ends up in the mainline' '
527 subtree_test_create_repo "$test_count" &&
528 subtree_test_create_repo "$test_count/sub proj" &&
529 test_create_commit "$test_count" main1 &&
530 test_create_commit "$test_count/sub proj" sub1 &&
533 git fetch ./"sub proj" HEAD &&
534 git subtree add --prefix="sub dir" FETCH_HEAD
536 test_create_commit "$test_count" "sub dir"/main-sub1 &&
537 test_create_commit "$test_count" main2 &&
538 test_create_commit "$test_count/sub proj" sub2 &&
539 test_create_commit "$test_count" "sub dir"/main-sub2 &&
542 git fetch ./"sub proj" HEAD &&
543 git subtree merge --prefix="sub dir" FETCH_HEAD &&
544 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
546 test_create_commit "$test_count/sub proj" sub3 &&
547 test_create_commit "$test_count" "sub dir"/main-sub3 &&
549 cd "$test_count/sub proj" &&
550 git fetch .. subproj-br &&
553 test_create_commit "$test_count/sub proj" sub4 &&
556 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
558 test_create_commit "$test_count" "sub dir"/main-sub4 &&
561 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
564 cd "$test_count/sub proj" &&
565 git fetch .. subproj-br &&
570 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
572 test_write_lines main1 main2 >chkm &&
573 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 >chkms &&
574 sed "s,^,sub dir/," chkms >chkms_sub &&
575 test_write_lines sub1 sub2 sub3 sub4 >chks &&
576 sed "s,^,sub dir/," chks >chks_sub &&
578 cat chkm chkms_sub chks_sub >expect &&
579 git ls-files >actual &&
580 test_cmp expect actual
584 test_expect_success 'make sure each filename changed exactly once in the entire history' '
585 subtree_test_create_repo "$test_count" &&
586 subtree_test_create_repo "$test_count/sub proj" &&
587 test_create_commit "$test_count" main1 &&
588 test_create_commit "$test_count/sub proj" sub1 &&
591 git config log.date relative &&
592 git fetch ./"sub proj" HEAD &&
593 git subtree add --prefix="sub dir" FETCH_HEAD
595 test_create_commit "$test_count" "sub dir"/main-sub1 &&
596 test_create_commit "$test_count" main2 &&
597 test_create_commit "$test_count/sub proj" sub2 &&
598 test_create_commit "$test_count" "sub dir"/main-sub2 &&
601 git fetch ./"sub proj" HEAD &&
602 git subtree merge --prefix="sub dir" FETCH_HEAD &&
603 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
605 test_create_commit "$test_count/sub proj" sub3 &&
606 test_create_commit "$test_count" "sub dir"/main-sub3 &&
608 cd "$test_count/sub proj" &&
609 git fetch .. subproj-br &&
612 test_create_commit "$test_count/sub proj" sub4 &&
615 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
617 test_create_commit "$test_count" "sub dir"/main-sub4 &&
620 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
623 cd "$test_count/sub proj" &&
624 git fetch .. subproj-br &&
629 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
631 test_write_lines main1 main2 >chkm &&
632 test_write_lines sub1 sub2 sub3 sub4 >chks &&
633 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 >chkms &&
634 sed "s,^,sub dir/," chkms >chkms_sub &&
636 # main-sub?? and /"sub dir"/main-sub?? both change, because those are the
637 # changes that were split into their own history. And "sub dir"/sub?? never
638 # change, since they were *only* changed in the subtree branch.
639 git log --name-only --pretty=format:"" >log &&
640 sort <log >sorted-log &&
641 sed "/^$/ d" sorted-log >actual &&
643 cat chkms chkm chks chkms_sub >expect-unsorted &&
644 sort expect-unsorted >expect &&
645 test_cmp expect actual
649 test_expect_success 'make sure the --rejoin commits never make it into subproj' '
650 subtree_test_create_repo "$test_count" &&
651 subtree_test_create_repo "$test_count/sub proj" &&
652 test_create_commit "$test_count" main1 &&
653 test_create_commit "$test_count/sub proj" sub1 &&
656 git fetch ./"sub proj" HEAD &&
657 git subtree add --prefix="sub dir" FETCH_HEAD
659 test_create_commit "$test_count" "sub dir"/main-sub1 &&
660 test_create_commit "$test_count" main2 &&
661 test_create_commit "$test_count/sub proj" sub2 &&
662 test_create_commit "$test_count" "sub dir"/main-sub2 &&
665 git fetch ./"sub proj" HEAD &&
666 git subtree merge --prefix="sub dir" FETCH_HEAD &&
667 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
669 test_create_commit "$test_count/sub proj" sub3 &&
670 test_create_commit "$test_count" "sub dir"/main-sub3 &&
672 cd "$test_count/sub proj" &&
673 git fetch .. subproj-br &&
676 test_create_commit "$test_count/sub proj" sub4 &&
679 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
681 test_create_commit "$test_count" "sub dir"/main-sub4 &&
684 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
687 cd "$test_count/sub proj" &&
688 git fetch .. subproj-br &&
693 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
694 test "$(git log --pretty=format:"%s" HEAD^2 | grep -i split)" = ""
698 test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' '
699 subtree_test_create_repo "$test_count" &&
700 subtree_test_create_repo "$test_count/sub proj" &&
701 test_create_commit "$test_count" main1 &&
702 test_create_commit "$test_count/sub proj" sub1 &&
705 git fetch ./"sub proj" HEAD &&
706 git subtree add --prefix="sub dir" FETCH_HEAD
708 test_create_commit "$test_count" "sub dir"/main-sub1 &&
709 test_create_commit "$test_count" main2 &&
710 test_create_commit "$test_count/sub proj" sub2 &&
711 test_create_commit "$test_count" "sub dir"/main-sub2 &&
714 git fetch ./"sub proj" HEAD &&
715 git subtree merge --prefix="sub dir" FETCH_HEAD &&
716 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
718 test_create_commit "$test_count/sub proj" sub3 &&
719 test_create_commit "$test_count" "sub dir"/main-sub3 &&
721 cd "$test_count/sub proj" &&
722 git fetch .. subproj-br &&
725 test_create_commit "$test_count/sub proj" sub4 &&
728 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
730 test_create_commit "$test_count" "sub dir"/main-sub4 &&
733 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
736 cd "$test_count/sub proj" &&
737 git fetch .. subproj-br &&
742 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
744 # They are meaningless to subproj since one side of the merge refers to the mainline
745 test "$(git log --pretty=format:"%s%n%b" HEAD^2 | grep "git-subtree.*:")" = ""
753 test_expect_success 'make sure "git subtree split" find the correct parent' '
754 subtree_test_create_repo "$test_count" &&
755 subtree_test_create_repo "$test_count/sub proj" &&
756 test_create_commit "$test_count" main1 &&
757 test_create_commit "$test_count/sub proj" sub1 &&
760 git fetch ./"sub proj" HEAD &&
761 git subtree add --prefix="sub dir" FETCH_HEAD
763 test_create_commit "$test_count/sub proj" sub2 &&
766 git fetch ./"sub proj" HEAD &&
767 git branch subproj-ref FETCH_HEAD &&
768 git subtree merge --prefix="sub dir" FETCH_HEAD
770 test_create_commit "$test_count" "sub dir"/main-sub1 &&
773 git subtree split --prefix="sub dir" --branch subproj-br &&
775 # at this point, the new commit parent should be subproj-ref, if it is
776 # not, something went wrong (the "newparent" of "HEAD~" commit should
777 # have been sub2, but it was not, because its cache was not set to
779 test "$(git log --pretty=format:%P -1 subproj-br)" = "$(git rev-parse subproj-ref)"
783 test_expect_success 'split a new subtree without --onto option' '
784 subtree_test_create_repo "$test_count" &&
785 subtree_test_create_repo "$test_count/sub proj" &&
786 test_create_commit "$test_count" main1 &&
787 test_create_commit "$test_count/sub proj" sub1 &&
790 git fetch ./"sub proj" HEAD &&
791 git subtree add --prefix="sub dir" FETCH_HEAD
793 test_create_commit "$test_count/sub proj" sub2 &&
796 git fetch ./"sub proj" HEAD &&
797 git subtree merge --prefix="sub dir" FETCH_HEAD
799 test_create_commit "$test_count" "sub dir"/main-sub1 &&
802 git subtree split --prefix="sub dir" --branch subproj-br
804 mkdir "$test_count"/"sub dir2" &&
805 test_create_commit "$test_count" "sub dir2"/main-sub2 &&
809 # also test that we still can split out an entirely new subtree
810 # if the parent of the first commit in the tree is not empty,
811 # then the new subtree has accidentally been attached to something
812 git subtree split --prefix="sub dir2" --branch subproj2-br &&
813 test "$(git log --pretty=format:%P -1 subproj2-br)" = ""
817 test_expect_success 'verify one file change per commit' '
818 subtree_test_create_repo "$test_count" &&
819 subtree_test_create_repo "$test_count/sub proj" &&
820 test_create_commit "$test_count" main1 &&
821 test_create_commit "$test_count/sub proj" sub1 &&
824 git fetch ./"sub proj" HEAD &&
825 git branch sub1 FETCH_HEAD &&
826 git subtree add --prefix="sub dir" sub1
828 test_create_commit "$test_count/sub proj" sub2 &&
831 git fetch ./"sub proj" HEAD &&
832 git subtree merge --prefix="sub dir" FETCH_HEAD
834 test_create_commit "$test_count" "sub dir"/main-sub1 &&
837 git subtree split --prefix="sub dir" --branch subproj-br
839 mkdir "$test_count"/"sub dir2" &&
840 test_create_commit "$test_count" "sub dir2"/main-sub2 &&
843 git subtree split --prefix="sub dir2" --branch subproj2-br &&
845 git log --format="%H" >commit-list &&
848 git log -n1 --format="" --name-only "$commit" >file-list &&
849 test_line_count -le 1 file-list || return 1
854 test_expect_success 'push split to subproj' '
855 subtree_test_create_repo "$test_count" &&
856 subtree_test_create_repo "$test_count/sub proj" &&
857 test_create_commit "$test_count" main1 &&
858 test_create_commit "$test_count/sub proj" sub1 &&
861 git fetch ./"sub proj" HEAD &&
862 git subtree add --prefix="sub dir" FETCH_HEAD
864 test_create_commit "$test_count" "sub dir"/main-sub1 &&
865 test_create_commit "$test_count" main2 &&
866 test_create_commit "$test_count/sub proj" sub2 &&
867 test_create_commit "$test_count" "sub dir"/main-sub2 &&
869 cd $test_count/"sub proj" &&
870 git branch sub-branch-1 &&
872 git fetch ./"sub proj" HEAD &&
873 git subtree merge --prefix="sub dir" FETCH_HEAD
875 test_create_commit "$test_count" "sub dir"/main-sub3 &&
878 git subtree push ./"sub proj" --prefix "sub dir" sub-branch-1 &&
880 git checkout sub-branch-1 &&
881 test "$(last_commit_subject)" = "sub dir/main-sub3"
886 # This test covers 2 cases in subtree split copy_or_skip code
887 # 1) Merges where one parent is a superset of the changes of the other
888 # parent regarding changes to the subtree, in this case the merge
889 # commit should be copied
890 # 2) Merges where only one parent operate on the subtree, and the merge
891 # commit should be skipped
893 # (1) is checked by ensuring subtree_tip is a descendent of subtree_branch
894 # (2) should have a check added (not_a_subtree_change shouldn't be present
895 # on the produced subtree)
897 # Other related cases which are not tested (or currently handled correctly)
898 # - Case (1) where there are more than 2 parents, it will sometimes correctly copy
899 # the merge, and sometimes not
900 # - Merge commit where both parents have same tree as the merge, currently
901 # will always be skipped, even if they reached that state via different
905 test_expect_success 'subtree descendant check' '
906 subtree_test_create_repo "$test_count" &&
907 defaultBranch=$(sed "s,ref: refs/heads/,," "$test_count/.git/HEAD") &&
908 test_create_commit "$test_count" folder_subtree/a &&
913 test_create_commit "$test_count" folder_subtree/0 &&
914 test_create_commit "$test_count" folder_subtree/b &&
915 cherry=$(cd "$test_count"; git rev-parse HEAD) &&
920 test_create_commit "$test_count" commit_on_branch &&
923 git cherry-pick $cherry &&
924 git checkout $defaultBranch &&
925 git merge -m "merge should be kept on subtree" branch &&
926 git branch no_subtree_work_branch
928 test_create_commit "$test_count" folder_subtree/d &&
931 git checkout no_subtree_work_branch
933 test_create_commit "$test_count" not_a_subtree_change &&
936 git checkout $defaultBranch &&
937 git merge -m "merge should be skipped on subtree" no_subtree_work_branch &&
939 git subtree split --prefix folder_subtree/ --branch subtree_tip $defaultBranch &&
940 git subtree split --prefix folder_subtree/ --branch subtree_branch branch &&
941 test $(git rev-list --count subtree_tip..subtree_branch) = 0