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