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
63 test_expect_success 'init subproj' '
64 test_create_repo subproj
70 test_expect_success 'add sub1' '
72 git commit -m "sub1" &&
74 git branch -m master subproj
77 # Save this hash for testing later.
79 subdir_hash=$(git rev-parse HEAD)
81 test_expect_success 'add sub2' '
83 git commit -m "sub2" &&
87 test_expect_success 'add sub3' '
89 git commit -m "sub3" &&
96 test_expect_success 'add main4' '
98 git commit -m "main4" &&
99 git branch -m master mainline &&
103 test_expect_success 'fetch subproj history' '
104 git fetch ./subproj sub1 &&
105 git branch sub1 FETCH_HEAD
108 test_expect_success 'no subtree exists in main tree' '
109 test_must_fail git subtree merge --prefix=subdir sub1
112 test_expect_success 'no pull from non-existant subtree' '
113 test_must_fail git subtree pull --prefix=subdir ./subproj sub1
116 test_expect_success 'check if --message works for add' '
117 git subtree add --prefix=subdir --message="Added subproject" sub1 &&
118 check_equal ''"$(last_commit_message)"'' "Added subproject" &&
122 test_expect_success 'check if --message works as -m and --prefix as -P' '
123 git subtree add -P subdir -m "Added subproject using git subtree" sub1 &&
124 check_equal ''"$(last_commit_message)"'' "Added subproject using git subtree" &&
128 test_expect_success 'check if --message works with squash too' '
129 git subtree add -P subdir -m "Added subproject with squash" --squash sub1 &&
130 check_equal ''"$(last_commit_message)"'' "Added subproject with squash" &&
134 test_expect_success 'add subproj to mainline' '
135 git subtree add --prefix=subdir/ FETCH_HEAD &&
136 check_equal ''"$(last_commit_message)"'' "Add '"'subdir/'"' from commit '"'"'''"$(git rev-parse sub1)"'''"'"'"
139 # this shouldn't actually do anything, since FETCH_HEAD is already a parent
140 test_expect_success 'merge fetched subproj' '
141 git merge -m "merge -s -ours" -s ours FETCH_HEAD
144 test_expect_success 'add main-sub5' '
145 create subdir/main-sub5 &&
146 git commit -m "main-sub5"
149 test_expect_success 'add main6' '
151 git commit -m "main6 boring"
154 test_expect_success 'add main-sub7' '
155 create subdir/main-sub7 &&
156 git commit -m "main-sub7"
159 test_expect_success 'fetch new subproj history' '
160 git fetch ./subproj sub2 &&
161 git branch sub2 FETCH_HEAD
164 test_expect_success 'check if --message works for merge' '
165 git subtree merge --prefix=subdir -m "Merged changes from subproject" sub2 &&
166 check_equal ''"$(last_commit_message)"'' "Merged changes from subproject" &&
170 test_expect_success 'check if --message for merge works with squash too' '
171 git subtree merge --prefix subdir -m "Merged changes from subproject using squash" --squash sub2 &&
172 check_equal ''"$(last_commit_message)"'' "Merged changes from subproject using squash" &&
176 test_expect_success 'merge new subproj history into subdir' '
177 git subtree merge --prefix=subdir FETCH_HEAD &&
178 git branch pre-split &&
179 check_equal ''"$(last_commit_message)"'' "Merge commit '"'"'"$(git rev-parse sub2)"'"'"' into mainline"
182 test_expect_success 'Check that prefix argument is required for split' '
183 echo "You must provide the --prefix option." > expected &&
184 test_must_fail git subtree split > actual 2>&1 &&
185 test_debug "printf '"'"'expected: '"'"'" &&
186 test_debug "cat expected" &&
187 test_debug "printf '"'"'actual: '"'"'" &&
188 test_debug "cat actual" &&
189 test_cmp expected actual &&
190 rm -f expected actual
193 test_expect_success 'Check that the <prefix> exists for a split' '
194 echo "'"'"'non-existent-directory'"'"'" does not exist\; use "'"'"'git subtree add'"'"'" > expected &&
195 test_must_fail git subtree split --prefix=non-existent-directory > actual 2>&1 &&
196 test_debug "printf '"'"'expected: '"'"'" &&
197 test_debug "cat expected" &&
198 test_debug "printf '"'"'actual: '"'"'" &&
199 test_debug "cat actual" &&
200 test_cmp expected actual
201 # rm -f expected actual
204 test_expect_success 'check if --message works for split+rejoin' '
205 spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
206 git branch spl1 "$spl1" &&
207 check_equal ''"$(last_commit_message)"'' "Split & rejoin" &&
211 test_expect_success 'check split with --branch' '
212 spl1=$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin) &&
214 git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --branch splitbr1 &&
215 check_equal ''"$(git rev-parse splitbr1)"'' "$spl1"
218 test_expect_success 'check hash of split' '
219 spl1=$(git subtree split --prefix subdir) &&
221 git subtree split --prefix subdir --branch splitbr1test &&
222 check_equal ''"$(git rev-parse splitbr1test)"'' "$spl1"
223 git checkout splitbr1test &&
224 new_hash=$(git rev-parse HEAD~2) &&
225 git checkout mainline &&
226 check_equal ''"$new_hash"'' "$subdir_hash"
229 test_expect_success 'check split with --branch for an existing branch' '
230 spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
232 git branch splitbr2 sub1 &&
233 git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --branch splitbr2 &&
234 check_equal ''"$(git rev-parse splitbr2)"'' "$spl1"
237 test_expect_success 'check split with --branch for an incompatible branch' '
238 test_must_fail git subtree split --prefix subdir --onto FETCH_HEAD --branch subdir
241 test_expect_success 'check split+rejoin' '
242 spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
244 git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --rejoin &&
245 check_equal ''"$(last_commit_message)"'' "Split '"'"'subdir/'"'"' into commit '"'"'"$spl1"'"'"'"
248 test_expect_success 'add main-sub8' '
249 create subdir/main-sub8 &&
250 git commit -m "main-sub8"
256 test_expect_success 'merge split into subproj' '
258 git branch spl1 FETCH_HEAD &&
262 test_expect_success 'add sub9' '
270 test_expect_success 'split for sub8' '
271 split2=''"$(git subtree split --annotate='"'*'"' --prefix subdir/ --rejoin)"''
272 git branch split2 "$split2"
275 test_expect_success 'add main-sub10' '
276 create subdir/main-sub10 &&
277 git commit -m "main-sub10"
280 test_expect_success 'split for sub10' '
281 spl3=''"$(git subtree split --annotate='"'*'"' --prefix subdir --rejoin)"'' &&
282 git branch spl3 "$spl3"
288 test_expect_success 'merge split into subproj' '
290 git branch spl3 FETCH_HEAD &&
291 git merge FETCH_HEAD &&
292 git branch subproj-merge-spl3
296 chkms="main-sub10 main-sub5 main-sub7 main-sub8"
297 chkms_sub=$(echo $chkms | multiline | sed 's,^,subdir/,' | fixnl)
298 chks="sub1 sub2 sub3 sub9"
299 chks_sub=$(echo $chks | multiline | sed 's,^,subdir/,' | fixnl)
301 test_expect_success 'make sure exactly the right set of files ends up in the subproj' '
302 subfiles=''"$(git ls-files | fixnl)"'' &&
303 check_equal "$subfiles" "$chkms $chks"
306 test_expect_success 'make sure the subproj history *only* contains commits that affect the subdir' '
307 allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | fixnl)"'' &&
308 check_equal "$allchanges" "$chkms $chks"
314 test_expect_success 'pull from subproj' '
315 git fetch ./subproj subproj-merge-spl3 &&
316 git branch subproj-merge-spl3 FETCH_HEAD &&
317 git subtree pull --prefix=subdir ./subproj subproj-merge-spl3
320 test_expect_success 'make sure exactly the right set of files ends up in the mainline' '
321 mainfiles=''"$(git ls-files | fixnl)"'' &&
322 check_equal "$mainfiles" "$chkm $chkms_sub $chks_sub"
325 test_expect_success 'make sure each filename changed exactly once in the entire history' '
326 # main-sub?? and /subdir/main-sub?? both change, because those are the
327 # changes that were split into their own history. And subdir/sub?? never
328 # change, since they were *only* changed in the subtree branch.
329 allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | fixnl)"'' &&
330 check_equal "$allchanges" ''"$(echo $chkms $chkm $chks $chkms_sub | multiline | sort | fixnl)"''
333 test_expect_success 'make sure the --rejoin commits never make it into subproj' '
334 check_equal ''"$(git log --pretty=format:'"'%s'"' HEAD^2 | grep -i split)"'' ""
337 test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' '
338 # They are meaningless to subproj since one side of the merge refers to the mainline
339 check_equal ''"$(git log --pretty=format:'"'%s%n%b'"' HEAD^2 | grep "git-subtree.*:")"'' ""
342 # prepare second pair of repositories
346 test_expect_success 'init main' '
347 test_create_repo main
352 test_expect_success 'add main1' '
354 git commit -m "main1"
359 test_expect_success 'init sub' '
365 test_expect_success 'add sub2' '
372 # check if split can find proper base without --onto
374 test_expect_success 'add sub as subdir in main' '
375 git fetch ../sub master &&
376 git branch sub2 FETCH_HEAD &&
377 git subtree add --prefix subdir sub2
382 test_expect_success 'add sub3' '
389 test_expect_success 'merge from sub' '
390 git fetch ../sub master &&
391 git branch sub3 FETCH_HEAD &&
392 git subtree merge --prefix subdir sub3
395 test_expect_success 'add main-sub4' '
396 create subdir/main-sub4 &&
397 git commit -m "main-sub4"
400 test_expect_success 'split for main-sub4 without --onto' '
401 git subtree split --prefix subdir --branch mainsub4
404 # at this point, the new commit parent should be sub3 if it is not,
405 # something went wrong (the "newparent" of "master~" commit should
406 # have been sub3, but it was not, because its cache was not set to
409 test_expect_success 'check that the commit parent is sub3' '
410 check_equal ''"$(git log --pretty=format:%P -1 mainsub4)"'' ''"$(git rev-parse sub3)"''
413 test_expect_success 'add main-sub5' '
415 create subdir2/main-sub5 &&
416 git commit -m "main-sub5"
419 test_expect_success 'split for main-sub5 without --onto' '
420 # also test that we still can split out an entirely new subtree
421 # if the parent of the first commit in the tree is not empty,
422 # then the new subtree has accidentally been attached to something
423 git subtree split --prefix subdir2 --branch mainsub5 &&
424 check_equal ''"$(git log --pretty=format:%P -1 mainsub5)"'' ""
427 # make sure no patch changes more than one file. The original set of commits
428 # changed only one file each. A multi-file change would imply that we pruned
429 # commits too aggressively.
438 elif [ "$x" = "commit:" ]; then
439 if [ -n "$commit" ]; then
451 test_expect_success 'verify one file change per commit' '
453 list=''"$(git log --pretty=format:'"'commit: %H'"' | joincommits)"'' &&
454 # test_debug "echo HERE" &&
455 # test_debug "echo ''"$list"''" &&
456 (git log --pretty=format:'"'commit: %H'"' | joincommits |
457 ( while read commit a b; do
458 test_debug "echo Verifying commit "''"$commit"''
459 test_debug "echo a: "''"$a"''
460 test_debug "echo b: "''"$b"''