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
40 test_expect_success 'shows short help text for -h' '
41 test_expect_code 129 git subtree -h >out 2>err &&
42 test_must_be_empty err &&
43 grep -e "^ *or: git subtree pull" out &&
44 grep -e --annotate out
48 # Tests for 'git subtree add'
51 test_expect_success 'no merge from non-existent subtree' '
52 subtree_test_create_repo "$test_count" &&
53 subtree_test_create_repo "$test_count/sub proj" &&
54 test_create_commit "$test_count" main1 &&
55 test_create_commit "$test_count/sub proj" sub1 &&
58 git fetch ./"sub proj" HEAD &&
59 test_must_fail git subtree merge --prefix="sub dir" FETCH_HEAD
63 test_expect_success 'no pull from non-existent subtree' '
64 subtree_test_create_repo "$test_count" &&
65 subtree_test_create_repo "$test_count/sub proj" &&
66 test_create_commit "$test_count" main1 &&
67 test_create_commit "$test_count/sub proj" sub1 &&
70 git fetch ./"sub proj" HEAD &&
71 test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" HEAD
75 test_expect_success 'add subproj as subtree into sub dir/ with --prefix' '
76 subtree_test_create_repo "$test_count" &&
77 subtree_test_create_repo "$test_count/sub proj" &&
78 test_create_commit "$test_count" main1 &&
79 test_create_commit "$test_count/sub proj" sub1 &&
82 git fetch ./"sub proj" HEAD &&
83 git subtree add --prefix="sub dir" FETCH_HEAD &&
84 test "$(last_commit_subject)" = "Add '\''sub dir/'\'' from commit '\''$(git rev-parse FETCH_HEAD)'\''"
88 test_expect_success 'add subproj as subtree into sub dir/ with --prefix and --message' '
89 subtree_test_create_repo "$test_count" &&
90 subtree_test_create_repo "$test_count/sub proj" &&
91 test_create_commit "$test_count" main1 &&
92 test_create_commit "$test_count/sub proj" sub1 &&
95 git fetch ./"sub proj" HEAD &&
96 git subtree add --prefix="sub dir" --message="Added subproject" FETCH_HEAD &&
97 test "$(last_commit_subject)" = "Added subproject"
101 test_expect_success 'add subproj as subtree into sub dir/ with --prefix as -P and --message as -m' '
102 subtree_test_create_repo "$test_count" &&
103 subtree_test_create_repo "$test_count/sub proj" &&
104 test_create_commit "$test_count" main1 &&
105 test_create_commit "$test_count/sub proj" sub1 &&
108 git fetch ./"sub proj" HEAD &&
109 git subtree add -P "sub dir" -m "Added subproject" FETCH_HEAD &&
110 test "$(last_commit_subject)" = "Added subproject"
114 test_expect_success 'add subproj as subtree into sub dir/ with --squash and --prefix and --message' '
115 subtree_test_create_repo "$test_count" &&
116 subtree_test_create_repo "$test_count/sub proj" &&
117 test_create_commit "$test_count" main1 &&
118 test_create_commit "$test_count/sub proj" sub1 &&
121 git fetch ./"sub proj" HEAD &&
122 git subtree add --prefix="sub dir" --message="Added subproject with squash" --squash FETCH_HEAD &&
123 test "$(last_commit_subject)" = "Added subproject with squash"
128 # Tests for 'git subtree merge'
131 test_expect_success 'merge new subproj history into sub dir/ with --prefix' '
132 subtree_test_create_repo "$test_count" &&
133 subtree_test_create_repo "$test_count/sub proj" &&
134 test_create_commit "$test_count" main1 &&
135 test_create_commit "$test_count/sub proj" sub1 &&
138 git fetch ./"sub proj" HEAD &&
139 git subtree add --prefix="sub dir" FETCH_HEAD
141 test_create_commit "$test_count/sub proj" sub2 &&
144 git fetch ./"sub proj" HEAD &&
145 git subtree merge --prefix="sub dir" FETCH_HEAD &&
146 test "$(last_commit_subject)" = "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''"
150 test_expect_success 'merge new subproj history into sub dir/ with --prefix and --message' '
151 subtree_test_create_repo "$test_count" &&
152 subtree_test_create_repo "$test_count/sub proj" &&
153 test_create_commit "$test_count" main1 &&
154 test_create_commit "$test_count/sub proj" sub1 &&
157 git fetch ./"sub proj" HEAD &&
158 git subtree add --prefix="sub dir" FETCH_HEAD
160 test_create_commit "$test_count/sub proj" sub2 &&
163 git fetch ./"sub proj" HEAD &&
164 git subtree merge --prefix="sub dir" --message="Merged changes from subproject" FETCH_HEAD &&
165 test "$(last_commit_subject)" = "Merged changes from subproject"
169 test_expect_success 'merge new subproj history into sub dir/ with --squash and --prefix and --message' '
170 subtree_test_create_repo "$test_count/sub proj" &&
171 subtree_test_create_repo "$test_count" &&
172 test_create_commit "$test_count" main1 &&
173 test_create_commit "$test_count/sub proj" sub1 &&
176 git fetch ./"sub proj" HEAD &&
177 git subtree add --prefix="sub dir" FETCH_HEAD
179 test_create_commit "$test_count/sub proj" sub2 &&
182 git fetch ./"sub proj" HEAD &&
183 git subtree merge --prefix="sub dir" --message="Merged changes from subproject using squash" --squash FETCH_HEAD &&
184 test "$(last_commit_subject)" = "Merged changes from subproject using squash"
188 test_expect_success 'merge the added subproj again, should do nothing' '
189 subtree_test_create_repo "$test_count" &&
190 subtree_test_create_repo "$test_count/sub proj" &&
191 test_create_commit "$test_count" main1 &&
192 test_create_commit "$test_count/sub proj" sub1 &&
195 git fetch ./"sub proj" HEAD &&
196 git subtree add --prefix="sub dir" FETCH_HEAD &&
197 # this shouldn not actually do anything, since FETCH_HEAD
198 # is already a parent
199 result=$(git merge -s ours -m "merge -s -ours" FETCH_HEAD) &&
200 test "${result}" = "Already up to date."
204 test_expect_success 'merge new subproj history into subdir/ with a slash appended to the argument of --prefix' '
205 subtree_test_create_repo "$test_count" &&
206 subtree_test_create_repo "$test_count/subproj" &&
207 test_create_commit "$test_count" main1 &&
208 test_create_commit "$test_count/subproj" sub1 &&
211 git fetch ./subproj HEAD &&
212 git subtree add --prefix=subdir/ FETCH_HEAD
214 test_create_commit "$test_count/subproj" sub2 &&
217 git fetch ./subproj HEAD &&
218 git subtree merge --prefix=subdir/ FETCH_HEAD &&
219 test "$(last_commit_subject)" = "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''"
224 # Tests for 'git subtree split'
227 test_expect_success 'split requires option --prefix' '
228 subtree_test_create_repo "$test_count" &&
229 subtree_test_create_repo "$test_count/sub proj" &&
230 test_create_commit "$test_count" main1 &&
231 test_create_commit "$test_count/sub proj" sub1 &&
234 git fetch ./"sub proj" HEAD &&
235 git subtree add --prefix="sub dir" FETCH_HEAD &&
236 echo "You must provide the --prefix option." >expected &&
237 test_must_fail git subtree split >actual 2>&1 &&
238 test_debug "printf '"expected: "'" &&
239 test_debug "cat expected" &&
240 test_debug "printf '"actual: "'" &&
241 test_debug "cat actual" &&
242 test_cmp expected actual
246 test_expect_success 'split requires path given by option --prefix must exist' '
247 subtree_test_create_repo "$test_count" &&
248 subtree_test_create_repo "$test_count/sub proj" &&
249 test_create_commit "$test_count" main1 &&
250 test_create_commit "$test_count/sub proj" sub1 &&
253 git fetch ./"sub proj" HEAD &&
254 git subtree add --prefix="sub dir" FETCH_HEAD &&
255 echo "'\''non-existent-directory'\'' does not exist; use '\''git subtree add'\''" >expected &&
256 test_must_fail git subtree split --prefix=non-existent-directory >actual 2>&1 &&
257 test_debug "printf '"expected: "'" &&
258 test_debug "cat expected" &&
259 test_debug "printf '"actual: "'" &&
260 test_debug "cat actual" &&
261 test_cmp expected actual
265 test_expect_success 'split sub dir/ with --rejoin' '
266 subtree_test_create_repo "$test_count" &&
267 subtree_test_create_repo "$test_count/sub proj" &&
268 test_create_commit "$test_count" main1 &&
269 test_create_commit "$test_count/sub proj" sub1 &&
272 git fetch ./"sub proj" HEAD &&
273 git subtree add --prefix="sub dir" FETCH_HEAD
275 test_create_commit "$test_count" "sub dir"/main-sub1 &&
276 test_create_commit "$test_count" main2 &&
277 test_create_commit "$test_count/sub proj" sub2 &&
278 test_create_commit "$test_count" "sub dir"/main-sub2 &&
281 git fetch ./"sub proj" HEAD &&
282 git subtree merge --prefix="sub dir" FETCH_HEAD &&
283 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
284 git subtree split --prefix="sub dir" --annotate="*" --rejoin &&
285 test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''"
289 test_expect_success 'split sub dir/ with --rejoin from scratch' '
290 subtree_test_create_repo "$test_count" &&
291 test_create_commit "$test_count" main1 &&
295 echo file >"sub dir"/file &&
296 git add "sub dir/file" &&
297 git commit -m"sub dir file" &&
298 split_hash=$(git subtree split --prefix="sub dir" --rejoin) &&
299 git subtree split --prefix="sub dir" --rejoin &&
300 test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''"
304 test_expect_success 'split sub dir/ with --rejoin and --message' '
305 subtree_test_create_repo "$test_count" &&
306 subtree_test_create_repo "$test_count/sub proj" &&
307 test_create_commit "$test_count" main1 &&
308 test_create_commit "$test_count/sub proj" sub1 &&
311 git fetch ./"sub proj" HEAD &&
312 git subtree add --prefix="sub dir" FETCH_HEAD
314 test_create_commit "$test_count" "sub dir"/main-sub1 &&
315 test_create_commit "$test_count" main2 &&
316 test_create_commit "$test_count/sub proj" sub2 &&
317 test_create_commit "$test_count" "sub dir"/main-sub2 &&
320 git fetch ./"sub proj" HEAD &&
321 git subtree merge --prefix="sub dir" FETCH_HEAD &&
322 git subtree split --prefix="sub dir" --message="Split & rejoin" --annotate="*" --rejoin &&
323 test "$(last_commit_subject)" = "Split & rejoin"
327 test_expect_success 'split "sub dir"/ with --branch' '
328 subtree_test_create_repo "$test_count" &&
329 subtree_test_create_repo "$test_count/sub proj" &&
330 test_create_commit "$test_count" main1 &&
331 test_create_commit "$test_count/sub proj" sub1 &&
334 git fetch ./"sub proj" HEAD &&
335 git subtree add --prefix="sub dir" FETCH_HEAD
337 test_create_commit "$test_count" "sub dir"/main-sub1 &&
338 test_create_commit "$test_count" main2 &&
339 test_create_commit "$test_count/sub proj" sub2 &&
340 test_create_commit "$test_count" "sub dir"/main-sub2 &&
343 git fetch ./"sub proj" HEAD &&
344 git subtree merge --prefix="sub dir" FETCH_HEAD &&
345 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
346 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
347 test "$(git rev-parse subproj-br)" = "$split_hash"
351 test_expect_success 'check hash of split' '
352 subtree_test_create_repo "$test_count" &&
353 subtree_test_create_repo "$test_count/sub proj" &&
354 test_create_commit "$test_count" main1 &&
355 test_create_commit "$test_count/sub proj" sub1 &&
358 git fetch ./"sub proj" HEAD &&
359 git subtree add --prefix="sub dir" FETCH_HEAD
361 test_create_commit "$test_count" "sub dir"/main-sub1 &&
362 test_create_commit "$test_count" main2 &&
363 test_create_commit "$test_count/sub proj" sub2 &&
364 test_create_commit "$test_count" "sub dir"/main-sub2 &&
367 git fetch ./"sub proj" HEAD &&
368 git subtree merge --prefix="sub dir" FETCH_HEAD &&
369 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
370 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
371 test "$(git rev-parse subproj-br)" = "$split_hash" &&
372 # Check hash of split
373 new_hash=$(git rev-parse subproj-br^2) &&
376 subdir_hash=$(git rev-parse HEAD) &&
377 test "$new_hash" = "$subdir_hash"
382 test_expect_success 'split "sub dir"/ with --branch for an existing branch' '
383 subtree_test_create_repo "$test_count" &&
384 subtree_test_create_repo "$test_count/sub proj" &&
385 test_create_commit "$test_count" main1 &&
386 test_create_commit "$test_count/sub proj" sub1 &&
389 git fetch ./"sub proj" HEAD &&
390 git branch subproj-br FETCH_HEAD &&
391 git subtree add --prefix="sub dir" FETCH_HEAD
393 test_create_commit "$test_count" "sub dir"/main-sub1 &&
394 test_create_commit "$test_count" main2 &&
395 test_create_commit "$test_count/sub proj" sub2 &&
396 test_create_commit "$test_count" "sub dir"/main-sub2 &&
399 git fetch ./"sub proj" HEAD &&
400 git subtree merge --prefix="sub dir" FETCH_HEAD &&
401 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
402 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
403 test "$(git rev-parse subproj-br)" = "$split_hash"
407 test_expect_success 'split "sub dir"/ with --branch for an incompatible branch' '
408 subtree_test_create_repo "$test_count" &&
409 subtree_test_create_repo "$test_count/sub proj" &&
410 test_create_commit "$test_count" main1 &&
411 test_create_commit "$test_count/sub proj" sub1 &&
414 git branch init HEAD &&
415 git fetch ./"sub proj" HEAD &&
416 git subtree add --prefix="sub dir" FETCH_HEAD
418 test_create_commit "$test_count" "sub dir"/main-sub1 &&
419 test_create_commit "$test_count" main2 &&
420 test_create_commit "$test_count/sub proj" sub2 &&
421 test_create_commit "$test_count" "sub dir"/main-sub2 &&
424 git fetch ./"sub proj" HEAD &&
425 git subtree merge --prefix="sub dir" FETCH_HEAD &&
426 test_must_fail git subtree split --prefix="sub dir" --branch init
431 # Tests for 'git subtree pull'
434 test_expect_success 'pull requires option --prefix' '
435 subtree_test_create_repo "$test_count" &&
436 subtree_test_create_repo "$test_count/sub proj" &&
437 test_create_commit "$test_count" main1 &&
438 test_create_commit "$test_count/sub proj" sub1 &&
441 git fetch ./"sub proj" HEAD &&
442 git subtree add --prefix="sub dir" FETCH_HEAD
444 test_create_commit "$test_count/sub proj" sub2 &&
447 test_must_fail git subtree pull ./"sub proj" HEAD >out 2>err &&
449 echo "You must provide the --prefix option." >expected &&
450 test_must_be_empty out &&
451 test_cmp expected err
455 test_expect_success 'pull requires path given by option --prefix must exist' '
456 subtree_test_create_repo "$test_count" &&
457 subtree_test_create_repo "$test_count/sub proj" &&
458 test_create_commit "$test_count" main1 &&
459 test_create_commit "$test_count/sub proj" sub1 &&
461 test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" HEAD >out 2>err &&
463 echo "'\''sub dir'\'' does not exist; use '\''git subtree add'\''" >expected &&
464 test_must_be_empty out &&
465 test_cmp expected err
469 test_expect_success 'pull basic operation' '
470 subtree_test_create_repo "$test_count" &&
471 subtree_test_create_repo "$test_count/sub proj" &&
472 test_create_commit "$test_count" main1 &&
473 test_create_commit "$test_count/sub proj" sub1 &&
476 git fetch ./"sub proj" HEAD &&
477 git subtree add --prefix="sub dir" FETCH_HEAD
479 test_create_commit "$test_count/sub proj" sub2 &&
482 exp=$(git -C "sub proj" rev-parse --verify HEAD:) &&
483 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
484 act=$(git rev-parse --verify HEAD:"sub dir") &&
490 # Tests for 'git subtree push'
493 test_expect_success 'push requires option --prefix' '
494 subtree_test_create_repo "$test_count" &&
495 subtree_test_create_repo "$test_count/sub proj" &&
496 test_create_commit "$test_count" main1 &&
497 test_create_commit "$test_count/sub proj" sub1 &&
500 git fetch ./"sub proj" HEAD &&
501 git subtree add --prefix="sub dir" FETCH_HEAD &&
502 echo "You must provide the --prefix option." >expected &&
503 test_must_fail git subtree push "./sub proj" from-mainline >actual 2>&1 &&
504 test_debug "printf '"expected: "'" &&
505 test_debug "cat expected" &&
506 test_debug "printf '"actual: "'" &&
507 test_debug "cat actual" &&
508 test_cmp expected actual
512 test_expect_success 'push requires path given by option --prefix must exist' '
513 subtree_test_create_repo "$test_count" &&
514 subtree_test_create_repo "$test_count/sub proj" &&
515 test_create_commit "$test_count" main1 &&
516 test_create_commit "$test_count/sub proj" sub1 &&
519 git fetch ./"sub proj" HEAD &&
520 git subtree add --prefix="sub dir" FETCH_HEAD &&
521 echo "'\''non-existent-directory'\'' does not exist; use '\''git subtree add'\''" >expected &&
522 test_must_fail git subtree push --prefix=non-existent-directory "./sub proj" from-mainline >actual 2>&1 &&
523 test_debug "printf '"expected: "'" &&
524 test_debug "cat expected" &&
525 test_debug "printf '"actual: "'" &&
526 test_debug "cat actual" &&
527 test_cmp expected actual
531 test_expect_success 'push basic operation' '
532 subtree_test_create_repo "$test_count" &&
533 subtree_test_create_repo "$test_count/sub proj" &&
534 test_create_commit "$test_count" main1 &&
535 test_create_commit "$test_count/sub proj" sub1 &&
538 git fetch ./"sub proj" HEAD &&
539 git subtree add --prefix="sub dir" FETCH_HEAD
541 test_create_commit "$test_count" "sub dir"/main-sub1 &&
542 test_create_commit "$test_count" main2 &&
543 test_create_commit "$test_count/sub proj" sub2 &&
544 test_create_commit "$test_count" "sub dir"/main-sub2 &&
547 git fetch ./"sub proj" HEAD &&
548 git subtree merge --prefix="sub dir" FETCH_HEAD &&
549 before=$(git rev-parse --verify HEAD) &&
550 split_hash=$(git subtree split --prefix="sub dir") &&
551 git subtree push --prefix="sub dir" ./"sub proj" from-mainline &&
552 test "$before" = "$(git rev-parse --verify HEAD)" &&
553 test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
561 test_expect_success 'make sure exactly the right set of files ends up in the subproj' '
562 subtree_test_create_repo "$test_count" &&
563 subtree_test_create_repo "$test_count/sub proj" &&
564 test_create_commit "$test_count" main1 &&
565 test_create_commit "$test_count/sub proj" sub1 &&
568 git fetch ./"sub proj" HEAD &&
569 git subtree add --prefix="sub dir" FETCH_HEAD
571 test_create_commit "$test_count" "sub dir"/main-sub1 &&
572 test_create_commit "$test_count" main2 &&
573 test_create_commit "$test_count/sub proj" sub2 &&
574 test_create_commit "$test_count" "sub dir"/main-sub2 &&
577 git fetch ./"sub proj" HEAD &&
578 git subtree merge --prefix="sub dir" FETCH_HEAD &&
579 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
581 test_create_commit "$test_count/sub proj" sub3 &&
582 test_create_commit "$test_count" "sub dir"/main-sub3 &&
584 cd "$test_count/sub proj" &&
585 git fetch .. subproj-br &&
588 test_create_commit "$test_count/sub proj" sub4 &&
591 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
593 test_create_commit "$test_count" "sub dir"/main-sub4 &&
596 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
599 cd "$test_count/sub proj" &&
600 git fetch .. subproj-br &&
601 git merge FETCH_HEAD &&
603 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 \
604 sub1 sub2 sub3 sub4 >expect &&
605 git ls-files >actual &&
606 test_cmp expect actual
610 test_expect_success 'make sure the subproj *only* contains commits that affect the "sub dir"' '
611 subtree_test_create_repo "$test_count" &&
612 subtree_test_create_repo "$test_count/sub proj" &&
613 test_create_commit "$test_count" main1 &&
614 test_create_commit "$test_count/sub proj" sub1 &&
617 git fetch ./"sub proj" HEAD &&
618 git subtree add --prefix="sub dir" FETCH_HEAD
620 test_create_commit "$test_count" "sub dir"/main-sub1 &&
621 test_create_commit "$test_count" main2 &&
622 test_create_commit "$test_count/sub proj" sub2 &&
623 test_create_commit "$test_count" "sub dir"/main-sub2 &&
626 git fetch ./"sub proj" HEAD &&
627 git subtree merge --prefix="sub dir" FETCH_HEAD &&
628 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
630 test_create_commit "$test_count/sub proj" sub3 &&
631 test_create_commit "$test_count" "sub dir"/main-sub3 &&
633 cd "$test_count/sub proj" &&
634 git fetch .. subproj-br &&
637 test_create_commit "$test_count/sub proj" sub4 &&
640 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
642 test_create_commit "$test_count" "sub dir"/main-sub4 &&
645 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
648 cd "$test_count/sub proj" &&
649 git fetch .. subproj-br &&
650 git merge FETCH_HEAD &&
652 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 \
653 sub1 sub2 sub3 sub4 >expect &&
654 git log --name-only --pretty=format:"" >log &&
655 sort <log | sed "/^\$/ d" >actual &&
656 test_cmp expect actual
660 test_expect_success 'make sure exactly the right set of files ends up in the mainline' '
661 subtree_test_create_repo "$test_count" &&
662 subtree_test_create_repo "$test_count/sub proj" &&
663 test_create_commit "$test_count" main1 &&
664 test_create_commit "$test_count/sub proj" sub1 &&
667 git fetch ./"sub proj" HEAD &&
668 git subtree add --prefix="sub dir" FETCH_HEAD
670 test_create_commit "$test_count" "sub dir"/main-sub1 &&
671 test_create_commit "$test_count" main2 &&
672 test_create_commit "$test_count/sub proj" sub2 &&
673 test_create_commit "$test_count" "sub dir"/main-sub2 &&
676 git fetch ./"sub proj" HEAD &&
677 git subtree merge --prefix="sub dir" FETCH_HEAD &&
678 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
680 test_create_commit "$test_count/sub proj" sub3 &&
681 test_create_commit "$test_count" "sub dir"/main-sub3 &&
683 cd "$test_count/sub proj" &&
684 git fetch .. subproj-br &&
687 test_create_commit "$test_count/sub proj" sub4 &&
690 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
692 test_create_commit "$test_count" "sub dir"/main-sub4 &&
695 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
698 cd "$test_count/sub proj" &&
699 git fetch .. subproj-br &&
704 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
706 test_write_lines main1 main2 >chkm &&
707 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 >chkms &&
708 sed "s,^,sub dir/," chkms >chkms_sub &&
709 test_write_lines sub1 sub2 sub3 sub4 >chks &&
710 sed "s,^,sub dir/," chks >chks_sub &&
712 cat chkm chkms_sub chks_sub >expect &&
713 git ls-files >actual &&
714 test_cmp expect actual
718 test_expect_success 'make sure each filename changed exactly once in the entire history' '
719 subtree_test_create_repo "$test_count" &&
720 subtree_test_create_repo "$test_count/sub proj" &&
721 test_create_commit "$test_count" main1 &&
722 test_create_commit "$test_count/sub proj" sub1 &&
725 git config log.date relative &&
726 git fetch ./"sub proj" HEAD &&
727 git subtree add --prefix="sub dir" FETCH_HEAD
729 test_create_commit "$test_count" "sub dir"/main-sub1 &&
730 test_create_commit "$test_count" main2 &&
731 test_create_commit "$test_count/sub proj" sub2 &&
732 test_create_commit "$test_count" "sub dir"/main-sub2 &&
735 git fetch ./"sub proj" HEAD &&
736 git subtree merge --prefix="sub dir" FETCH_HEAD &&
737 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
739 test_create_commit "$test_count/sub proj" sub3 &&
740 test_create_commit "$test_count" "sub dir"/main-sub3 &&
742 cd "$test_count/sub proj" &&
743 git fetch .. subproj-br &&
746 test_create_commit "$test_count/sub proj" sub4 &&
749 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
751 test_create_commit "$test_count" "sub dir"/main-sub4 &&
754 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
757 cd "$test_count/sub proj" &&
758 git fetch .. subproj-br &&
763 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
765 test_write_lines main1 main2 >chkm &&
766 test_write_lines sub1 sub2 sub3 sub4 >chks &&
767 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 >chkms &&
768 sed "s,^,sub dir/," chkms >chkms_sub &&
770 # main-sub?? and /"sub dir"/main-sub?? both change, because those are the
771 # changes that were split into their own history. And "sub dir"/sub?? never
772 # change, since they were *only* changed in the subtree branch.
773 git log --name-only --pretty=format:"" >log &&
774 sort <log >sorted-log &&
775 sed "/^$/ d" sorted-log >actual &&
777 cat chkms chkm chks chkms_sub >expect-unsorted &&
778 sort expect-unsorted >expect &&
779 test_cmp expect actual
783 test_expect_success 'make sure the --rejoin commits never make it into subproj' '
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 dir"/main-sub1 &&
794 test_create_commit "$test_count" main2 &&
795 test_create_commit "$test_count/sub proj" sub2 &&
796 test_create_commit "$test_count" "sub dir"/main-sub2 &&
799 git fetch ./"sub proj" HEAD &&
800 git subtree merge --prefix="sub dir" FETCH_HEAD &&
801 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
803 test_create_commit "$test_count/sub proj" sub3 &&
804 test_create_commit "$test_count" "sub dir"/main-sub3 &&
806 cd "$test_count/sub proj" &&
807 git fetch .. subproj-br &&
810 test_create_commit "$test_count/sub proj" sub4 &&
813 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
815 test_create_commit "$test_count" "sub dir"/main-sub4 &&
818 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
821 cd "$test_count/sub proj" &&
822 git fetch .. subproj-br &&
827 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
828 test "$(git log --pretty=format:"%s" HEAD^2 | grep -i split)" = ""
832 test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' '
833 subtree_test_create_repo "$test_count" &&
834 subtree_test_create_repo "$test_count/sub proj" &&
835 test_create_commit "$test_count" main1 &&
836 test_create_commit "$test_count/sub proj" sub1 &&
839 git fetch ./"sub proj" HEAD &&
840 git subtree add --prefix="sub dir" FETCH_HEAD
842 test_create_commit "$test_count" "sub dir"/main-sub1 &&
843 test_create_commit "$test_count" main2 &&
844 test_create_commit "$test_count/sub proj" sub2 &&
845 test_create_commit "$test_count" "sub dir"/main-sub2 &&
848 git fetch ./"sub proj" HEAD &&
849 git subtree merge --prefix="sub dir" FETCH_HEAD &&
850 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
852 test_create_commit "$test_count/sub proj" sub3 &&
853 test_create_commit "$test_count" "sub dir"/main-sub3 &&
855 cd "$test_count/sub proj" &&
856 git fetch .. subproj-br &&
859 test_create_commit "$test_count/sub proj" sub4 &&
862 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
864 test_create_commit "$test_count" "sub dir"/main-sub4 &&
867 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
870 cd "$test_count/sub proj" &&
871 git fetch .. subproj-br &&
876 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
878 # They are meaningless to subproj since one side of the merge refers to the mainline
879 test "$(git log --pretty=format:"%s%n%b" HEAD^2 | grep "git-subtree.*:")" = ""
887 test_expect_success 'make sure "git subtree split" find the correct parent' '
888 subtree_test_create_repo "$test_count" &&
889 subtree_test_create_repo "$test_count/sub proj" &&
890 test_create_commit "$test_count" main1 &&
891 test_create_commit "$test_count/sub proj" sub1 &&
894 git fetch ./"sub proj" HEAD &&
895 git subtree add --prefix="sub dir" FETCH_HEAD
897 test_create_commit "$test_count/sub proj" sub2 &&
900 git fetch ./"sub proj" HEAD &&
901 git branch subproj-ref FETCH_HEAD &&
902 git subtree merge --prefix="sub dir" FETCH_HEAD
904 test_create_commit "$test_count" "sub dir"/main-sub1 &&
907 git subtree split --prefix="sub dir" --branch subproj-br &&
909 # at this point, the new commit parent should be subproj-ref, if it is
910 # not, something went wrong (the "newparent" of "HEAD~" commit should
911 # have been sub2, but it was not, because its cache was not set to
913 test "$(git log --pretty=format:%P -1 subproj-br)" = "$(git rev-parse subproj-ref)"
917 test_expect_success 'split a new subtree without --onto option' '
918 subtree_test_create_repo "$test_count" &&
919 subtree_test_create_repo "$test_count/sub proj" &&
920 test_create_commit "$test_count" main1 &&
921 test_create_commit "$test_count/sub proj" sub1 &&
924 git fetch ./"sub proj" HEAD &&
925 git subtree add --prefix="sub dir" FETCH_HEAD
927 test_create_commit "$test_count/sub proj" sub2 &&
930 git fetch ./"sub proj" HEAD &&
931 git subtree merge --prefix="sub dir" FETCH_HEAD
933 test_create_commit "$test_count" "sub dir"/main-sub1 &&
936 git subtree split --prefix="sub dir" --branch subproj-br
938 mkdir "$test_count"/"sub dir2" &&
939 test_create_commit "$test_count" "sub dir2"/main-sub2 &&
943 # also test that we still can split out an entirely new subtree
944 # if the parent of the first commit in the tree is not empty,
945 # then the new subtree has accidentally been attached to something
946 git subtree split --prefix="sub dir2" --branch subproj2-br &&
947 test "$(git log --pretty=format:%P -1 subproj2-br)" = ""
951 test_expect_success 'verify one file change per commit' '
952 subtree_test_create_repo "$test_count" &&
953 subtree_test_create_repo "$test_count/sub proj" &&
954 test_create_commit "$test_count" main1 &&
955 test_create_commit "$test_count/sub proj" sub1 &&
958 git fetch ./"sub proj" HEAD &&
959 git branch sub1 FETCH_HEAD &&
960 git subtree add --prefix="sub dir" sub1
962 test_create_commit "$test_count/sub proj" sub2 &&
965 git fetch ./"sub proj" HEAD &&
966 git subtree merge --prefix="sub dir" FETCH_HEAD
968 test_create_commit "$test_count" "sub dir"/main-sub1 &&
971 git subtree split --prefix="sub dir" --branch subproj-br
973 mkdir "$test_count"/"sub dir2" &&
974 test_create_commit "$test_count" "sub dir2"/main-sub2 &&
977 git subtree split --prefix="sub dir2" --branch subproj2-br &&
979 git log --format="%H" >commit-list &&
982 git log -n1 --format="" --name-only "$commit" >file-list &&
983 test_line_count -le 1 file-list || return 1
988 test_expect_success 'push split to subproj' '
989 subtree_test_create_repo "$test_count" &&
990 subtree_test_create_repo "$test_count/sub proj" &&
991 test_create_commit "$test_count" main1 &&
992 test_create_commit "$test_count/sub proj" sub1 &&
995 git fetch ./"sub proj" HEAD &&
996 git subtree add --prefix="sub dir" FETCH_HEAD
998 test_create_commit "$test_count" "sub dir"/main-sub1 &&
999 test_create_commit "$test_count" main2 &&
1000 test_create_commit "$test_count/sub proj" sub2 &&
1001 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1003 cd $test_count/"sub proj" &&
1004 git branch sub-branch-1 &&
1006 git fetch ./"sub proj" HEAD &&
1007 git subtree merge --prefix="sub dir" FETCH_HEAD
1009 test_create_commit "$test_count" "sub dir"/main-sub3 &&
1012 git subtree push ./"sub proj" --prefix "sub dir" sub-branch-1 &&
1014 git checkout sub-branch-1 &&
1015 test "$(last_commit_subject)" = "sub dir/main-sub3"
1020 # This test covers 2 cases in subtree split copy_or_skip code
1021 # 1) Merges where one parent is a superset of the changes of the other
1022 # parent regarding changes to the subtree, in this case the merge
1023 # commit should be copied
1024 # 2) Merges where only one parent operate on the subtree, and the merge
1025 # commit should be skipped
1027 # (1) is checked by ensuring subtree_tip is a descendent of subtree_branch
1028 # (2) should have a check added (not_a_subtree_change shouldn't be present
1029 # on the produced subtree)
1031 # Other related cases which are not tested (or currently handled correctly)
1032 # - Case (1) where there are more than 2 parents, it will sometimes correctly copy
1033 # the merge, and sometimes not
1034 # - Merge commit where both parents have same tree as the merge, currently
1035 # will always be skipped, even if they reached that state via different
1039 test_expect_success 'subtree descendant check' '
1040 subtree_test_create_repo "$test_count" &&
1041 defaultBranch=$(sed "s,ref: refs/heads/,," "$test_count/.git/HEAD") &&
1042 test_create_commit "$test_count" folder_subtree/a &&
1047 test_create_commit "$test_count" folder_subtree/0 &&
1048 test_create_commit "$test_count" folder_subtree/b &&
1049 cherry=$(cd "$test_count"; git rev-parse HEAD) &&
1054 test_create_commit "$test_count" commit_on_branch &&
1057 git cherry-pick $cherry &&
1058 git checkout $defaultBranch &&
1059 git merge -m "merge should be kept on subtree" branch &&
1060 git branch no_subtree_work_branch
1062 test_create_commit "$test_count" folder_subtree/d &&
1065 git checkout no_subtree_work_branch
1067 test_create_commit "$test_count" not_a_subtree_change &&
1070 git checkout $defaultBranch &&
1071 git merge -m "merge should be skipped on subtree" no_subtree_work_branch &&
1073 git subtree split --prefix folder_subtree/ --branch subtree_tip $defaultBranch &&
1074 git subtree split --prefix folder_subtree/ --branch subtree_branch branch &&
1075 test $(git rev-list --count subtree_tip..subtree_branch) = 0