3 # Copyright (c) 2012 Avery Pennaraum
4 # Copyright (c) 2015 Alexey Shumkin
6 test_description
='Basic porcelain support for subtrees
8 This test verifies the basic operation of the add, merge, split, pull,
9 and push subcommands of git subtree.
12 TEST_DIRECTORY
=$
(pwd)/..
/..
/..
/t
13 .
"$TEST_DIRECTORY"/test-lib.sh
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
25 test_create_commit
() (
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"
37 test_must_fail
"$@" >out
2>err
&&
38 test_must_be_empty out
&&
39 grep "flag does not make sense with" err
42 last_commit_subject
() {
43 git log
--pretty=format
:%s
-1
46 # Upon 'git subtree add|merge --squash' of an annotated tag,
47 # pre-2.32.0 versions of 'git subtree' would write the hash of the tag
48 # (sub1 below), instead of the commit (sub1^{commit}) in the
49 # "git-subtree-split" trailer.
50 # We imitate this behaviour below using a replace ref.
51 # This function creates 3 repositories:
53 # - $1-sub (added as subtree "sub" in $1)
54 # - $1-clone (clone of $1)
55 test_create_pre2_32_repo
() {
56 subtree_test_create_repo
"$1" &&
57 subtree_test_create_repo
"$1-sub" &&
58 test_commit
-C "$1" main1
&&
59 test_commit
-C "$1-sub" --annotate sub1
&&
60 git
-C "$1" subtree add
--prefix="sub" --squash "../$1-sub" sub1
&&
61 tag
=$
(git
-C "$1" rev-parse FETCH_HEAD
) &&
62 commit
=$
(git
-C "$1" rev-parse FETCH_HEAD^
{commit
}) &&
63 git
-C "$1" log
-1 --format=%B HEAD^
2 >msg
&&
64 test_commit
-C "$1-sub" --annotate sub2
&&
65 git clone
--no-local "$1" "$1-clone" &&
66 new_commit
=$
(sed -e "s/$commit/$tag/" msg | git
-C "$1-clone" commit-tree HEAD^
2^
{tree
}) &&
67 git
-C "$1-clone" replace HEAD^
2 $new_commit
70 test_expect_success
'shows short help text for -h' '
71 test_expect_code 129 git subtree -h >out 2>err &&
72 test_must_be_empty err &&
73 grep -e "^ *or: git subtree pull" out &&
74 grep -F -e "--[no-]annotate" out
78 # Tests for 'git subtree add'
81 test_expect_success
'no merge from non-existent subtree' '
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 &&
88 git fetch ./"sub proj" HEAD &&
89 test_must_fail git subtree merge --prefix="sub dir" FETCH_HEAD
93 test_expect_success
'no pull from non-existent subtree' '
94 subtree_test_create_repo "$test_count" &&
95 subtree_test_create_repo "$test_count/sub proj" &&
96 test_create_commit "$test_count" main1 &&
97 test_create_commit "$test_count/sub proj" sub1 &&
100 git fetch ./"sub proj" HEAD &&
101 test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" HEAD
105 test_expect_success
'add rejects flags for split' '
106 subtree_test_create_repo "$test_count" &&
107 subtree_test_create_repo "$test_count/sub proj" &&
108 test_create_commit "$test_count" main1 &&
109 test_create_commit "$test_count/sub proj" sub1 &&
112 git fetch ./"sub proj" HEAD &&
113 test_wrong_flag git subtree add --prefix="sub dir" --annotate=foo FETCH_HEAD &&
114 test_wrong_flag git subtree add --prefix="sub dir" --branch=foo FETCH_HEAD &&
115 test_wrong_flag git subtree add --prefix="sub dir" --ignore-joins FETCH_HEAD &&
116 test_wrong_flag git subtree add --prefix="sub dir" --onto=foo FETCH_HEAD &&
117 test_wrong_flag git subtree add --prefix="sub dir" --rejoin FETCH_HEAD
121 test_expect_success
'add subproj as subtree into sub dir/ with --prefix' '
122 subtree_test_create_repo "$test_count" &&
123 subtree_test_create_repo "$test_count/sub proj" &&
124 test_create_commit "$test_count" main1 &&
125 test_create_commit "$test_count/sub proj" sub1 &&
128 git fetch ./"sub proj" HEAD &&
129 git subtree add --prefix="sub dir" FETCH_HEAD &&
130 test "$(last_commit_subject)" = "Add '\''sub dir/'\'' from commit '\''$(git rev-parse FETCH_HEAD)'\''"
134 test_expect_success
'add subproj as subtree into sub dir/ with --prefix and --message' '
135 subtree_test_create_repo "$test_count" &&
136 subtree_test_create_repo "$test_count/sub proj" &&
137 test_create_commit "$test_count" main1 &&
138 test_create_commit "$test_count/sub proj" sub1 &&
141 git fetch ./"sub proj" HEAD &&
142 git subtree add --prefix="sub dir" --message="Added subproject" FETCH_HEAD &&
143 test "$(last_commit_subject)" = "Added subproject"
147 test_expect_success
'add subproj as subtree into sub dir/ with --prefix as -P and --message as -m' '
148 subtree_test_create_repo "$test_count" &&
149 subtree_test_create_repo "$test_count/sub proj" &&
150 test_create_commit "$test_count" main1 &&
151 test_create_commit "$test_count/sub proj" sub1 &&
154 git fetch ./"sub proj" HEAD &&
155 git subtree add -P "sub dir" -m "Added subproject" FETCH_HEAD &&
156 test "$(last_commit_subject)" = "Added subproject"
160 test_expect_success
'add subproj as subtree into sub dir/ with --squash and --prefix and --message' '
161 subtree_test_create_repo "$test_count" &&
162 subtree_test_create_repo "$test_count/sub proj" &&
163 test_create_commit "$test_count" main1 &&
164 test_create_commit "$test_count/sub proj" sub1 &&
167 git fetch ./"sub proj" HEAD &&
168 git subtree add --prefix="sub dir" --message="Added subproject with squash" --squash FETCH_HEAD &&
169 test "$(last_commit_subject)" = "Added subproject with squash"
174 # Tests for 'git subtree merge'
177 test_expect_success
'merge rejects flags for split' '
178 subtree_test_create_repo "$test_count" &&
179 subtree_test_create_repo "$test_count/sub proj" &&
180 test_create_commit "$test_count" main1 &&
181 test_create_commit "$test_count/sub proj" sub1 &&
184 git fetch ./"sub proj" HEAD &&
185 git subtree add --prefix="sub dir" FETCH_HEAD
187 test_create_commit "$test_count/sub proj" sub2 &&
190 git fetch ./"sub proj" HEAD &&
191 test_wrong_flag git subtree merge --prefix="sub dir" --annotate=foo FETCH_HEAD &&
192 test_wrong_flag git subtree merge --prefix="sub dir" --branch=foo FETCH_HEAD &&
193 test_wrong_flag git subtree merge --prefix="sub dir" --ignore-joins FETCH_HEAD &&
194 test_wrong_flag git subtree merge --prefix="sub dir" --onto=foo FETCH_HEAD &&
195 test_wrong_flag git subtree merge --prefix="sub dir" --rejoin FETCH_HEAD
199 test_expect_success
'merge new subproj history into sub dir/ with --prefix' '
200 subtree_test_create_repo "$test_count" &&
201 subtree_test_create_repo "$test_count/sub proj" &&
202 test_create_commit "$test_count" main1 &&
203 test_create_commit "$test_count/sub proj" sub1 &&
206 git fetch ./"sub proj" HEAD &&
207 git subtree add --prefix="sub dir" FETCH_HEAD
209 test_create_commit "$test_count/sub proj" sub2 &&
212 git fetch ./"sub proj" HEAD &&
213 git subtree merge --prefix="sub dir" FETCH_HEAD &&
214 test "$(last_commit_subject)" = "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''"
218 test_expect_success
'merge new subproj history into sub dir/ with --prefix and --message' '
219 subtree_test_create_repo "$test_count" &&
220 subtree_test_create_repo "$test_count/sub proj" &&
221 test_create_commit "$test_count" main1 &&
222 test_create_commit "$test_count/sub proj" sub1 &&
225 git fetch ./"sub proj" HEAD &&
226 git subtree add --prefix="sub dir" FETCH_HEAD
228 test_create_commit "$test_count/sub proj" sub2 &&
231 git fetch ./"sub proj" HEAD &&
232 git subtree merge --prefix="sub dir" --message="Merged changes from subproject" FETCH_HEAD &&
233 test "$(last_commit_subject)" = "Merged changes from subproject"
237 test_expect_success
'merge new subproj history into sub dir/ with --squash and --prefix and --message' '
238 subtree_test_create_repo "$test_count/sub proj" &&
239 subtree_test_create_repo "$test_count" &&
240 test_create_commit "$test_count" main1 &&
241 test_create_commit "$test_count/sub proj" sub1 &&
244 git fetch ./"sub proj" HEAD &&
245 git subtree add --prefix="sub dir" FETCH_HEAD
247 test_create_commit "$test_count/sub proj" sub2 &&
250 git fetch ./"sub proj" HEAD &&
251 git subtree merge --prefix="sub dir" --message="Merged changes from subproject using squash" --squash FETCH_HEAD &&
252 test "$(last_commit_subject)" = "Merged changes from subproject using squash"
256 test_expect_success
'merge the added subproj again, should do nothing' '
257 subtree_test_create_repo "$test_count" &&
258 subtree_test_create_repo "$test_count/sub proj" &&
259 test_create_commit "$test_count" main1 &&
260 test_create_commit "$test_count/sub proj" sub1 &&
263 git fetch ./"sub proj" HEAD &&
264 git subtree add --prefix="sub dir" FETCH_HEAD &&
265 # this shouldn not actually do anything, since FETCH_HEAD
266 # is already a parent
267 result=$(git merge -s ours -m "merge -s -ours" FETCH_HEAD) &&
268 test "${result}" = "Already up to date."
272 test_expect_success
'merge new subproj history into subdir/ with a slash appended to the argument of --prefix' '
273 subtree_test_create_repo "$test_count" &&
274 subtree_test_create_repo "$test_count/subproj" &&
275 test_create_commit "$test_count" main1 &&
276 test_create_commit "$test_count/subproj" sub1 &&
279 git fetch ./subproj HEAD &&
280 git subtree add --prefix=subdir/ FETCH_HEAD
282 test_create_commit "$test_count/subproj" sub2 &&
285 git fetch ./subproj HEAD &&
286 git subtree merge --prefix=subdir/ FETCH_HEAD &&
287 test "$(last_commit_subject)" = "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''"
291 test_expect_success
'merge with --squash after annotated tag was added/merged with --squash pre-v2.32.0 ' '
292 test_create_pre2_32_repo "$test_count" &&
293 git -C "$test_count-clone" fetch "../$test_count-sub" sub2 &&
294 test_must_fail git -C "$test_count-clone" subtree merge --prefix="sub" --squash FETCH_HEAD &&
295 git -C "$test_count-clone" subtree merge --prefix="sub" --squash FETCH_HEAD "../$test_count-sub"
299 # Tests for 'git subtree split'
302 test_expect_success
'split requires option --prefix' '
303 subtree_test_create_repo "$test_count" &&
304 subtree_test_create_repo "$test_count/sub proj" &&
305 test_create_commit "$test_count" main1 &&
306 test_create_commit "$test_count/sub proj" sub1 &&
309 git fetch ./"sub proj" HEAD &&
310 git subtree add --prefix="sub dir" FETCH_HEAD &&
311 echo "fatal: you must provide the --prefix option." >expected &&
312 test_must_fail git subtree split >actual 2>&1 &&
313 test_debug "printf '"expected: "'" &&
314 test_debug "cat expected" &&
315 test_debug "printf '"actual: "'" &&
316 test_debug "cat actual" &&
317 test_cmp expected actual
321 test_expect_success
'split requires path given by option --prefix must exist' '
322 subtree_test_create_repo "$test_count" &&
323 subtree_test_create_repo "$test_count/sub proj" &&
324 test_create_commit "$test_count" main1 &&
325 test_create_commit "$test_count/sub proj" sub1 &&
328 git fetch ./"sub proj" HEAD &&
329 git subtree add --prefix="sub dir" FETCH_HEAD &&
330 echo "fatal: '\''non-existent-directory'\'' does not exist; use '\''git subtree add'\''" >expected &&
331 test_must_fail git subtree split --prefix=non-existent-directory >actual 2>&1 &&
332 test_debug "printf '"expected: "'" &&
333 test_debug "cat expected" &&
334 test_debug "printf '"actual: "'" &&
335 test_debug "cat actual" &&
336 test_cmp expected actual
340 test_expect_success
'split rejects flags for add' '
341 subtree_test_create_repo "$test_count" &&
342 subtree_test_create_repo "$test_count/sub proj" &&
343 test_create_commit "$test_count" main1 &&
344 test_create_commit "$test_count/sub proj" sub1 &&
347 git fetch ./"sub proj" HEAD &&
348 git subtree add --prefix="sub dir" FETCH_HEAD
350 test_create_commit "$test_count" "sub dir"/main-sub1 &&
351 test_create_commit "$test_count" main2 &&
352 test_create_commit "$test_count/sub proj" sub2 &&
353 test_create_commit "$test_count" "sub dir"/main-sub2 &&
356 git fetch ./"sub proj" HEAD &&
357 git subtree merge --prefix="sub dir" FETCH_HEAD &&
358 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
359 test_wrong_flag git subtree split --prefix="sub dir" --squash &&
360 test_wrong_flag git subtree split --prefix="sub dir" --message=foo
364 test_expect_success
'split sub dir/ with --rejoin' '
365 subtree_test_create_repo "$test_count" &&
366 subtree_test_create_repo "$test_count/sub proj" &&
367 test_create_commit "$test_count" main1 &&
368 test_create_commit "$test_count/sub proj" sub1 &&
371 git fetch ./"sub proj" HEAD &&
372 git subtree add --prefix="sub dir" FETCH_HEAD
374 test_create_commit "$test_count" "sub dir"/main-sub1 &&
375 test_create_commit "$test_count" main2 &&
376 test_create_commit "$test_count/sub proj" sub2 &&
377 test_create_commit "$test_count" "sub dir"/main-sub2 &&
380 git fetch ./"sub proj" HEAD &&
381 git subtree merge --prefix="sub dir" FETCH_HEAD &&
382 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
383 git subtree split --prefix="sub dir" --annotate="*" --rejoin &&
384 test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''"
388 # Tests that commits from other subtrees are not processed as
391 # This test performs the following:
392 # - Creates Repo with subtrees 'subA' and 'subB'
393 # - Creates commits in the repo including changes to subtrees
394 # - Runs the following 'split' and commit' commands in order:
395 # - Perform 'split' on subtree A
396 # - Perform 'split' on subtree B
397 # - Create new commits with changes to subtree A and B
398 # - Perform split on subtree A
399 # - Check that the commits in subtree B are not processed
400 # as part of the subtree A split
401 test_expect_success
'split with multiple subtrees' '
402 subtree_test_create_repo "$test_count" &&
403 subtree_test_create_repo "$test_count/subA" &&
404 subtree_test_create_repo "$test_count/subB" &&
405 test_create_commit "$test_count" main1 &&
406 test_create_commit "$test_count/subA" subA1 &&
407 test_create_commit "$test_count/subA" subA2 &&
408 test_create_commit "$test_count/subA" subA3 &&
409 test_create_commit "$test_count/subB" subB1 &&
410 git -C "$test_count" fetch ./subA HEAD &&
411 git -C "$test_count" subtree add --prefix=subADir FETCH_HEAD &&
412 git -C "$test_count" fetch ./subB HEAD &&
413 git -C "$test_count" subtree add --prefix=subBDir FETCH_HEAD &&
414 test_create_commit "$test_count" subADir/main-subA1 &&
415 test_create_commit "$test_count" subBDir/main-subB1 &&
416 git -C "$test_count" subtree split --prefix=subADir \
417 --squash --rejoin -m "Sub A Split 1" &&
418 git -C "$test_count" subtree split --prefix=subBDir \
419 --squash --rejoin -m "Sub B Split 1" &&
420 test_create_commit "$test_count" subADir/main-subA2 &&
421 test_create_commit "$test_count" subBDir/main-subB2 &&
422 git -C "$test_count" subtree split --prefix=subADir \
423 --squash --rejoin -m "Sub A Split 2" &&
424 test "$(git -C "$test_count" subtree split --prefix=subBDir \
425 --squash --rejoin -d -m "Sub B Split 1" 2>&1 | grep -w "\[1\]")" = ""
428 test_expect_success
'split sub dir/ with --rejoin from scratch' '
429 subtree_test_create_repo "$test_count" &&
430 test_create_commit "$test_count" main1 &&
434 echo file >"sub dir"/file &&
435 git add "sub dir/file" &&
436 git commit -m"sub dir file" &&
437 split_hash=$(git subtree split --prefix="sub dir" --rejoin) &&
438 git subtree split --prefix="sub dir" --rejoin &&
439 test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''"
443 test_expect_success
'split sub dir/ with --rejoin and --message' '
444 subtree_test_create_repo "$test_count" &&
445 subtree_test_create_repo "$test_count/sub proj" &&
446 test_create_commit "$test_count" main1 &&
447 test_create_commit "$test_count/sub proj" sub1 &&
450 git fetch ./"sub proj" HEAD &&
451 git subtree add --prefix="sub dir" FETCH_HEAD
453 test_create_commit "$test_count" "sub dir"/main-sub1 &&
454 test_create_commit "$test_count" main2 &&
455 test_create_commit "$test_count/sub proj" sub2 &&
456 test_create_commit "$test_count" "sub dir"/main-sub2 &&
459 git fetch ./"sub proj" HEAD &&
460 git subtree merge --prefix="sub dir" FETCH_HEAD &&
461 git subtree split --prefix="sub dir" --message="Split & rejoin" --annotate="*" --rejoin &&
462 test "$(last_commit_subject)" = "Split & rejoin"
466 test_expect_success
'split "sub dir"/ with --rejoin and --squash' '
467 subtree_test_create_repo "$test_count" &&
468 subtree_test_create_repo "$test_count/sub proj" &&
469 test_create_commit "$test_count" main1 &&
470 test_create_commit "$test_count/sub proj" sub1 &&
473 git fetch ./"sub proj" HEAD &&
474 git subtree add --prefix="sub dir" --squash FETCH_HEAD
476 test_create_commit "$test_count" "sub dir"/main-sub1 &&
477 test_create_commit "$test_count" main2 &&
478 test_create_commit "$test_count/sub proj" sub2 &&
479 test_create_commit "$test_count" "sub dir"/main-sub2 &&
482 git subtree pull --prefix="sub dir" --squash ./"sub proj" HEAD &&
483 MAIN=$(git rev-parse --verify HEAD) &&
484 SUB=$(git -C "sub proj" rev-parse --verify HEAD) &&
486 SPLIT=$(git subtree split --prefix="sub dir" --annotate="*" --rejoin --squash) &&
488 test_must_fail git merge-base --is-ancestor $SUB HEAD &&
489 test_must_fail git merge-base --is-ancestor $SPLIT HEAD &&
490 git rev-list HEAD ^$MAIN >commit-list &&
491 test_line_count = 2 commit-list &&
492 test "$(git rev-parse --verify HEAD:)" = "$(git rev-parse --verify $MAIN:)" &&
493 test "$(git rev-parse --verify HEAD:"sub dir")" = "$(git rev-parse --verify $SPLIT:)" &&
494 test "$(git rev-parse --verify HEAD^1)" = $MAIN &&
495 test "$(git rev-parse --verify HEAD^2)" != $SPLIT &&
496 test "$(git rev-parse --verify HEAD^2:)" = "$(git rev-parse --verify $SPLIT:)" &&
497 test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$SPLIT'\''"
501 test_expect_success
'split then pull "sub dir"/ with --rejoin and --squash' '
503 subtree_test_create_repo "$test_count" &&
504 subtree_test_create_repo "$test_count/sub proj" &&
505 test_create_commit "$test_count" main1 &&
506 test_create_commit "$test_count/sub proj" sub1 &&
507 git -C "$test_count" subtree --prefix="sub dir" add --squash ./"sub proj" HEAD &&
509 # 2. commit from parent
510 test_create_commit "$test_count" "sub dir"/main-sub1 &&
512 # 3. "split --rejoin --squash"
513 git -C "$test_count" subtree --prefix="sub dir" split --rejoin --squash &&
516 test_create_commit "$test_count/sub proj" sub2 &&
517 git -C "$test_count" subtree -d --prefix="sub dir" pull --squash ./"sub proj" HEAD &&
519 test_must_fail git merge-base HEAD FETCH_HEAD
522 test_expect_success
'split "sub dir"/ with --branch' '
523 subtree_test_create_repo "$test_count" &&
524 subtree_test_create_repo "$test_count/sub proj" &&
525 test_create_commit "$test_count" main1 &&
526 test_create_commit "$test_count/sub proj" sub1 &&
529 git fetch ./"sub proj" HEAD &&
530 git subtree add --prefix="sub dir" FETCH_HEAD
532 test_create_commit "$test_count" "sub dir"/main-sub1 &&
533 test_create_commit "$test_count" main2 &&
534 test_create_commit "$test_count/sub proj" sub2 &&
535 test_create_commit "$test_count" "sub dir"/main-sub2 &&
538 git fetch ./"sub proj" HEAD &&
539 git subtree merge --prefix="sub dir" FETCH_HEAD &&
540 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
541 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
542 test "$(git rev-parse subproj-br)" = "$split_hash"
546 test_expect_success
'check hash of split' '
547 subtree_test_create_repo "$test_count" &&
548 subtree_test_create_repo "$test_count/sub proj" &&
549 test_create_commit "$test_count" main1 &&
550 test_create_commit "$test_count/sub proj" sub1 &&
553 git fetch ./"sub proj" HEAD &&
554 git subtree add --prefix="sub dir" FETCH_HEAD
556 test_create_commit "$test_count" "sub dir"/main-sub1 &&
557 test_create_commit "$test_count" main2 &&
558 test_create_commit "$test_count/sub proj" sub2 &&
559 test_create_commit "$test_count" "sub dir"/main-sub2 &&
562 git fetch ./"sub proj" HEAD &&
563 git subtree merge --prefix="sub dir" FETCH_HEAD &&
564 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
565 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
566 test "$(git rev-parse subproj-br)" = "$split_hash" &&
567 # Check hash of split
568 new_hash=$(git rev-parse subproj-br^2) &&
571 subdir_hash=$(git rev-parse HEAD) &&
572 test "$new_hash" = "$subdir_hash"
577 test_expect_success
'split "sub dir"/ with --branch for an existing branch' '
578 subtree_test_create_repo "$test_count" &&
579 subtree_test_create_repo "$test_count/sub proj" &&
580 test_create_commit "$test_count" main1 &&
581 test_create_commit "$test_count/sub proj" sub1 &&
584 git fetch ./"sub proj" HEAD &&
585 git branch subproj-br FETCH_HEAD &&
586 git subtree add --prefix="sub dir" FETCH_HEAD
588 test_create_commit "$test_count" "sub dir"/main-sub1 &&
589 test_create_commit "$test_count" main2 &&
590 test_create_commit "$test_count/sub proj" sub2 &&
591 test_create_commit "$test_count" "sub dir"/main-sub2 &&
594 git fetch ./"sub proj" HEAD &&
595 git subtree merge --prefix="sub dir" FETCH_HEAD &&
596 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
597 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
598 test "$(git rev-parse subproj-br)" = "$split_hash"
602 test_expect_success
'split "sub dir"/ with --branch for an incompatible branch' '
603 subtree_test_create_repo "$test_count" &&
604 subtree_test_create_repo "$test_count/sub proj" &&
605 test_create_commit "$test_count" main1 &&
606 test_create_commit "$test_count/sub proj" sub1 &&
609 git branch init HEAD &&
610 git fetch ./"sub proj" HEAD &&
611 git subtree add --prefix="sub dir" FETCH_HEAD
613 test_create_commit "$test_count" "sub dir"/main-sub1 &&
614 test_create_commit "$test_count" main2 &&
615 test_create_commit "$test_count/sub proj" sub2 &&
616 test_create_commit "$test_count" "sub dir"/main-sub2 &&
619 git fetch ./"sub proj" HEAD &&
620 git subtree merge --prefix="sub dir" FETCH_HEAD &&
621 test_must_fail git subtree split --prefix="sub dir" --branch init
625 test_expect_success
'split after annotated tag was added/merged with --squash pre-v2.32.0' '
626 test_create_pre2_32_repo "$test_count" &&
627 test_must_fail git -C "$test_count-clone" subtree split --prefix="sub" HEAD &&
628 git -C "$test_count-clone" subtree split --prefix="sub" HEAD "../$test_count-sub"
632 # Tests for 'git subtree pull'
635 test_expect_success
'pull requires option --prefix' '
636 subtree_test_create_repo "$test_count" &&
637 subtree_test_create_repo "$test_count/sub proj" &&
638 test_create_commit "$test_count" main1 &&
639 test_create_commit "$test_count/sub proj" sub1 &&
642 git fetch ./"sub proj" HEAD &&
643 git subtree add --prefix="sub dir" FETCH_HEAD
645 test_create_commit "$test_count/sub proj" sub2 &&
648 test_must_fail git subtree pull ./"sub proj" HEAD >out 2>err &&
650 echo "fatal: you must provide the --prefix option." >expected &&
651 test_must_be_empty out &&
652 test_cmp expected err
656 test_expect_success
'pull 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 &&
662 test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" HEAD >out 2>err &&
664 echo "fatal: '\''sub dir'\'' does not exist; use '\''git subtree add'\''" >expected &&
665 test_must_be_empty out &&
666 test_cmp expected err
670 test_expect_success
'pull basic operation' '
671 subtree_test_create_repo "$test_count" &&
672 subtree_test_create_repo "$test_count/sub proj" &&
673 test_create_commit "$test_count" main1 &&
674 test_create_commit "$test_count/sub proj" sub1 &&
677 git fetch ./"sub proj" HEAD &&
678 git subtree add --prefix="sub dir" FETCH_HEAD
680 test_create_commit "$test_count/sub proj" sub2 &&
683 exp=$(git -C "sub proj" rev-parse --verify HEAD:) &&
684 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
685 act=$(git rev-parse --verify HEAD:"sub dir") &&
690 test_expect_success
'pull rejects flags for split' '
691 subtree_test_create_repo "$test_count" &&
692 subtree_test_create_repo "$test_count/sub proj" &&
693 test_create_commit "$test_count" main1 &&
694 test_create_commit "$test_count/sub proj" sub1 &&
697 git fetch ./"sub proj" HEAD &&
698 git subtree add --prefix="sub dir" FETCH_HEAD
700 test_create_commit "$test_count/sub proj" sub2 &&
702 test_must_fail git subtree pull --prefix="sub dir" --annotate=foo ./"sub proj" HEAD &&
703 test_must_fail git subtree pull --prefix="sub dir" --branch=foo ./"sub proj" HEAD &&
704 test_must_fail git subtree pull --prefix="sub dir" --ignore-joins ./"sub proj" HEAD &&
705 test_must_fail git subtree pull --prefix="sub dir" --onto=foo ./"sub proj" HEAD &&
706 test_must_fail git subtree pull --prefix="sub dir" --rejoin ./"sub proj" HEAD
710 test_expect_success
'pull with --squash after annotated tag was added/merged with --squash pre-v2.32.0 ' '
711 test_create_pre2_32_repo "$test_count" &&
712 git -C "$test_count-clone" subtree -d pull --prefix="sub" --squash "../$test_count-sub" sub2
716 # Tests for 'git subtree push'
719 test_expect_success
'push requires option --prefix' '
720 subtree_test_create_repo "$test_count" &&
721 subtree_test_create_repo "$test_count/sub proj" &&
722 test_create_commit "$test_count" main1 &&
723 test_create_commit "$test_count/sub proj" sub1 &&
726 git fetch ./"sub proj" HEAD &&
727 git subtree add --prefix="sub dir" FETCH_HEAD &&
728 echo "fatal: you must provide the --prefix option." >expected &&
729 test_must_fail git subtree push "./sub proj" from-mainline >actual 2>&1 &&
730 test_debug "printf '"expected: "'" &&
731 test_debug "cat expected" &&
732 test_debug "printf '"actual: "'" &&
733 test_debug "cat actual" &&
734 test_cmp expected actual
738 test_expect_success
'push requires path given by option --prefix must exist' '
739 subtree_test_create_repo "$test_count" &&
740 subtree_test_create_repo "$test_count/sub proj" &&
741 test_create_commit "$test_count" main1 &&
742 test_create_commit "$test_count/sub proj" sub1 &&
745 git fetch ./"sub proj" HEAD &&
746 git subtree add --prefix="sub dir" FETCH_HEAD &&
747 echo "fatal: '\''non-existent-directory'\'' does not exist; use '\''git subtree add'\''" >expected &&
748 test_must_fail git subtree push --prefix=non-existent-directory "./sub proj" from-mainline >actual 2>&1 &&
749 test_debug "printf '"expected: "'" &&
750 test_debug "cat expected" &&
751 test_debug "printf '"actual: "'" &&
752 test_debug "cat actual" &&
753 test_cmp expected actual
757 test_expect_success
'push rejects flags for add' '
758 subtree_test_create_repo "$test_count" &&
759 subtree_test_create_repo "$test_count/sub proj" &&
760 test_create_commit "$test_count" main1 &&
761 test_create_commit "$test_count/sub proj" sub1 &&
764 git fetch ./"sub proj" HEAD &&
765 git subtree add --prefix="sub dir" FETCH_HEAD
767 test_create_commit "$test_count" "sub dir"/main-sub1 &&
768 test_create_commit "$test_count" main2 &&
769 test_create_commit "$test_count/sub proj" sub2 &&
770 test_create_commit "$test_count" "sub dir"/main-sub2 &&
773 git fetch ./"sub proj" HEAD &&
774 git subtree merge --prefix="sub dir" FETCH_HEAD &&
775 test_wrong_flag git subtree split --prefix="sub dir" --squash ./"sub proj" from-mainline &&
776 test_wrong_flag git subtree split --prefix="sub dir" --message=foo ./"sub proj" from-mainline
780 test_expect_success
'push basic operation' '
781 subtree_test_create_repo "$test_count" &&
782 subtree_test_create_repo "$test_count/sub proj" &&
783 test_create_commit "$test_count" main1 &&
784 test_create_commit "$test_count/sub proj" sub1 &&
787 git fetch ./"sub proj" HEAD &&
788 git subtree add --prefix="sub dir" FETCH_HEAD
790 test_create_commit "$test_count" "sub dir"/main-sub1 &&
791 test_create_commit "$test_count" main2 &&
792 test_create_commit "$test_count/sub proj" sub2 &&
793 test_create_commit "$test_count" "sub dir"/main-sub2 &&
796 git fetch ./"sub proj" HEAD &&
797 git subtree merge --prefix="sub dir" FETCH_HEAD &&
798 before=$(git rev-parse --verify HEAD) &&
799 split_hash=$(git subtree split --prefix="sub dir") &&
800 git subtree push --prefix="sub dir" ./"sub proj" from-mainline &&
801 test "$before" = "$(git rev-parse --verify HEAD)" &&
802 test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
806 test_expect_success
'push sub dir/ with --rejoin' '
807 subtree_test_create_repo "$test_count" &&
808 subtree_test_create_repo "$test_count/sub proj" &&
809 test_create_commit "$test_count" main1 &&
810 test_create_commit "$test_count/sub proj" sub1 &&
813 git fetch ./"sub proj" HEAD &&
814 git subtree add --prefix="sub dir" FETCH_HEAD
816 test_create_commit "$test_count" "sub dir"/main-sub1 &&
817 test_create_commit "$test_count" main2 &&
818 test_create_commit "$test_count/sub proj" sub2 &&
819 test_create_commit "$test_count" "sub dir"/main-sub2 &&
822 git fetch ./"sub proj" HEAD &&
823 git subtree merge --prefix="sub dir" FETCH_HEAD &&
824 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
825 git subtree push --prefix="sub dir" --annotate="*" --rejoin ./"sub proj" from-mainline &&
826 test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" &&
827 test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
831 test_expect_success
'push sub dir/ with --rejoin from scratch' '
832 subtree_test_create_repo "$test_count" &&
833 test_create_commit "$test_count" main1 &&
837 echo file >"sub dir"/file &&
838 git add "sub dir/file" &&
839 git commit -m"sub dir file" &&
840 split_hash=$(git subtree split --prefix="sub dir" --rejoin) &&
841 git init --bare "sub proj.git" &&
842 git subtree push --prefix="sub dir" --rejoin ./"sub proj.git" from-mainline &&
843 test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" &&
844 test "$split_hash" = "$(git -C "sub proj.git" rev-parse --verify refs/heads/from-mainline)"
848 test_expect_success
'push sub dir/ with --rejoin and --message' '
849 subtree_test_create_repo "$test_count" &&
850 subtree_test_create_repo "$test_count/sub proj" &&
851 test_create_commit "$test_count" main1 &&
852 test_create_commit "$test_count/sub proj" sub1 &&
855 git fetch ./"sub proj" HEAD &&
856 git subtree add --prefix="sub dir" FETCH_HEAD
858 test_create_commit "$test_count" "sub dir"/main-sub1 &&
859 test_create_commit "$test_count" main2 &&
860 test_create_commit "$test_count/sub proj" sub2 &&
861 test_create_commit "$test_count" "sub dir"/main-sub2 &&
864 git fetch ./"sub proj" HEAD &&
865 git subtree merge --prefix="sub dir" FETCH_HEAD &&
866 git subtree push --prefix="sub dir" --message="Split & rejoin" --annotate="*" --rejoin ./"sub proj" from-mainline &&
867 test "$(last_commit_subject)" = "Split & rejoin" &&
868 split_hash="$(git rev-parse --verify HEAD^2)" &&
869 test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
873 test_expect_success
'push "sub dir"/ with --rejoin and --squash' '
874 subtree_test_create_repo "$test_count" &&
875 subtree_test_create_repo "$test_count/sub proj" &&
876 test_create_commit "$test_count" main1 &&
877 test_create_commit "$test_count/sub proj" sub1 &&
880 git fetch ./"sub proj" HEAD &&
881 git subtree add --prefix="sub dir" --squash FETCH_HEAD
883 test_create_commit "$test_count" "sub dir"/main-sub1 &&
884 test_create_commit "$test_count" main2 &&
885 test_create_commit "$test_count/sub proj" sub2 &&
886 test_create_commit "$test_count" "sub dir"/main-sub2 &&
889 git subtree pull --prefix="sub dir" --squash ./"sub proj" HEAD &&
890 MAIN=$(git rev-parse --verify HEAD) &&
891 SUB=$(git -C "sub proj" rev-parse --verify HEAD) &&
893 SPLIT=$(git subtree split --prefix="sub dir" --annotate="*") &&
894 git subtree push --prefix="sub dir" --annotate="*" --rejoin --squash ./"sub proj" from-mainline &&
896 test_must_fail git merge-base --is-ancestor $SUB HEAD &&
897 test_must_fail git merge-base --is-ancestor $SPLIT HEAD &&
898 git rev-list HEAD ^$MAIN >commit-list &&
899 test_line_count = 2 commit-list &&
900 test "$(git rev-parse --verify HEAD:)" = "$(git rev-parse --verify $MAIN:)" &&
901 test "$(git rev-parse --verify HEAD:"sub dir")" = "$(git rev-parse --verify $SPLIT:)" &&
902 test "$(git rev-parse --verify HEAD^1)" = $MAIN &&
903 test "$(git rev-parse --verify HEAD^2)" != $SPLIT &&
904 test "$(git rev-parse --verify HEAD^2:)" = "$(git rev-parse --verify $SPLIT:)" &&
905 test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$SPLIT'\''" &&
906 test "$SPLIT" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
910 test_expect_success
'push "sub dir"/ with --branch' '
911 subtree_test_create_repo "$test_count" &&
912 subtree_test_create_repo "$test_count/sub proj" &&
913 test_create_commit "$test_count" main1 &&
914 test_create_commit "$test_count/sub proj" sub1 &&
917 git fetch ./"sub proj" HEAD &&
918 git subtree add --prefix="sub dir" FETCH_HEAD
920 test_create_commit "$test_count" "sub dir"/main-sub1 &&
921 test_create_commit "$test_count" main2 &&
922 test_create_commit "$test_count/sub proj" sub2 &&
923 test_create_commit "$test_count" "sub dir"/main-sub2 &&
926 git fetch ./"sub proj" HEAD &&
927 git subtree merge --prefix="sub dir" FETCH_HEAD &&
928 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
929 git subtree push --prefix="sub dir" --annotate="*" --branch subproj-br ./"sub proj" from-mainline &&
930 test "$(git rev-parse subproj-br)" = "$split_hash" &&
931 test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
935 test_expect_success
'check hash of push' '
936 subtree_test_create_repo "$test_count" &&
937 subtree_test_create_repo "$test_count/sub proj" &&
938 test_create_commit "$test_count" main1 &&
939 test_create_commit "$test_count/sub proj" sub1 &&
942 git fetch ./"sub proj" HEAD &&
943 git subtree add --prefix="sub dir" FETCH_HEAD
945 test_create_commit "$test_count" "sub dir"/main-sub1 &&
946 test_create_commit "$test_count" main2 &&
947 test_create_commit "$test_count/sub proj" sub2 &&
948 test_create_commit "$test_count" "sub dir"/main-sub2 &&
951 git fetch ./"sub proj" HEAD &&
952 git subtree merge --prefix="sub dir" FETCH_HEAD &&
953 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
954 git subtree push --prefix="sub dir" --annotate="*" --branch subproj-br ./"sub proj" from-mainline &&
955 test "$(git rev-parse subproj-br)" = "$split_hash" &&
956 # Check hash of split
957 new_hash=$(git rev-parse subproj-br^2) &&
960 subdir_hash=$(git rev-parse HEAD) &&
961 test "$new_hash" = "$subdir_hash"
963 test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
967 test_expect_success
'push "sub dir"/ with --branch for an existing branch' '
968 subtree_test_create_repo "$test_count" &&
969 subtree_test_create_repo "$test_count/sub proj" &&
970 test_create_commit "$test_count" main1 &&
971 test_create_commit "$test_count/sub proj" sub1 &&
974 git fetch ./"sub proj" HEAD &&
975 git branch subproj-br FETCH_HEAD &&
976 git subtree add --prefix="sub dir" FETCH_HEAD
978 test_create_commit "$test_count" "sub dir"/main-sub1 &&
979 test_create_commit "$test_count" main2 &&
980 test_create_commit "$test_count/sub proj" sub2 &&
981 test_create_commit "$test_count" "sub dir"/main-sub2 &&
984 git fetch ./"sub proj" HEAD &&
985 git subtree merge --prefix="sub dir" FETCH_HEAD &&
986 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
987 git subtree push --prefix="sub dir" --annotate="*" --branch subproj-br ./"sub proj" from-mainline &&
988 test "$(git rev-parse subproj-br)" = "$split_hash" &&
989 test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
993 test_expect_success
'push "sub dir"/ with --branch for an incompatible branch' '
994 subtree_test_create_repo "$test_count" &&
995 subtree_test_create_repo "$test_count/sub proj" &&
996 test_create_commit "$test_count" main1 &&
997 test_create_commit "$test_count/sub proj" sub1 &&
1000 git branch init HEAD &&
1001 git fetch ./"sub proj" HEAD &&
1002 git subtree add --prefix="sub dir" FETCH_HEAD
1004 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1005 test_create_commit "$test_count" main2 &&
1006 test_create_commit "$test_count/sub proj" sub2 &&
1007 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1010 git fetch ./"sub proj" HEAD &&
1011 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1012 test_must_fail git subtree push --prefix="sub dir" --branch init "./sub proj" from-mainline
1016 test_expect_success
'push "sub dir"/ with a local rev' '
1017 subtree_test_create_repo "$test_count" &&
1018 subtree_test_create_repo "$test_count/sub proj" &&
1019 test_create_commit "$test_count" main1 &&
1020 test_create_commit "$test_count/sub proj" sub1 &&
1023 git fetch ./"sub proj" HEAD &&
1024 git subtree add --prefix="sub dir" FETCH_HEAD
1026 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1027 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1030 bad_tree=$(git rev-parse --verify HEAD:"sub dir") &&
1031 good_tree=$(git rev-parse --verify HEAD^:"sub dir") &&
1032 git subtree push --prefix="sub dir" --annotate="*" ./"sub proj" HEAD^:from-mainline &&
1033 split_tree=$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline:) &&
1034 test "$split_tree" = "$good_tree"
1038 test_expect_success
'push after annotated tag was added/merged with --squash pre-v2.32.0' '
1039 test_create_pre2_32_repo "$test_count" &&
1040 test_create_commit "$test_count-clone" sub/main-sub1 &&
1041 git -C "$test_count-clone" subtree push --prefix="sub" "../$test_count-sub" from-mainline
1048 test_expect_success
'make sure exactly the right set of files ends up in the subproj' '
1049 subtree_test_create_repo "$test_count" &&
1050 subtree_test_create_repo "$test_count/sub proj" &&
1051 test_create_commit "$test_count" main1 &&
1052 test_create_commit "$test_count/sub proj" sub1 &&
1055 git fetch ./"sub proj" HEAD &&
1056 git subtree add --prefix="sub dir" FETCH_HEAD
1058 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1059 test_create_commit "$test_count" main2 &&
1060 test_create_commit "$test_count/sub proj" sub2 &&
1061 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1064 git fetch ./"sub proj" HEAD &&
1065 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1066 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1068 test_create_commit "$test_count/sub proj" sub3 &&
1069 test_create_commit "$test_count" "sub dir"/main-sub3 &&
1071 cd "$test_count/sub proj" &&
1072 git fetch .. subproj-br &&
1073 git merge FETCH_HEAD
1075 test_create_commit "$test_count/sub proj" sub4 &&
1078 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1080 test_create_commit "$test_count" "sub dir"/main-sub4 &&
1083 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1086 cd "$test_count/sub proj" &&
1087 git fetch .. subproj-br &&
1088 git merge FETCH_HEAD &&
1090 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 \
1091 sub1 sub2 sub3 sub4 >expect &&
1092 git ls-files >actual &&
1093 test_cmp expect actual
1097 test_expect_success
'make sure the subproj *only* contains commits that affect the "sub dir"' '
1098 subtree_test_create_repo "$test_count" &&
1099 subtree_test_create_repo "$test_count/sub proj" &&
1100 test_create_commit "$test_count" main1 &&
1101 test_create_commit "$test_count/sub proj" sub1 &&
1104 git fetch ./"sub proj" HEAD &&
1105 git subtree add --prefix="sub dir" FETCH_HEAD
1107 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1108 test_create_commit "$test_count" main2 &&
1109 test_create_commit "$test_count/sub proj" sub2 &&
1110 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1113 git fetch ./"sub proj" HEAD &&
1114 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1115 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1117 test_create_commit "$test_count/sub proj" sub3 &&
1118 test_create_commit "$test_count" "sub dir"/main-sub3 &&
1120 cd "$test_count/sub proj" &&
1121 git fetch .. subproj-br &&
1122 git merge FETCH_HEAD
1124 test_create_commit "$test_count/sub proj" sub4 &&
1127 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1129 test_create_commit "$test_count" "sub dir"/main-sub4 &&
1132 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1135 cd "$test_count/sub proj" &&
1136 git fetch .. subproj-br &&
1137 git merge FETCH_HEAD &&
1139 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 \
1140 sub1 sub2 sub3 sub4 >expect &&
1141 git log --name-only --pretty=format:"" >log &&
1142 sort <log | sed "/^\$/ d" >actual &&
1143 test_cmp expect actual
1147 test_expect_success
'make sure exactly the right set of files ends up in the mainline' '
1148 subtree_test_create_repo "$test_count" &&
1149 subtree_test_create_repo "$test_count/sub proj" &&
1150 test_create_commit "$test_count" main1 &&
1151 test_create_commit "$test_count/sub proj" sub1 &&
1154 git fetch ./"sub proj" HEAD &&
1155 git subtree add --prefix="sub dir" FETCH_HEAD
1157 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1158 test_create_commit "$test_count" main2 &&
1159 test_create_commit "$test_count/sub proj" sub2 &&
1160 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1163 git fetch ./"sub proj" HEAD &&
1164 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1165 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1167 test_create_commit "$test_count/sub proj" sub3 &&
1168 test_create_commit "$test_count" "sub dir"/main-sub3 &&
1170 cd "$test_count/sub proj" &&
1171 git fetch .. subproj-br &&
1172 git merge FETCH_HEAD
1174 test_create_commit "$test_count/sub proj" sub4 &&
1177 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1179 test_create_commit "$test_count" "sub dir"/main-sub4 &&
1182 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1185 cd "$test_count/sub proj" &&
1186 git fetch .. subproj-br &&
1187 git merge FETCH_HEAD
1191 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
1193 test_write_lines main1 main2 >chkm &&
1194 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 >chkms &&
1195 sed "s,^,sub dir/," chkms >chkms_sub &&
1196 test_write_lines sub1 sub2 sub3 sub4 >chks &&
1197 sed "s,^,sub dir/," chks >chks_sub &&
1199 cat chkm chkms_sub chks_sub >expect &&
1200 git ls-files >actual &&
1201 test_cmp expect actual
1205 test_expect_success
'make sure each filename changed exactly once in the entire history' '
1206 subtree_test_create_repo "$test_count" &&
1207 subtree_test_create_repo "$test_count/sub proj" &&
1208 test_create_commit "$test_count" main1 &&
1209 test_create_commit "$test_count/sub proj" sub1 &&
1212 git config log.date relative &&
1213 git fetch ./"sub proj" HEAD &&
1214 git subtree add --prefix="sub dir" FETCH_HEAD
1216 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1217 test_create_commit "$test_count" main2 &&
1218 test_create_commit "$test_count/sub proj" sub2 &&
1219 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1222 git fetch ./"sub proj" HEAD &&
1223 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1224 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1226 test_create_commit "$test_count/sub proj" sub3 &&
1227 test_create_commit "$test_count" "sub dir"/main-sub3 &&
1229 cd "$test_count/sub proj" &&
1230 git fetch .. subproj-br &&
1231 git merge FETCH_HEAD
1233 test_create_commit "$test_count/sub proj" sub4 &&
1236 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1238 test_create_commit "$test_count" "sub dir"/main-sub4 &&
1241 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1244 cd "$test_count/sub proj" &&
1245 git fetch .. subproj-br &&
1246 git merge FETCH_HEAD
1250 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
1252 test_write_lines main1 main2 >chkm &&
1253 test_write_lines sub1 sub2 sub3 sub4 >chks &&
1254 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 >chkms &&
1255 sed "s,^,sub dir/," chkms >chkms_sub &&
1257 # main-sub?? and /"sub dir"/main-sub?? both change, because those are the
1258 # changes that were split into their own history. And "sub dir"/sub?? never
1259 # change, since they were *only* changed in the subtree branch.
1260 git log --name-only --pretty=format:"" >log &&
1261 sort <log >sorted-log &&
1262 sed "/^$/ d" sorted-log >actual &&
1264 cat chkms chkm chks chkms_sub >expect-unsorted &&
1265 sort expect-unsorted >expect &&
1266 test_cmp expect actual
1270 test_expect_success
'make sure the --rejoin commits never make it into subproj' '
1271 subtree_test_create_repo "$test_count" &&
1272 subtree_test_create_repo "$test_count/sub proj" &&
1273 test_create_commit "$test_count" main1 &&
1274 test_create_commit "$test_count/sub proj" sub1 &&
1277 git fetch ./"sub proj" HEAD &&
1278 git subtree add --prefix="sub dir" FETCH_HEAD
1280 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1281 test_create_commit "$test_count" main2 &&
1282 test_create_commit "$test_count/sub proj" sub2 &&
1283 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1286 git fetch ./"sub proj" HEAD &&
1287 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1288 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1290 test_create_commit "$test_count/sub proj" sub3 &&
1291 test_create_commit "$test_count" "sub dir"/main-sub3 &&
1293 cd "$test_count/sub proj" &&
1294 git fetch .. subproj-br &&
1295 git merge FETCH_HEAD
1297 test_create_commit "$test_count/sub proj" sub4 &&
1300 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1302 test_create_commit "$test_count" "sub dir"/main-sub4 &&
1305 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1308 cd "$test_count/sub proj" &&
1309 git fetch .. subproj-br &&
1310 git merge FETCH_HEAD
1314 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
1315 test "$(git log --pretty=format:"%s" HEAD^2 | grep -i split)" = ""
1319 test_expect_success
'make sure no "git subtree" tagged commits make it into subproj' '
1320 subtree_test_create_repo "$test_count" &&
1321 subtree_test_create_repo "$test_count/sub proj" &&
1322 test_create_commit "$test_count" main1 &&
1323 test_create_commit "$test_count/sub proj" sub1 &&
1326 git fetch ./"sub proj" HEAD &&
1327 git subtree add --prefix="sub dir" FETCH_HEAD
1329 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1330 test_create_commit "$test_count" main2 &&
1331 test_create_commit "$test_count/sub proj" sub2 &&
1332 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1335 git fetch ./"sub proj" HEAD &&
1336 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1337 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1339 test_create_commit "$test_count/sub proj" sub3 &&
1340 test_create_commit "$test_count" "sub dir"/main-sub3 &&
1342 cd "$test_count/sub proj" &&
1343 git fetch .. subproj-br &&
1344 git merge FETCH_HEAD
1346 test_create_commit "$test_count/sub proj" sub4 &&
1349 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1351 test_create_commit "$test_count" "sub dir"/main-sub4 &&
1354 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1357 cd "$test_count/sub proj" &&
1358 git fetch .. subproj-br &&
1359 git merge FETCH_HEAD
1363 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
1365 # They are meaningless to subproj since one side of the merge refers to the mainline
1366 test "$(git log --pretty=format:"%s%n%b" HEAD^2 | grep "git-subtree.*:")" = ""
1371 # A new set of tests
1374 test_expect_success
'make sure "git subtree split" find the correct parent' '
1375 subtree_test_create_repo "$test_count" &&
1376 subtree_test_create_repo "$test_count/sub proj" &&
1377 test_create_commit "$test_count" main1 &&
1378 test_create_commit "$test_count/sub proj" sub1 &&
1381 git fetch ./"sub proj" HEAD &&
1382 git subtree add --prefix="sub dir" FETCH_HEAD
1384 test_create_commit "$test_count/sub proj" sub2 &&
1387 git fetch ./"sub proj" HEAD &&
1388 git branch subproj-ref FETCH_HEAD &&
1389 git subtree merge --prefix="sub dir" FETCH_HEAD
1391 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1394 git subtree split --prefix="sub dir" --branch subproj-br &&
1396 # at this point, the new commit parent should be subproj-ref, if it is
1397 # not, something went wrong (the "newparent" of "HEAD~" commit should
1398 # have been sub2, but it was not, because its cache was not set to
1400 test "$(git log --pretty=format:%P -1 subproj-br)" = "$(git rev-parse subproj-ref)"
1404 test_expect_success
'split a new subtree without --onto option' '
1405 subtree_test_create_repo "$test_count" &&
1406 subtree_test_create_repo "$test_count/sub proj" &&
1407 test_create_commit "$test_count" main1 &&
1408 test_create_commit "$test_count/sub proj" sub1 &&
1411 git fetch ./"sub proj" HEAD &&
1412 git subtree add --prefix="sub dir" FETCH_HEAD
1414 test_create_commit "$test_count/sub proj" sub2 &&
1417 git fetch ./"sub proj" HEAD &&
1418 git subtree merge --prefix="sub dir" FETCH_HEAD
1420 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1423 git subtree split --prefix="sub dir" --branch subproj-br
1425 mkdir "$test_count"/"sub dir2" &&
1426 test_create_commit "$test_count" "sub dir2"/main-sub2 &&
1430 # also test that we still can split out an entirely new subtree
1431 # if the parent of the first commit in the tree is not empty,
1432 # then the new subtree has accidentally been attached to something
1433 git subtree split --prefix="sub dir2" --branch subproj2-br &&
1434 test "$(git log --pretty=format:%P -1 subproj2-br)" = ""
1438 test_expect_success
'verify one file change per commit' '
1439 subtree_test_create_repo "$test_count" &&
1440 subtree_test_create_repo "$test_count/sub proj" &&
1441 test_create_commit "$test_count" main1 &&
1442 test_create_commit "$test_count/sub proj" sub1 &&
1445 git fetch ./"sub proj" HEAD &&
1446 git branch sub1 FETCH_HEAD &&
1447 git subtree add --prefix="sub dir" sub1
1449 test_create_commit "$test_count/sub proj" sub2 &&
1452 git fetch ./"sub proj" HEAD &&
1453 git subtree merge --prefix="sub dir" FETCH_HEAD
1455 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1458 git subtree split --prefix="sub dir" --branch subproj-br
1460 mkdir "$test_count"/"sub dir2" &&
1461 test_create_commit "$test_count" "sub dir2"/main-sub2 &&
1464 git subtree split --prefix="sub dir2" --branch subproj2-br &&
1466 git log --format="%H" >commit-list &&
1469 git log -n1 --format="" --name-only "$commit" >file-list &&
1470 test_line_count -le 1 file-list || return 1
1475 test_expect_success
'push split to subproj' '
1476 subtree_test_create_repo "$test_count" &&
1477 subtree_test_create_repo "$test_count/sub proj" &&
1478 test_create_commit "$test_count" main1 &&
1479 test_create_commit "$test_count/sub proj" sub1 &&
1482 git fetch ./"sub proj" HEAD &&
1483 git subtree add --prefix="sub dir" FETCH_HEAD
1485 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1486 test_create_commit "$test_count" main2 &&
1487 test_create_commit "$test_count/sub proj" sub2 &&
1488 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1490 cd $test_count/"sub proj" &&
1491 git branch sub-branch-1 &&
1493 git fetch ./"sub proj" HEAD &&
1494 git subtree merge --prefix="sub dir" FETCH_HEAD
1496 test_create_commit "$test_count" "sub dir"/main-sub3 &&
1499 git subtree push ./"sub proj" --prefix "sub dir" sub-branch-1 &&
1501 git checkout sub-branch-1 &&
1502 test "$(last_commit_subject)" = "sub dir/main-sub3"
1507 # This test covers 2 cases in subtree split copy_or_skip code
1508 # 1) Merges where one parent is a superset of the changes of the other
1509 # parent regarding changes to the subtree, in this case the merge
1510 # commit should be copied
1511 # 2) Merges where only one parent operate on the subtree, and the merge
1512 # commit should be skipped
1514 # (1) is checked by ensuring subtree_tip is a descendent of subtree_branch
1515 # (2) should have a check added (not_a_subtree_change shouldn't be present
1516 # on the produced subtree)
1518 # Other related cases which are not tested (or currently handled correctly)
1519 # - Case (1) where there are more than 2 parents, it will sometimes correctly copy
1520 # the merge, and sometimes not
1521 # - Merge commit where both parents have same tree as the merge, currently
1522 # will always be skipped, even if they reached that state via different
1526 test_expect_success
'subtree descendant check' '
1527 subtree_test_create_repo "$test_count" &&
1528 defaultBranch=$(sed "s,ref: refs/heads/,," "$test_count/.git/HEAD") &&
1529 test_create_commit "$test_count" folder_subtree/a &&
1534 test_create_commit "$test_count" folder_subtree/0 &&
1535 test_create_commit "$test_count" folder_subtree/b &&
1536 cherry=$(cd "$test_count" && git rev-parse HEAD) &&
1541 test_create_commit "$test_count" commit_on_branch &&
1544 git cherry-pick $cherry &&
1545 git checkout $defaultBranch &&
1546 git merge -m "merge should be kept on subtree" branch &&
1547 git branch no_subtree_work_branch
1549 test_create_commit "$test_count" folder_subtree/d &&
1552 git checkout no_subtree_work_branch
1554 test_create_commit "$test_count" not_a_subtree_change &&
1557 git checkout $defaultBranch &&
1558 git merge -m "merge should be skipped on subtree" no_subtree_work_branch &&
1560 git subtree split --prefix folder_subtree/ --branch subtree_tip $defaultBranch &&
1561 git subtree split --prefix folder_subtree/ --branch subtree_branch branch &&
1562 test $(git rev-list --count subtree_tip..subtree_branch) = 0