Merge branch 'jk/test-lint-forbid-when-finished-in-subshell' into maint
[git/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
6
7 This test verifies the basic operation of the merge, pull, add
8 and split subcommands of git subtree.
9 '
10
11 TEST_DIRECTORY=$(pwd)/../../../t
12 export TEST_DIRECTORY
13
14 . ../../../t/test-lib.sh
15
16 create()
17 {
18 echo "$1" >"$1"
19 git add "$1"
20 }
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 fixnl()
36 {
37 t=""
38 while read x; do
39 t="$t$x "
40 done
41 echo $t
42 }
43
44 multiline()
45 {
46 while read x; do
47 set -- $x
48 for d in "$@"; do
49 echo "$d"
50 done
51 done
52 }
53
54 undo()
55 {
56 git reset --hard HEAD~
57 }
58
59 last_commit_message()
60 {
61 git log --pretty=format:%s -1
62 }
63
64 test_expect_success 'init subproj' '
65 test_create_repo subproj
66 '
67
68 # To the subproject!
69 cd subproj
70
71 test_expect_success 'add sub1' '
72 create sub1 &&
73 git commit -m "sub1" &&
74 git branch sub1 &&
75 git branch -m master subproj
76 '
77
78 # Save this hash for testing later.
79
80 subdir_hash=$(git rev-parse HEAD)
81
82 test_expect_success 'add sub2' '
83 create sub2 &&
84 git commit -m "sub2" &&
85 git branch sub2
86 '
87
88 test_expect_success 'add sub3' '
89 create sub3 &&
90 git commit -m "sub3" &&
91 git branch sub3
92 '
93
94 # Back to mainline
95 cd ..
96
97 test_expect_success 'enable log.date=relative to catch errors' '
98 git config log.date relative
99 '
100
101 test_expect_success 'add main4' '
102 create main4 &&
103 git commit -m "main4" &&
104 git branch -m master mainline &&
105 git branch subdir
106 '
107
108 test_expect_success 'fetch subproj history' '
109 git fetch ./subproj sub1 &&
110 git branch sub1 FETCH_HEAD
111 '
112
113 test_expect_success 'no subtree exists in main tree' '
114 test_must_fail git subtree merge --prefix=subdir sub1
115 '
116
117 test_expect_success 'no pull from non-existant subtree' '
118 test_must_fail git subtree pull --prefix=subdir ./subproj sub1
119 '
120
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
125 '
126
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
131 '
132
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
137 '
138
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)"'''"'"'"
142 '
143
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
147 '
148
149 test_expect_success 'add main-sub5' '
150 create subdir/main-sub5 &&
151 git commit -m "main-sub5"
152 '
153
154 test_expect_success 'add main6' '
155 create main6 &&
156 git commit -m "main6 boring"
157 '
158
159 test_expect_success 'add main-sub7' '
160 create subdir/main-sub7 &&
161 git commit -m "main-sub7"
162 '
163
164 test_expect_success 'fetch new subproj history' '
165 git fetch ./subproj sub2 &&
166 git branch sub2 FETCH_HEAD
167 '
168
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
173 '
174
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
179 '
180
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" &&
185 undo
186 '
187
188 test_expect_success 'Check that prefix argument is required for split' '
189 echo "You must provide the --prefix option." > expected &&
190 test_must_fail git subtree split > actual 2>&1 &&
191 test_debug "printf '"'"'expected: '"'"'" &&
192 test_debug "cat expected" &&
193 test_debug "printf '"'"'actual: '"'"'" &&
194 test_debug "cat actual" &&
195 test_cmp expected actual &&
196 rm -f expected actual
197 '
198
199 test_expect_success 'Check that the <prefix> exists for a split' '
200 echo "'"'"'non-existent-directory'"'"'" does not exist\; use "'"'"'git subtree add'"'"'" > expected &&
201 test_must_fail git subtree split --prefix=non-existent-directory > actual 2>&1 &&
202 test_debug "printf '"'"'expected: '"'"'" &&
203 test_debug "cat expected" &&
204 test_debug "printf '"'"'actual: '"'"'" &&
205 test_debug "cat actual" &&
206 test_cmp expected actual
207 # rm -f expected actual
208 '
209
210 test_expect_success 'check if --message works for split+rejoin' '
211 spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
212 git branch spl1 "$spl1" &&
213 check_equal ''"$(last_commit_message)"'' "Split & rejoin" &&
214 undo
215 '
216
217 test_expect_success 'check split with --branch' '
218 spl1=$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin) &&
219 undo &&
220 git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --branch splitbr1 &&
221 check_equal ''"$(git rev-parse splitbr1)"'' "$spl1"
222 '
223
224 test_expect_success 'check hash of split' '
225 spl1=$(git subtree split --prefix subdir) &&
226 git subtree split --prefix subdir --branch splitbr1test &&
227 check_equal ''"$(git rev-parse splitbr1test)"'' "$spl1" &&
228 new_hash=$(git rev-parse splitbr1test~2) &&
229 check_equal ''"$new_hash"'' "$subdir_hash"
230 '
231
232 test_expect_success 'check split with --branch for an existing branch' '
233 spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
234 undo &&
235 git branch splitbr2 sub1 &&
236 git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --branch splitbr2 &&
237 check_equal ''"$(git rev-parse splitbr2)"'' "$spl1"
238 '
239
240 test_expect_success 'check split with --branch for an incompatible branch' '
241 test_must_fail git subtree split --prefix subdir --onto FETCH_HEAD --branch subdir
242 '
243
244 test_expect_success 'check split+rejoin' '
245 spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
246 undo &&
247 git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --rejoin &&
248 check_equal ''"$(last_commit_message)"'' "Split '"'"'subdir/'"'"' into commit '"'"'"$spl1"'"'"'"
249 '
250
251 test_expect_success 'add main-sub8' '
252 create subdir/main-sub8 &&
253 git commit -m "main-sub8"
254 '
255
256 # To the subproject!
257 cd ./subproj
258
259 test_expect_success 'merge split into subproj' '
260 git fetch .. spl1 &&
261 git branch spl1 FETCH_HEAD &&
262 git merge FETCH_HEAD
263 '
264
265 test_expect_success 'add sub9' '
266 create sub9 &&
267 git commit -m "sub9"
268 '
269
270 # Back to mainline
271 cd ..
272
273 test_expect_success 'split for sub8' '
274 split2=''"$(git subtree split --annotate='"'*'"' --prefix subdir/ --rejoin)"'' &&
275 git branch split2 "$split2"
276 '
277
278 test_expect_success 'add main-sub10' '
279 create subdir/main-sub10 &&
280 git commit -m "main-sub10"
281 '
282
283 test_expect_success 'split for sub10' '
284 spl3=''"$(git subtree split --annotate='"'*'"' --prefix subdir --rejoin)"'' &&
285 git branch spl3 "$spl3"
286 '
287
288 # To the subproject!
289 cd ./subproj
290
291 test_expect_success 'merge split into subproj' '
292 git fetch .. spl3 &&
293 git branch spl3 FETCH_HEAD &&
294 git merge FETCH_HEAD &&
295 git branch subproj-merge-spl3
296 '
297
298 chkm="main4 main6"
299 chkms="main-sub10 main-sub5 main-sub7 main-sub8"
300 chkms_sub=$(echo $chkms | multiline | sed 's,^,subdir/,' | fixnl)
301 chks="sub1 sub2 sub3 sub9"
302 chks_sub=$(echo $chks | multiline | sed 's,^,subdir/,' | fixnl)
303
304 test_expect_success 'make sure exactly the right set of files ends up in the subproj' '
305 subfiles=''"$(git ls-files | fixnl)"'' &&
306 check_equal "$subfiles" "$chkms $chks"
307 '
308
309 test_expect_success 'make sure the subproj history *only* contains commits that affect the subdir' '
310 allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | fixnl)"'' &&
311 check_equal "$allchanges" "$chkms $chks"
312 '
313
314 # Back to mainline
315 cd ..
316
317 test_expect_success 'pull from subproj' '
318 git fetch ./subproj subproj-merge-spl3 &&
319 git branch subproj-merge-spl3 FETCH_HEAD &&
320 git subtree pull --prefix=subdir ./subproj subproj-merge-spl3
321 '
322
323 test_expect_success 'make sure exactly the right set of files ends up in the mainline' '
324 mainfiles=''"$(git ls-files | fixnl)"'' &&
325 check_equal "$mainfiles" "$chkm $chkms_sub $chks_sub"
326 '
327
328 test_expect_success 'make sure each filename changed exactly once in the entire history' '
329 # main-sub?? and /subdir/main-sub?? both change, because those are the
330 # changes that were split into their own history. And subdir/sub?? never
331 # change, since they were *only* changed in the subtree branch.
332 allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | fixnl)"'' &&
333 check_equal "$allchanges" ''"$(echo $chkms $chkm $chks $chkms_sub | multiline | sort | fixnl)"''
334 '
335
336 test_expect_success 'make sure the --rejoin commits never make it into subproj' '
337 check_equal ''"$(git log --pretty=format:'"'%s'"' HEAD^2 | grep -i split)"'' ""
338 '
339
340 test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' '
341 # They are meaningless to subproj since one side of the merge refers to the mainline
342 check_equal ''"$(git log --pretty=format:'"'%s%n%b'"' HEAD^2 | grep "git-subtree.*:")"'' ""
343 '
344
345 # prepare second pair of repositories
346 mkdir test2
347 cd test2
348
349 test_expect_success 'init main' '
350 test_create_repo main
351 '
352
353 cd main
354
355 test_expect_success 'add main1' '
356 create main1 &&
357 git commit -m "main1"
358 '
359
360 cd ..
361
362 test_expect_success 'init sub' '
363 test_create_repo sub
364 '
365
366 cd sub
367
368 test_expect_success 'add sub2' '
369 create sub2 &&
370 git commit -m "sub2"
371 '
372
373 cd ../main
374
375 # check if split can find proper base without --onto
376
377 test_expect_success 'add sub as subdir in main' '
378 git fetch ../sub master &&
379 git branch sub2 FETCH_HEAD &&
380 git subtree add --prefix subdir sub2
381 '
382
383 cd ../sub
384
385 test_expect_success 'add sub3' '
386 create sub3 &&
387 git commit -m "sub3"
388 '
389
390 cd ../main
391
392 test_expect_success 'merge from sub' '
393 git fetch ../sub master &&
394 git branch sub3 FETCH_HEAD &&
395 git subtree merge --prefix subdir sub3
396 '
397
398 test_expect_success 'add main-sub4' '
399 create subdir/main-sub4 &&
400 git commit -m "main-sub4"
401 '
402
403 test_expect_success 'split for main-sub4 without --onto' '
404 git subtree split --prefix subdir --branch mainsub4
405 '
406
407 # at this point, the new commit parent should be sub3 if it is not,
408 # something went wrong (the "newparent" of "master~" commit should
409 # have been sub3, but it was not, because its cache was not set to
410 # itself)
411
412 test_expect_success 'check that the commit parent is sub3' '
413 check_equal ''"$(git log --pretty=format:%P -1 mainsub4)"'' ''"$(git rev-parse sub3)"''
414 '
415
416 test_expect_success 'add main-sub5' '
417 mkdir subdir2 &&
418 create subdir2/main-sub5 &&
419 git commit -m "main-sub5"
420 '
421
422 test_expect_success 'split for main-sub5 without --onto' '
423 # also test that we still can split out an entirely new subtree
424 # if the parent of the first commit in the tree is not empty,
425 # then the new subtree has accidentally been attached to something
426 git subtree split --prefix subdir2 --branch mainsub5 &&
427 check_equal ''"$(git log --pretty=format:%P -1 mainsub5)"'' ""
428 '
429
430 # make sure no patch changes more than one file. The original set of commits
431 # changed only one file each. A multi-file change would imply that we pruned
432 # commits too aggressively.
433 joincommits()
434 {
435 commit=
436 all=
437 while read x y; do
438 #echo "{$x}" >&2
439 if [ -z "$x" ]; then
440 continue
441 elif [ "$x" = "commit:" ]; then
442 if [ -n "$commit" ]; then
443 echo "$commit $all"
444 all=
445 fi
446 commit="$y"
447 else
448 all="$all $y"
449 fi
450 done
451 echo "$commit $all"
452 }
453
454 test_expect_success 'verify one file change per commit' '
455 x= &&
456 list=''"$(git log --pretty=format:'"'commit: %H'"' | joincommits)"'' &&
457 # test_debug "echo HERE" &&
458 # test_debug "echo ''"$list"''" &&
459 (git log --pretty=format:'"'commit: %H'"' | joincommits |
460 ( while read commit a b; do
461 test_debug "echo Verifying commit "''"$commit"''
462 test_debug "echo a: "''"$a"''
463 test_debug "echo b: "''"$b"''
464 check_equal "$b" ""
465 x=1
466 done
467 check_equal "$x" 1
468 ))
469 '
470
471 test_done