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