subtree: t7900: use test-lib.sh's test_count
[git] / contrib / subtree / t / t7900-subtree.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2012 Avery Pennaraum
4 # Copyright (c) 2015 Alexey Shumkin
5 #
6 test_description='Basic porcelain support for subtrees
7
8 This test verifies the basic operation of the add, pull, merge
9 and split subcommands of git subtree.
10 '
11
12 TEST_DIRECTORY=$(pwd)/../../../t
13 export TEST_DIRECTORY
14
15 . ../../../t/test-lib.sh
16
17 subtree_test_create_repo()
18 {
19         test_create_repo "$1" &&
20         (
21                 cd "$1" &&
22                 git config log.date relative
23         )
24 }
25
26 create()
27 {
28         echo "$1" >"$1" &&
29         git add "$1"
30 }
31
32 check_equal()
33 {
34         test_debug 'echo'
35         test_debug "echo \"check a:\" \"{$1}\""
36         test_debug "echo \"      b:\" \"{$2}\""
37         if [ "$1" = "$2" ]; then
38                 return 0
39         else
40                 return 1
41         fi
42 }
43
44 undo()
45 {
46         git reset --hard HEAD~
47 }
48
49 # Make sure no patch changes more than one file.
50 # The original set of commits changed only one file each.
51 # A multi-file change would imply that we pruned commits
52 # too aggressively.
53 join_commits()
54 {
55         commit=
56         all=
57         while read x y; do
58                 if [ -z "$x" ]; then
59                         continue
60                 elif [ "$x" = "commit:" ]; then
61                         if [ -n "$commit" ]; then
62                                 echo "$commit $all"
63                                 all=
64                         fi
65                         commit="$y"
66                 else
67                         all="$all $y"
68                 fi
69         done
70         echo "$commit $all"
71 }
72
73 test_create_commit() (
74         repo=$1 &&
75         commit=$2 &&
76         cd "$repo" &&
77         mkdir -p "$(dirname "$commit")" \
78         || error "Could not create directory for commit"
79         echo "$commit" >"$commit" &&
80         git add "$commit" || error "Could not add commit"
81         git commit -m "$commit" || error "Could not commit"
82 )
83
84 last_commit_message()
85 {
86         git log --pretty=format:%s -1
87 }
88
89 #
90 # Tests for 'git subtree add'
91 #
92
93 test_expect_success 'no merge from non-existent subtree' '
94         subtree_test_create_repo "$test_count" &&
95         subtree_test_create_repo "$test_count/sub proj" &&
96         test_create_commit "$test_count" main1 &&
97         test_create_commit "$test_count/sub proj" sub1 &&
98         (
99                 cd "$test_count" &&
100                 git fetch ./"sub proj" HEAD &&
101                 test_must_fail git subtree merge --prefix="sub dir" FETCH_HEAD
102         )
103 '
104
105 test_expect_success 'no pull from non-existent subtree' '
106         subtree_test_create_repo "$test_count" &&
107         subtree_test_create_repo "$test_count/sub proj" &&
108         test_create_commit "$test_count" main1 &&
109         test_create_commit "$test_count/sub proj" sub1 &&
110         (
111                 cd "$test_count" &&
112                 git fetch ./"sub proj" HEAD &&
113                 test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" HEAD
114         )'
115
116 test_expect_success 'add subproj as subtree into sub dir/ with --prefix' '
117         subtree_test_create_repo "$test_count" &&
118         subtree_test_create_repo "$test_count/sub proj" &&
119         test_create_commit "$test_count" main1 &&
120         test_create_commit "$test_count/sub proj" sub1 &&
121         (
122                 cd "$test_count" &&
123                 git fetch ./"sub proj" HEAD &&
124                 git subtree add --prefix="sub dir" FETCH_HEAD &&
125                 check_equal "$(last_commit_message)" "Add '\''sub dir/'\'' from commit '\''$(git rev-parse FETCH_HEAD)'\''"
126         )
127 '
128
129 test_expect_success 'add subproj as subtree into sub dir/ with --prefix and --message' '
130         subtree_test_create_repo "$test_count" &&
131         subtree_test_create_repo "$test_count/sub proj" &&
132         test_create_commit "$test_count" main1 &&
133         test_create_commit "$test_count/sub proj" sub1 &&
134         (
135                 cd "$test_count" &&
136                 git fetch ./"sub proj" HEAD &&
137                 git subtree add --prefix="sub dir" --message="Added subproject" FETCH_HEAD &&
138                 check_equal "$(last_commit_message)" "Added subproject"
139         )
140 '
141
142 test_expect_success 'add subproj as subtree into sub dir/ with --prefix as -P and --message as -m' '
143         subtree_test_create_repo "$test_count" &&
144         subtree_test_create_repo "$test_count/sub proj" &&
145         test_create_commit "$test_count" main1 &&
146         test_create_commit "$test_count/sub proj" sub1 &&
147         (
148                 cd "$test_count" &&
149                 git fetch ./"sub proj" HEAD &&
150                 git subtree add -P "sub dir" -m "Added subproject" FETCH_HEAD &&
151                 check_equal "$(last_commit_message)" "Added subproject"
152         )
153 '
154
155 test_expect_success 'add subproj as subtree into sub dir/ with --squash and --prefix and --message' '
156         subtree_test_create_repo "$test_count" &&
157         subtree_test_create_repo "$test_count/sub proj" &&
158         test_create_commit "$test_count" main1 &&
159         test_create_commit "$test_count/sub proj" sub1 &&
160         (
161                 cd "$test_count" &&
162                 git fetch ./"sub proj" HEAD &&
163                 git subtree add --prefix="sub dir" --message="Added subproject with squash" --squash FETCH_HEAD &&
164                 check_equal "$(last_commit_message)" "Added subproject with squash"
165         )
166 '
167
168 #
169 # Tests for 'git subtree merge'
170 #
171
172 test_expect_success 'merge new subproj history into sub dir/ with --prefix' '
173         subtree_test_create_repo "$test_count" &&
174         subtree_test_create_repo "$test_count/sub proj" &&
175         test_create_commit "$test_count" main1 &&
176         test_create_commit "$test_count/sub proj" sub1 &&
177         (
178                 cd "$test_count" &&
179                 git fetch ./"sub proj" HEAD &&
180                 git subtree add --prefix="sub dir" FETCH_HEAD
181         ) &&
182         test_create_commit "$test_count/sub proj" sub2 &&
183         (
184                 cd "$test_count" &&
185                 git fetch ./"sub proj" HEAD &&
186                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
187                 check_equal "$(last_commit_message)" "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''"
188         )
189 '
190
191 test_expect_success 'merge new subproj history into sub dir/ with --prefix and --message' '
192         subtree_test_create_repo "$test_count" &&
193         subtree_test_create_repo "$test_count/sub proj" &&
194         test_create_commit "$test_count" main1 &&
195         test_create_commit "$test_count/sub proj" sub1 &&
196         (
197                 cd "$test_count" &&
198                 git fetch ./"sub proj" HEAD &&
199                 git subtree add --prefix="sub dir" FETCH_HEAD
200         ) &&
201         test_create_commit "$test_count/sub proj" sub2 &&
202         (
203                 cd "$test_count" &&
204                 git fetch ./"sub proj" HEAD &&
205                 git subtree merge --prefix="sub dir" --message="Merged changes from subproject" FETCH_HEAD &&
206                 check_equal "$(last_commit_message)" "Merged changes from subproject"
207         )
208 '
209
210 test_expect_success 'merge new subproj history into sub dir/ with --squash and --prefix and --message' '
211         subtree_test_create_repo "$test_count/sub proj" &&
212         subtree_test_create_repo "$test_count" &&
213         test_create_commit "$test_count" main1 &&
214         test_create_commit "$test_count/sub proj" sub1 &&
215         (
216                 cd "$test_count" &&
217                 git fetch ./"sub proj" HEAD &&
218                 git subtree add --prefix="sub dir" FETCH_HEAD
219         ) &&
220         test_create_commit "$test_count/sub proj" sub2 &&
221         (
222                 cd "$test_count" &&
223                 git fetch ./"sub proj" HEAD &&
224                 git subtree merge --prefix="sub dir" --message="Merged changes from subproject using squash" --squash FETCH_HEAD &&
225                 check_equal "$(last_commit_message)" "Merged changes from subproject using squash"
226         )
227 '
228
229 test_expect_success 'merge the added subproj again, should do nothing' '
230         subtree_test_create_repo "$test_count" &&
231         subtree_test_create_repo "$test_count/sub proj" &&
232         test_create_commit "$test_count" main1 &&
233         test_create_commit "$test_count/sub proj" sub1 &&
234         (
235                 cd "$test_count" &&
236                 git fetch ./"sub proj" HEAD &&
237                 git subtree add --prefix="sub dir" FETCH_HEAD &&
238                 # this shouldn not actually do anything, since FETCH_HEAD
239                 # is already a parent
240                 result=$(git merge -s ours -m "merge -s -ours" FETCH_HEAD) &&
241                 check_equal "${result}" "Already up to date."
242         )
243 '
244
245 test_expect_success 'merge new subproj history into subdir/ with a slash appended to the argument of --prefix' '
246         test_create_repo "$test_count" &&
247         test_create_repo "$test_count/subproj" &&
248         test_create_commit "$test_count" main1 &&
249         test_create_commit "$test_count/subproj" sub1 &&
250         (
251                 cd "$test_count" &&
252                 git fetch ./subproj HEAD &&
253                 git subtree add --prefix=subdir/ FETCH_HEAD
254         ) &&
255         test_create_commit "$test_count/subproj" sub2 &&
256         (
257                 cd "$test_count" &&
258                 git fetch ./subproj HEAD &&
259                 git subtree merge --prefix=subdir/ FETCH_HEAD &&
260                 check_equal "$(last_commit_message)" "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''"
261         )
262 '
263
264 #
265 # Tests for 'git subtree split'
266 #
267
268 test_expect_success 'split requires option --prefix' '
269         subtree_test_create_repo "$test_count" &&
270         subtree_test_create_repo "$test_count/sub proj" &&
271         test_create_commit "$test_count" main1 &&
272         test_create_commit "$test_count/sub proj" sub1 &&
273         (
274                 cd "$test_count" &&
275                 git fetch ./"sub proj" HEAD &&
276                 git subtree add --prefix="sub dir" FETCH_HEAD &&
277                 echo "You must provide the --prefix option." > expected &&
278                 test_must_fail git subtree split > actual 2>&1 &&
279                 test_debug "printf '"expected: "'" &&
280                 test_debug "cat expected" &&
281                 test_debug "printf '"actual: "'" &&
282                 test_debug "cat actual" &&
283                 test_cmp expected actual
284         )
285 '
286
287 test_expect_success 'split requires path given by option --prefix must exist' '
288         subtree_test_create_repo "$test_count" &&
289         subtree_test_create_repo "$test_count/sub proj" &&
290         test_create_commit "$test_count" main1 &&
291         test_create_commit "$test_count/sub proj" sub1 &&
292         (
293                 cd "$test_count" &&
294                 git fetch ./"sub proj" HEAD &&
295                 git subtree add --prefix="sub dir" FETCH_HEAD &&
296                 echo "'\''non-existent-directory'\'' does not exist; use '\''git subtree add'\''" > expected &&
297                 test_must_fail git subtree split --prefix=non-existent-directory > actual 2>&1 &&
298                 test_debug "printf '"expected: "'" &&
299                 test_debug "cat expected" &&
300                 test_debug "printf '"actual: "'" &&
301                 test_debug "cat actual" &&
302                 test_cmp expected actual
303         )
304 '
305
306 test_expect_success 'split sub dir/ with --rejoin' '
307         subtree_test_create_repo "$test_count" &&
308         subtree_test_create_repo "$test_count/sub proj" &&
309         test_create_commit "$test_count" main1 &&
310         test_create_commit "$test_count/sub proj" sub1 &&
311         (
312                 cd "$test_count" &&
313                 git fetch ./"sub proj" HEAD &&
314                 git subtree add --prefix="sub dir" FETCH_HEAD
315         ) &&
316         test_create_commit "$test_count" "sub dir"/main-sub1 &&
317         test_create_commit "$test_count" main2 &&
318         test_create_commit "$test_count/sub proj" sub2 &&
319         test_create_commit "$test_count" "sub dir"/main-sub2 &&
320         (
321                 cd "$test_count" &&
322                 git fetch ./"sub proj" HEAD &&
323                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
324                 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
325                 git subtree split --prefix="sub dir" --annotate="*" --rejoin &&
326                 check_equal "$(last_commit_message)" "Split '\''sub dir/'\'' into commit '\''$split_hash'\''"
327         )
328  '
329
330 test_expect_success 'split sub dir/ with --rejoin from scratch' '
331         subtree_test_create_repo "$test_count" &&
332         test_create_commit "$test_count" main1 &&
333         (
334                 cd "$test_count" &&
335                 mkdir "sub dir" &&
336                 echo file >"sub dir"/file &&
337                 git add "sub dir/file" &&
338                 git commit -m"sub dir file" &&
339                 split_hash=$(git subtree split --prefix="sub dir" --rejoin) &&
340                 git subtree split --prefix="sub dir" --rejoin &&
341                 check_equal "$(last_commit_message)" "Split '\''sub dir/'\'' into commit '\''$split_hash'\''"
342         )
343  '
344
345 test_expect_success 'split sub dir/ with --rejoin and --message' '
346         subtree_test_create_repo "$test_count" &&
347         subtree_test_create_repo "$test_count/sub proj" &&
348         test_create_commit "$test_count" main1 &&
349         test_create_commit "$test_count/sub proj" sub1 &&
350         (
351                 cd "$test_count" &&
352                 git fetch ./"sub proj" HEAD &&
353                 git subtree add --prefix="sub dir" FETCH_HEAD
354         ) &&
355         test_create_commit "$test_count" "sub dir"/main-sub1 &&
356         test_create_commit "$test_count" main2 &&
357         test_create_commit "$test_count/sub proj" sub2 &&
358         test_create_commit "$test_count" "sub dir"/main-sub2 &&
359         (
360                 cd "$test_count" &&
361                 git fetch ./"sub proj" HEAD &&
362                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
363                 git subtree split --prefix="sub dir" --message="Split & rejoin" --annotate="*" --rejoin &&
364                 check_equal "$(last_commit_message)" "Split & rejoin"
365         )
366 '
367
368 test_expect_success 'split "sub dir"/ with --branch' '
369         subtree_test_create_repo "$test_count" &&
370         subtree_test_create_repo "$test_count/sub proj" &&
371         test_create_commit "$test_count" main1 &&
372         test_create_commit "$test_count/sub proj" sub1 &&
373         (
374                 cd "$test_count" &&
375                 git fetch ./"sub proj" HEAD &&
376                 git subtree add --prefix="sub dir" FETCH_HEAD
377         ) &&
378         test_create_commit "$test_count" "sub dir"/main-sub1 &&
379         test_create_commit "$test_count" main2 &&
380         test_create_commit "$test_count/sub proj" sub2 &&
381         test_create_commit "$test_count" "sub dir"/main-sub2 &&
382         (
383                 cd "$test_count" &&
384                 git fetch ./"sub proj" HEAD &&
385                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
386                 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
387                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
388                 check_equal "$(git rev-parse subproj-br)" "$split_hash"
389         )
390 '
391
392 test_expect_success 'check hash of split' '
393         subtree_test_create_repo "$test_count" &&
394         subtree_test_create_repo "$test_count/sub proj" &&
395         test_create_commit "$test_count" main1 &&
396         test_create_commit "$test_count/sub proj" sub1 &&
397         (
398                 cd "$test_count" &&
399                 git fetch ./"sub proj" HEAD &&
400                 git subtree add --prefix="sub dir" FETCH_HEAD
401         ) &&
402         test_create_commit "$test_count" "sub dir"/main-sub1 &&
403         test_create_commit "$test_count" main2 &&
404         test_create_commit "$test_count/sub proj" sub2 &&
405         test_create_commit "$test_count" "sub dir"/main-sub2 &&
406         (
407                 cd "$test_count" &&
408                 git fetch ./"sub proj" HEAD &&
409                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
410                 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
411                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
412                 check_equal "$(git rev-parse subproj-br)" "$split_hash" &&
413                 # Check hash of split
414                 new_hash=$(git rev-parse subproj-br^2) &&
415                 (
416                         cd ./"sub proj" &&
417                         subdir_hash=$(git rev-parse HEAD) &&
418                         check_equal ''"$new_hash"'' "$subdir_hash"
419                 )
420         )
421 '
422
423 test_expect_success 'split "sub dir"/ with --branch for an existing branch' '
424         subtree_test_create_repo "$test_count" &&
425         subtree_test_create_repo "$test_count/sub proj" &&
426         test_create_commit "$test_count" main1 &&
427         test_create_commit "$test_count/sub proj" sub1 &&
428         (
429                 cd "$test_count" &&
430                 git fetch ./"sub proj" HEAD &&
431                 git branch subproj-br FETCH_HEAD &&
432                 git subtree add --prefix="sub dir" FETCH_HEAD
433         ) &&
434         test_create_commit "$test_count" "sub dir"/main-sub1 &&
435         test_create_commit "$test_count" main2 &&
436         test_create_commit "$test_count/sub proj" sub2 &&
437         test_create_commit "$test_count" "sub dir"/main-sub2 &&
438         (
439                 cd "$test_count" &&
440                 git fetch ./"sub proj" HEAD &&
441                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
442                 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
443                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
444                 check_equal "$(git rev-parse subproj-br)" "$split_hash"
445         )
446 '
447
448 test_expect_success 'split "sub dir"/ with --branch for an incompatible branch' '
449         subtree_test_create_repo "$test_count" &&
450         subtree_test_create_repo "$test_count/sub proj" &&
451         test_create_commit "$test_count" main1 &&
452         test_create_commit "$test_count/sub proj" sub1 &&
453         (
454                 cd "$test_count" &&
455                 git branch init HEAD &&
456                 git fetch ./"sub proj" HEAD &&
457                 git subtree add --prefix="sub dir" FETCH_HEAD
458         ) &&
459         test_create_commit "$test_count" "sub dir"/main-sub1 &&
460         test_create_commit "$test_count" main2 &&
461         test_create_commit "$test_count/sub proj" sub2 &&
462         test_create_commit "$test_count" "sub dir"/main-sub2 &&
463         (
464                 cd "$test_count" &&
465                 git fetch ./"sub proj" HEAD &&
466                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
467                 test_must_fail git subtree split --prefix="sub dir" --branch init
468         )
469 '
470
471 #
472 # Validity checking
473 #
474
475 test_expect_success 'make sure exactly the right set of files ends up in the subproj' '
476         subtree_test_create_repo "$test_count" &&
477         subtree_test_create_repo "$test_count/sub proj" &&
478         test_create_commit "$test_count" main1 &&
479         test_create_commit "$test_count/sub proj" sub1 &&
480         (
481                 cd "$test_count" &&
482                 git fetch ./"sub proj" HEAD &&
483                 git subtree add --prefix="sub dir" FETCH_HEAD
484         ) &&
485         test_create_commit "$test_count" "sub dir"/main-sub1 &&
486         test_create_commit "$test_count" main2 &&
487         test_create_commit "$test_count/sub proj" sub2 &&
488         test_create_commit "$test_count" "sub dir"/main-sub2 &&
489         (
490                 cd "$test_count" &&
491                 git fetch ./"sub proj" HEAD &&
492                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
493                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
494         ) &&
495         test_create_commit "$test_count/sub proj" sub3 &&
496         test_create_commit "$test_count" "sub dir"/main-sub3 &&
497         (
498                 cd "$test_count/sub proj" &&
499                 git fetch .. subproj-br &&
500                 git merge FETCH_HEAD
501         ) &&
502         test_create_commit "$test_count/sub proj" sub4 &&
503         (
504                 cd "$test_count" &&
505                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
506         ) &&
507         test_create_commit "$test_count" "sub dir"/main-sub4 &&
508         (
509                 cd "$test_count" &&
510                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
511         ) &&
512         (
513                 cd "$test_count/sub proj" &&
514                 git fetch .. subproj-br &&
515                 git merge FETCH_HEAD &&
516
517                 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 \
518                         sub1 sub2 sub3 sub4 >expect &&
519                 git ls-files >actual &&
520                 test_cmp expect actual
521         )
522 '
523
524 test_expect_success 'make sure the subproj *only* contains commits that affect the "sub dir"' '
525         subtree_test_create_repo "$test_count" &&
526         subtree_test_create_repo "$test_count/sub proj" &&
527         test_create_commit "$test_count" main1 &&
528         test_create_commit "$test_count/sub proj" sub1 &&
529         (
530                 cd "$test_count" &&
531                 git fetch ./"sub proj" HEAD &&
532                 git subtree add --prefix="sub dir" FETCH_HEAD
533         ) &&
534         test_create_commit "$test_count" "sub dir"/main-sub1 &&
535         test_create_commit "$test_count" main2 &&
536         test_create_commit "$test_count/sub proj" sub2 &&
537         test_create_commit "$test_count" "sub dir"/main-sub2 &&
538         (
539                 cd "$test_count" &&
540                 git fetch ./"sub proj" HEAD &&
541                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
542                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
543         ) &&
544         test_create_commit "$test_count/sub proj" sub3 &&
545         test_create_commit "$test_count" "sub dir"/main-sub3 &&
546         (
547                 cd "$test_count/sub proj" &&
548                 git fetch .. subproj-br &&
549                 git merge FETCH_HEAD
550         ) &&
551         test_create_commit "$test_count/sub proj" sub4 &&
552         (
553                 cd "$test_count" &&
554                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
555         ) &&
556         test_create_commit "$test_count" "sub dir"/main-sub4 &&
557         (
558                 cd "$test_count" &&
559                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
560         ) &&
561         (
562                 cd "$test_count/sub proj" &&
563                 git fetch .. subproj-br &&
564                 git merge FETCH_HEAD &&
565
566                 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 \
567                         sub1 sub2 sub3 sub4 >expect &&
568                 git log --name-only --pretty=format:"" >log &&
569                 sort <log | sed "/^\$/ d" >actual &&
570                 test_cmp expect actual
571         )
572 '
573
574 test_expect_success 'make sure exactly the right set of files ends up in the mainline' '
575         subtree_test_create_repo "$test_count" &&
576         subtree_test_create_repo "$test_count/sub proj" &&
577         test_create_commit "$test_count" main1 &&
578         test_create_commit "$test_count/sub proj" sub1 &&
579         (
580                 cd "$test_count" &&
581                 git fetch ./"sub proj" HEAD &&
582                 git subtree add --prefix="sub dir" FETCH_HEAD
583         ) &&
584         test_create_commit "$test_count" "sub dir"/main-sub1 &&
585         test_create_commit "$test_count" main2 &&
586         test_create_commit "$test_count/sub proj" sub2 &&
587         test_create_commit "$test_count" "sub dir"/main-sub2 &&
588         (
589                 cd "$test_count" &&
590                 git fetch ./"sub proj" HEAD &&
591                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
592                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
593         ) &&
594         test_create_commit "$test_count/sub proj" sub3 &&
595         test_create_commit "$test_count" "sub dir"/main-sub3 &&
596         (
597                 cd "$test_count/sub proj" &&
598                 git fetch .. subproj-br &&
599                 git merge FETCH_HEAD
600         ) &&
601         test_create_commit "$test_count/sub proj" sub4 &&
602         (
603                 cd "$test_count" &&
604                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
605         ) &&
606         test_create_commit "$test_count" "sub dir"/main-sub4 &&
607         (
608                 cd "$test_count" &&
609                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
610         ) &&
611         (
612                 cd "$test_count/sub proj" &&
613                 git fetch .. subproj-br &&
614                 git merge FETCH_HEAD
615         ) &&
616         (
617                 cd "$test_count" &&
618                 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
619
620                 test_write_lines main1 main2 >chkm &&
621                 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 >chkms &&
622                 sed "s,^,sub dir/," chkms >chkms_sub &&
623                 test_write_lines sub1 sub2 sub3 sub4 >chks &&
624                 sed "s,^,sub dir/," chks >chks_sub &&
625
626                 cat chkm chkms_sub chks_sub >expect &&
627                 git ls-files >actual &&
628                 test_cmp expect actual
629         )
630 '
631
632 test_expect_success 'make sure each filename changed exactly once in the entire history' '
633         subtree_test_create_repo "$test_count" &&
634         subtree_test_create_repo "$test_count/sub proj" &&
635         test_create_commit "$test_count" main1 &&
636         test_create_commit "$test_count/sub proj" sub1 &&
637         (
638                 cd "$test_count" &&
639                 git config log.date relative &&
640                 git fetch ./"sub proj" HEAD &&
641                 git subtree add --prefix="sub dir" FETCH_HEAD
642         ) &&
643         test_create_commit "$test_count" "sub dir"/main-sub1 &&
644         test_create_commit "$test_count" main2 &&
645         test_create_commit "$test_count/sub proj" sub2 &&
646         test_create_commit "$test_count" "sub dir"/main-sub2 &&
647         (
648                 cd "$test_count" &&
649                 git fetch ./"sub proj" HEAD &&
650                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
651                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
652         ) &&
653         test_create_commit "$test_count/sub proj" sub3 &&
654         test_create_commit "$test_count" "sub dir"/main-sub3 &&
655         (
656                 cd "$test_count/sub proj" &&
657                 git fetch .. subproj-br &&
658                 git merge FETCH_HEAD
659         ) &&
660         test_create_commit "$test_count/sub proj" sub4 &&
661         (
662                 cd "$test_count" &&
663                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
664         ) &&
665         test_create_commit "$test_count" "sub dir"/main-sub4 &&
666         (
667                 cd "$test_count" &&
668                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
669         ) &&
670         (
671                 cd "$test_count/sub proj" &&
672                 git fetch .. subproj-br &&
673                 git merge FETCH_HEAD
674         ) &&
675         (
676                 cd "$test_count" &&
677                 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
678
679                 test_write_lines main1 main2 >chkm &&
680                 test_write_lines sub1 sub2 sub3 sub4 >chks &&
681                 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 >chkms &&
682                 sed "s,^,sub dir/," chkms >chkms_sub &&
683
684                 # main-sub?? and /"sub dir"/main-sub?? both change, because those are the
685                 # changes that were split into their own history.  And "sub dir"/sub?? never
686                 # change, since they were *only* changed in the subtree branch.
687                 git log --name-only --pretty=format:"" >log &&
688                 sort <log >sorted-log &&
689                 sed "/^$/ d" sorted-log >actual &&
690
691                 cat chkms chkm chks chkms_sub >expect-unsorted &&
692                 sort expect-unsorted >expect &&
693                 test_cmp expect actual
694         )
695 '
696
697 test_expect_success 'make sure the --rejoin commits never make it into subproj' '
698         subtree_test_create_repo "$test_count" &&
699         subtree_test_create_repo "$test_count/sub proj" &&
700         test_create_commit "$test_count" main1 &&
701         test_create_commit "$test_count/sub proj" sub1 &&
702         (
703                 cd "$test_count" &&
704                 git fetch ./"sub proj" HEAD &&
705                 git subtree add --prefix="sub dir" FETCH_HEAD
706         ) &&
707         test_create_commit "$test_count" "sub dir"/main-sub1 &&
708         test_create_commit "$test_count" main2 &&
709         test_create_commit "$test_count/sub proj" sub2 &&
710         test_create_commit "$test_count" "sub dir"/main-sub2 &&
711         (
712                 cd "$test_count" &&
713                 git fetch ./"sub proj" HEAD &&
714                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
715                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
716         ) &&
717         test_create_commit "$test_count/sub proj" sub3 &&
718         test_create_commit "$test_count" "sub dir"/main-sub3 &&
719         (
720                 cd "$test_count/sub proj" &&
721                 git fetch .. subproj-br &&
722                 git merge FETCH_HEAD
723         ) &&
724         test_create_commit "$test_count/sub proj" sub4 &&
725         (
726                 cd "$test_count" &&
727                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
728         ) &&
729         test_create_commit "$test_count" "sub dir"/main-sub4 &&
730         (
731                 cd "$test_count" &&
732                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
733         ) &&
734         (
735                 cd "$test_count/sub proj" &&
736                 git fetch .. subproj-br &&
737                 git merge FETCH_HEAD
738         ) &&
739         (
740                 cd "$test_count" &&
741                 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
742                 check_equal "$(git log --pretty=format:"%s" HEAD^2 | grep -i split)" ""
743         )
744 '
745
746 test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' '
747         subtree_test_create_repo "$test_count" &&
748         subtree_test_create_repo "$test_count/sub proj" &&
749         test_create_commit "$test_count" main1 &&
750         test_create_commit "$test_count/sub proj" sub1 &&
751         (
752                 cd "$test_count" &&
753                 git fetch ./"sub proj" HEAD &&
754                 git subtree add --prefix="sub dir" FETCH_HEAD
755         ) &&
756         test_create_commit "$test_count" "sub dir"/main-sub1 &&
757         test_create_commit "$test_count" main2 &&
758         test_create_commit "$test_count/sub proj" sub2 &&
759         test_create_commit "$test_count" "sub dir"/main-sub2 &&
760         (
761                 cd "$test_count" &&
762                 git fetch ./"sub proj" HEAD &&
763                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
764                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
765         ) &&
766         test_create_commit "$test_count/sub proj" sub3 &&
767         test_create_commit "$test_count" "sub dir"/main-sub3 &&
768         (
769                 cd "$test_count/sub proj" &&
770                 git fetch .. subproj-br &&
771                  git merge FETCH_HEAD
772         ) &&
773         test_create_commit "$test_count/sub proj" sub4 &&
774         (
775                 cd "$test_count" &&
776                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
777         ) &&
778         test_create_commit "$test_count" "sub dir"/main-sub4 &&
779         (
780                 cd "$test_count" &&
781                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
782         ) &&
783         (
784                 cd "$test_count/sub proj" &&
785                 git fetch .. subproj-br &&
786                 git merge FETCH_HEAD
787         ) &&
788         (
789                 cd "$test_count" &&
790                 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
791
792                 # They are meaningless to subproj since one side of the merge refers to the mainline
793                 check_equal "$(git log --pretty=format:"%s%n%b" HEAD^2 | grep "git-subtree.*:")" ""
794         )
795 '
796
797 #
798 # A new set of tests
799 #
800
801 test_expect_success 'make sure "git subtree split" find the correct parent' '
802         subtree_test_create_repo "$test_count" &&
803         subtree_test_create_repo "$test_count/sub proj" &&
804         test_create_commit "$test_count" main1 &&
805         test_create_commit "$test_count/sub proj" sub1 &&
806         (
807                 cd "$test_count" &&
808                 git fetch ./"sub proj" HEAD &&
809                 git subtree add --prefix="sub dir" FETCH_HEAD
810         ) &&
811         test_create_commit "$test_count/sub proj" sub2 &&
812         (
813                 cd "$test_count" &&
814                 git fetch ./"sub proj" HEAD &&
815                 git branch subproj-ref FETCH_HEAD &&
816                 git subtree merge --prefix="sub dir" FETCH_HEAD
817         ) &&
818         test_create_commit "$test_count" "sub dir"/main-sub1 &&
819         (
820                 cd "$test_count" &&
821                 git subtree split --prefix="sub dir" --branch subproj-br &&
822
823                 # at this point, the new commit parent should be subproj-ref, if it is
824                 # not, something went wrong (the "newparent" of "HEAD~" commit should
825                 # have been sub2, but it was not, because its cache was not set to
826                 # itself)
827                 check_equal "$(git log --pretty=format:%P -1 subproj-br)" "$(git rev-parse subproj-ref)"
828         )
829 '
830
831 test_expect_success 'split a new subtree without --onto option' '
832         subtree_test_create_repo "$test_count" &&
833         subtree_test_create_repo "$test_count/sub proj" &&
834         test_create_commit "$test_count" main1 &&
835         test_create_commit "$test_count/sub proj" sub1 &&
836         (
837                 cd "$test_count" &&
838                 git fetch ./"sub proj" HEAD &&
839                 git subtree add --prefix="sub dir" FETCH_HEAD
840         ) &&
841         test_create_commit "$test_count/sub proj" sub2 &&
842         (
843                 cd "$test_count" &&
844                 git fetch ./"sub proj" HEAD &&
845                 git subtree merge --prefix="sub dir" FETCH_HEAD
846         ) &&
847         test_create_commit "$test_count" "sub dir"/main-sub1 &&
848         (
849                 cd "$test_count" &&
850                 git subtree split --prefix="sub dir" --branch subproj-br
851         ) &&
852         mkdir "$test_count"/"sub dir2" &&
853         test_create_commit "$test_count" "sub dir2"/main-sub2 &&
854         (
855                 cd "$test_count" &&
856
857                 # also test that we still can split out an entirely new subtree
858                 # if the parent of the first commit in the tree is not empty,
859                 # then the new subtree has accidentally been attached to something
860                 git subtree split --prefix="sub dir2" --branch subproj2-br &&
861                 check_equal "$(git log --pretty=format:%P -1 subproj2-br)" ""
862         )
863 '
864
865 test_expect_success 'verify one file change per commit' '
866         subtree_test_create_repo "$test_count" &&
867         subtree_test_create_repo "$test_count/sub proj" &&
868         test_create_commit "$test_count" main1 &&
869         test_create_commit "$test_count/sub proj" sub1 &&
870         (
871                 cd "$test_count" &&
872                 git fetch ./"sub proj" HEAD &&
873                 git branch sub1 FETCH_HEAD &&
874                 git subtree add --prefix="sub dir" sub1
875         ) &&
876         test_create_commit "$test_count/sub proj" sub2 &&
877         (
878                 cd "$test_count" &&
879                 git fetch ./"sub proj" HEAD &&
880                 git subtree merge --prefix="sub dir" FETCH_HEAD
881         ) &&
882         test_create_commit "$test_count" "sub dir"/main-sub1 &&
883         (
884                 cd "$test_count" &&
885                 git subtree split --prefix="sub dir" --branch subproj-br
886         ) &&
887         mkdir "$test_count"/"sub dir2" &&
888         test_create_commit "$test_count" "sub dir2"/main-sub2 &&
889         (
890                 cd "$test_count" &&
891                 git subtree split --prefix="sub dir2" --branch subproj2-br &&
892
893                 x= &&
894                 git log --pretty=format:"commit: %H" | join_commits |
895                 (
896                         while read commit a b; do
897                                 test_debug "echo Verifying commit $commit"
898                                 test_debug "echo a: $a"
899                                 test_debug "echo b: $b"
900                                 check_equal "$b" ""
901                                 x=1
902                         done
903                         check_equal "$x" 1
904                 )
905         )
906 '
907
908 test_expect_success 'push split to subproj' '
909         subtree_test_create_repo "$test_count" &&
910         subtree_test_create_repo "$test_count/sub proj" &&
911         test_create_commit "$test_count" main1 &&
912         test_create_commit "$test_count/sub proj" sub1 &&
913         (
914                 cd "$test_count" &&
915                 git fetch ./"sub proj" HEAD &&
916                 git subtree add --prefix="sub dir" FETCH_HEAD
917         ) &&
918         test_create_commit "$test_count" "sub dir"/main-sub1 &&
919         test_create_commit "$test_count" main2 &&
920         test_create_commit "$test_count/sub proj" sub2 &&
921         test_create_commit "$test_count" "sub dir"/main-sub2 &&
922         (
923                 cd $test_count/"sub proj" &&
924                 git branch sub-branch-1 &&
925                 cd .. &&
926                 git fetch ./"sub proj" HEAD &&
927                 git subtree merge --prefix="sub dir" FETCH_HEAD
928         ) &&
929         test_create_commit "$test_count" "sub dir"/main-sub3 &&
930         (
931                 cd "$test_count" &&
932                 git subtree push ./"sub proj" --prefix "sub dir" sub-branch-1 &&
933                 cd ./"sub proj" &&
934                 git checkout sub-branch-1 &&
935                 check_equal "$(last_commit_message)" "sub dir/main-sub3"
936         )
937 '
938
939 #
940 # This test covers 2 cases in subtree split copy_or_skip code
941 # 1) Merges where one parent is a superset of the changes of the other
942 #    parent regarding changes to the subtree, in this case the merge
943 #    commit should be copied
944 # 2) Merges where only one parent operate on the subtree, and the merge
945 #    commit should be skipped
946 #
947 # (1) is checked by ensuring subtree_tip is a descendent of subtree_branch
948 # (2) should have a check added (not_a_subtree_change shouldn't be present
949 #     on the produced subtree)
950 #
951 # Other related cases which are not tested (or currently handled correctly)
952 # - Case (1) where there are more than 2 parents, it will sometimes correctly copy
953 #   the merge, and sometimes not
954 # - Merge commit where both parents have same tree as the merge, currently
955 #   will always be skipped, even if they reached that state via different
956 #   set of commits.
957 #
958
959 test_expect_success 'subtree descendant check' '
960         subtree_test_create_repo "$test_count" &&
961         defaultBranch=$(sed "s,ref: refs/heads/,," "$test_count/.git/HEAD") &&
962         test_create_commit "$test_count" folder_subtree/a &&
963         (
964                 cd "$test_count" &&
965                 git branch branch
966         ) &&
967         test_create_commit "$test_count" folder_subtree/0 &&
968         test_create_commit "$test_count" folder_subtree/b &&
969         cherry=$(cd "$test_count"; git rev-parse HEAD) &&
970         (
971                 cd "$test_count" &&
972                 git checkout branch
973         ) &&
974         test_create_commit "$test_count" commit_on_branch &&
975         (
976                 cd "$test_count" &&
977                 git cherry-pick $cherry &&
978                 git checkout $defaultBranch &&
979                 git merge -m "merge should be kept on subtree" branch &&
980                 git branch no_subtree_work_branch
981         ) &&
982         test_create_commit "$test_count" folder_subtree/d &&
983         (
984                 cd "$test_count" &&
985                 git checkout no_subtree_work_branch
986         ) &&
987         test_create_commit "$test_count" not_a_subtree_change &&
988         (
989                 cd "$test_count" &&
990                 git checkout $defaultBranch &&
991                 git merge -m "merge should be skipped on subtree" no_subtree_work_branch &&
992
993                 git subtree split --prefix folder_subtree/ --branch subtree_tip $defaultBranch &&
994                 git subtree split --prefix folder_subtree/ --branch subtree_branch branch &&
995                 check_equal $(git rev-list --count subtree_tip..subtree_branch) 0
996         )
997 '
998
999 test_done