contrib/subtree: Add merge tests
[git] / contrib / subtree / t / t7900-subtree.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2012 Avery Pennaraum
4 # Copyright (c) 2015 Alexey Shumkin
5 #
6 test_description='Basic porcelain support for subtrees
7
8 This test verifies the basic operation of the add, pull, merge
9 and split subcommands of git subtree.
10 '
11
12 TEST_DIRECTORY=$(pwd)/../../../t
13 export TEST_DIRECTORY
14
15 . ../../../t/test-lib.sh
16
17 create()
18 {
19         echo "$1" >"$1"
20         git add "$1"
21 }
22
23 check_equal()
24 {
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
32         fi
33 }
34
35 undo()
36 {
37         git reset --hard HEAD~
38 }
39
40 # Make sure no patch changes more than one file.
41 # The original set of commits changed only one file each.
42 # A multi-file change would imply that we pruned commits
43 # too aggressively.
44 join_commits()
45 {
46         commit=
47         all=
48         while read x y; do
49                 if [ -z "$x" ]; then
50                         continue
51                 elif [ "$x" = "commit:" ]; then
52                         if [ -n "$commit" ]; then
53                                 echo "$commit $all"
54                                 all=
55                         fi
56                         commit="$y"
57                 else
58                         all="$all $y"
59                 fi
60         done
61         echo "$commit $all"
62 }
63
64 last_commit_message()
65 {
66         git log --pretty=format:%s -1
67 }
68
69 test_expect_success 'init subproj' '
70         test_create_repo "sub proj"
71 '
72
73 # To the subproject!
74 cd ./"sub proj"
75
76 test_expect_success 'add sub1' '
77         create sub1 &&
78         git commit -m "sub1" &&
79         git branch sub1 &&
80         git branch -m master subproj
81 '
82
83 # Save this hash for testing later.
84
85 subdir_hash=$(git rev-parse HEAD)
86
87 test_expect_success 'add sub2' '
88         create sub2 &&
89         git commit -m "sub2" &&
90         git branch sub2
91 '
92
93 test_expect_success 'add sub3' '
94         create sub3 &&
95         git commit -m "sub3" &&
96         git branch sub3
97 '
98
99 # Back to mainline
100 cd ..
101
102 test_expect_success 'enable log.date=relative to catch errors' '
103         git config log.date relative
104 '
105
106 test_expect_success 'add main4' '
107         create main4 &&
108         git commit -m "main4" &&
109         git branch -m master mainline &&
110         git branch subdir
111 '
112
113 test_expect_success 'fetch subproj history' '
114         git fetch ./"sub proj" sub1 &&
115         git branch sub1 FETCH_HEAD
116 '
117
118 test_expect_success 'no subtree exists in main tree' '
119         test_must_fail git subtree merge --prefix="sub dir" sub1
120 '
121
122 test_expect_success 'no pull from non-existant subtree' '
123         test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" sub1
124 '
125
126 test_expect_success 'no merge from non-existent subtree' '
127         test_must_fail git subtree merge --prefix="sub dir" FETCH_HEAD
128 '
129
130 test_expect_success 'add subproj as subtree into sub dir/ with --prefix' '
131         git subtree add --prefix="sub dir" sub1 &&
132         check_equal "$(last_commit_message)" "Add '\''sub dir/'\'' from commit '\''$(git rev-parse sub1)'\''" &&
133         undo
134 '
135
136 test_expect_success 'check if --message works for add' '
137         git subtree add --prefix="sub dir" --message="Added subproject" sub1 &&
138         check_equal ''"$(last_commit_message)"'' "Added subproject" &&
139         undo
140 '
141
142 test_expect_success 'add subproj as subtree into sub dir/ with --prefix and --message' '
143         git subtree add --prefix="sub dir" --message="Added subproject" sub1 &&
144         check_equal "$(last_commit_message)" "Added subproject" &&
145         undo
146 '
147
148 test_expect_success 'check if --message works as -m and --prefix as -P' '
149         git subtree add -P "sub dir" -m "Added subproject using git subtree" sub1 &&
150         check_equal ''"$(last_commit_message)"'' "Added subproject using git subtree" &&
151         undo
152 '
153
154 test_expect_success 'check if --message works with squash too' '
155         git subtree add -P "sub dir" -m "Added subproject with squash" --squash sub1 &&
156         check_equal ''"$(last_commit_message)"'' "Added subproject with squash" &&
157         undo
158 '
159
160 test_expect_success 'add subproj as subtree into sub dir/ with --squash and --prefix and --message' '
161         git subtree add --prefix="sub dir" --message="Added subproject with squash" --squash sub1 &&
162         check_equal "$(last_commit_message)" "Added subproject with squash" &&
163         undo
164 '
165
166 # Maybe delete
167 test_expect_success 'add subproj to mainline' '
168         git subtree add --prefix="sub dir"/ FETCH_HEAD &&
169         check_equal ''"$(last_commit_message)"'' "Add '"'sub dir/'"' from commit '"'"'''"$(git rev-parse sub1)"'''"'"'"
170 '
171
172 test_expect_success 'merge the added subproj again, should do nothing' '
173         # this shouldn not actually do anything, since FETCH_HEAD
174         # is already a parent
175         result=$(git merge -s ours -m "merge -s -ours" FETCH_HEAD) &&
176         check_equal "${result}" "Already up-to-date."
177 '
178
179 test_expect_success 'add main-sub5' '
180         create "sub dir/main-sub5" &&
181         git commit -m "main-sub5"
182 '
183
184 test_expect_success 'add main6' '
185         create main6 &&
186         git commit -m "main6 boring"
187 '
188
189 test_expect_success 'add main-sub7' '
190         create "sub dir/main-sub7" &&
191         git commit -m "main-sub7"
192 '
193
194 test_expect_success 'fetch new subproj history' '
195         git fetch ./"sub proj" sub2 &&
196         git branch sub2 FETCH_HEAD
197 '
198
199 test_expect_success 'check if --message works for merge' '
200         git subtree merge --prefix="sub dir" -m "Merged changes from subproject" sub2 &&
201         check_equal ''"$(last_commit_message)"'' "Merged changes from subproject" &&
202         undo
203 '
204
205 test_expect_success 'check if --message for merge works with squash too' '
206         git subtree merge --prefix "sub dir" -m "Merged changes from subproject using squash" --squash sub2 &&
207         check_equal ''"$(last_commit_message)"'' "Merged changes from subproject using squash" &&
208         undo
209 '
210
211 test_expect_success 'merge new subproj history into subdir' '
212         git subtree merge --prefix="sub dir" FETCH_HEAD &&
213         check_equal ''"$(last_commit_message)"'' "Merge commit '"'"'"$(git rev-parse sub2)"'"'"' into mainline" &&
214         undo
215 '
216
217 test_expect_success 'merge new subproj history into subdir/ with --prefix and --message' '
218         git subtree merge --prefix="sub dir" --message="Merged changes from subproject" FETCH_HEAD &&
219         check_equal "$(last_commit_message)" "Merged changes from subproject" &&
220         undo
221 '
222
223 test_expect_success 'merge new subproj history into subdir/ with --squash and --prefix and --message' '
224         git subtree merge --prefix="sub dir" --message="Merged changes from subproject using squash" --squash FETCH_HEAD &&
225         check_equal "$(last_commit_message)" "Merged changes from subproject using squash" &&
226         undo
227 '
228
229 test_expect_success 'split requires option --prefix' '
230         echo "You must provide the --prefix option." > expected &&
231         test_must_fail git subtree split > actual 2>&1 &&
232         test_debug "printf '"'"'expected: '"'"'" &&
233         test_debug "cat expected" &&
234         test_debug "printf '"'"'actual: '"'"'" &&
235         test_debug "cat actual" &&
236         test_cmp expected actual &&
237         rm -f expected actual
238 '
239
240 test_expect_success 'split requires path given by option --prefix must exist' '
241         echo "'\''non-existent-directory'\'' does not exist; use '\''git subtree add'\''" > expected &&
242         test_must_fail git subtree split --prefix=non-existent-directory > actual 2>&1 &&
243         test_debug "printf '"'"'expected: '"'"'" &&
244         test_debug "cat expected" &&
245         test_debug "printf '"'"'actual: '"'"'" &&
246         test_debug "cat actual" &&
247         test_cmp expected actual &&
248         rm -f expected actual
249 '
250
251 test_expect_success 'check if --message works for split+rejoin' '
252         spl1=''"$(git subtree split --annotate='"'*'"' --prefix "sub dir" --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
253         git branch spl1 "$spl1" &&
254         check_equal ''"$(last_commit_message)"'' "Split & rejoin" &&
255         undo
256 '
257
258 test_expect_success 'check split with --branch' '
259         spl1=$(git subtree split --annotate='"'*'"' --prefix "sub dir" --onto FETCH_HEAD --message "Split & rejoin" --rejoin) &&
260         undo &&
261         git subtree split --annotate='"'*'"' --prefix "sub dir" --onto FETCH_HEAD --branch splitbr1 &&
262         check_equal ''"$(git rev-parse splitbr1)"'' "$spl1"
263 '
264
265 test_expect_success 'check hash of split' '
266         spl1=$(git subtree split --prefix "sub dir") &&
267         git subtree split --prefix "sub dir" --branch splitbr1test &&
268         check_equal ''"$(git rev-parse splitbr1test)"'' "$spl1" &&
269         new_hash=$(git rev-parse splitbr1test~2) &&
270         check_equal ''"$new_hash"'' "$subdir_hash"
271 '
272
273 test_expect_success 'check split with --branch for an existing branch' '
274         spl1=''"$(git subtree split --annotate='"'*'"' --prefix "sub dir" --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
275         undo &&
276         git branch splitbr2 sub1 &&
277         git subtree split --annotate='"'*'"' --prefix "sub dir" --onto FETCH_HEAD --branch splitbr2 &&
278         check_equal ''"$(git rev-parse splitbr2)"'' "$spl1"
279 '
280
281 test_expect_success 'check split with --branch for an incompatible branch' '
282         test_must_fail git subtree split --prefix "sub dir" --onto FETCH_HEAD --branch subdir
283 '
284
285 test_expect_success 'check split+rejoin' '
286         spl1=''"$(git subtree split --annotate='"'*'"' --prefix "sub dir" --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
287         undo &&
288         git subtree split --annotate='"'*'"' --prefix "sub dir" --onto FETCH_HEAD --rejoin &&
289         check_equal ''"$(last_commit_message)"'' "Split '"'"'sub dir/'"'"' into commit '"'"'"$spl1"'"'"'"
290 '
291
292 test_expect_success 'add main-sub8' '
293         create "sub dir/main-sub8" &&
294         git commit -m "main-sub8"
295 '
296
297 # To the subproject!
298 cd ./"sub proj"
299
300 test_expect_success 'merge split into subproj' '
301         git fetch .. spl1 &&
302         git branch spl1 FETCH_HEAD &&
303         git merge FETCH_HEAD
304 '
305
306 test_expect_success 'add sub9' '
307         create sub9 &&
308         git commit -m "sub9"
309 '
310
311 # Back to mainline
312 cd ..
313
314 test_expect_success 'split for sub8' '
315         split2=''"$(git subtree split --annotate='"'*'"' --prefix "sub dir/" --rejoin)"'' &&
316         git branch split2 "$split2"
317 '
318
319 test_expect_success 'add main-sub10' '
320         create "sub dir/main-sub10" &&
321         git commit -m "main-sub10"
322 '
323
324 test_expect_success 'split for sub10' '
325         spl3=''"$(git subtree split --annotate='"'*'"' --prefix "sub dir" --rejoin)"'' &&
326         git branch spl3 "$spl3"
327 '
328
329 # To the subproject!
330 cd ./"sub proj"
331
332 test_expect_success 'merge split into subproj' '
333         git fetch .. spl3 &&
334         git branch spl3 FETCH_HEAD &&
335         git merge FETCH_HEAD &&
336         git branch subproj-merge-spl3
337 '
338
339 chkm="main4
340 main6"
341
342 chkms="main-sub10
343 main-sub5
344 main-sub7
345 main-sub8"
346
347 chkms_sub=$(cat <<TXT | sed 's,^,sub dir/,'
348 $chkms
349 TXT
350 )
351
352 chks="sub1
353 sub2
354 sub3
355 sub9"
356
357 chks_sub=$(cat <<TXT | sed 's,^,sub dir/,'
358 $chks
359 TXT
360 )
361
362 test_expect_success 'make sure exactly the right set of files ends up in the subproj' '
363         subfiles="$(git ls-files)" &&
364         check_equal "$subfiles" "$chkms
365 $chks"
366 '
367
368 test_expect_success 'make sure the subproj history *only* contains commits that affect the subdir' '
369         allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | sed "/^$/d")"'' &&
370         check_equal "$allchanges" "$chkms
371 $chks"
372 '
373
374 # Back to mainline
375 cd ..
376
377 test_expect_success 'pull from subproj' '
378         git fetch ./"sub proj" subproj-merge-spl3 &&
379         git branch subproj-merge-spl3 FETCH_HEAD &&
380         git subtree pull --prefix="sub dir" ./"sub proj" subproj-merge-spl3
381 '
382
383 test_expect_success 'make sure exactly the right set of files ends up in the mainline' '
384         mainfiles=$(git ls-files) &&
385         check_equal "$mainfiles" "$chkm
386 $chkms_sub
387 $chks_sub"
388 '
389
390 test_expect_success 'make sure each filename changed exactly once in the entire history' '
391         # main-sub?? and sub dir/main-sub?? both change, because those are the
392         # changes that were split into their own history.  And sub dir/sub?? never
393         # change, since they were *only* changed in the subtree branch.
394         allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | sed "/^$/d")"'' &&
395         expected=''"$(cat <<TXT | sort
396 $chkms
397 $chkm
398 $chks
399 $chkms_sub
400 TXT
401 )"'' &&
402         check_equal "$allchanges" "$expected"
403 '
404
405 test_expect_success 'make sure the --rejoin commits never make it into subproj' '
406         check_equal "$(git log --pretty=format:"%s" HEAD^2 | grep -i split)" ""
407 '
408
409 test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' '
410         # They are meaningless to subproj since one side of the merge refers to the mainline
411         check_equal "$(git log --pretty=format:"%s%n%b" HEAD^2 | grep "git-subtree.*:")" ""
412 '
413
414 # prepare second pair of repositories
415 mkdir test2
416 cd test2
417
418 test_expect_success 'init main' '
419         test_create_repo main
420 '
421
422 cd main
423
424 test_expect_success 'add main1' '
425         create main1 &&
426         git commit -m "main1"
427 '
428
429 cd ..
430
431 test_expect_success 'init sub' '
432         test_create_repo sub
433 '
434
435 cd sub
436
437 test_expect_success 'add sub2' '
438         create sub2 &&
439         git commit -m "sub2"
440 '
441
442 cd ../main
443
444 # check if split can find proper base without --onto
445
446 test_expect_success 'add sub as subdir in main' '
447         git fetch ../sub master &&
448         git branch sub2 FETCH_HEAD &&
449         git subtree add --prefix "sub dir" sub2
450 '
451
452 cd ../sub
453
454 test_expect_success 'add sub3' '
455         create sub3 &&
456         git commit -m "sub3"
457 '
458
459 cd ../main
460
461 test_expect_success 'merge from sub' '
462         git fetch ../sub master &&
463         git branch sub3 FETCH_HEAD &&
464         git subtree merge --prefix "sub dir" sub3
465 '
466
467 test_expect_success 'add main-sub4' '
468         create "sub dir/main-sub4" &&
469         git commit -m "main-sub4"
470 '
471
472 test_expect_success 'split for main-sub4 without --onto' '
473         git subtree split --prefix "sub dir" --branch mainsub4
474 '
475
476 # At this point, the new commit parent should be sub3.  If it is not,
477 # something went wrong (the "newparent" of "master~" commit should
478 # have been sub3, but it was not, because its cache was not set to
479 # itself).
480
481 test_expect_success 'check that the commit parent is sub3' '
482         check_equal "$(git log --pretty=format:%P -1 mainsub4)" "$(git rev-parse sub3)"
483 '
484
485 test_expect_success 'add main-sub5' '
486         mkdir subdir2 &&
487         create subdir2/main-sub5 &&
488         git commit -m "main-sub5"
489 '
490
491 test_expect_success 'split for main-sub5 without --onto' '
492         # also test that we still can split out an entirely new subtree
493         # if the parent of the first commit in the tree is not empty,
494         # then the new subtree has accidentally been attached to something
495         git subtree split --prefix subdir2 --branch mainsub5 &&
496         check_equal ''"$(git log --pretty=format:%P -1 mainsub5)"'' ""
497 '
498
499 test_expect_success 'verify one file change per commit' '
500         x= &&
501         list=''"$(git log --pretty=format:'"'commit: %H'"' | join_commits)"'' &&
502 #       test_debug "echo HERE" &&
503 #       test_debug "echo ''"$list"''" &&
504         git log --pretty=format:'"'commit: %H'"' | join_commits |
505         (       while read commit a b; do
506                         test_debug "echo Verifying commit "''"$commit"''
507                         test_debug "echo a: "''"$a"''
508                         test_debug "echo b: "''"$b"''
509                         check_equal "$b" ""
510                         x=1
511                 done
512                 check_equal "$x" "1"
513         )
514 '
515
516 # test push
517
518 cd ../..
519
520 mkdir -p test-push
521
522 cd test-push
523
524 test_expect_success 'init main' '
525         test_create_repo main
526 '
527
528 test_expect_success 'init sub' '
529         test_create_repo "sub project"
530 '
531
532 cd ./"sub project"
533
534 test_expect_success 'add subproject' '
535         create "sub project" &&
536         git commit -m "Sub project: 1" &&
537         git branch sub-branch-1
538 '
539
540 cd ../main
541
542 test_expect_success 'make first commit and add subproject' '
543         create "main-1" &&
544         git commit -m "main: 1" &&
545         git subtree add "../sub project" --prefix "sub dir" --message "Added subproject" sub-branch-1 &&
546         check_equal "$(last_commit_message)" "Added subproject"
547 '
548
549 test_expect_success 'make second commit to a subproject file and push it into a sub project' '
550         create "sub dir/sub1" &&
551         git commit -m "Sub project: 2" &&
552         git subtree push "../sub project" --prefix "sub dir" sub-branch-1
553 '
554
555 cd ../"sub project"
556
557 test_expect_success 'Test second commit is pushed' '
558         git checkout sub-branch-1 &&
559         check_equal "$(last_commit_message)" "Sub project: 2"
560 '
561
562 test_done