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
26 test_debug "echo \"check a:\" \"{$1}\""
27 test_debug "echo \" b:\" \"{$2}\""
28 if [ "$1" = "$2" ]; then
37 git reset --hard HEAD~
40 # Make sure no patch changes more than one file.
41 # The original set of commits changed only one file each.
42 # A multi-file change would imply that we pruned commits
51 elif [ "$x" = "commit:" ]; then
52 if [ -n "$commit" ]; then
66 git log --pretty=format:%s -1
69 test_expect_success 'init subproj' '
70 test_create_repo "sub proj"
76 test_expect_success 'add sub1' '
78 git commit -m "sub1" &&
80 git branch -m master subproj
83 # Save this hash for testing later.
85 subdir_hash=$(git rev-parse HEAD)
87 test_expect_success 'add sub2' '
89 git commit -m "sub2" &&
93 test_expect_success 'add sub3' '
95 git commit -m "sub3" &&
102 test_expect_success 'enable log.date=relative to catch errors' '
103 git config log.date relative
106 test_expect_success 'add main4' '
108 git commit -m "main4" &&
109 git branch -m master mainline &&
113 test_expect_success 'fetch subproj history' '
114 git fetch ./"sub proj" sub1 &&
115 git branch sub1 FETCH_HEAD
118 test_expect_success 'no subtree exists in main tree' '
119 test_must_fail git subtree merge --prefix="sub dir" sub1
122 test_expect_success 'no pull from non-existant subtree' '
123 test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" sub1
126 test_expect_success 'no merge from non-existent subtree' '
127 test_must_fail git subtree merge --prefix="sub dir" FETCH_HEAD
130 test_expect_success 'add subproj as subtree into sub dir/ with --prefix' '
131 git subtree add --prefix="sub dir" sub1 &&
132 check_equal "$(last_commit_message)" "Add '\''sub dir/'\'' from commit '\''$(git rev-parse sub1)'\''" &&
136 test_expect_success 'check if --message works for add' '
137 git subtree add --prefix="sub dir" --message="Added subproject" sub1 &&
138 check_equal ''"$(last_commit_message)"'' "Added subproject" &&
142 test_expect_success 'add subproj as subtree into sub dir/ with --prefix and --message' '
143 git subtree add --prefix="sub dir" --message="Added subproject" sub1 &&
144 check_equal "$(last_commit_message)" "Added subproject" &&
148 test_expect_success 'check if --message works as -m and --prefix as -P' '
149 git subtree add -P "sub dir" -m "Added subproject using git subtree" sub1 &&
150 check_equal ''"$(last_commit_message)"'' "Added subproject using git subtree" &&
154 test_expect_success 'check if --message works with squash too' '
155 git subtree add -P "sub dir" -m "Added subproject with squash" --squash sub1 &&
156 check_equal ''"$(last_commit_message)"'' "Added subproject with squash" &&
160 test_expect_success 'add subproj as subtree into sub dir/ with --squash and --prefix and --message' '
161 git subtree add --prefix="sub dir" --message="Added subproject with squash" --squash sub1 &&
162 check_equal "$(last_commit_message)" "Added subproject with squash" &&
167 test_expect_success 'add subproj to mainline' '
168 git subtree add --prefix="sub dir"/ FETCH_HEAD &&
169 check_equal ''"$(last_commit_message)"'' "Add '"'sub dir/'"' from commit '"'"'''"$(git rev-parse sub1)"'''"'"'"
172 test_expect_success 'merge the added subproj again, should do nothing' '
173 # this shouldn not actually do anything, since FETCH_HEAD
174 # is already a parent
175 result=$(git merge -s ours -m "merge -s -ours" FETCH_HEAD) &&
176 check_equal "${result}" "Already up-to-date."
179 test_expect_success 'add main-sub5' '
180 create "sub dir/main-sub5" &&
181 git commit -m "main-sub5"
184 test_expect_success 'add main6' '
186 git commit -m "main6 boring"
189 test_expect_success 'add main-sub7' '
190 create "sub dir/main-sub7" &&
191 git commit -m "main-sub7"
194 test_expect_success 'fetch new subproj history' '
195 git fetch ./"sub proj" sub2 &&
196 git branch sub2 FETCH_HEAD
199 test_expect_success 'check if --message works for merge' '
200 git subtree merge --prefix="sub dir" -m "Merged changes from subproject" sub2 &&
201 check_equal ''"$(last_commit_message)"'' "Merged changes from subproject" &&
205 test_expect_success 'check if --message for merge works with squash too' '
206 git subtree merge --prefix "sub dir" -m "Merged changes from subproject using squash" --squash sub2 &&
207 check_equal ''"$(last_commit_message)"'' "Merged changes from subproject using squash" &&
211 test_expect_success 'merge new subproj history into subdir' '
212 git subtree merge --prefix="sub dir" FETCH_HEAD &&
213 check_equal ''"$(last_commit_message)"'' "Merge commit '"'"'"$(git rev-parse sub2)"'"'"' into mainline" &&
217 test_expect_success 'merge new subproj history into subdir/ with --prefix and --message' '
218 git subtree merge --prefix="sub dir" --message="Merged changes from subproject" FETCH_HEAD &&
219 check_equal "$(last_commit_message)" "Merged changes from subproject" &&
223 test_expect_success 'merge new subproj history into subdir/ with --squash and --prefix and --message' '
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 'split requires option --prefix' '
230 echo "You must provide the --prefix option." > expected &&
231 test_must_fail git subtree split > actual 2>&1 &&
232 test_debug "printf '"'"'expected: '"'"'" &&
233 test_debug "cat expected" &&
234 test_debug "printf '"'"'actual: '"'"'" &&
235 test_debug "cat actual" &&
236 test_cmp expected actual &&
237 rm -f expected actual
240 test_expect_success 'split requires path given by option --prefix must exist' '
241 echo "'\''non-existent-directory'\'' does not exist; use '\''git subtree add'\''" > expected &&
242 test_must_fail git subtree split --prefix=non-existent-directory > actual 2>&1 &&
243 test_debug "printf '"'"'expected: '"'"'" &&
244 test_debug "cat expected" &&
245 test_debug "printf '"'"'actual: '"'"'" &&
246 test_debug "cat actual" &&
247 test_cmp expected actual &&
248 rm -f expected actual
251 test_expect_success 'check if --message works for split+rejoin' '
252 spl1=''"$(git subtree split --annotate='"'*'"' --prefix "sub dir" --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
253 check_equal ''"$(last_commit_message)"'' "Split & rejoin" &&
257 test_expect_success 'check split with --branch' '
258 spl1=$(git subtree split --annotate='"'*'"' --prefix "sub dir" --onto FETCH_HEAD --message "Split & rejoin" --rejoin) &&
260 git subtree split --annotate='"'*'"' --prefix "sub dir" --onto FETCH_HEAD --branch splitbr1 &&
261 check_equal ''"$(git rev-parse splitbr1)"'' "$spl1"
264 test_expect_success 'check hash of split' '
265 spl1=$(git subtree split --prefix "sub dir") &&
266 git subtree split --prefix "sub dir" --branch splitbr1test &&
267 check_equal ''"$(git rev-parse splitbr1test)"'' "$spl1" &&
268 new_hash=$(git rev-parse splitbr1test~2) &&
269 check_equal ''"$new_hash"'' "$subdir_hash"
272 test_expect_success 'check split with --branch for an existing branch' '
273 spl1=''"$(git subtree split --annotate='"'*'"' --prefix "sub dir" --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
275 git branch splitbr2 sub1 &&
276 git subtree split --annotate='"'*'"' --prefix "sub dir" --onto FETCH_HEAD --branch splitbr2 &&
277 check_equal ''"$(git rev-parse splitbr2)"'' "$spl1"
280 test_expect_success 'check split with --branch for an incompatible branch' '
281 test_must_fail git subtree split --prefix "sub dir" --onto FETCH_HEAD --branch subdir
284 test_expect_success 'split sub dir/ with --rejoin' '
285 spl1=$(git subtree split --prefix="sub dir" --annotate="*") &&
286 git branch spl1 "$spl1" &&
287 git subtree split --prefix="sub dir" --annotate="*" --rejoin &&
288 check_equal "$(last_commit_message)" "Split '\''sub dir/'\'' into commit '\''$spl1'\''" &&
292 test_expect_success 'split sub dir/ with --rejoin and --message' '
293 git subtree split --prefix="sub dir" --message="Split & rejoin" --annotate="*" --rejoin &&
294 check_equal "$(last_commit_message)" "Split & rejoin" &&
298 test_expect_success 'check split+rejoin+onto' '
299 spl1=''"$(git subtree split --annotate='"'*'"' --prefix "sub dir" --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
301 git subtree split --annotate='"'*'"' --prefix "sub dir" --onto FETCH_HEAD --rejoin &&
302 check_equal ''"$(last_commit_message)"'' "Split '"'"'sub dir/'"'"' into commit '"'"'"$spl1"'"'"'"
305 test_expect_success 'add main-sub8' '
306 create "sub dir/main-sub8" &&
307 git commit -m "main-sub8"
313 test_expect_success 'merge split into subproj' '
315 git branch spl1 FETCH_HEAD &&
319 test_expect_success 'add sub9' '
327 test_expect_success 'split for sub8' '
328 split2=''"$(git subtree split --annotate='"'*'"' --prefix "sub dir/" --rejoin)"'' &&
329 git branch split2 "$split2"
332 test_expect_success 'add main-sub10' '
333 create "sub dir/main-sub10" &&
334 git commit -m "main-sub10"
337 test_expect_success 'split for sub10' '
338 spl3=''"$(git subtree split --annotate='"'*'"' --prefix "sub dir" --rejoin)"'' &&
339 git branch spl3 "$spl3"
345 test_expect_success 'merge split into subproj' '
347 git branch spl3 FETCH_HEAD &&
348 git merge FETCH_HEAD &&
349 git branch subproj-merge-spl3
360 chkms_sub=$(cat <<TXT | sed 's,^,sub dir/,'
370 chks_sub=$(cat <<TXT | sed 's,^,sub dir/,'
375 test_expect_success 'make sure exactly the right set of files ends up in the subproj' '
376 subfiles="$(git ls-files)" &&
377 check_equal "$subfiles" "$chkms
381 test_expect_success 'make sure the subproj history *only* contains commits that affect the subdir' '
382 allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | sed "/^$/d")"'' &&
383 check_equal "$allchanges" "$chkms
390 test_expect_success 'pull from subproj' '
391 git fetch ./"sub proj" subproj-merge-spl3 &&
392 git branch subproj-merge-spl3 FETCH_HEAD &&
393 git subtree pull --prefix="sub dir" ./"sub proj" subproj-merge-spl3
396 test_expect_success 'make sure exactly the right set of files ends up in the mainline' '
397 mainfiles=$(git ls-files) &&
398 check_equal "$mainfiles" "$chkm
403 test_expect_success 'make sure each filename changed exactly once in the entire history' '
404 # main-sub?? and sub dir/main-sub?? both change, because those are the
405 # changes that were split into their own history. And sub dir/sub?? never
406 # change, since they were *only* changed in the subtree branch.
407 allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | sed "/^$/d")"'' &&
408 expected=''"$(cat <<TXT | sort
415 check_equal "$allchanges" "$expected"
418 test_expect_success 'make sure the --rejoin commits never make it into subproj' '
419 check_equal "$(git log --pretty=format:"%s" HEAD^2 | grep -i split)" ""
422 test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' '
423 # They are meaningless to subproj since one side of the merge refers to the mainline
424 check_equal "$(git log --pretty=format:"%s%n%b" HEAD^2 | grep "git-subtree.*:")" ""
427 # prepare second pair of repositories
431 test_expect_success 'init main' '
432 test_create_repo main
437 test_expect_success 'add main1' '
439 git commit -m "main1"
444 test_expect_success 'init sub' '
450 test_expect_success 'add sub2' '
457 # check if split can find proper base without --onto
459 test_expect_success 'add sub as subdir in main' '
460 git fetch ../sub master &&
461 git branch sub2 FETCH_HEAD &&
462 git subtree add --prefix "sub dir" sub2
467 test_expect_success 'add sub3' '
474 test_expect_success 'merge from sub' '
475 git fetch ../sub master &&
476 git branch sub3 FETCH_HEAD &&
477 git subtree merge --prefix "sub dir" sub3
480 test_expect_success 'add main-sub4' '
481 create "sub dir/main-sub4" &&
482 git commit -m "main-sub4"
485 test_expect_success 'split for main-sub4 without --onto' '
486 git subtree split --prefix "sub dir" --branch mainsub4
489 # At this point, the new commit parent should be sub3. If it is not,
490 # something went wrong (the "newparent" of "master~" commit should
491 # have been sub3, but it was not, because its cache was not set to
494 test_expect_success 'check that the commit parent is sub3' '
495 check_equal "$(git log --pretty=format:%P -1 mainsub4)" "$(git rev-parse sub3)"
498 test_expect_success 'add main-sub5' '
500 create subdir2/main-sub5 &&
501 git commit -m "main-sub5"
504 test_expect_success 'split for main-sub5 without --onto' '
505 # also test that we still can split out an entirely new subtree
506 # if the parent of the first commit in the tree is not empty,
507 # then the new subtree has accidentally been attached to something
508 git subtree split --prefix subdir2 --branch mainsub5 &&
509 check_equal ''"$(git log --pretty=format:%P -1 mainsub5)"'' ""
512 test_expect_success 'verify one file change per commit' '
514 list=''"$(git log --pretty=format:'"'commit: %H'"' | join_commits)"'' &&
515 # test_debug "echo HERE" &&
516 # test_debug "echo ''"$list"''" &&
517 git log --pretty=format:'"'commit: %H'"' | join_commits |
518 ( while read commit a b; do
519 test_debug "echo Verifying commit "''"$commit"''
520 test_debug "echo a: "''"$a"''
521 test_debug "echo b: "''"$b"''
537 test_expect_success 'init main' '
538 test_create_repo main
541 test_expect_success 'init sub' '
542 test_create_repo "sub project"
547 test_expect_success 'add subproject' '
548 create "sub project" &&
549 git commit -m "Sub project: 1" &&
550 git branch sub-branch-1
555 test_expect_success 'make first commit and add subproject' '
557 git commit -m "main: 1" &&
558 git subtree add "../sub project" --prefix "sub dir" --message "Added subproject" sub-branch-1 &&
559 check_equal "$(last_commit_message)" "Added subproject"
562 test_expect_success 'make second commit to a subproject file and push it into a sub project' '
563 create "sub dir/sub1" &&
564 git commit -m "Sub project: 2" &&
565 git subtree push "../sub project" --prefix "sub dir" sub-branch-1
570 test_expect_success 'Test second commit is pushed' '
571 git checkout sub-branch-1 &&
572 check_equal "$(last_commit_message)" "Sub project: 2"