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