3 # Copyright (c) 2012 Avery Pennaraum
5 test_description='Basic porcelain support for subtrees
7 This test verifies the basic operation of the merge, pull, add
8 and split subcommands of git subtree.
11 export TEST_DIRECTORY=$(pwd)/../../../t
13 . ../../../t/test-lib.sh
25 test_debug "echo \"check a:\" \"{$1}\""
26 test_debug "echo \" b:\" \"{$2}\""
27 if [ "$1" = "$2" ]; then
55 git reset --hard HEAD~
60 git log --pretty=format:%s -1
64 test_expect_success 'init subproj' '
65 test_create_repo subproj
72 test_expect_success 'add sub1' '
74 git commit -m "sub1" &&
76 git branch -m master subproj
80 test_expect_success 'add sub2' '
82 git commit -m "sub2" &&
87 test_expect_success 'add sub3' '
89 git commit -m "sub3" &&
97 test_expect_success 'add main4' '
99 git commit -m "main4" &&
100 git branch -m master mainline &&
105 test_expect_success 'fetch subproj history' '
106 git fetch ./subproj sub1 &&
107 git branch sub1 FETCH_HEAD
111 test_expect_success 'no subtree exists in main tree' '
112 test_must_fail git subtree merge --prefix=subdir sub1
116 test_expect_success 'no pull from non-existant subtree' '
117 test_must_fail git subtree pull --prefix=subdir ./subproj sub1
121 test_expect_success 'check if --message works for add' '
122 git subtree add --prefix=subdir --message="Added subproject" sub1 &&
123 check_equal ''"$(last_commit_message)"'' "Added subproject" &&
128 test_expect_success 'check if --message works as -m and --prefix as -P' '
129 git subtree add -P subdir -m "Added subproject using git subtree" sub1 &&
130 check_equal ''"$(last_commit_message)"'' "Added subproject using git subtree" &&
135 test_expect_success 'check if --message works with squash too' '
136 git subtree add -P subdir -m "Added subproject with squash" --squash sub1 &&
137 check_equal ''"$(last_commit_message)"'' "Added subproject with squash" &&
142 test_expect_success 'add subproj to mainline' '
143 git subtree add --prefix=subdir/ FETCH_HEAD &&
144 check_equal ''"$(last_commit_message)"'' "Add '"'subdir/'"' from commit '"'"'''"$(git rev-parse sub1)"'''"'"'"
148 # this shouldn't actually do anything, since FETCH_HEAD is already a parent
149 test_expect_success 'merge fetched subproj' '
150 git merge -m "merge -s -ours" -s ours FETCH_HEAD
154 test_expect_success 'add main-sub5' '
155 create subdir/main-sub5 &&
156 git commit -m "main-sub5"
160 test_expect_success 'add main6' '
162 git commit -m "main6 boring"
166 test_expect_success 'add main-sub7' '
167 create subdir/main-sub7 &&
168 git commit -m "main-sub7"
172 test_expect_success 'fetch new subproj history' '
173 git fetch ./subproj sub2 &&
174 git branch sub2 FETCH_HEAD
178 test_expect_success 'check if --message works for merge' '
179 git subtree merge --prefix=subdir -m "Merged changes from subproject" sub2 &&
180 check_equal ''"$(last_commit_message)"'' "Merged changes from subproject" &&
185 test_expect_success 'check if --message for merge works with squash too' '
186 git subtree merge --prefix subdir -m "Merged changes from subproject using squash" --squash sub2 &&
187 check_equal ''"$(last_commit_message)"'' "Merged changes from subproject using squash" &&
192 test_expect_success 'merge new subproj history into subdir' '
193 git subtree merge --prefix=subdir FETCH_HEAD &&
194 git branch pre-split &&
195 check_equal ''"$(last_commit_message)"'' "Merge commit '"'"'"$(git rev-parse sub2)"'"'"' into mainline"
199 test_expect_success 'Check that prefix argument is required for split' '
200 echo "You must provide the --prefix option." > expected &&
201 test_must_fail git subtree split > actual 2>&1 &&
202 test_debug "echo -n expected: " &&
203 test_debug "cat expected" &&
204 test_debug "echo -n actual: " &&
205 test_debug "cat actual" &&
206 test_cmp expected actual &&
207 rm -f expected actual
211 test_expect_success 'Check that the <prefix> exists for a split' '
212 echo "'"'"'non-existent-directory'"'"'" does not exist\; use "'"'"'git subtree add'"'"'" > expected &&
213 test_must_fail git subtree split --prefix=non-existent-directory > actual 2>&1 &&
214 test_debug "echo -n expected: " &&
215 test_debug "cat expected" &&
216 test_debug "echo -n actual: " &&
217 test_debug "cat actual" &&
218 test_cmp expected actual
219 # rm -f expected actual
223 test_expect_success 'check if --message works for split+rejoin' '
224 spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
225 git branch spl1 "$spl1" &&
226 check_equal ''"$(last_commit_message)"'' "Split & rejoin" &&
231 test_expect_success 'check split with --branch' '
232 spl1=$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin) &&
234 git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --branch splitbr1 &&
235 check_equal ''"$(git rev-parse splitbr1)"'' "$spl1"
239 test_expect_success 'check split with --branch for an existing branch' '
240 spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
242 git branch splitbr2 sub1 &&
243 git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --branch splitbr2 &&
244 check_equal ''"$(git rev-parse splitbr2)"'' "$spl1"
248 test_expect_success 'check split with --branch for an incompatible branch' '
249 test_must_fail git subtree split --prefix subdir --onto FETCH_HEAD --branch subdir
254 test_expect_success 'check split+rejoin' '
255 spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
257 git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --rejoin &&
258 check_equal ''"$(last_commit_message)"'' "Split '"'"'subdir/'"'"' into commit '"'"'"$spl1"'"'"'"
262 test_expect_success 'add main-sub8' '
263 create subdir/main-sub8 &&
264 git commit -m "main-sub8"
271 test_expect_success 'merge split into subproj' '
273 git branch spl1 FETCH_HEAD &&
278 test_expect_success 'add sub9' '
287 test_expect_success 'split for sub8' '
288 split2=''"$(git subtree split --annotate='"'*'"' --prefix subdir/ --rejoin)"''
289 git branch split2 "$split2"
293 test_expect_success 'add main-sub10' '
294 create subdir/main-sub10 &&
295 git commit -m "main-sub10"
299 test_expect_success 'split for sub10' '
300 spl3=''"$(git subtree split --annotate='"'*'"' --prefix subdir --rejoin)"'' &&
301 git branch spl3 "$spl3"
308 test_expect_success 'merge split into subproj' '
310 git branch spl3 FETCH_HEAD &&
311 git merge FETCH_HEAD &&
312 git branch subproj-merge-spl3
316 chkms="main-sub10 main-sub5 main-sub7 main-sub8"
317 chkms_sub=$(echo $chkms | multiline | sed 's,^,subdir/,' | fixnl)
318 chks="sub1 sub2 sub3 sub9"
319 chks_sub=$(echo $chks | multiline | sed 's,^,subdir/,' | fixnl)
322 test_expect_success 'make sure exactly the right set of files ends up in the subproj' '
323 subfiles=''"$(git ls-files | fixnl)"'' &&
324 check_equal "$subfiles" "$chkms $chks"
328 test_expect_success 'make sure the subproj history *only* contains commits that affect the subdir' '
329 allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | fixnl)"'' &&
330 check_equal "$allchanges" "$chkms $chks"
337 test_expect_success 'pull from subproj' '
338 git fetch ./subproj subproj-merge-spl3 &&
339 git branch subproj-merge-spl3 FETCH_HEAD &&
340 git subtree pull --prefix=subdir ./subproj subproj-merge-spl3
344 test_expect_success 'make sure exactly the right set of files ends up in the mainline' '
345 mainfiles=''"$(git ls-files | fixnl)"'' &&
346 check_equal "$mainfiles" "$chkm $chkms_sub $chks_sub"
350 test_expect_success 'make sure each filename changed exactly once in the entire history' '
351 # main-sub?? and /subdir/main-sub?? both change, because those are the
352 # changes that were split into their own history. And subdir/sub?? never
353 # change, since they were *only* changed in the subtree branch.
354 allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | fixnl)"'' &&
355 check_equal "$allchanges" ''"$(echo $chkms $chkm $chks $chkms_sub | multiline | sort | fixnl)"''
359 test_expect_success 'make sure the --rejoin commits never make it into subproj' '
360 check_equal ''"$(git log --pretty=format:'"'%s'"' HEAD^2 | grep -i split)"'' ""
364 test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' '
365 # They are meaningless to subproj since one side of the merge refers to the mainline
366 check_equal ''"$(git log --pretty=format:'"'%s%n%b'"' HEAD^2 | grep "git-subtree.*:")"'' ""
369 # prepare second pair of repositories
374 test_expect_success 'init main' '
375 test_create_repo main
381 test_expect_success 'add main1' '
383 git commit -m "main1"
389 test_expect_success 'init sub' '
396 test_expect_success 'add sub2' '
403 # check if split can find proper base without --onto
406 test_expect_success 'add sub as subdir in main' '
407 git fetch ../sub master &&
408 git branch sub2 FETCH_HEAD &&
409 git subtree add --prefix subdir sub2
415 test_expect_success 'add sub3' '
423 test_expect_success 'merge from sub' '
424 git fetch ../sub master &&
425 git branch sub3 FETCH_HEAD &&
426 git subtree merge --prefix subdir sub3
430 test_expect_success 'add main-sub4' '
431 create subdir/main-sub4 &&
432 git commit -m "main-sub4"
436 test_expect_success 'split for main-sub4 without --onto' '
437 git subtree split --prefix subdir --branch mainsub4
440 # at this point, the new commit parent should be sub3 if it is not,
441 # something went wrong (the "newparent" of "master~" commit should
442 # have been sub3, but it was not, because its cache was not set to
446 test_expect_success 'check that the commit parent is sub3' '
447 check_equal ''"$(git log --pretty=format:%P -1 mainsub4)"'' ''"$(git rev-parse sub3)"''
451 test_expect_success 'add main-sub5' '
453 create subdir2/main-sub5 &&
454 git commit -m "main-sub5"
458 test_expect_success 'split for main-sub5 without --onto' '
459 # also test that we still can split out an entirely new subtree
460 # if the parent of the first commit in the tree is not empty,
461 # then the new subtree has accidently been attached to something
462 git subtree split --prefix subdir2 --branch mainsub5 &&
463 check_equal ''"$(git log --pretty=format:%P -1 mainsub5)"'' ""
466 # make sure no patch changes more than one file. The original set of commits
467 # changed only one file each. A multi-file change would imply that we pruned
468 # commits too aggressively.
477 elif [ "$x" = "commit:" ]; then
478 if [ -n "$commit" ]; then
491 test_expect_success 'verify one file change per commit' '
493 list=''"$(git log --pretty=format:'"'commit: %H'"' | joincommits)"'' &&
494 # test_debug "echo HERE" &&
495 # test_debug "echo ''"$list"''" &&
496 (git log --pretty=format:'"'commit: %H'"' | joincommits |
497 ( while read commit a b; do
498 test_debug "echo Verifying commit "''"$commit"''
499 test_debug "echo a: "''"$a"''
500 test_debug "echo b: "''"$b"''