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