1#!/bin/sh
2#
3# Copyright (c) 2012 Avery Pennaraum
4#
5test_description='Basic porcelain support for subtrees
6
7This test verifies the basic operation of the merge, pull, add
8and split subcommands of git subtree.
9'
10
11export TEST_DIRECTORY=$(pwd)/../../../t
12
13. ../../../t/test-lib.sh
14
15create()
16{
17 echo "$1" >"$1"
18 git add "$1"
19}
20
21
22check_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
34fixnl()
35{
36 t=""
37 while read x; do
38 t="$t$x "
39 done
40 echo $t
41}
42
43multiline()
44{
45 while read x; do
46 set -- $x
47 for d in "$@"; do
48 echo "$d"
49 done
50 done
51}
52
53undo()
54{
55 git reset --hard HEAD~
56}
57
58last_commit_message()
59{
60 git log --pretty=format:%s -1
61}
62
63# 1
64test_expect_success 'init subproj' '
65 test_create_repo subproj
66'
67
68# To the subproject!
69cd subproj
70
71# 2
72test_expect_success 'add sub1' '
73 create sub1 &&
74 git commit -m "sub1" &&
75 git branch sub1 &&
76 git branch -m master subproj
77'
78
79# 3
80test_expect_success 'add sub2' '
81 create sub2 &&
82 git commit -m "sub2" &&
83 git branch sub2
84'
85
86# 4
87test_expect_success 'add sub3' '
88 create sub3 &&
89 git commit -m "sub3" &&
90 git branch sub3
91'
92
93# Back to mainline
94cd ..
95
96# 5
97test_expect_success 'add main4' '
98 create main4 &&
99 git commit -m "main4" &&
100 git branch -m master mainline &&
101 git branch subdir
102'
103
104# 6
105test_expect_success 'fetch subproj history' '
106 git fetch ./subproj sub1 &&
107 git branch sub1 FETCH_HEAD
108'
109
110# 7
111test_expect_success 'no subtree exists in main tree' '
112 test_must_fail git subtree merge --prefix=subdir sub1
113'
114
115# 8
116test_expect_success 'no pull from non-existant subtree' '
117 test_must_fail git subtree pull --prefix=subdir ./subproj sub1
118'
119
120# 9
121test_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# 10
128test_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
132'
133
134# 11
135test_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
139'
140
141# 12
142test_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)"'''"'"'"
145'
146
147# 13
148# this shouldn't actually do anything, since FETCH_HEAD is already a parent
149test_expect_success 'merge fetched subproj' '
150 git merge -m "merge -s -ours" -s ours FETCH_HEAD
151'
152
153# 14
154test_expect_success 'add main-sub5' '
155 create subdir/main-sub5 &&
156 git commit -m "main-sub5"
157'
158
159# 15
160test_expect_success 'add main6' '
161 create main6 &&
162 git commit -m "main6 boring"
163'
164
165# 16
166test_expect_success 'add main-sub7' '
167 create subdir/main-sub7 &&
168 git commit -m "main-sub7"
169'
170
171# 17
172test_expect_success 'fetch new subproj history' '
173 git fetch ./subproj sub2 &&
174 git branch sub2 FETCH_HEAD
175'
176
177# 18
178test_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
182'
183
184# 19
185test_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
189'
190
191# 20
192test_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"
196'
197
198# 21
199test_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
208'
209
210# 22
211test_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
220'
221
222# 23
223test_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
228'
229
230# 24
231test_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"
236'
237
238# 25
239test_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"
245'
246
247# 26
248test_expect_success 'check split with --branch for an incompatible branch' '
249 test_must_fail git subtree split --prefix subdir --onto FETCH_HEAD --branch subdir
250'
251
252
253# 27
254test_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"'"'"'"
259'
260
261# 28
262test_expect_success 'add main-sub8' '
263 create subdir/main-sub8 &&
264 git commit -m "main-sub8"
265'
266
267# To the subproject!
268cd ./subproj
269
270# 29
271test_expect_success 'merge split into subproj' '
272 git fetch .. spl1 &&
273 git branch spl1 FETCH_HEAD &&
274 git merge FETCH_HEAD
275'
276
277# 30
278test_expect_success 'add sub9' '
279 create sub9 &&
280 git commit -m "sub9"
281'
282
283# Back to mainline
284cd ..
285
286# 31
287test_expect_success 'split for sub8' '
288 split2=''"$(git subtree split --annotate='"'*'"' --prefix subdir/ --rejoin)"''
289 git branch split2 "$split2"
290'
291
292# 32
293test_expect_success 'add main-sub10' '
294 create subdir/main-sub10 &&
295 git commit -m "main-sub10"
296'
297
298# 33
299test_expect_success 'split for sub10' '
300 spl3=''"$(git subtree split --annotate='"'*'"' --prefix subdir --rejoin)"'' &&
301 git branch spl3 "$spl3"
302'
303
304# To the subproject!
305cd ./subproj
306
307# 34
308test_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
313'
314
315chkm="main4 main6"
316chkms="main-sub10 main-sub5 main-sub7 main-sub8"
317chkms_sub=$(echo $chkms | multiline | sed 's,^,subdir/,' | fixnl)
318chks="sub1 sub2 sub3 sub9"
319chks_sub=$(echo $chks | multiline | sed 's,^,subdir/,' | fixnl)
320
321# 35
322test_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"
325'
326
327# 36
328test_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"
331'
332
333# Back to mainline
334cd ..
335
336# 37
337test_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
341'
342
343# 38
344test_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"
347'
348
349# 39
350test_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)"''
356'
357
358# 40
359test_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)"'' ""
361'
362
363# 41
364test_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.*:")"'' ""
367'
368
369# prepare second pair of repositories
370mkdir test2
371cd test2
372
373# 42
374test_expect_success 'init main' '
375 test_create_repo main
376'
377
378cd main
379
380# 43
381test_expect_success 'add main1' '
382 create main1 &&
383 git commit -m "main1"
384'
385
386cd ..
387
388# 44
389test_expect_success 'init sub' '
390 test_create_repo sub
391'
392
393cd sub
394
395# 45
396test_expect_success 'add sub2' '
397 create sub2 &&
398 git commit -m "sub2"
399'
400
401cd ../main
402
403# check if split can find proper base without --onto
404
405# 46
406test_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
410'
411
412cd ../sub
413
414# 47
415test_expect_success 'add sub3' '
416 create sub3 &&
417 git commit -m "sub3"
418'
419
420cd ../main
421
422# 48
423test_expect_success 'merge from sub' '
424 git fetch ../sub master &&
425 git branch sub3 FETCH_HEAD &&
426 git subtree merge --prefix subdir sub3
427'
428
429# 49
430test_expect_success 'add main-sub4' '
431 create subdir/main-sub4 &&
432 git commit -m "main-sub4"
433'
434
435# 50
436test_expect_success 'split for main-sub4 without --onto' '
437 git subtree split --prefix subdir --branch mainsub4
438'
439
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)
444
445# 51
446test_expect_success 'check that the commit parent is sub3' '
447 check_equal ''"$(git log --pretty=format:%P -1 mainsub4)"'' ''"$(git rev-parse sub3)"''
448'
449
450# 52
451test_expect_success 'add main-sub5' '
452 mkdir subdir2 &&
453 create subdir2/main-sub5 &&
454 git commit -m "main-sub5"
455'
456
457# 53
458test_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)"'' ""
464'
465
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.
469joincommits()
470{
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"
488}
489
490# 54
491test_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 ))
506'
507
508test_done