Code

Set TEST_DIRECTORY
[git.git] / contrib / subtree / t / t7900-subtree.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2012 Avery Pennaraum
4 #
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.
9 '
11 export TEST_DIRECTORY=$(pwd)/../../../t
13 . ../../../t/test-lib.sh
15 create()
16 {
17         echo "$1" >"$1"
18         git add "$1"
19 }
22 check_equal()
23 {
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
31         fi
32 }
34 fixnl()
35 {
36         t=""
37         while read x; do
38                 t="$t$x "
39         done
40         echo $t
41 }
43 multiline()
44 {
45         while read x; do
46                 set -- $x
47                 for d in "$@"; do
48                         echo "$d"
49                 done
50         done
51 }
53 undo()
54 {
55         git reset --hard HEAD~
56 }
58 last_commit_message()
59 {
60         git log --pretty=format:%s -1
61 }
63 # 1
64 test_expect_success 'init subproj' '
65         test_create_repo subproj
66 '
68 # To the subproject!
69 cd subproj
71 # 2
72 test_expect_success 'add sub1' '
73         create sub1 &&
74         git commit -m "sub1" &&
75         git branch sub1 &&
76         git branch -m master subproj
77 '
79 # 3
80 test_expect_success 'add sub2' '
81         create sub2 &&
82         git commit -m "sub2" &&
83         git branch sub2
84 '
86 # 4
87 test_expect_success 'add sub3' '
88         create sub3 &&
89         git commit -m "sub3" &&
90         git branch sub3
91 '
93 # Back to mainline
94 cd ..
96 # 5
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 # 6
105 test_expect_success 'fetch subproj history' '
106         git fetch ./subproj sub1 &&
107         git branch sub1 FETCH_HEAD
110 # 7
111 test_expect_success 'no subtree exists in main tree' '
112         test_must_fail git subtree merge --prefix=subdir sub1
115 # 8
116 test_expect_success 'no pull from non-existant subtree' '
117         test_must_fail git subtree pull --prefix=subdir ./subproj sub1
120 # 9
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 # 10
128 test_expect_success 'check if --message works as -m and --prefix as -P' '
129         git subtree add -P subdir -m "Added subproject using git subtree" sub1 &&
130         check_equal ''"$(last_commit_message)"'' "Added subproject using git subtree" &&
131         undo
134 # 11
135 test_expect_success 'check if --message works with squash too' '
136         git subtree add -P subdir -m "Added subproject with squash" --squash sub1 &&
137         check_equal ''"$(last_commit_message)"'' "Added subproject with squash" &&
138         undo
141 # 12
142 test_expect_success 'add subproj to mainline' '
143         git subtree add --prefix=subdir/ FETCH_HEAD &&
144         check_equal ''"$(last_commit_message)"'' "Add '"'subdir/'"' from commit '"'"'''"$(git rev-parse sub1)"'''"'"'"
147 # 13
148 # this shouldn't actually do anything, since FETCH_HEAD is already a parent
149 test_expect_success 'merge fetched subproj' '
150         git merge -m "merge -s -ours" -s ours FETCH_HEAD
153 # 14
154 test_expect_success 'add main-sub5' '
155         create subdir/main-sub5 &&
156         git commit -m "main-sub5"
159 # 15
160 test_expect_success 'add main6' '
161         create main6 &&
162         git commit -m "main6 boring"
165 # 16
166 test_expect_success 'add main-sub7' '
167         create subdir/main-sub7 &&
168         git commit -m "main-sub7"
171 # 17
172 test_expect_success 'fetch new subproj history' '
173         git fetch ./subproj sub2 &&
174         git branch sub2 FETCH_HEAD
177 # 18
178 test_expect_success 'check if --message works for merge' '
179         git subtree merge --prefix=subdir -m "Merged changes from subproject" sub2 &&
180         check_equal ''"$(last_commit_message)"'' "Merged changes from subproject" &&
181         undo
184 # 19
185 test_expect_success 'check if --message for merge works with squash too' '
186         git subtree merge --prefix subdir -m "Merged changes from subproject using squash" --squash sub2 &&
187         check_equal ''"$(last_commit_message)"'' "Merged changes from subproject using squash" &&
188         undo
191 # 20
192 test_expect_success 'merge new subproj history into subdir' '
193         git subtree merge --prefix=subdir FETCH_HEAD &&
194         git branch pre-split &&
195         check_equal ''"$(last_commit_message)"'' "Merge commit '"'"'"$(git rev-parse sub2)"'"'"' into mainline"
198 # 21
199 test_expect_success 'Check that prefix argument is required for split' '
200         echo "You must provide the --prefix option." > expected &&
201         test_must_fail git subtree split > actual 2>&1 &&
202         test_debug "echo -n expected: " &&
203         test_debug "cat expected" &&
204         test_debug "echo -n actual: " &&
205         test_debug "cat actual" &&
206         test_cmp expected actual &&
207         rm -f expected actual
210 # 22
211 test_expect_success 'Check that the <prefix> exists for a split' '
212         echo "'"'"'non-existent-directory'"'"'" does not exist\; use "'"'"'git subtree add'"'"'" > expected &&
213         test_must_fail git subtree split --prefix=non-existent-directory > actual 2>&1 &&
214         test_debug "echo -n expected: " &&
215         test_debug "cat expected" &&
216         test_debug "echo -n actual: " &&
217         test_debug "cat actual" &&
218         test_cmp expected actual
219 #        rm -f expected actual
222 # 23
223 test_expect_success 'check if --message works for split+rejoin' '
224         spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
225         git branch spl1 "$spl1" &&
226         check_equal ''"$(last_commit_message)"'' "Split & rejoin" &&
227         undo
230 # 24
231 test_expect_success 'check split with --branch' '
232         spl1=$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin) &&
233         undo &&
234         git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --branch splitbr1 &&
235         check_equal ''"$(git rev-parse splitbr1)"'' "$spl1"
238 # 25
239 test_expect_success 'check split with --branch for an existing branch' '
240         spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
241         undo &&
242         git branch splitbr2 sub1 &&
243         git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --branch splitbr2 &&
244         check_equal ''"$(git rev-parse splitbr2)"'' "$spl1"
247 # 26
248 test_expect_success 'check split with --branch for an incompatible branch' '
249         test_must_fail git subtree split --prefix subdir --onto FETCH_HEAD --branch subdir
253 # 27
254 test_expect_success 'check split+rejoin' '
255         spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
256         undo &&
257         git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --rejoin &&
258         check_equal ''"$(last_commit_message)"'' "Split '"'"'subdir/'"'"' into commit '"'"'"$spl1"'"'"'"
261 # 28
262 test_expect_success 'add main-sub8' '
263         create subdir/main-sub8 &&
264         git commit -m "main-sub8"
267 # To the subproject!
268 cd ./subproj
270 # 29
271 test_expect_success 'merge split into subproj' '
272         git fetch .. spl1 &&
273         git branch spl1 FETCH_HEAD &&
274         git merge FETCH_HEAD
277 # 30
278 test_expect_success 'add sub9' '
279         create sub9 &&
280         git commit -m "sub9"
283 # Back to mainline
284 cd ..
286 # 31
287 test_expect_success 'split for sub8' '
288         split2=''"$(git subtree split --annotate='"'*'"' --prefix subdir/ --rejoin)"''
289         git branch split2 "$split2"
292 # 32
293 test_expect_success 'add main-sub10' '
294         create subdir/main-sub10 &&
295         git commit -m "main-sub10"
298 # 33
299 test_expect_success 'split for sub10' '
300         spl3=''"$(git subtree split --annotate='"'*'"' --prefix subdir --rejoin)"'' &&
301         git branch spl3 "$spl3"
304 # To the subproject!
305 cd ./subproj
307 # 34
308 test_expect_success 'merge split into subproj' '
309         git fetch .. spl3 &&
310         git branch spl3 FETCH_HEAD &&
311         git merge FETCH_HEAD &&
312         git branch subproj-merge-spl3
315 chkm="main4 main6"
316 chkms="main-sub10 main-sub5 main-sub7 main-sub8"
317 chkms_sub=$(echo $chkms | multiline | sed 's,^,subdir/,' | fixnl)
318 chks="sub1 sub2 sub3 sub9"
319 chks_sub=$(echo $chks | multiline | sed 's,^,subdir/,' | fixnl)
321 # 35
322 test_expect_success 'make sure exactly the right set of files ends up in the subproj' '
323         subfiles=''"$(git ls-files | fixnl)"'' &&
324         check_equal "$subfiles" "$chkms $chks"
327 # 36
328 test_expect_success 'make sure the subproj history *only* contains commits that affect the subdir' '
329         allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | fixnl)"'' &&
330         check_equal "$allchanges" "$chkms $chks"
333 # Back to mainline
334 cd ..
336 # 37
337 test_expect_success 'pull from subproj' '
338         git fetch ./subproj subproj-merge-spl3 &&
339         git branch subproj-merge-spl3 FETCH_HEAD &&
340         git subtree pull --prefix=subdir ./subproj subproj-merge-spl3
343 # 38
344 test_expect_success 'make sure exactly the right set of files ends up in the mainline' '
345         mainfiles=''"$(git ls-files | fixnl)"'' &&
346         check_equal "$mainfiles" "$chkm $chkms_sub $chks_sub"
349 # 39
350 test_expect_success 'make sure each filename changed exactly once in the entire history' '
351         # main-sub?? and /subdir/main-sub?? both change, because those are the
352         # changes that were split into their own history.  And subdir/sub?? never
353         # change, since they were *only* changed in the subtree branch.
354         allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | fixnl)"'' &&
355         check_equal "$allchanges" ''"$(echo $chkms $chkm $chks $chkms_sub | multiline | sort | fixnl)"''
358 # 40
359 test_expect_success 'make sure the --rejoin commits never make it into subproj' '
360         check_equal ''"$(git log --pretty=format:'"'%s'"' HEAD^2 | grep -i split)"'' ""
363 # 41
364 test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' '
365         # They are meaningless to subproj since one side of the merge refers to the mainline
366         check_equal ''"$(git log --pretty=format:'"'%s%n%b'"' HEAD^2 | grep "git-subtree.*:")"'' ""
369 # prepare second pair of repositories
370 mkdir test2
371 cd test2
373 # 42
374 test_expect_success 'init main' '
375         test_create_repo main
378 cd main
380 # 43
381 test_expect_success 'add main1' '
382         create main1 &&
383         git commit -m "main1"
386 cd ..
388 # 44
389 test_expect_success 'init sub' '
390         test_create_repo sub
393 cd sub
395 # 45
396 test_expect_success 'add sub2' '
397         create sub2 &&
398         git commit -m "sub2"
401 cd ../main
403 # check if split can find proper base without --onto
405 # 46
406 test_expect_success 'add sub as subdir in main' '
407         git fetch ../sub master &&
408         git branch sub2 FETCH_HEAD &&
409         git subtree add --prefix subdir sub2
412 cd ../sub
414 # 47
415 test_expect_success 'add sub3' '
416         create sub3 &&
417         git commit -m "sub3"
420 cd ../main
422 # 48
423 test_expect_success 'merge from sub' '
424         git fetch ../sub master &&
425         git branch sub3 FETCH_HEAD &&
426         git subtree merge --prefix subdir sub3
429 # 49
430 test_expect_success 'add main-sub4' '
431         create subdir/main-sub4 &&
432         git commit -m "main-sub4"
435 # 50
436 test_expect_success 'split for main-sub4 without --onto' '
437         git subtree split --prefix subdir --branch mainsub4
440 # at this point, the new commit parent should be sub3 if it is not,
441 # something went wrong (the "newparent" of "master~" commit should
442 # have been sub3, but it was not, because its cache was not set to
443 # itself)
445 # 51
446 test_expect_success 'check that the commit parent is sub3' '
447         check_equal ''"$(git log --pretty=format:%P -1 mainsub4)"'' ''"$(git rev-parse sub3)"''
450 # 52
451 test_expect_success 'add main-sub5' '
452         mkdir subdir2 &&
453         create subdir2/main-sub5 &&
454         git commit -m "main-sub5"
457 # 53
458 test_expect_success 'split for main-sub5 without --onto' '
459         # also test that we still can split out an entirely new subtree
460         # if the parent of the first commit in the tree is not empty,
461         # then the new subtree has accidently been attached to something
462         git subtree split --prefix subdir2 --branch mainsub5 &&
463         check_equal ''"$(git log --pretty=format:%P -1 mainsub5)"'' ""
466 # make sure no patch changes more than one file.  The original set of commits
467 # changed only one file each.  A multi-file change would imply that we pruned
468 # commits too aggressively.
469 joincommits()
471         commit=
472         all=
473         while read x y; do
474                 #echo "{$x}" >&2
475                 if [ -z "$x" ]; then
476                         continue
477                 elif [ "$x" = "commit:" ]; then
478                         if [ -n "$commit" ]; then
479                                 echo "$commit $all"
480                                 all=
481                         fi
482                         commit="$y"
483                 else
484                         all="$all $y"
485                 fi
486         done
487         echo "$commit $all"
490 # 54
491 test_expect_success 'verify one file change per commit' '
492         x= &&
493         list=''"$(git log --pretty=format:'"'commit: %H'"' | joincommits)"'' &&
494 #        test_debug "echo HERE" &&
495 #        test_debug "echo ''"$list"''" &&
496         (git log --pretty=format:'"'commit: %H'"' | joincommits |
497         (       while read commit a b; do
498                         test_debug "echo Verifying commit "''"$commit"''
499                         test_debug "echo a: "''"$a"''
500                         test_debug "echo b: "''"$b"''
501                         check_equal "$b" ""
502                         x=1
503                 done
504                 check_equal "$x" 1
505         ))
508 test_done