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