contrib/subtree: ignore log.date configuration
[git/debian.git] / contrib / subtree / t / t7900-subtree.sh
blobda709f96e57d3395b976968048a12bee005efcb9
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 'enable log.date=relative to catch errors' '
98 git config log.date relative
101 test_expect_success 'add main4' '
102 create main4 &&
103 git commit -m "main4" &&
104 git branch -m master mainline &&
105 git branch subdir
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" &&
124 undo
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" &&
130 undo
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" &&
136 undo
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' '
155 create 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" &&
172 undo
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" &&
178 undo
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"
187 test_expect_success 'Check that prefix argument is required for split' '
188 echo "You must provide the --prefix option." > expected &&
189 test_must_fail git subtree split > actual 2>&1 &&
190 test_debug "printf '"'"'expected: '"'"'" &&
191 test_debug "cat expected" &&
192 test_debug "printf '"'"'actual: '"'"'" &&
193 test_debug "cat actual" &&
194 test_cmp expected actual &&
195 rm -f expected actual
198 test_expect_success 'Check that the <prefix> exists for a split' '
199 echo "'"'"'non-existent-directory'"'"'" does not exist\; use "'"'"'git subtree add'"'"'" > expected &&
200 test_must_fail git subtree split --prefix=non-existent-directory > actual 2>&1 &&
201 test_debug "printf '"'"'expected: '"'"'" &&
202 test_debug "cat expected" &&
203 test_debug "printf '"'"'actual: '"'"'" &&
204 test_debug "cat actual" &&
205 test_cmp expected actual
206 # rm -f expected actual
209 test_expect_success 'check if --message works for split+rejoin' '
210 spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
211 git branch spl1 "$spl1" &&
212 check_equal ''"$(last_commit_message)"'' "Split & rejoin" &&
213 undo
216 test_expect_success 'check split with --branch' '
217 spl1=$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin) &&
218 undo &&
219 git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --branch splitbr1 &&
220 check_equal ''"$(git rev-parse splitbr1)"'' "$spl1"
223 test_expect_success 'check hash of split' '
224 spl1=$(git subtree split --prefix subdir) &&
225 undo &&
226 git subtree split --prefix subdir --branch splitbr1test &&
227 check_equal ''"$(git rev-parse splitbr1test)"'' "$spl1"
228 git checkout splitbr1test &&
229 new_hash=$(git rev-parse HEAD~2) &&
230 git checkout mainline &&
231 check_equal ''"$new_hash"'' "$subdir_hash"
234 test_expect_success 'check split with --branch for an existing branch' '
235 spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
236 undo &&
237 git branch splitbr2 sub1 &&
238 git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --branch splitbr2 &&
239 check_equal ''"$(git rev-parse splitbr2)"'' "$spl1"
242 test_expect_success 'check split with --branch for an incompatible branch' '
243 test_must_fail git subtree split --prefix subdir --onto FETCH_HEAD --branch subdir
246 test_expect_success 'check split+rejoin' '
247 spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
248 undo &&
249 git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --rejoin &&
250 check_equal ''"$(last_commit_message)"'' "Split '"'"'subdir/'"'"' into commit '"'"'"$spl1"'"'"'"
253 test_expect_success 'add main-sub8' '
254 create subdir/main-sub8 &&
255 git commit -m "main-sub8"
258 # To the subproject!
259 cd ./subproj
261 test_expect_success 'merge split into subproj' '
262 git fetch .. spl1 &&
263 git branch spl1 FETCH_HEAD &&
264 git merge FETCH_HEAD
267 test_expect_success 'add sub9' '
268 create sub9 &&
269 git commit -m "sub9"
272 # Back to mainline
273 cd ..
275 test_expect_success 'split for sub8' '
276 split2=''"$(git subtree split --annotate='"'*'"' --prefix subdir/ --rejoin)"''
277 git branch split2 "$split2"
280 test_expect_success 'add main-sub10' '
281 create subdir/main-sub10 &&
282 git commit -m "main-sub10"
285 test_expect_success 'split for sub10' '
286 spl3=''"$(git subtree split --annotate='"'*'"' --prefix subdir --rejoin)"'' &&
287 git branch spl3 "$spl3"
290 # To the subproject!
291 cd ./subproj
293 test_expect_success 'merge split into subproj' '
294 git fetch .. spl3 &&
295 git branch spl3 FETCH_HEAD &&
296 git merge FETCH_HEAD &&
297 git branch subproj-merge-spl3
300 chkm="main4 main6"
301 chkms="main-sub10 main-sub5 main-sub7 main-sub8"
302 chkms_sub=$(echo $chkms | multiline | sed 's,^,subdir/,' | fixnl)
303 chks="sub1 sub2 sub3 sub9"
304 chks_sub=$(echo $chks | multiline | sed 's,^,subdir/,' | fixnl)
306 test_expect_success 'make sure exactly the right set of files ends up in the subproj' '
307 subfiles=''"$(git ls-files | fixnl)"'' &&
308 check_equal "$subfiles" "$chkms $chks"
311 test_expect_success 'make sure the subproj history *only* contains commits that affect the subdir' '
312 allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | fixnl)"'' &&
313 check_equal "$allchanges" "$chkms $chks"
316 # Back to mainline
317 cd ..
319 test_expect_success 'pull from subproj' '
320 git fetch ./subproj subproj-merge-spl3 &&
321 git branch subproj-merge-spl3 FETCH_HEAD &&
322 git subtree pull --prefix=subdir ./subproj subproj-merge-spl3
325 test_expect_success 'make sure exactly the right set of files ends up in the mainline' '
326 mainfiles=''"$(git ls-files | fixnl)"'' &&
327 check_equal "$mainfiles" "$chkm $chkms_sub $chks_sub"
330 test_expect_success 'make sure each filename changed exactly once in the entire history' '
331 # main-sub?? and /subdir/main-sub?? both change, because those are the
332 # changes that were split into their own history. And subdir/sub?? never
333 # change, since they were *only* changed in the subtree branch.
334 allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | fixnl)"'' &&
335 check_equal "$allchanges" ''"$(echo $chkms $chkm $chks $chkms_sub | multiline | sort | fixnl)"''
338 test_expect_success 'make sure the --rejoin commits never make it into subproj' '
339 check_equal ''"$(git log --pretty=format:'"'%s'"' HEAD^2 | grep -i split)"'' ""
342 test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' '
343 # They are meaningless to subproj since one side of the merge refers to the mainline
344 check_equal ''"$(git log --pretty=format:'"'%s%n%b'"' HEAD^2 | grep "git-subtree.*:")"'' ""
347 # prepare second pair of repositories
348 mkdir test2
349 cd test2
351 test_expect_success 'init main' '
352 test_create_repo main
355 cd main
357 test_expect_success 'add main1' '
358 create main1 &&
359 git commit -m "main1"
362 cd ..
364 test_expect_success 'init sub' '
365 test_create_repo sub
368 cd sub
370 test_expect_success 'add sub2' '
371 create sub2 &&
372 git commit -m "sub2"
375 cd ../main
377 # check if split can find proper base without --onto
379 test_expect_success 'add sub as subdir in main' '
380 git fetch ../sub master &&
381 git branch sub2 FETCH_HEAD &&
382 git subtree add --prefix subdir sub2
385 cd ../sub
387 test_expect_success 'add sub3' '
388 create sub3 &&
389 git commit -m "sub3"
392 cd ../main
394 test_expect_success 'merge from sub' '
395 git fetch ../sub master &&
396 git branch sub3 FETCH_HEAD &&
397 git subtree merge --prefix subdir sub3
400 test_expect_success 'add main-sub4' '
401 create subdir/main-sub4 &&
402 git commit -m "main-sub4"
405 test_expect_success 'split for main-sub4 without --onto' '
406 git subtree split --prefix subdir --branch mainsub4
409 # at this point, the new commit parent should be sub3 if it is not,
410 # something went wrong (the "newparent" of "master~" commit should
411 # have been sub3, but it was not, because its cache was not set to
412 # itself)
414 test_expect_success 'check that the commit parent is sub3' '
415 check_equal ''"$(git log --pretty=format:%P -1 mainsub4)"'' ''"$(git rev-parse sub3)"''
418 test_expect_success 'add main-sub5' '
419 mkdir subdir2 &&
420 create subdir2/main-sub5 &&
421 git commit -m "main-sub5"
424 test_expect_success 'split for main-sub5 without --onto' '
425 # also test that we still can split out an entirely new subtree
426 # if the parent of the first commit in the tree is not empty,
427 # then the new subtree has accidentally been attached to something
428 git subtree split --prefix subdir2 --branch mainsub5 &&
429 check_equal ''"$(git log --pretty=format:%P -1 mainsub5)"'' ""
432 # make sure no patch changes more than one file. The original set of commits
433 # changed only one file each. A multi-file change would imply that we pruned
434 # commits too aggressively.
435 joincommits()
437 commit=
438 all=
439 while read x y; do
440 #echo "{$x}" >&2
441 if [ -z "$x" ]; then
442 continue
443 elif [ "$x" = "commit:" ]; then
444 if [ -n "$commit" ]; then
445 echo "$commit $all"
446 all=
448 commit="$y"
449 else
450 all="$all $y"
452 done
453 echo "$commit $all"
456 test_expect_success 'verify one file change per commit' '
457 x= &&
458 list=''"$(git log --pretty=format:'"'commit: %H'"' | joincommits)"'' &&
459 # test_debug "echo HERE" &&
460 # test_debug "echo ''"$list"''" &&
461 (git log --pretty=format:'"'commit: %H'"' | joincommits |
462 ( while read commit a b; do
463 test_debug "echo Verifying commit "''"$commit"''
464 test_debug "echo a: "''"$a"''
465 test_debug "echo b: "''"$b"''
466 check_equal "$b" ""
468 done
469 check_equal "$x" 1
473 test_done