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 'enable log.date=relative to catch errors' '
98 git config log.date relative
101 test_expect_success 'add main4' '
103 git commit -m "main4" &&
104 git branch -m master mainline &&
108 test_expect_success 'fetch subproj history' '
109 git fetch ./subproj sub1 &&
110 git branch sub1 FETCH_HEAD
113 test_expect_success 'no subtree exists in main tree' '
114 test_must_fail git subtree merge --prefix=subdir sub1
117 test_expect_success 'no pull from non-existant subtree' '
118 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" &&
127 test_expect_success 'check if --message works as -m and --prefix as -P' '
128 git subtree add -P subdir -m "Added subproject using git subtree" sub1 &&
129 check_equal ''"$(last_commit_message)"'' "Added subproject using git subtree" &&
133 test_expect_success 'check if --message works with squash too' '
134 git subtree add -P subdir -m "Added subproject with squash" --squash sub1 &&
135 check_equal ''"$(last_commit_message)"'' "Added subproject with squash" &&
139 test_expect_success 'add subproj to mainline' '
140 git subtree add --prefix=subdir/ FETCH_HEAD &&
141 check_equal ''"$(last_commit_message)"'' "Add '"'subdir/'"' from commit '"'"'''"$(git rev-parse sub1)"'''"'"'"
144 # this shouldn't actually do anything, since FETCH_HEAD is already a parent
145 test_expect_success 'merge fetched subproj' '
146 git merge -m "merge -s -ours" -s ours FETCH_HEAD
149 test_expect_success 'add main-sub5' '
150 create subdir/main-sub5 &&
151 git commit -m "main-sub5"
154 test_expect_success 'add main6' '
156 git commit -m "main6 boring"
159 test_expect_success 'add main-sub7' '
160 create subdir/main-sub7 &&
161 git commit -m "main-sub7"
164 test_expect_success 'fetch new subproj history' '
165 git fetch ./subproj sub2 &&
166 git branch sub2 FETCH_HEAD
169 test_expect_success 'check if --message works for merge' '
170 git subtree merge --prefix=subdir -m "Merged changes from subproject" sub2 &&
171 check_equal ''"$(last_commit_message)"'' "Merged changes from subproject" &&
175 test_expect_success 'check if --message for merge works with squash too' '
176 git subtree merge --prefix subdir -m "Merged changes from subproject using squash" --squash sub2 &&
177 check_equal ''"$(last_commit_message)"'' "Merged changes from subproject using squash" &&
181 test_expect_success 'merge new subproj history into subdir' '
182 git subtree merge --prefix=subdir FETCH_HEAD &&
183 git branch pre-split &&
184 check_equal ''"$(last_commit_message)"'' "Merge commit '"'"'"$(git rev-parse sub2)"'"'"' into mainline" &&
188 test_expect_success 'Check that prefix argument is required for split' '
189 echo "You must provide the --prefix option." > expected &&
190 test_must_fail git subtree split > actual 2>&1 &&
191 test_debug "printf '"'"'expected: '"'"'" &&
192 test_debug "cat expected" &&
193 test_debug "printf '"'"'actual: '"'"'" &&
194 test_debug "cat actual" &&
195 test_cmp expected actual &&
196 rm -f expected actual
199 test_expect_success 'Check that the <prefix> exists for a split' '
200 echo "'"'"'non-existent-directory'"'"'" does not exist\; use "'"'"'git subtree add'"'"'" > expected &&
201 test_must_fail git subtree split --prefix=non-existent-directory > actual 2>&1 &&
202 test_debug "printf '"'"'expected: '"'"'" &&
203 test_debug "cat expected" &&
204 test_debug "printf '"'"'actual: '"'"'" &&
205 test_debug "cat actual" &&
206 test_cmp expected actual
207 # rm -f expected actual
210 test_expect_success 'check if --message works for split+rejoin' '
211 spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
212 git branch spl1 "$spl1" &&
213 check_equal ''"$(last_commit_message)"'' "Split & rejoin" &&
217 test_expect_success 'check split with --branch' '
218 spl1=$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin) &&
220 git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --branch splitbr1 &&
221 check_equal ''"$(git rev-parse splitbr1)"'' "$spl1"
224 test_expect_success 'check hash of split' '
225 spl1=$(git subtree split --prefix subdir) &&
226 git subtree split --prefix subdir --branch splitbr1test &&
227 check_equal ''"$(git rev-parse splitbr1test)"'' "$spl1" &&
228 new_hash=$(git rev-parse splitbr1test~2) &&
229 check_equal ''"$new_hash"'' "$subdir_hash"
232 test_expect_success 'check split with --branch for an existing branch' '
233 spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
235 git branch splitbr2 sub1 &&
236 git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --branch splitbr2 &&
237 check_equal ''"$(git rev-parse splitbr2)"'' "$spl1"
240 test_expect_success 'check split with --branch for an incompatible branch' '
241 test_must_fail git subtree split --prefix subdir --onto FETCH_HEAD --branch subdir
244 test_expect_success 'check split+rejoin' '
245 spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
247 git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --rejoin &&
248 check_equal ''"$(last_commit_message)"'' "Split '"'"'subdir/'"'"' into commit '"'"'"$spl1"'"'"'"
251 test_expect_success 'add main-sub8' '
252 create subdir/main-sub8 &&
253 git commit -m "main-sub8"
259 test_expect_success 'merge split into subproj' '
261 git branch spl1 FETCH_HEAD &&
265 test_expect_success 'add sub9' '
273 test_expect_success 'split for sub8' '
274 split2=''"$(git subtree split --annotate='"'*'"' --prefix subdir/ --rejoin)"'' &&
275 git branch split2 "$split2"
278 test_expect_success 'add main-sub10' '
279 create subdir/main-sub10 &&
280 git commit -m "main-sub10"
283 test_expect_success 'split for sub10' '
284 spl3=''"$(git subtree split --annotate='"'*'"' --prefix subdir --rejoin)"'' &&
285 git branch spl3 "$spl3"
291 test_expect_success 'merge split into subproj' '
293 git branch spl3 FETCH_HEAD &&
294 git merge FETCH_HEAD &&
295 git branch subproj-merge-spl3
299 chkms="main-sub10 main-sub5 main-sub7 main-sub8"
300 chkms_sub=$(echo $chkms | multiline | sed 's,^,subdir/,' | fixnl)
301 chks="sub1 sub2 sub3 sub9"
302 chks_sub=$(echo $chks | multiline | sed 's,^,subdir/,' | fixnl)
304 test_expect_success 'make sure exactly the right set of files ends up in the subproj' '
305 subfiles=''"$(git ls-files | fixnl)"'' &&
306 check_equal "$subfiles" "$chkms $chks"
309 test_expect_success 'make sure the subproj history *only* contains commits that affect the subdir' '
310 allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | fixnl)"'' &&
311 check_equal "$allchanges" "$chkms $chks"
317 test_expect_success 'pull from subproj' '
318 git fetch ./subproj subproj-merge-spl3 &&
319 git branch subproj-merge-spl3 FETCH_HEAD &&
320 git subtree pull --prefix=subdir ./subproj subproj-merge-spl3
323 test_expect_success 'make sure exactly the right set of files ends up in the mainline' '
324 mainfiles=''"$(git ls-files | fixnl)"'' &&
325 check_equal "$mainfiles" "$chkm $chkms_sub $chks_sub"
328 test_expect_success 'make sure each filename changed exactly once in the entire history' '
329 # main-sub?? and /subdir/main-sub?? both change, because those are the
330 # changes that were split into their own history. And subdir/sub?? never
331 # change, since they were *only* changed in the subtree branch.
332 allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | fixnl)"'' &&
333 check_equal "$allchanges" ''"$(echo $chkms $chkm $chks $chkms_sub | multiline | sort | fixnl)"''
336 test_expect_success 'make sure the --rejoin commits never make it into subproj' '
337 check_equal ''"$(git log --pretty=format:'"'%s'"' HEAD^2 | grep -i split)"'' ""
340 test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' '
341 # They are meaningless to subproj since one side of the merge refers to the mainline
342 check_equal ''"$(git log --pretty=format:'"'%s%n%b'"' HEAD^2 | grep "git-subtree.*:")"'' ""
345 # prepare second pair of repositories
349 test_expect_success 'init main' '
350 test_create_repo main
355 test_expect_success 'add main1' '
357 git commit -m "main1"
362 test_expect_success 'init sub' '
368 test_expect_success 'add sub2' '
375 # check if split can find proper base without --onto
377 test_expect_success 'add sub as subdir in main' '
378 git fetch ../sub master &&
379 git branch sub2 FETCH_HEAD &&
380 git subtree add --prefix subdir sub2
385 test_expect_success 'add sub3' '
392 test_expect_success 'merge from sub' '
393 git fetch ../sub master &&
394 git branch sub3 FETCH_HEAD &&
395 git subtree merge --prefix subdir sub3
398 test_expect_success 'add main-sub4' '
399 create subdir/main-sub4 &&
400 git commit -m "main-sub4"
403 test_expect_success 'split for main-sub4 without --onto' '
404 git subtree split --prefix subdir --branch mainsub4
407 # at this point, the new commit parent should be sub3 if it is not,
408 # something went wrong (the "newparent" of "master~" commit should
409 # have been sub3, but it was not, because its cache was not set to
412 test_expect_success 'check that the commit parent is sub3' '
413 check_equal ''"$(git log --pretty=format:%P -1 mainsub4)"'' ''"$(git rev-parse sub3)"''
416 test_expect_success 'add main-sub5' '
418 create subdir2/main-sub5 &&
419 git commit -m "main-sub5"
422 test_expect_success 'split for main-sub5 without --onto' '
423 # also test that we still can split out an entirely new subtree
424 # if the parent of the first commit in the tree is not empty,
425 # then the new subtree has accidentally been attached to something
426 git subtree split --prefix subdir2 --branch mainsub5 &&
427 check_equal ''"$(git log --pretty=format:%P -1 mainsub5)"'' ""
430 # make sure no patch changes more than one file. The original set of commits
431 # changed only one file each. A multi-file change would imply that we pruned
432 # commits too aggressively.
441 elif [ "$x" = "commit:" ]; then
442 if [ -n "$commit" ]; then
454 test_expect_success 'verify one file change per commit' '
456 list=''"$(git log --pretty=format:'"'commit: %H'"' | joincommits)"'' &&
457 # test_debug "echo HERE" &&
458 # test_debug "echo ''"$list"''" &&
459 (git log --pretty=format:'"'commit: %H'"' | joincommits |
460 ( while read commit a b; do
461 test_debug "echo Verifying commit "''"$commit"''
462 test_debug "echo a: "''"$a"''
463 test_debug "echo b: "''"$b"''