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