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