contrib/subtree: fix spelling of accidentally
[alt-git.git] / contrib / subtree / t / t7900-subtree.sh
blobb0f8536fca58d910f85334727b89925651bfa8ae
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 export TEST_DIRECTORY=$(pwd)/../../../t
13 . ../../../t/test-lib.sh
15 create()
17 echo "$1" >"$1"
18 git add "$1"
22 check_equal()
24 test_debug 'echo'
25 test_debug "echo \"check a:\" \"{$1}\""
26 test_debug "echo \" b:\" \"{$2}\""
27 if [ "$1" = "$2" ]; then
28 return 0
29 else
30 return 1
34 fixnl()
36 t=""
37 while read x; do
38 t="$t$x "
39 done
40 echo $t
43 multiline()
45 while read x; do
46 set -- $x
47 for d in "$@"; do
48 echo "$d"
49 done
50 done
53 undo()
55 git reset --hard HEAD~
58 last_commit_message()
60 git log --pretty=format:%s -1
63 test_expect_success 'init subproj' '
64 test_create_repo subproj
67 # To the subproject!
68 cd subproj
70 test_expect_success 'add sub1' '
71 create sub1 &&
72 git commit -m "sub1" &&
73 git branch 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' '
82 create sub2 &&
83 git commit -m "sub2" &&
84 git branch sub2
87 test_expect_success 'add sub3' '
88 create sub3 &&
89 git commit -m "sub3" &&
90 git branch sub3
93 # Back to mainline
94 cd ..
96 test_expect_success 'add main4' '
97 create main4 &&
98 git commit -m "main4" &&
99 git branch -m master mainline &&
100 git branch subdir
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" &&
119 undo
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" &&
125 undo
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" &&
131 undo
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' '
150 create 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" &&
167 undo
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" &&
173 undo
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 "echo -n expected: " &&
186 test_debug "cat expected" &&
187 test_debug "echo -n 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 "echo -n expected: " &&
197 test_debug "cat expected" &&
198 test_debug "echo -n 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" &&
208 undo
211 test_expect_success 'check split with --branch' '
212 spl1=$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin) &&
213 undo &&
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) &&
220 undo &&
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)"'' &&
231 undo &&
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)"'' &&
243 undo &&
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"
253 # To the subproject!
254 cd ./subproj
256 test_expect_success 'merge split into subproj' '
257 git fetch .. spl1 &&
258 git branch spl1 FETCH_HEAD &&
259 git merge FETCH_HEAD
262 test_expect_success 'add sub9' '
263 create sub9 &&
264 git commit -m "sub9"
267 # Back to mainline
268 cd ..
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"
285 # To the subproject!
286 cd ./subproj
288 test_expect_success 'merge split into subproj' '
289 git fetch .. spl3 &&
290 git branch spl3 FETCH_HEAD &&
291 git merge FETCH_HEAD &&
292 git branch subproj-merge-spl3
295 chkm="main4 main6"
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"
311 # Back to mainline
312 cd ..
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
343 mkdir test2
344 cd test2
346 test_expect_success 'init main' '
347 test_create_repo main
350 cd main
352 test_expect_success 'add main1' '
353 create main1 &&
354 git commit -m "main1"
357 cd ..
359 test_expect_success 'init sub' '
360 test_create_repo sub
363 cd sub
365 test_expect_success 'add sub2' '
366 create sub2 &&
367 git commit -m "sub2"
370 cd ../main
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
380 cd ../sub
382 test_expect_success 'add sub3' '
383 create sub3 &&
384 git commit -m "sub3"
387 cd ../main
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
407 # itself)
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' '
414 mkdir subdir2 &&
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.
430 joincommits()
432 commit=
433 all=
434 while read x y; do
435 #echo "{$x}" >&2
436 if [ -z "$x" ]; then
437 continue
438 elif [ "$x" = "commit:" ]; then
439 if [ -n "$commit" ]; then
440 echo "$commit $all"
441 all=
443 commit="$y"
444 else
445 all="$all $y"
447 done
448 echo "$commit $all"
451 test_expect_success 'verify one file change per commit' '
452 x= &&
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"''
461 check_equal "$b" ""
463 done
464 check_equal "$x" 1
468 test_done