Merge branch 'ep/shell-assign-and-export-vars'
[git.git] / contrib / subtree / t / t7900-subtree.sh
blob6309d124ca2c97b2b1b9262c7b9470c576870069
1 #!/bin/sh
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
12 export TEST_DIRECTORY
14 . ../../../t/test-lib.sh
16 create()
18 echo "$1" >"$1"
19 git add "$1"
23 check_equal()
25 test_debug 'echo'
26 test_debug "echo \"check a:\" \"{$1}\""
27 test_debug "echo \" b:\" \"{$2}\""
28 if [ "$1" = "$2" ]; then
29 return 0
30 else
31 return 1
35 fixnl()
37 t=""
38 while read x; do
39 t="$t$x "
40 done
41 echo $t
44 multiline()
46 while read x; do
47 set -- $x
48 for d in "$@"; do
49 echo "$d"
50 done
51 done
54 undo()
56 git reset --hard HEAD~
59 last_commit_message()
61 git log --pretty=format:%s -1
64 test_expect_success 'init subproj' '
65 test_create_repo subproj
68 # To the subproject!
69 cd subproj
71 test_expect_success 'add sub1' '
72 create sub1 &&
73 git commit -m "sub1" &&
74 git branch 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' '
83 create sub2 &&
84 git commit -m "sub2" &&
85 git branch sub2
88 test_expect_success 'add sub3' '
89 create sub3 &&
90 git commit -m "sub3" &&
91 git branch sub3
94 # Back to mainline
95 cd ..
97 test_expect_success 'add main4' '
98 create main4 &&
99 git commit -m "main4" &&
100 git branch -m master mainline &&
101 git branch subdir
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" &&
120 undo
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" &&
126 undo
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" &&
132 undo
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' '
151 create 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" &&
168 undo
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" &&
174 undo
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"
183 test_expect_success 'Check that prefix argument is required for split' '
184 echo "You must provide the --prefix option." > expected &&
185 test_must_fail git subtree split > actual 2>&1 &&
186 test_debug "printf '"'"'expected: '"'"'" &&
187 test_debug "cat expected" &&
188 test_debug "printf '"'"'actual: '"'"'" &&
189 test_debug "cat actual" &&
190 test_cmp expected actual &&
191 rm -f expected actual
194 test_expect_success 'Check that the <prefix> exists for a split' '
195 echo "'"'"'non-existent-directory'"'"'" does not exist\; use "'"'"'git subtree add'"'"'" > expected &&
196 test_must_fail git subtree split --prefix=non-existent-directory > actual 2>&1 &&
197 test_debug "printf '"'"'expected: '"'"'" &&
198 test_debug "cat expected" &&
199 test_debug "printf '"'"'actual: '"'"'" &&
200 test_debug "cat actual" &&
201 test_cmp expected actual
202 # rm -f expected actual
205 test_expect_success 'check if --message works for split+rejoin' '
206 spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
207 git branch spl1 "$spl1" &&
208 check_equal ''"$(last_commit_message)"'' "Split & rejoin" &&
209 undo
212 test_expect_success 'check split with --branch' '
213 spl1=$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin) &&
214 undo &&
215 git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --branch splitbr1 &&
216 check_equal ''"$(git rev-parse splitbr1)"'' "$spl1"
219 test_expect_success 'check hash of split' '
220 spl1=$(git subtree split --prefix subdir) &&
221 undo &&
222 git subtree split --prefix subdir --branch splitbr1test &&
223 check_equal ''"$(git rev-parse splitbr1test)"'' "$spl1"
224 git checkout splitbr1test &&
225 new_hash=$(git rev-parse HEAD~2) &&
226 git checkout mainline &&
227 check_equal ''"$new_hash"'' "$subdir_hash"
230 test_expect_success 'check split with --branch for an existing branch' '
231 spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
232 undo &&
233 git branch splitbr2 sub1 &&
234 git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --branch splitbr2 &&
235 check_equal ''"$(git rev-parse splitbr2)"'' "$spl1"
238 test_expect_success 'check split with --branch for an incompatible branch' '
239 test_must_fail git subtree split --prefix subdir --onto FETCH_HEAD --branch subdir
242 test_expect_success 'check split+rejoin' '
243 spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
244 undo &&
245 git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --rejoin &&
246 check_equal ''"$(last_commit_message)"'' "Split '"'"'subdir/'"'"' into commit '"'"'"$spl1"'"'"'"
249 test_expect_success 'add main-sub8' '
250 create subdir/main-sub8 &&
251 git commit -m "main-sub8"
254 # To the subproject!
255 cd ./subproj
257 test_expect_success 'merge split into subproj' '
258 git fetch .. spl1 &&
259 git branch spl1 FETCH_HEAD &&
260 git merge FETCH_HEAD
263 test_expect_success 'add sub9' '
264 create sub9 &&
265 git commit -m "sub9"
268 # Back to mainline
269 cd ..
271 test_expect_success 'split for sub8' '
272 split2=''"$(git subtree split --annotate='"'*'"' --prefix subdir/ --rejoin)"''
273 git branch split2 "$split2"
276 test_expect_success 'add main-sub10' '
277 create subdir/main-sub10 &&
278 git commit -m "main-sub10"
281 test_expect_success 'split for sub10' '
282 spl3=''"$(git subtree split --annotate='"'*'"' --prefix subdir --rejoin)"'' &&
283 git branch spl3 "$spl3"
286 # To the subproject!
287 cd ./subproj
289 test_expect_success 'merge split into subproj' '
290 git fetch .. spl3 &&
291 git branch spl3 FETCH_HEAD &&
292 git merge FETCH_HEAD &&
293 git branch subproj-merge-spl3
296 chkm="main4 main6"
297 chkms="main-sub10 main-sub5 main-sub7 main-sub8"
298 chkms_sub=$(echo $chkms | multiline | sed 's,^,subdir/,' | fixnl)
299 chks="sub1 sub2 sub3 sub9"
300 chks_sub=$(echo $chks | multiline | sed 's,^,subdir/,' | fixnl)
302 test_expect_success 'make sure exactly the right set of files ends up in the subproj' '
303 subfiles=''"$(git ls-files | fixnl)"'' &&
304 check_equal "$subfiles" "$chkms $chks"
307 test_expect_success 'make sure the subproj history *only* contains commits that affect the subdir' '
308 allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | fixnl)"'' &&
309 check_equal "$allchanges" "$chkms $chks"
312 # Back to mainline
313 cd ..
315 test_expect_success 'pull from subproj' '
316 git fetch ./subproj subproj-merge-spl3 &&
317 git branch subproj-merge-spl3 FETCH_HEAD &&
318 git subtree pull --prefix=subdir ./subproj subproj-merge-spl3
321 test_expect_success 'make sure exactly the right set of files ends up in the mainline' '
322 mainfiles=''"$(git ls-files | fixnl)"'' &&
323 check_equal "$mainfiles" "$chkm $chkms_sub $chks_sub"
326 test_expect_success 'make sure each filename changed exactly once in the entire history' '
327 # main-sub?? and /subdir/main-sub?? both change, because those are the
328 # changes that were split into their own history. And subdir/sub?? never
329 # change, since they were *only* changed in the subtree branch.
330 allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | fixnl)"'' &&
331 check_equal "$allchanges" ''"$(echo $chkms $chkm $chks $chkms_sub | multiline | sort | fixnl)"''
334 test_expect_success 'make sure the --rejoin commits never make it into subproj' '
335 check_equal ''"$(git log --pretty=format:'"'%s'"' HEAD^2 | grep -i split)"'' ""
338 test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' '
339 # They are meaningless to subproj since one side of the merge refers to the mainline
340 check_equal ''"$(git log --pretty=format:'"'%s%n%b'"' HEAD^2 | grep "git-subtree.*:")"'' ""
343 # prepare second pair of repositories
344 mkdir test2
345 cd test2
347 test_expect_success 'init main' '
348 test_create_repo main
351 cd main
353 test_expect_success 'add main1' '
354 create main1 &&
355 git commit -m "main1"
358 cd ..
360 test_expect_success 'init sub' '
361 test_create_repo sub
364 cd sub
366 test_expect_success 'add sub2' '
367 create sub2 &&
368 git commit -m "sub2"
371 cd ../main
373 # check if split can find proper base without --onto
375 test_expect_success 'add sub as subdir in main' '
376 git fetch ../sub master &&
377 git branch sub2 FETCH_HEAD &&
378 git subtree add --prefix subdir sub2
381 cd ../sub
383 test_expect_success 'add sub3' '
384 create sub3 &&
385 git commit -m "sub3"
388 cd ../main
390 test_expect_success 'merge from sub' '
391 git fetch ../sub master &&
392 git branch sub3 FETCH_HEAD &&
393 git subtree merge --prefix subdir sub3
396 test_expect_success 'add main-sub4' '
397 create subdir/main-sub4 &&
398 git commit -m "main-sub4"
401 test_expect_success 'split for main-sub4 without --onto' '
402 git subtree split --prefix subdir --branch mainsub4
405 # at this point, the new commit parent should be sub3 if it is not,
406 # something went wrong (the "newparent" of "master~" commit should
407 # have been sub3, but it was not, because its cache was not set to
408 # itself)
410 test_expect_success 'check that the commit parent is sub3' '
411 check_equal ''"$(git log --pretty=format:%P -1 mainsub4)"'' ''"$(git rev-parse sub3)"''
414 test_expect_success 'add main-sub5' '
415 mkdir subdir2 &&
416 create subdir2/main-sub5 &&
417 git commit -m "main-sub5"
420 test_expect_success 'split for main-sub5 without --onto' '
421 # also test that we still can split out an entirely new subtree
422 # if the parent of the first commit in the tree is not empty,
423 # then the new subtree has accidentally been attached to something
424 git subtree split --prefix subdir2 --branch mainsub5 &&
425 check_equal ''"$(git log --pretty=format:%P -1 mainsub5)"'' ""
428 # make sure no patch changes more than one file. The original set of commits
429 # changed only one file each. A multi-file change would imply that we pruned
430 # commits too aggressively.
431 joincommits()
433 commit=
434 all=
435 while read x y; do
436 #echo "{$x}" >&2
437 if [ -z "$x" ]; then
438 continue
439 elif [ "$x" = "commit:" ]; then
440 if [ -n "$commit" ]; then
441 echo "$commit $all"
442 all=
444 commit="$y"
445 else
446 all="$all $y"
448 done
449 echo "$commit $all"
452 test_expect_success 'verify one file change per commit' '
453 x= &&
454 list=''"$(git log --pretty=format:'"'commit: %H'"' | joincommits)"'' &&
455 # test_debug "echo HERE" &&
456 # test_debug "echo ''"$list"''" &&
457 (git log --pretty=format:'"'commit: %H'"' | joincommits |
458 ( while read commit a b; do
459 test_debug "echo Verifying commit "''"$commit"''
460 test_debug "echo a: "''"$a"''
461 test_debug "echo b: "''"$b"''
462 check_equal "$b" ""
464 done
465 check_equal "$x" 1
469 test_done