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