Merge branch 'mt/t2080-cp-symlink-fix'
[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, merge, split, pull,
9 and push 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 test_wrong_flag() {
37         test_must_fail "$@" >out 2>err &&
38         test_must_be_empty out &&
39         grep "flag does not make sense with" err
40 }
41
42 last_commit_subject () {
43         git log --pretty=format:%s -1
44 }
45
46 test_expect_success 'shows short help text for -h' '
47         test_expect_code 129 git subtree -h >out 2>err &&
48         test_must_be_empty err &&
49         grep -e "^ *or: git subtree pull" out &&
50         grep -e --annotate out
51 '
52
53 #
54 # Tests for 'git subtree add'
55 #
56
57 test_expect_success 'no merge from non-existent subtree' '
58         subtree_test_create_repo "$test_count" &&
59         subtree_test_create_repo "$test_count/sub proj" &&
60         test_create_commit "$test_count" main1 &&
61         test_create_commit "$test_count/sub proj" sub1 &&
62         (
63                 cd "$test_count" &&
64                 git fetch ./"sub proj" HEAD &&
65                 test_must_fail git subtree merge --prefix="sub dir" FETCH_HEAD
66         )
67 '
68
69 test_expect_success 'no pull from non-existent subtree' '
70         subtree_test_create_repo "$test_count" &&
71         subtree_test_create_repo "$test_count/sub proj" &&
72         test_create_commit "$test_count" main1 &&
73         test_create_commit "$test_count/sub proj" sub1 &&
74         (
75                 cd "$test_count" &&
76                 git fetch ./"sub proj" HEAD &&
77                 test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" HEAD
78         )
79 '
80
81 test_expect_success 'add rejects flags for split' '
82         subtree_test_create_repo "$test_count" &&
83         subtree_test_create_repo "$test_count/sub proj" &&
84         test_create_commit "$test_count" main1 &&
85         test_create_commit "$test_count/sub proj" sub1 &&
86         (
87                 cd "$test_count" &&
88                 git fetch ./"sub proj" HEAD &&
89                 test_wrong_flag git subtree add --prefix="sub dir" --annotate=foo FETCH_HEAD &&
90                 test_wrong_flag git subtree add --prefix="sub dir" --branch=foo FETCH_HEAD &&
91                 test_wrong_flag git subtree add --prefix="sub dir" --ignore-joins FETCH_HEAD &&
92                 test_wrong_flag git subtree add --prefix="sub dir" --onto=foo FETCH_HEAD &&
93                 test_wrong_flag git subtree add --prefix="sub dir" --rejoin FETCH_HEAD
94         )
95 '
96
97 test_expect_success 'add subproj as subtree into sub dir/ with --prefix' '
98         subtree_test_create_repo "$test_count" &&
99         subtree_test_create_repo "$test_count/sub proj" &&
100         test_create_commit "$test_count" main1 &&
101         test_create_commit "$test_count/sub proj" sub1 &&
102         (
103                 cd "$test_count" &&
104                 git fetch ./"sub proj" HEAD &&
105                 git subtree add --prefix="sub dir" FETCH_HEAD &&
106                 test "$(last_commit_subject)" = "Add '\''sub dir/'\'' from commit '\''$(git rev-parse FETCH_HEAD)'\''"
107         )
108 '
109
110 test_expect_success 'add subproj as subtree into sub dir/ with --prefix and --message' '
111         subtree_test_create_repo "$test_count" &&
112         subtree_test_create_repo "$test_count/sub proj" &&
113         test_create_commit "$test_count" main1 &&
114         test_create_commit "$test_count/sub proj" sub1 &&
115         (
116                 cd "$test_count" &&
117                 git fetch ./"sub proj" HEAD &&
118                 git subtree add --prefix="sub dir" --message="Added subproject" FETCH_HEAD &&
119                 test "$(last_commit_subject)" = "Added subproject"
120         )
121 '
122
123 test_expect_success 'add subproj as subtree into sub dir/ with --prefix as -P and --message as -m' '
124         subtree_test_create_repo "$test_count" &&
125         subtree_test_create_repo "$test_count/sub proj" &&
126         test_create_commit "$test_count" main1 &&
127         test_create_commit "$test_count/sub proj" sub1 &&
128         (
129                 cd "$test_count" &&
130                 git fetch ./"sub proj" HEAD &&
131                 git subtree add -P "sub dir" -m "Added subproject" FETCH_HEAD &&
132                 test "$(last_commit_subject)" = "Added subproject"
133         )
134 '
135
136 test_expect_success 'add subproj as subtree into sub dir/ with --squash and --prefix and --message' '
137         subtree_test_create_repo "$test_count" &&
138         subtree_test_create_repo "$test_count/sub proj" &&
139         test_create_commit "$test_count" main1 &&
140         test_create_commit "$test_count/sub proj" sub1 &&
141         (
142                 cd "$test_count" &&
143                 git fetch ./"sub proj" HEAD &&
144                 git subtree add --prefix="sub dir" --message="Added subproject with squash" --squash FETCH_HEAD &&
145                 test "$(last_commit_subject)" = "Added subproject with squash"
146         )
147 '
148
149 #
150 # Tests for 'git subtree merge'
151 #
152
153 test_expect_success 'merge rejects flags for split' '
154         subtree_test_create_repo "$test_count" &&
155         subtree_test_create_repo "$test_count/sub proj" &&
156         test_create_commit "$test_count" main1 &&
157         test_create_commit "$test_count/sub proj" sub1 &&
158         (
159                 cd "$test_count" &&
160                 git fetch ./"sub proj" HEAD &&
161                 git subtree add --prefix="sub dir" FETCH_HEAD
162         ) &&
163         test_create_commit "$test_count/sub proj" sub2 &&
164         (
165                 cd "$test_count" &&
166                 git fetch ./"sub proj" HEAD &&
167                 test_wrong_flag git subtree merge --prefix="sub dir" --annotate=foo FETCH_HEAD &&
168                 test_wrong_flag git subtree merge --prefix="sub dir" --branch=foo FETCH_HEAD &&
169                 test_wrong_flag git subtree merge --prefix="sub dir" --ignore-joins FETCH_HEAD &&
170                 test_wrong_flag git subtree merge --prefix="sub dir" --onto=foo FETCH_HEAD &&
171                 test_wrong_flag git subtree merge --prefix="sub dir" --rejoin FETCH_HEAD
172         )
173 '
174
175 test_expect_success 'merge new subproj history into sub dir/ with --prefix' '
176         subtree_test_create_repo "$test_count" &&
177         subtree_test_create_repo "$test_count/sub proj" &&
178         test_create_commit "$test_count" main1 &&
179         test_create_commit "$test_count/sub proj" sub1 &&
180         (
181                 cd "$test_count" &&
182                 git fetch ./"sub proj" HEAD &&
183                 git subtree add --prefix="sub dir" FETCH_HEAD
184         ) &&
185         test_create_commit "$test_count/sub proj" sub2 &&
186         (
187                 cd "$test_count" &&
188                 git fetch ./"sub proj" HEAD &&
189                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
190                 test "$(last_commit_subject)" = "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''"
191         )
192 '
193
194 test_expect_success 'merge new subproj history into sub dir/ with --prefix and --message' '
195         subtree_test_create_repo "$test_count" &&
196         subtree_test_create_repo "$test_count/sub proj" &&
197         test_create_commit "$test_count" main1 &&
198         test_create_commit "$test_count/sub proj" sub1 &&
199         (
200                 cd "$test_count" &&
201                 git fetch ./"sub proj" HEAD &&
202                 git subtree add --prefix="sub dir" FETCH_HEAD
203         ) &&
204         test_create_commit "$test_count/sub proj" sub2 &&
205         (
206                 cd "$test_count" &&
207                 git fetch ./"sub proj" HEAD &&
208                 git subtree merge --prefix="sub dir" --message="Merged changes from subproject" FETCH_HEAD &&
209                 test "$(last_commit_subject)" = "Merged changes from subproject"
210         )
211 '
212
213 test_expect_success 'merge new subproj history into sub dir/ with --squash and --prefix and --message' '
214         subtree_test_create_repo "$test_count/sub proj" &&
215         subtree_test_create_repo "$test_count" &&
216         test_create_commit "$test_count" main1 &&
217         test_create_commit "$test_count/sub proj" sub1 &&
218         (
219                 cd "$test_count" &&
220                 git fetch ./"sub proj" HEAD &&
221                 git subtree add --prefix="sub dir" FETCH_HEAD
222         ) &&
223         test_create_commit "$test_count/sub proj" sub2 &&
224         (
225                 cd "$test_count" &&
226                 git fetch ./"sub proj" HEAD &&
227                 git subtree merge --prefix="sub dir" --message="Merged changes from subproject using squash" --squash FETCH_HEAD &&
228                 test "$(last_commit_subject)" = "Merged changes from subproject using squash"
229         )
230 '
231
232 test_expect_success 'merge the added subproj again, should do nothing' '
233         subtree_test_create_repo "$test_count" &&
234         subtree_test_create_repo "$test_count/sub proj" &&
235         test_create_commit "$test_count" main1 &&
236         test_create_commit "$test_count/sub proj" sub1 &&
237         (
238                 cd "$test_count" &&
239                 git fetch ./"sub proj" HEAD &&
240                 git subtree add --prefix="sub dir" FETCH_HEAD &&
241                 # this shouldn not actually do anything, since FETCH_HEAD
242                 # is already a parent
243                 result=$(git merge -s ours -m "merge -s -ours" FETCH_HEAD) &&
244                 test "${result}" = "Already up to date."
245         )
246 '
247
248 test_expect_success 'merge new subproj history into subdir/ with a slash appended to the argument of --prefix' '
249         subtree_test_create_repo "$test_count" &&
250         subtree_test_create_repo "$test_count/subproj" &&
251         test_create_commit "$test_count" main1 &&
252         test_create_commit "$test_count/subproj" sub1 &&
253         (
254                 cd "$test_count" &&
255                 git fetch ./subproj HEAD &&
256                 git subtree add --prefix=subdir/ FETCH_HEAD
257         ) &&
258         test_create_commit "$test_count/subproj" sub2 &&
259         (
260                 cd "$test_count" &&
261                 git fetch ./subproj HEAD &&
262                 git subtree merge --prefix=subdir/ FETCH_HEAD &&
263                 test "$(last_commit_subject)" = "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''"
264         )
265 '
266
267 #
268 # Tests for 'git subtree split'
269 #
270
271 test_expect_success 'split requires option --prefix' '
272         subtree_test_create_repo "$test_count" &&
273         subtree_test_create_repo "$test_count/sub proj" &&
274         test_create_commit "$test_count" main1 &&
275         test_create_commit "$test_count/sub proj" sub1 &&
276         (
277                 cd "$test_count" &&
278                 git fetch ./"sub proj" HEAD &&
279                 git subtree add --prefix="sub dir" FETCH_HEAD &&
280                 echo "You must provide the --prefix option." >expected &&
281                 test_must_fail git subtree split >actual 2>&1 &&
282                 test_debug "printf '"expected: "'" &&
283                 test_debug "cat expected" &&
284                 test_debug "printf '"actual: "'" &&
285                 test_debug "cat actual" &&
286                 test_cmp expected actual
287         )
288 '
289
290 test_expect_success 'split requires path given by option --prefix must exist' '
291         subtree_test_create_repo "$test_count" &&
292         subtree_test_create_repo "$test_count/sub proj" &&
293         test_create_commit "$test_count" main1 &&
294         test_create_commit "$test_count/sub proj" sub1 &&
295         (
296                 cd "$test_count" &&
297                 git fetch ./"sub proj" HEAD &&
298                 git subtree add --prefix="sub dir" FETCH_HEAD &&
299                 echo "'\''non-existent-directory'\'' does not exist; use '\''git subtree add'\''" >expected &&
300                 test_must_fail git subtree split --prefix=non-existent-directory >actual 2>&1 &&
301                 test_debug "printf '"expected: "'" &&
302                 test_debug "cat expected" &&
303                 test_debug "printf '"actual: "'" &&
304                 test_debug "cat actual" &&
305                 test_cmp expected actual
306         )
307 '
308
309 test_expect_success 'split rejects flags for add' '
310         subtree_test_create_repo "$test_count" &&
311         subtree_test_create_repo "$test_count/sub proj" &&
312         test_create_commit "$test_count" main1 &&
313         test_create_commit "$test_count/sub proj" sub1 &&
314         (
315                 cd "$test_count" &&
316                 git fetch ./"sub proj" HEAD &&
317                 git subtree add --prefix="sub dir" FETCH_HEAD
318         ) &&
319         test_create_commit "$test_count" "sub dir"/main-sub1 &&
320         test_create_commit "$test_count" main2 &&
321         test_create_commit "$test_count/sub proj" sub2 &&
322         test_create_commit "$test_count" "sub dir"/main-sub2 &&
323         (
324                 cd "$test_count" &&
325                 git fetch ./"sub proj" HEAD &&
326                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
327                 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
328                 test_wrong_flag git subtree split --prefix="sub dir" --squash &&
329                 test_wrong_flag git subtree split --prefix="sub dir" --message=foo
330         )
331 '
332
333 test_expect_success 'split sub dir/ with --rejoin' '
334         subtree_test_create_repo "$test_count" &&
335         subtree_test_create_repo "$test_count/sub proj" &&
336         test_create_commit "$test_count" main1 &&
337         test_create_commit "$test_count/sub proj" sub1 &&
338         (
339                 cd "$test_count" &&
340                 git fetch ./"sub proj" HEAD &&
341                 git subtree add --prefix="sub dir" FETCH_HEAD
342         ) &&
343         test_create_commit "$test_count" "sub dir"/main-sub1 &&
344         test_create_commit "$test_count" main2 &&
345         test_create_commit "$test_count/sub proj" sub2 &&
346         test_create_commit "$test_count" "sub dir"/main-sub2 &&
347         (
348                 cd "$test_count" &&
349                 git fetch ./"sub proj" HEAD &&
350                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
351                 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
352                 git subtree split --prefix="sub dir" --annotate="*" --rejoin &&
353                 test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''"
354         )
355 '
356
357 test_expect_success 'split sub dir/ with --rejoin from scratch' '
358         subtree_test_create_repo "$test_count" &&
359         test_create_commit "$test_count" main1 &&
360         (
361                 cd "$test_count" &&
362                 mkdir "sub dir" &&
363                 echo file >"sub dir"/file &&
364                 git add "sub dir/file" &&
365                 git commit -m"sub dir file" &&
366                 split_hash=$(git subtree split --prefix="sub dir" --rejoin) &&
367                 git subtree split --prefix="sub dir" --rejoin &&
368                 test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''"
369         )
370 '
371
372 test_expect_success 'split sub dir/ with --rejoin and --message' '
373         subtree_test_create_repo "$test_count" &&
374         subtree_test_create_repo "$test_count/sub proj" &&
375         test_create_commit "$test_count" main1 &&
376         test_create_commit "$test_count/sub proj" sub1 &&
377         (
378                 cd "$test_count" &&
379                 git fetch ./"sub proj" HEAD &&
380                 git subtree add --prefix="sub dir" FETCH_HEAD
381         ) &&
382         test_create_commit "$test_count" "sub dir"/main-sub1 &&
383         test_create_commit "$test_count" main2 &&
384         test_create_commit "$test_count/sub proj" sub2 &&
385         test_create_commit "$test_count" "sub dir"/main-sub2 &&
386         (
387                 cd "$test_count" &&
388                 git fetch ./"sub proj" HEAD &&
389                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
390                 git subtree split --prefix="sub dir" --message="Split & rejoin" --annotate="*" --rejoin &&
391                 test "$(last_commit_subject)" = "Split & rejoin"
392         )
393 '
394
395 test_expect_success 'split "sub dir"/ with --rejoin and --squash' '
396         subtree_test_create_repo "$test_count" &&
397         subtree_test_create_repo "$test_count/sub proj" &&
398         test_create_commit "$test_count" main1 &&
399         test_create_commit "$test_count/sub proj" sub1 &&
400         (
401                 cd "$test_count" &&
402                 git fetch ./"sub proj" HEAD &&
403                 git subtree add --prefix="sub dir" --squash FETCH_HEAD
404         ) &&
405         test_create_commit "$test_count" "sub dir"/main-sub1 &&
406         test_create_commit "$test_count" main2 &&
407         test_create_commit "$test_count/sub proj" sub2 &&
408         test_create_commit "$test_count" "sub dir"/main-sub2 &&
409         (
410                 cd "$test_count" &&
411                 git subtree pull --prefix="sub dir" --squash ./"sub proj" HEAD &&
412                 MAIN=$(git rev-parse --verify HEAD) &&
413                 SUB=$(git -C "sub proj" rev-parse --verify HEAD) &&
414
415                 SPLIT=$(git subtree split --prefix="sub dir" --annotate="*" --rejoin --squash) &&
416
417                 test_must_fail git merge-base --is-ancestor $SUB HEAD &&
418                 test_must_fail git merge-base --is-ancestor $SPLIT HEAD &&
419                 git rev-list HEAD ^$MAIN >commit-list &&
420                 test_line_count = 2 commit-list &&
421                 test "$(git rev-parse --verify HEAD:)"           = "$(git rev-parse --verify $MAIN:)" &&
422                 test "$(git rev-parse --verify HEAD:"sub dir")"  = "$(git rev-parse --verify $SPLIT:)" &&
423                 test "$(git rev-parse --verify HEAD^1)"          = $MAIN &&
424                 test "$(git rev-parse --verify HEAD^2)"         != $SPLIT &&
425                 test "$(git rev-parse --verify HEAD^2:)"         = "$(git rev-parse --verify $SPLIT:)" &&
426                 test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$SPLIT'\''"
427         )
428 '
429
430 test_expect_success 'split then pull "sub dir"/ with --rejoin and --squash' '
431         # 1. "add"
432         subtree_test_create_repo "$test_count" &&
433         subtree_test_create_repo "$test_count/sub proj" &&
434         test_create_commit "$test_count" main1 &&
435         test_create_commit "$test_count/sub proj" sub1 &&
436         git -C "$test_count" subtree --prefix="sub dir" add --squash ./"sub proj" HEAD &&
437
438         # 2. commit from parent
439         test_create_commit "$test_count" "sub dir"/main-sub1 &&
440
441         # 3. "split --rejoin --squash"
442         git -C "$test_count" subtree --prefix="sub dir" split --rejoin --squash &&
443
444         # 4. "pull --squash"
445         test_create_commit "$test_count/sub proj" sub2 &&
446         git -C "$test_count" subtree -d --prefix="sub dir" pull --squash ./"sub proj" HEAD &&
447
448         test_must_fail git merge-base HEAD FETCH_HEAD
449 '
450
451 test_expect_success 'split "sub dir"/ with --branch' '
452         subtree_test_create_repo "$test_count" &&
453         subtree_test_create_repo "$test_count/sub proj" &&
454         test_create_commit "$test_count" main1 &&
455         test_create_commit "$test_count/sub proj" sub1 &&
456         (
457                 cd "$test_count" &&
458                 git fetch ./"sub proj" HEAD &&
459                 git subtree add --prefix="sub dir" FETCH_HEAD
460         ) &&
461         test_create_commit "$test_count" "sub dir"/main-sub1 &&
462         test_create_commit "$test_count" main2 &&
463         test_create_commit "$test_count/sub proj" sub2 &&
464         test_create_commit "$test_count" "sub dir"/main-sub2 &&
465         (
466                 cd "$test_count" &&
467                 git fetch ./"sub proj" HEAD &&
468                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
469                 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
470                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
471                 test "$(git rev-parse subproj-br)" = "$split_hash"
472         )
473 '
474
475 test_expect_success 'check hash of split' '
476         subtree_test_create_repo "$test_count" &&
477         subtree_test_create_repo "$test_count/sub proj" &&
478         test_create_commit "$test_count" main1 &&
479         test_create_commit "$test_count/sub proj" sub1 &&
480         (
481                 cd "$test_count" &&
482                 git fetch ./"sub proj" HEAD &&
483                 git subtree add --prefix="sub dir" FETCH_HEAD
484         ) &&
485         test_create_commit "$test_count" "sub dir"/main-sub1 &&
486         test_create_commit "$test_count" main2 &&
487         test_create_commit "$test_count/sub proj" sub2 &&
488         test_create_commit "$test_count" "sub dir"/main-sub2 &&
489         (
490                 cd "$test_count" &&
491                 git fetch ./"sub proj" HEAD &&
492                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
493                 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
494                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
495                 test "$(git rev-parse subproj-br)" = "$split_hash" &&
496                 # Check hash of split
497                 new_hash=$(git rev-parse subproj-br^2) &&
498                 (
499                         cd ./"sub proj" &&
500                         subdir_hash=$(git rev-parse HEAD) &&
501                         test "$new_hash" = "$subdir_hash"
502                 )
503         )
504 '
505
506 test_expect_success 'split "sub dir"/ with --branch for an existing branch' '
507         subtree_test_create_repo "$test_count" &&
508         subtree_test_create_repo "$test_count/sub proj" &&
509         test_create_commit "$test_count" main1 &&
510         test_create_commit "$test_count/sub proj" sub1 &&
511         (
512                 cd "$test_count" &&
513                 git fetch ./"sub proj" HEAD &&
514                 git branch subproj-br FETCH_HEAD &&
515                 git subtree add --prefix="sub dir" FETCH_HEAD
516         ) &&
517         test_create_commit "$test_count" "sub dir"/main-sub1 &&
518         test_create_commit "$test_count" main2 &&
519         test_create_commit "$test_count/sub proj" sub2 &&
520         test_create_commit "$test_count" "sub dir"/main-sub2 &&
521         (
522                 cd "$test_count" &&
523                 git fetch ./"sub proj" HEAD &&
524                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
525                 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
526                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
527                 test "$(git rev-parse subproj-br)" = "$split_hash"
528         )
529 '
530
531 test_expect_success 'split "sub dir"/ with --branch for an incompatible branch' '
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 branch init HEAD &&
539                 git fetch ./"sub proj" HEAD &&
540                 git subtree add --prefix="sub dir" FETCH_HEAD
541         ) &&
542         test_create_commit "$test_count" "sub dir"/main-sub1 &&
543         test_create_commit "$test_count" main2 &&
544         test_create_commit "$test_count/sub proj" sub2 &&
545         test_create_commit "$test_count" "sub dir"/main-sub2 &&
546         (
547                 cd "$test_count" &&
548                 git fetch ./"sub proj" HEAD &&
549                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
550                 test_must_fail git subtree split --prefix="sub dir" --branch init
551         )
552 '
553
554 #
555 # Tests for 'git subtree pull'
556 #
557
558 test_expect_success 'pull requires option --prefix' '
559         subtree_test_create_repo "$test_count" &&
560         subtree_test_create_repo "$test_count/sub proj" &&
561         test_create_commit "$test_count" main1 &&
562         test_create_commit "$test_count/sub proj" sub1 &&
563         (
564                 cd "$test_count" &&
565                 git fetch ./"sub proj" HEAD &&
566                 git subtree add --prefix="sub dir" FETCH_HEAD
567         ) &&
568         test_create_commit "$test_count/sub proj" sub2 &&
569         (
570                 cd "$test_count" &&
571                 test_must_fail git subtree pull ./"sub proj" HEAD >out 2>err &&
572
573                 echo "You must provide the --prefix option." >expected &&
574                 test_must_be_empty out &&
575                 test_cmp expected err
576         )
577 '
578
579 test_expect_success 'pull requires path given by option --prefix must exist' '
580         subtree_test_create_repo "$test_count" &&
581         subtree_test_create_repo "$test_count/sub proj" &&
582         test_create_commit "$test_count" main1 &&
583         test_create_commit "$test_count/sub proj" sub1 &&
584         (
585                 test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" HEAD >out 2>err &&
586
587                 echo "'\''sub dir'\'' does not exist; use '\''git subtree add'\''" >expected &&
588                 test_must_be_empty out &&
589                 test_cmp expected err
590         )
591 '
592
593 test_expect_success 'pull basic operation' '
594         subtree_test_create_repo "$test_count" &&
595         subtree_test_create_repo "$test_count/sub proj" &&
596         test_create_commit "$test_count" main1 &&
597         test_create_commit "$test_count/sub proj" sub1 &&
598         (
599                 cd "$test_count" &&
600                 git fetch ./"sub proj" HEAD &&
601                 git subtree add --prefix="sub dir" FETCH_HEAD
602         ) &&
603         test_create_commit "$test_count/sub proj" sub2 &&
604         (
605                 cd "$test_count" &&
606                 exp=$(git -C "sub proj" rev-parse --verify HEAD:) &&
607                 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
608                 act=$(git rev-parse --verify HEAD:"sub dir") &&
609                 test "$act" = "$exp"
610         )
611 '
612
613 test_expect_success 'pull rejects flags for split' '
614         subtree_test_create_repo "$test_count" &&
615         subtree_test_create_repo "$test_count/sub proj" &&
616         test_create_commit "$test_count" main1 &&
617         test_create_commit "$test_count/sub proj" sub1 &&
618         (
619                 cd "$test_count" &&
620                 git fetch ./"sub proj" HEAD &&
621                 git subtree add --prefix="sub dir" FETCH_HEAD
622         ) &&
623         test_create_commit "$test_count/sub proj" sub2 &&
624         (
625                 test_must_fail git subtree pull --prefix="sub dir" --annotate=foo ./"sub proj" HEAD &&
626                 test_must_fail git subtree pull --prefix="sub dir" --branch=foo ./"sub proj" HEAD &&
627                 test_must_fail git subtree pull --prefix="sub dir" --ignore-joins ./"sub proj" HEAD &&
628                 test_must_fail git subtree pull --prefix="sub dir" --onto=foo ./"sub proj" HEAD &&
629                 test_must_fail git subtree pull --prefix="sub dir" --rejoin ./"sub proj" HEAD
630         )
631 '
632
633 #
634 # Tests for 'git subtree push'
635 #
636
637 test_expect_success 'push requires option --prefix' '
638         subtree_test_create_repo "$test_count" &&
639         subtree_test_create_repo "$test_count/sub proj" &&
640         test_create_commit "$test_count" main1 &&
641         test_create_commit "$test_count/sub proj" sub1 &&
642         (
643                 cd "$test_count" &&
644                 git fetch ./"sub proj" HEAD &&
645                 git subtree add --prefix="sub dir" FETCH_HEAD &&
646                 echo "You must provide the --prefix option." >expected &&
647                 test_must_fail git subtree push "./sub proj" from-mainline >actual 2>&1 &&
648                 test_debug "printf '"expected: "'" &&
649                 test_debug "cat expected" &&
650                 test_debug "printf '"actual: "'" &&
651                 test_debug "cat actual" &&
652                 test_cmp expected actual
653         )
654 '
655
656 test_expect_success 'push requires path given by option --prefix must exist' '
657         subtree_test_create_repo "$test_count" &&
658         subtree_test_create_repo "$test_count/sub proj" &&
659         test_create_commit "$test_count" main1 &&
660         test_create_commit "$test_count/sub proj" sub1 &&
661         (
662                 cd "$test_count" &&
663                 git fetch ./"sub proj" HEAD &&
664                 git subtree add --prefix="sub dir" FETCH_HEAD &&
665                 echo "'\''non-existent-directory'\'' does not exist; use '\''git subtree add'\''" >expected &&
666                 test_must_fail git subtree push --prefix=non-existent-directory "./sub proj" from-mainline >actual 2>&1 &&
667                 test_debug "printf '"expected: "'" &&
668                 test_debug "cat expected" &&
669                 test_debug "printf '"actual: "'" &&
670                 test_debug "cat actual" &&
671                 test_cmp expected actual
672         )
673 '
674
675 test_expect_success 'push rejects flags for add' '
676         subtree_test_create_repo "$test_count" &&
677         subtree_test_create_repo "$test_count/sub proj" &&
678         test_create_commit "$test_count" main1 &&
679         test_create_commit "$test_count/sub proj" sub1 &&
680         (
681                 cd "$test_count" &&
682                 git fetch ./"sub proj" HEAD &&
683                 git subtree add --prefix="sub dir" FETCH_HEAD
684         ) &&
685         test_create_commit "$test_count" "sub dir"/main-sub1 &&
686         test_create_commit "$test_count" main2 &&
687         test_create_commit "$test_count/sub proj" sub2 &&
688         test_create_commit "$test_count" "sub dir"/main-sub2 &&
689         (
690                 cd "$test_count" &&
691                 git fetch ./"sub proj" HEAD &&
692                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
693                 test_wrong_flag git subtree split --prefix="sub dir" --squash ./"sub proj" from-mainline &&
694                 test_wrong_flag git subtree split --prefix="sub dir" --message=foo ./"sub proj" from-mainline
695         )
696 '
697
698 test_expect_success 'push basic operation' '
699         subtree_test_create_repo "$test_count" &&
700         subtree_test_create_repo "$test_count/sub proj" &&
701         test_create_commit "$test_count" main1 &&
702         test_create_commit "$test_count/sub proj" sub1 &&
703         (
704                 cd "$test_count" &&
705                 git fetch ./"sub proj" HEAD &&
706                 git subtree add --prefix="sub dir" FETCH_HEAD
707         ) &&
708         test_create_commit "$test_count" "sub dir"/main-sub1 &&
709         test_create_commit "$test_count" main2 &&
710         test_create_commit "$test_count/sub proj" sub2 &&
711         test_create_commit "$test_count" "sub dir"/main-sub2 &&
712         (
713                 cd "$test_count" &&
714                 git fetch ./"sub proj" HEAD &&
715                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
716                 before=$(git rev-parse --verify HEAD) &&
717                 split_hash=$(git subtree split --prefix="sub dir") &&
718                 git subtree push --prefix="sub dir" ./"sub proj" from-mainline &&
719                 test "$before" = "$(git rev-parse --verify HEAD)" &&
720                 test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
721         )
722 '
723
724 test_expect_success 'push sub dir/ with --rejoin' '
725         subtree_test_create_repo "$test_count" &&
726         subtree_test_create_repo "$test_count/sub proj" &&
727         test_create_commit "$test_count" main1 &&
728         test_create_commit "$test_count/sub proj" sub1 &&
729         (
730                 cd "$test_count" &&
731                 git fetch ./"sub proj" HEAD &&
732                 git subtree add --prefix="sub dir" FETCH_HEAD
733         ) &&
734         test_create_commit "$test_count" "sub dir"/main-sub1 &&
735         test_create_commit "$test_count" main2 &&
736         test_create_commit "$test_count/sub proj" sub2 &&
737         test_create_commit "$test_count" "sub dir"/main-sub2 &&
738         (
739                 cd "$test_count" &&
740                 git fetch ./"sub proj" HEAD &&
741                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
742                 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
743                 git subtree push --prefix="sub dir" --annotate="*" --rejoin ./"sub proj" from-mainline &&
744                 test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" &&
745                 test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
746         )
747 '
748
749 test_expect_success 'push sub dir/ with --rejoin from scratch' '
750         subtree_test_create_repo "$test_count" &&
751         test_create_commit "$test_count" main1 &&
752         (
753                 cd "$test_count" &&
754                 mkdir "sub dir" &&
755                 echo file >"sub dir"/file &&
756                 git add "sub dir/file" &&
757                 git commit -m"sub dir file" &&
758                 split_hash=$(git subtree split --prefix="sub dir" --rejoin) &&
759                 git init --bare "sub proj.git" &&
760                 git subtree push --prefix="sub dir" --rejoin ./"sub proj.git" from-mainline &&
761                 test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" &&
762                 test "$split_hash" = "$(git -C "sub proj.git" rev-parse --verify refs/heads/from-mainline)"
763         )
764 '
765
766 test_expect_success 'push sub dir/ with --rejoin and --message' '
767         subtree_test_create_repo "$test_count" &&
768         subtree_test_create_repo "$test_count/sub proj" &&
769         test_create_commit "$test_count" main1 &&
770         test_create_commit "$test_count/sub proj" sub1 &&
771         (
772                 cd "$test_count" &&
773                 git fetch ./"sub proj" HEAD &&
774                 git subtree add --prefix="sub dir" FETCH_HEAD
775         ) &&
776         test_create_commit "$test_count" "sub dir"/main-sub1 &&
777         test_create_commit "$test_count" main2 &&
778         test_create_commit "$test_count/sub proj" sub2 &&
779         test_create_commit "$test_count" "sub dir"/main-sub2 &&
780         (
781                 cd "$test_count" &&
782                 git fetch ./"sub proj" HEAD &&
783                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
784                 git subtree push --prefix="sub dir" --message="Split & rejoin" --annotate="*" --rejoin ./"sub proj" from-mainline &&
785                 test "$(last_commit_subject)" = "Split & rejoin" &&
786                 split_hash="$(git rev-parse --verify HEAD^2)" &&
787                 test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
788         )
789 '
790
791 test_expect_success 'push "sub dir"/ with --rejoin and --squash' '
792         subtree_test_create_repo "$test_count" &&
793         subtree_test_create_repo "$test_count/sub proj" &&
794         test_create_commit "$test_count" main1 &&
795         test_create_commit "$test_count/sub proj" sub1 &&
796         (
797                 cd "$test_count" &&
798                 git fetch ./"sub proj" HEAD &&
799                 git subtree add --prefix="sub dir" --squash FETCH_HEAD
800         ) &&
801         test_create_commit "$test_count" "sub dir"/main-sub1 &&
802         test_create_commit "$test_count" main2 &&
803         test_create_commit "$test_count/sub proj" sub2 &&
804         test_create_commit "$test_count" "sub dir"/main-sub2 &&
805         (
806                 cd "$test_count" &&
807                 git subtree pull --prefix="sub dir" --squash ./"sub proj" HEAD &&
808                 MAIN=$(git rev-parse --verify HEAD) &&
809                 SUB=$(git -C "sub proj" rev-parse --verify HEAD) &&
810
811                 SPLIT=$(git subtree split --prefix="sub dir" --annotate="*") &&
812                 git subtree push --prefix="sub dir" --annotate="*" --rejoin --squash ./"sub proj" from-mainline &&
813
814                 test_must_fail git merge-base --is-ancestor $SUB HEAD &&
815                 test_must_fail git merge-base --is-ancestor $SPLIT HEAD &&
816                 git rev-list HEAD ^$MAIN >commit-list &&
817                 test_line_count = 2 commit-list &&
818                 test "$(git rev-parse --verify HEAD:)"           = "$(git rev-parse --verify $MAIN:)" &&
819                 test "$(git rev-parse --verify HEAD:"sub dir")"  = "$(git rev-parse --verify $SPLIT:)" &&
820                 test "$(git rev-parse --verify HEAD^1)"          = $MAIN &&
821                 test "$(git rev-parse --verify HEAD^2)"         != $SPLIT &&
822                 test "$(git rev-parse --verify HEAD^2:)"         = "$(git rev-parse --verify $SPLIT:)" &&
823                 test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$SPLIT'\''" &&
824                 test "$SPLIT" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
825         )
826 '
827
828 test_expect_success 'push "sub dir"/ with --branch' '
829         subtree_test_create_repo "$test_count" &&
830         subtree_test_create_repo "$test_count/sub proj" &&
831         test_create_commit "$test_count" main1 &&
832         test_create_commit "$test_count/sub proj" sub1 &&
833         (
834                 cd "$test_count" &&
835                 git fetch ./"sub proj" HEAD &&
836                 git subtree add --prefix="sub dir" FETCH_HEAD
837         ) &&
838         test_create_commit "$test_count" "sub dir"/main-sub1 &&
839         test_create_commit "$test_count" main2 &&
840         test_create_commit "$test_count/sub proj" sub2 &&
841         test_create_commit "$test_count" "sub dir"/main-sub2 &&
842         (
843                 cd "$test_count" &&
844                 git fetch ./"sub proj" HEAD &&
845                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
846                 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
847                 git subtree push --prefix="sub dir" --annotate="*" --branch subproj-br ./"sub proj" from-mainline &&
848                 test "$(git rev-parse subproj-br)" = "$split_hash" &&
849                 test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
850         )
851 '
852
853 test_expect_success 'check hash of push' '
854         subtree_test_create_repo "$test_count" &&
855         subtree_test_create_repo "$test_count/sub proj" &&
856         test_create_commit "$test_count" main1 &&
857         test_create_commit "$test_count/sub proj" sub1 &&
858         (
859                 cd "$test_count" &&
860                 git fetch ./"sub proj" HEAD &&
861                 git subtree add --prefix="sub dir" FETCH_HEAD
862         ) &&
863         test_create_commit "$test_count" "sub dir"/main-sub1 &&
864         test_create_commit "$test_count" main2 &&
865         test_create_commit "$test_count/sub proj" sub2 &&
866         test_create_commit "$test_count" "sub dir"/main-sub2 &&
867         (
868                 cd "$test_count" &&
869                 git fetch ./"sub proj" HEAD &&
870                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
871                 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
872                 git subtree push --prefix="sub dir" --annotate="*" --branch subproj-br ./"sub proj" from-mainline &&
873                 test "$(git rev-parse subproj-br)" = "$split_hash" &&
874                 # Check hash of split
875                 new_hash=$(git rev-parse subproj-br^2) &&
876                 (
877                         cd ./"sub proj" &&
878                         subdir_hash=$(git rev-parse HEAD) &&
879                         test "$new_hash" = "$subdir_hash"
880                 ) &&
881                 test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
882         )
883 '
884
885 test_expect_success 'push "sub dir"/ with --branch for an existing branch' '
886         subtree_test_create_repo "$test_count" &&
887         subtree_test_create_repo "$test_count/sub proj" &&
888         test_create_commit "$test_count" main1 &&
889         test_create_commit "$test_count/sub proj" sub1 &&
890         (
891                 cd "$test_count" &&
892                 git fetch ./"sub proj" HEAD &&
893                 git branch subproj-br FETCH_HEAD &&
894                 git subtree add --prefix="sub dir" FETCH_HEAD
895         ) &&
896         test_create_commit "$test_count" "sub dir"/main-sub1 &&
897         test_create_commit "$test_count" main2 &&
898         test_create_commit "$test_count/sub proj" sub2 &&
899         test_create_commit "$test_count" "sub dir"/main-sub2 &&
900         (
901                 cd "$test_count" &&
902                 git fetch ./"sub proj" HEAD &&
903                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
904                 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
905                 git subtree push --prefix="sub dir" --annotate="*" --branch subproj-br ./"sub proj" from-mainline &&
906                 test "$(git rev-parse subproj-br)" = "$split_hash" &&
907                 test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
908         )
909 '
910
911 test_expect_success 'push "sub dir"/ with --branch for an incompatible branch' '
912         subtree_test_create_repo "$test_count" &&
913         subtree_test_create_repo "$test_count/sub proj" &&
914         test_create_commit "$test_count" main1 &&
915         test_create_commit "$test_count/sub proj" sub1 &&
916         (
917                 cd "$test_count" &&
918                 git branch init HEAD &&
919                 git fetch ./"sub proj" HEAD &&
920                 git subtree add --prefix="sub dir" FETCH_HEAD
921         ) &&
922         test_create_commit "$test_count" "sub dir"/main-sub1 &&
923         test_create_commit "$test_count" main2 &&
924         test_create_commit "$test_count/sub proj" sub2 &&
925         test_create_commit "$test_count" "sub dir"/main-sub2 &&
926         (
927                 cd "$test_count" &&
928                 git fetch ./"sub proj" HEAD &&
929                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
930                 test_must_fail git subtree push --prefix="sub dir" --branch init "./sub proj" from-mainline
931         )
932 '
933
934 test_expect_success 'push "sub dir"/ with a local rev' '
935         subtree_test_create_repo "$test_count" &&
936         subtree_test_create_repo "$test_count/sub proj" &&
937         test_create_commit "$test_count" main1 &&
938         test_create_commit "$test_count/sub proj" sub1 &&
939         (
940                 cd "$test_count" &&
941                 git fetch ./"sub proj" HEAD &&
942                 git subtree add --prefix="sub dir" FETCH_HEAD
943         ) &&
944         test_create_commit "$test_count" "sub dir"/main-sub1 &&
945         test_create_commit "$test_count" "sub dir"/main-sub2 &&
946         (
947                 cd "$test_count" &&
948                 bad_tree=$(git rev-parse --verify HEAD:"sub dir") &&
949                 good_tree=$(git rev-parse --verify HEAD^:"sub dir") &&
950                 git subtree push --prefix="sub dir" --annotate="*" ./"sub proj" HEAD^:from-mainline &&
951                 split_tree=$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline:) &&
952                 test "$split_tree" = "$good_tree"
953         )
954 '
955
956 #
957 # Validity checking
958 #
959
960 test_expect_success 'make sure exactly the right set of files ends up in the subproj' '
961         subtree_test_create_repo "$test_count" &&
962         subtree_test_create_repo "$test_count/sub proj" &&
963         test_create_commit "$test_count" main1 &&
964         test_create_commit "$test_count/sub proj" sub1 &&
965         (
966                 cd "$test_count" &&
967                 git fetch ./"sub proj" HEAD &&
968                 git subtree add --prefix="sub dir" FETCH_HEAD
969         ) &&
970         test_create_commit "$test_count" "sub dir"/main-sub1 &&
971         test_create_commit "$test_count" main2 &&
972         test_create_commit "$test_count/sub proj" sub2 &&
973         test_create_commit "$test_count" "sub dir"/main-sub2 &&
974         (
975                 cd "$test_count" &&
976                 git fetch ./"sub proj" HEAD &&
977                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
978                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
979         ) &&
980         test_create_commit "$test_count/sub proj" sub3 &&
981         test_create_commit "$test_count" "sub dir"/main-sub3 &&
982         (
983                 cd "$test_count/sub proj" &&
984                 git fetch .. subproj-br &&
985                 git merge FETCH_HEAD
986         ) &&
987         test_create_commit "$test_count/sub proj" sub4 &&
988         (
989                 cd "$test_count" &&
990                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
991         ) &&
992         test_create_commit "$test_count" "sub dir"/main-sub4 &&
993         (
994                 cd "$test_count" &&
995                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
996         ) &&
997         (
998                 cd "$test_count/sub proj" &&
999                 git fetch .. subproj-br &&
1000                 git merge FETCH_HEAD &&
1001
1002                 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 \
1003                         sub1 sub2 sub3 sub4 >expect &&
1004                 git ls-files >actual &&
1005                 test_cmp expect actual
1006         )
1007 '
1008
1009 test_expect_success 'make sure the subproj *only* contains commits that affect the "sub dir"' '
1010         subtree_test_create_repo "$test_count" &&
1011         subtree_test_create_repo "$test_count/sub proj" &&
1012         test_create_commit "$test_count" main1 &&
1013         test_create_commit "$test_count/sub proj" sub1 &&
1014         (
1015                 cd "$test_count" &&
1016                 git fetch ./"sub proj" HEAD &&
1017                 git subtree add --prefix="sub dir" FETCH_HEAD
1018         ) &&
1019         test_create_commit "$test_count" "sub dir"/main-sub1 &&
1020         test_create_commit "$test_count" main2 &&
1021         test_create_commit "$test_count/sub proj" sub2 &&
1022         test_create_commit "$test_count" "sub dir"/main-sub2 &&
1023         (
1024                 cd "$test_count" &&
1025                 git fetch ./"sub proj" HEAD &&
1026                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1027                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1028         ) &&
1029         test_create_commit "$test_count/sub proj" sub3 &&
1030         test_create_commit "$test_count" "sub dir"/main-sub3 &&
1031         (
1032                 cd "$test_count/sub proj" &&
1033                 git fetch .. subproj-br &&
1034                 git merge FETCH_HEAD
1035         ) &&
1036         test_create_commit "$test_count/sub proj" sub4 &&
1037         (
1038                 cd "$test_count" &&
1039                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1040         ) &&
1041         test_create_commit "$test_count" "sub dir"/main-sub4 &&
1042         (
1043                 cd "$test_count" &&
1044                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1045         ) &&
1046         (
1047                 cd "$test_count/sub proj" &&
1048                 git fetch .. subproj-br &&
1049                 git merge FETCH_HEAD &&
1050
1051                 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 \
1052                         sub1 sub2 sub3 sub4 >expect &&
1053                 git log --name-only --pretty=format:"" >log &&
1054                 sort <log | sed "/^\$/ d" >actual &&
1055                 test_cmp expect actual
1056         )
1057 '
1058
1059 test_expect_success 'make sure exactly the right set of files ends up in the mainline' '
1060         subtree_test_create_repo "$test_count" &&
1061         subtree_test_create_repo "$test_count/sub proj" &&
1062         test_create_commit "$test_count" main1 &&
1063         test_create_commit "$test_count/sub proj" sub1 &&
1064         (
1065                 cd "$test_count" &&
1066                 git fetch ./"sub proj" HEAD &&
1067                 git subtree add --prefix="sub dir" FETCH_HEAD
1068         ) &&
1069         test_create_commit "$test_count" "sub dir"/main-sub1 &&
1070         test_create_commit "$test_count" main2 &&
1071         test_create_commit "$test_count/sub proj" sub2 &&
1072         test_create_commit "$test_count" "sub dir"/main-sub2 &&
1073         (
1074                 cd "$test_count" &&
1075                 git fetch ./"sub proj" HEAD &&
1076                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1077                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1078         ) &&
1079         test_create_commit "$test_count/sub proj" sub3 &&
1080         test_create_commit "$test_count" "sub dir"/main-sub3 &&
1081         (
1082                 cd "$test_count/sub proj" &&
1083                 git fetch .. subproj-br &&
1084                 git merge FETCH_HEAD
1085         ) &&
1086         test_create_commit "$test_count/sub proj" sub4 &&
1087         (
1088                 cd "$test_count" &&
1089                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1090         ) &&
1091         test_create_commit "$test_count" "sub dir"/main-sub4 &&
1092         (
1093                 cd "$test_count" &&
1094                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1095         ) &&
1096         (
1097                 cd "$test_count/sub proj" &&
1098                 git fetch .. subproj-br &&
1099                 git merge FETCH_HEAD
1100         ) &&
1101         (
1102                 cd "$test_count" &&
1103                 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
1104
1105                 test_write_lines main1 main2 >chkm &&
1106                 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 >chkms &&
1107                 sed "s,^,sub dir/," chkms >chkms_sub &&
1108                 test_write_lines sub1 sub2 sub3 sub4 >chks &&
1109                 sed "s,^,sub dir/," chks >chks_sub &&
1110
1111                 cat chkm chkms_sub chks_sub >expect &&
1112                 git ls-files >actual &&
1113                 test_cmp expect actual
1114         )
1115 '
1116
1117 test_expect_success 'make sure each filename changed exactly once in the entire history' '
1118         subtree_test_create_repo "$test_count" &&
1119         subtree_test_create_repo "$test_count/sub proj" &&
1120         test_create_commit "$test_count" main1 &&
1121         test_create_commit "$test_count/sub proj" sub1 &&
1122         (
1123                 cd "$test_count" &&
1124                 git config log.date relative &&
1125                 git fetch ./"sub proj" HEAD &&
1126                 git subtree add --prefix="sub dir" FETCH_HEAD
1127         ) &&
1128         test_create_commit "$test_count" "sub dir"/main-sub1 &&
1129         test_create_commit "$test_count" main2 &&
1130         test_create_commit "$test_count/sub proj" sub2 &&
1131         test_create_commit "$test_count" "sub dir"/main-sub2 &&
1132         (
1133                 cd "$test_count" &&
1134                 git fetch ./"sub proj" HEAD &&
1135                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1136                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1137         ) &&
1138         test_create_commit "$test_count/sub proj" sub3 &&
1139         test_create_commit "$test_count" "sub dir"/main-sub3 &&
1140         (
1141                 cd "$test_count/sub proj" &&
1142                 git fetch .. subproj-br &&
1143                 git merge FETCH_HEAD
1144         ) &&
1145         test_create_commit "$test_count/sub proj" sub4 &&
1146         (
1147                 cd "$test_count" &&
1148                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1149         ) &&
1150         test_create_commit "$test_count" "sub dir"/main-sub4 &&
1151         (
1152                 cd "$test_count" &&
1153                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1154         ) &&
1155         (
1156                 cd "$test_count/sub proj" &&
1157                 git fetch .. subproj-br &&
1158                 git merge FETCH_HEAD
1159         ) &&
1160         (
1161                 cd "$test_count" &&
1162                 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
1163
1164                 test_write_lines main1 main2 >chkm &&
1165                 test_write_lines sub1 sub2 sub3 sub4 >chks &&
1166                 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 >chkms &&
1167                 sed "s,^,sub dir/," chkms >chkms_sub &&
1168
1169                 # main-sub?? and /"sub dir"/main-sub?? both change, because those are the
1170                 # changes that were split into their own history.  And "sub dir"/sub?? never
1171                 # change, since they were *only* changed in the subtree branch.
1172                 git log --name-only --pretty=format:"" >log &&
1173                 sort <log >sorted-log &&
1174                 sed "/^$/ d" sorted-log >actual &&
1175
1176                 cat chkms chkm chks chkms_sub >expect-unsorted &&
1177                 sort expect-unsorted >expect &&
1178                 test_cmp expect actual
1179         )
1180 '
1181
1182 test_expect_success 'make sure the --rejoin commits never make it into subproj' '
1183         subtree_test_create_repo "$test_count" &&
1184         subtree_test_create_repo "$test_count/sub proj" &&
1185         test_create_commit "$test_count" main1 &&
1186         test_create_commit "$test_count/sub proj" sub1 &&
1187         (
1188                 cd "$test_count" &&
1189                 git fetch ./"sub proj" HEAD &&
1190                 git subtree add --prefix="sub dir" FETCH_HEAD
1191         ) &&
1192         test_create_commit "$test_count" "sub dir"/main-sub1 &&
1193         test_create_commit "$test_count" main2 &&
1194         test_create_commit "$test_count/sub proj" sub2 &&
1195         test_create_commit "$test_count" "sub dir"/main-sub2 &&
1196         (
1197                 cd "$test_count" &&
1198                 git fetch ./"sub proj" HEAD &&
1199                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1200                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1201         ) &&
1202         test_create_commit "$test_count/sub proj" sub3 &&
1203         test_create_commit "$test_count" "sub dir"/main-sub3 &&
1204         (
1205                 cd "$test_count/sub proj" &&
1206                 git fetch .. subproj-br &&
1207                 git merge FETCH_HEAD
1208         ) &&
1209         test_create_commit "$test_count/sub proj" sub4 &&
1210         (
1211                 cd "$test_count" &&
1212                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1213         ) &&
1214         test_create_commit "$test_count" "sub dir"/main-sub4 &&
1215         (
1216                 cd "$test_count" &&
1217                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1218         ) &&
1219         (
1220                 cd "$test_count/sub proj" &&
1221                 git fetch .. subproj-br &&
1222                 git merge FETCH_HEAD
1223         ) &&
1224         (
1225                 cd "$test_count" &&
1226                 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
1227                 test "$(git log --pretty=format:"%s" HEAD^2 | grep -i split)" = ""
1228         )
1229 '
1230
1231 test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' '
1232         subtree_test_create_repo "$test_count" &&
1233         subtree_test_create_repo "$test_count/sub proj" &&
1234         test_create_commit "$test_count" main1 &&
1235         test_create_commit "$test_count/sub proj" sub1 &&
1236         (
1237                 cd "$test_count" &&
1238                 git fetch ./"sub proj" HEAD &&
1239                 git subtree add --prefix="sub dir" FETCH_HEAD
1240         ) &&
1241         test_create_commit "$test_count" "sub dir"/main-sub1 &&
1242         test_create_commit "$test_count" main2 &&
1243         test_create_commit "$test_count/sub proj" sub2 &&
1244         test_create_commit "$test_count" "sub dir"/main-sub2 &&
1245         (
1246                 cd "$test_count" &&
1247                 git fetch ./"sub proj" HEAD &&
1248                 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1249                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1250         ) &&
1251         test_create_commit "$test_count/sub proj" sub3 &&
1252         test_create_commit "$test_count" "sub dir"/main-sub3 &&
1253         (
1254                 cd "$test_count/sub proj" &&
1255                 git fetch .. subproj-br &&
1256                  git merge FETCH_HEAD
1257         ) &&
1258         test_create_commit "$test_count/sub proj" sub4 &&
1259         (
1260                 cd "$test_count" &&
1261                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1262         ) &&
1263         test_create_commit "$test_count" "sub dir"/main-sub4 &&
1264         (
1265                 cd "$test_count" &&
1266                 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1267         ) &&
1268         (
1269                 cd "$test_count/sub proj" &&
1270                 git fetch .. subproj-br &&
1271                 git merge FETCH_HEAD
1272         ) &&
1273         (
1274                 cd "$test_count" &&
1275                 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
1276
1277                 # They are meaningless to subproj since one side of the merge refers to the mainline
1278                 test "$(git log --pretty=format:"%s%n%b" HEAD^2 | grep "git-subtree.*:")" = ""
1279         )
1280 '
1281
1282 #
1283 # A new set of tests
1284 #
1285
1286 test_expect_success 'make sure "git subtree split" find the correct parent' '
1287         subtree_test_create_repo "$test_count" &&
1288         subtree_test_create_repo "$test_count/sub proj" &&
1289         test_create_commit "$test_count" main1 &&
1290         test_create_commit "$test_count/sub proj" sub1 &&
1291         (
1292                 cd "$test_count" &&
1293                 git fetch ./"sub proj" HEAD &&
1294                 git subtree add --prefix="sub dir" FETCH_HEAD
1295         ) &&
1296         test_create_commit "$test_count/sub proj" sub2 &&
1297         (
1298                 cd "$test_count" &&
1299                 git fetch ./"sub proj" HEAD &&
1300                 git branch subproj-ref FETCH_HEAD &&
1301                 git subtree merge --prefix="sub dir" FETCH_HEAD
1302         ) &&
1303         test_create_commit "$test_count" "sub dir"/main-sub1 &&
1304         (
1305                 cd "$test_count" &&
1306                 git subtree split --prefix="sub dir" --branch subproj-br &&
1307
1308                 # at this point, the new commit parent should be subproj-ref, if it is
1309                 # not, something went wrong (the "newparent" of "HEAD~" commit should
1310                 # have been sub2, but it was not, because its cache was not set to
1311                 # itself)
1312                 test "$(git log --pretty=format:%P -1 subproj-br)" = "$(git rev-parse subproj-ref)"
1313         )
1314 '
1315
1316 test_expect_success 'split a new subtree without --onto option' '
1317         subtree_test_create_repo "$test_count" &&
1318         subtree_test_create_repo "$test_count/sub proj" &&
1319         test_create_commit "$test_count" main1 &&
1320         test_create_commit "$test_count/sub proj" sub1 &&
1321         (
1322                 cd "$test_count" &&
1323                 git fetch ./"sub proj" HEAD &&
1324                 git subtree add --prefix="sub dir" FETCH_HEAD
1325         ) &&
1326         test_create_commit "$test_count/sub proj" sub2 &&
1327         (
1328                 cd "$test_count" &&
1329                 git fetch ./"sub proj" HEAD &&
1330                 git subtree merge --prefix="sub dir" FETCH_HEAD
1331         ) &&
1332         test_create_commit "$test_count" "sub dir"/main-sub1 &&
1333         (
1334                 cd "$test_count" &&
1335                 git subtree split --prefix="sub dir" --branch subproj-br
1336         ) &&
1337         mkdir "$test_count"/"sub dir2" &&
1338         test_create_commit "$test_count" "sub dir2"/main-sub2 &&
1339         (
1340                 cd "$test_count" &&
1341
1342                 # also test that we still can split out an entirely new subtree
1343                 # if the parent of the first commit in the tree is not empty,
1344                 # then the new subtree has accidentally been attached to something
1345                 git subtree split --prefix="sub dir2" --branch subproj2-br &&
1346                 test "$(git log --pretty=format:%P -1 subproj2-br)" = ""
1347         )
1348 '
1349
1350 test_expect_success 'verify one file change per commit' '
1351         subtree_test_create_repo "$test_count" &&
1352         subtree_test_create_repo "$test_count/sub proj" &&
1353         test_create_commit "$test_count" main1 &&
1354         test_create_commit "$test_count/sub proj" sub1 &&
1355         (
1356                 cd "$test_count" &&
1357                 git fetch ./"sub proj" HEAD &&
1358                 git branch sub1 FETCH_HEAD &&
1359                 git subtree add --prefix="sub dir" sub1
1360         ) &&
1361         test_create_commit "$test_count/sub proj" sub2 &&
1362         (
1363                 cd "$test_count" &&
1364                 git fetch ./"sub proj" HEAD &&
1365                 git subtree merge --prefix="sub dir" FETCH_HEAD
1366         ) &&
1367         test_create_commit "$test_count" "sub dir"/main-sub1 &&
1368         (
1369                 cd "$test_count" &&
1370                 git subtree split --prefix="sub dir" --branch subproj-br
1371         ) &&
1372         mkdir "$test_count"/"sub dir2" &&
1373         test_create_commit "$test_count" "sub dir2"/main-sub2 &&
1374         (
1375                 cd "$test_count" &&
1376                 git subtree split --prefix="sub dir2" --branch subproj2-br &&
1377
1378                 git log --format="%H" >commit-list &&
1379                 while read commit
1380                 do
1381                         git log -n1 --format="" --name-only "$commit" >file-list &&
1382                         test_line_count -le 1 file-list || return 1
1383                 done <commit-list
1384         )
1385 '
1386
1387 test_expect_success 'push split to subproj' '
1388         subtree_test_create_repo "$test_count" &&
1389         subtree_test_create_repo "$test_count/sub proj" &&
1390         test_create_commit "$test_count" main1 &&
1391         test_create_commit "$test_count/sub proj" sub1 &&
1392         (
1393                 cd "$test_count" &&
1394                 git fetch ./"sub proj" HEAD &&
1395                 git subtree add --prefix="sub dir" FETCH_HEAD
1396         ) &&
1397         test_create_commit "$test_count" "sub dir"/main-sub1 &&
1398         test_create_commit "$test_count" main2 &&
1399         test_create_commit "$test_count/sub proj" sub2 &&
1400         test_create_commit "$test_count" "sub dir"/main-sub2 &&
1401         (
1402                 cd $test_count/"sub proj" &&
1403                 git branch sub-branch-1 &&
1404                 cd .. &&
1405                 git fetch ./"sub proj" HEAD &&
1406                 git subtree merge --prefix="sub dir" FETCH_HEAD
1407         ) &&
1408         test_create_commit "$test_count" "sub dir"/main-sub3 &&
1409         (
1410                 cd "$test_count" &&
1411                 git subtree push ./"sub proj" --prefix "sub dir" sub-branch-1 &&
1412                 cd ./"sub proj" &&
1413                 git checkout sub-branch-1 &&
1414                 test "$(last_commit_subject)" = "sub dir/main-sub3"
1415         )
1416 '
1417
1418 #
1419 # This test covers 2 cases in subtree split copy_or_skip code
1420 # 1) Merges where one parent is a superset of the changes of the other
1421 #    parent regarding changes to the subtree, in this case the merge
1422 #    commit should be copied
1423 # 2) Merges where only one parent operate on the subtree, and the merge
1424 #    commit should be skipped
1425 #
1426 # (1) is checked by ensuring subtree_tip is a descendent of subtree_branch
1427 # (2) should have a check added (not_a_subtree_change shouldn't be present
1428 #     on the produced subtree)
1429 #
1430 # Other related cases which are not tested (or currently handled correctly)
1431 # - Case (1) where there are more than 2 parents, it will sometimes correctly copy
1432 #   the merge, and sometimes not
1433 # - Merge commit where both parents have same tree as the merge, currently
1434 #   will always be skipped, even if they reached that state via different
1435 #   set of commits.
1436 #
1437
1438 test_expect_success 'subtree descendant check' '
1439         subtree_test_create_repo "$test_count" &&
1440         defaultBranch=$(sed "s,ref: refs/heads/,," "$test_count/.git/HEAD") &&
1441         test_create_commit "$test_count" folder_subtree/a &&
1442         (
1443                 cd "$test_count" &&
1444                 git branch branch
1445         ) &&
1446         test_create_commit "$test_count" folder_subtree/0 &&
1447         test_create_commit "$test_count" folder_subtree/b &&
1448         cherry=$(cd "$test_count"; git rev-parse HEAD) &&
1449         (
1450                 cd "$test_count" &&
1451                 git checkout branch
1452         ) &&
1453         test_create_commit "$test_count" commit_on_branch &&
1454         (
1455                 cd "$test_count" &&
1456                 git cherry-pick $cherry &&
1457                 git checkout $defaultBranch &&
1458                 git merge -m "merge should be kept on subtree" branch &&
1459                 git branch no_subtree_work_branch
1460         ) &&
1461         test_create_commit "$test_count" folder_subtree/d &&
1462         (
1463                 cd "$test_count" &&
1464                 git checkout no_subtree_work_branch
1465         ) &&
1466         test_create_commit "$test_count" not_a_subtree_change &&
1467         (
1468                 cd "$test_count" &&
1469                 git checkout $defaultBranch &&
1470                 git merge -m "merge should be skipped on subtree" no_subtree_work_branch &&
1471
1472                 git subtree split --prefix folder_subtree/ --branch subtree_tip $defaultBranch &&
1473                 git subtree split --prefix folder_subtree/ --branch subtree_branch branch &&
1474                 test $(git rev-list --count subtree_tip..subtree_branch) = 0
1475         )
1476 '
1477
1478 test_done