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 TEST_DIRECTORY=$(pwd)/../../../t
14 . ../../../t/test-lib.sh
26 test_debug "echo \"check a:\" \"{$1}\""
27 test_debug "echo \" b:\" \"{$2}\""
28 if [ "$1" = "$2" ]; then
56 git reset --hard HEAD~
61 git log --pretty=format:%s -1
64 test_expect_success 'init subproj' '
65 test_create_repo subproj
71 test_expect_success 'add sub1' '
73 git commit -m "sub1" &&
75 git branch -m master subproj
78 # Save this hash for testing later.
80 subdir_hash=$(git rev-parse HEAD)
82 test_expect_success 'add sub2' '
84 git commit -m "sub2" &&
88 test_expect_success 'add sub3' '
90 git commit -m "sub3" &&
97 test_expect_success 'add main4' '
99 git commit -m "main4" &&
100 git branch -m master mainline &&
104 test_expect_success 'fetch subproj history' '
105 git fetch ./subproj sub1 &&
106 git branch sub1 FETCH_HEAD
109 test_expect_success 'no subtree exists in main tree' '
110 test_must_fail git subtree merge --prefix=subdir sub1
113 test_expect_success 'no pull from non-existant subtree' '
114 test_must_fail git subtree pull --prefix=subdir ./subproj sub1
117 test_expect_success 'check if --message works for add' '
118 git subtree add --prefix=subdir --message="Added subproject" sub1 &&
119 check_equal ''"$(last_commit_message)"'' "Added subproject" &&
123 test_expect_success 'check if --message works as -m and --prefix as -P' '
124 git subtree add -P subdir -m "Added subproject using git subtree" sub1 &&
125 check_equal ''"$(last_commit_message)"'' "Added subproject using git subtree" &&
129 test_expect_success 'check if --message works with squash too' '
130 git subtree add -P subdir -m "Added subproject with squash" --squash sub1 &&
131 check_equal ''"$(last_commit_message)"'' "Added subproject with squash" &&
135 test_expect_success 'add subproj to mainline' '
136 git subtree add --prefix=subdir/ FETCH_HEAD &&
137 check_equal ''"$(last_commit_message)"'' "Add '"'subdir/'"' from commit '"'"'''"$(git rev-parse sub1)"'''"'"'"
140 # this shouldn't actually do anything, since FETCH_HEAD is already a parent
141 test_expect_success 'merge fetched subproj' '
142 git merge -m "merge -s -ours" -s ours FETCH_HEAD
145 test_expect_success 'add main-sub5' '
146 create subdir/main-sub5 &&
147 git commit -m "main-sub5"
150 test_expect_success 'add main6' '
152 git commit -m "main6 boring"
155 test_expect_success 'add main-sub7' '
156 create subdir/main-sub7 &&
157 git commit -m "main-sub7"
160 test_expect_success 'fetch new subproj history' '
161 git fetch ./subproj sub2 &&
162 git branch sub2 FETCH_HEAD
165 test_expect_success 'check if --message works for merge' '
166 git subtree merge --prefix=subdir -m "Merged changes from subproject" sub2 &&
167 check_equal ''"$(last_commit_message)"'' "Merged changes from subproject" &&
171 test_expect_success 'check if --message for merge works with squash too' '
172 git subtree merge --prefix subdir -m "Merged changes from subproject using squash" --squash sub2 &&
173 check_equal ''"$(last_commit_message)"'' "Merged changes from subproject using squash" &&
177 test_expect_success 'merge new subproj history into subdir' '
178 git subtree merge --prefix=subdir FETCH_HEAD &&
179 git branch pre-split &&
180 check_equal ''"$(last_commit_message)"'' "Merge commit '"'"'"$(git rev-parse sub2)"'"'"' into mainline" &&
184 test_expect_success 'Check that prefix argument is required for split' '
185 echo "You must provide the --prefix option." > expected &&
186 test_must_fail git subtree split > actual 2>&1 &&
187 test_debug "printf '"'"'expected: '"'"'" &&
188 test_debug "cat expected" &&
189 test_debug "printf '"'"'actual: '"'"'" &&
190 test_debug "cat actual" &&
191 test_cmp expected actual &&
192 rm -f expected actual
195 test_expect_success 'Check that the <prefix> exists for a split' '
196 echo "'"'"'non-existent-directory'"'"'" does not exist\; use "'"'"'git subtree add'"'"'" > expected &&
197 test_must_fail git subtree split --prefix=non-existent-directory > actual 2>&1 &&
198 test_debug "printf '"'"'expected: '"'"'" &&
199 test_debug "cat expected" &&
200 test_debug "printf '"'"'actual: '"'"'" &&
201 test_debug "cat actual" &&
202 test_cmp expected actual
203 # rm -f expected actual
206 test_expect_success 'check if --message works for split+rejoin' '
207 spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
208 git branch spl1 "$spl1" &&
209 check_equal ''"$(last_commit_message)"'' "Split & rejoin" &&
213 test_expect_success 'check split with --branch' '
214 spl1=$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin) &&
216 git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --branch splitbr1 &&
217 check_equal ''"$(git rev-parse splitbr1)"'' "$spl1"
220 test_expect_success 'check hash of split' '
221 spl1=$(git subtree split --prefix subdir) &&
222 git subtree split --prefix subdir --branch splitbr1test &&
223 check_equal ''"$(git rev-parse splitbr1test)"'' "$spl1" &&
224 new_hash=$(git rev-parse splitbr1test~2) &&
225 check_equal ''"$new_hash"'' "$subdir_hash"
228 test_expect_success 'check split with --branch for an existing branch' '
229 spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
231 git branch splitbr2 sub1 &&
232 git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --branch splitbr2 &&
233 check_equal ''"$(git rev-parse splitbr2)"'' "$spl1"
236 test_expect_success 'check split with --branch for an incompatible branch' '
237 test_must_fail git subtree split --prefix subdir --onto FETCH_HEAD --branch subdir
240 test_expect_success 'check split+rejoin' '
241 spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
243 git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --rejoin &&
244 check_equal ''"$(last_commit_message)"'' "Split '"'"'subdir/'"'"' into commit '"'"'"$spl1"'"'"'"
247 test_expect_success 'add main-sub8' '
248 create subdir/main-sub8 &&
249 git commit -m "main-sub8"
255 test_expect_success 'merge split into subproj' '
257 git branch spl1 FETCH_HEAD &&
261 test_expect_success 'add sub9' '
269 test_expect_success 'split for sub8' '
270 split2=''"$(git subtree split --annotate='"'*'"' --prefix subdir/ --rejoin)"'' &&
271 git branch split2 "$split2"
274 test_expect_success 'add main-sub10' '
275 create subdir/main-sub10 &&
276 git commit -m "main-sub10"
279 test_expect_success 'split for sub10' '
280 spl3=''"$(git subtree split --annotate='"'*'"' --prefix subdir --rejoin)"'' &&
281 git branch spl3 "$spl3"
287 test_expect_success 'merge split into subproj' '
289 git branch spl3 FETCH_HEAD &&
290 git merge FETCH_HEAD &&
291 git branch subproj-merge-spl3
295 chkms="main-sub10 main-sub5 main-sub7 main-sub8"
296 chkms_sub=$(echo $chkms | multiline | sed 's,^,subdir/,' | fixnl)
297 chks="sub1 sub2 sub3 sub9"
298 chks_sub=$(echo $chks | multiline | sed 's,^,subdir/,' | fixnl)
300 test_expect_success 'make sure exactly the right set of files ends up in the subproj' '
301 subfiles=''"$(git ls-files | fixnl)"'' &&
302 check_equal "$subfiles" "$chkms $chks"
305 test_expect_success 'make sure the subproj history *only* contains commits that affect the subdir' '
306 allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | fixnl)"'' &&
307 check_equal "$allchanges" "$chkms $chks"
313 test_expect_success 'pull from subproj' '
314 git fetch ./subproj subproj-merge-spl3 &&
315 git branch subproj-merge-spl3 FETCH_HEAD &&
316 git subtree pull --prefix=subdir ./subproj subproj-merge-spl3
319 test_expect_success 'make sure exactly the right set of files ends up in the mainline' '
320 mainfiles=''"$(git ls-files | fixnl)"'' &&
321 check_equal "$mainfiles" "$chkm $chkms_sub $chks_sub"
324 test_expect_success 'make sure each filename changed exactly once in the entire history' '
325 # main-sub?? and /subdir/main-sub?? both change, because those are the
326 # changes that were split into their own history. And subdir/sub?? never
327 # change, since they were *only* changed in the subtree branch.
328 allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | fixnl)"'' &&
329 check_equal "$allchanges" ''"$(echo $chkms $chkm $chks $chkms_sub | multiline | sort | fixnl)"''
332 test_expect_success 'make sure the --rejoin commits never make it into subproj' '
333 check_equal ''"$(git log --pretty=format:'"'%s'"' HEAD^2 | grep -i split)"'' ""
336 test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' '
337 # They are meaningless to subproj since one side of the merge refers to the mainline
338 check_equal ''"$(git log --pretty=format:'"'%s%n%b'"' HEAD^2 | grep "git-subtree.*:")"'' ""
341 # prepare second pair of repositories
345 test_expect_success 'init main' '
346 test_create_repo main
351 test_expect_success 'add main1' '
353 git commit -m "main1"
358 test_expect_success 'init sub' '
364 test_expect_success 'add sub2' '
371 # check if split can find proper base without --onto
373 test_expect_success 'add sub as subdir in main' '
374 git fetch ../sub master &&
375 git branch sub2 FETCH_HEAD &&
376 git subtree add --prefix subdir sub2
381 test_expect_success 'add sub3' '
388 test_expect_success 'merge from sub' '
389 git fetch ../sub master &&
390 git branch sub3 FETCH_HEAD &&
391 git subtree merge --prefix subdir sub3
394 test_expect_success 'add main-sub4' '
395 create subdir/main-sub4 &&
396 git commit -m "main-sub4"
399 test_expect_success 'split for main-sub4 without --onto' '
400 git subtree split --prefix subdir --branch mainsub4
403 # at this point, the new commit parent should be sub3 if it is not,
404 # something went wrong (the "newparent" of "master~" commit should
405 # have been sub3, but it was not, because its cache was not set to
408 test_expect_success 'check that the commit parent is sub3' '
409 check_equal ''"$(git log --pretty=format:%P -1 mainsub4)"'' ''"$(git rev-parse sub3)"''
412 test_expect_success 'add main-sub5' '
414 create subdir2/main-sub5 &&
415 git commit -m "main-sub5"
418 test_expect_success 'split for main-sub5 without --onto' '
419 # also test that we still can split out an entirely new subtree
420 # if the parent of the first commit in the tree is not empty,
421 # then the new subtree has accidentally been attached to something
422 git subtree split --prefix subdir2 --branch mainsub5 &&
423 check_equal ''"$(git log --pretty=format:%P -1 mainsub5)"'' ""
426 # make sure no patch changes more than one file. The original set of commits
427 # changed only one file each. A multi-file change would imply that we pruned
428 # commits too aggressively.
437 elif [ "$x" = "commit:" ]; then
438 if [ -n "$commit" ]; then
450 test_expect_success 'verify one file change per commit' '
452 list=''"$(git log --pretty=format:'"'commit: %H'"' | joincommits)"'' &&
453 # test_debug "echo HERE" &&
454 # test_debug "echo ''"$list"''" &&
455 (git log --pretty=format:'"'commit: %H'"' | joincommits |
456 ( while read commit a b; do
457 test_debug "echo Verifying commit "''"$commit"''
458 test_debug "echo a: "''"$a"''
459 test_debug "echo b: "''"$b"''