Raw
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 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
13 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
14
15 TEST_DIRECTORY=$(pwd)/../../../t
16 . "$TEST_DIRECTORY"/test-lib.sh
17 . "$TEST_DIRECTORY"/lib-gpg.sh
18
19 # Use our own wrapper around test-lib.sh's test_create_repo, in order
20 # to set log.date=relative. `git subtree` parses the output of `git
21 # log`, and so it must be careful to not be affected by settings that
22 # change the `git log` output. We test this by setting
23 # log.date=relative for every repo in the tests.
24 subtree_test_create_repo () {
25 test_create_repo "$1" &&
26 git -C "$1" config log.date relative
27 }
28
29 test_create_commit () (
30 repo=$1 &&
31 commit=$2 &&
32 cd "$repo" &&
33 mkdir -p "$(dirname "$commit")" \
34 || error "Could not create directory for commit"
35 echo "$commit" >"$commit" &&
36 git add "$commit" || error "Could not add commit"
37 git commit -m "$commit" || error "Could not commit"
38 )
39
40 test_wrong_flag() {
41 test_must_fail "$@" >out 2>err &&
42 test_must_be_empty out &&
43 grep "flag does not make sense with" err
44 }
45
46 last_commit_subject () {
47 git log --pretty=format:%s -1
48 }
49
50 # Upon 'git subtree add|merge --squash' of an annotated tag,
51 # pre-2.32.0 versions of 'git subtree' would write the hash of the tag
52 # (sub1 below), instead of the commit (sub1^{commit}) in the
53 # "git-subtree-split" trailer.
54 # We imitate this behaviour below using a replace ref.
55 # This function creates 3 repositories:
56 # - $1
57 # - $1-sub (added as subtree "sub" in $1)
58 # - $1-clone (clone of $1)
59 test_create_pre2_32_repo () {
60 subtree_test_create_repo "$1" &&
61 subtree_test_create_repo "$1-sub" &&
62 test_commit -C "$1" main1 &&
63 test_commit -C "$1-sub" --annotate sub1 &&
64 git -C "$1" subtree add --prefix="sub" --squash "../$1-sub" sub1 &&
65 tag=$(git -C "$1" rev-parse FETCH_HEAD) &&
66 commit=$(git -C "$1" rev-parse FETCH_HEAD^{commit}) &&
67 git -C "$1" log -1 --format=%B HEAD^2 >msg &&
68 test_commit -C "$1-sub" --annotate sub2 &&
69 git clone --no-local "$1" "$1-clone" &&
70 new_commit=$(sed -e "s/$commit/$tag/" msg | git -C "$1-clone" commit-tree HEAD^2^{tree}) &&
71 git -C "$1-clone" replace HEAD^2 $new_commit
72 }
73
74 # test_create_subtree_add REPO ORPHAN PREFIX FILENAME ...
75 #
76 # Create a simple subtree on a new branch named ORPHAN in REPO.
77 # The subtree is then merged into the current branch of REPO,
78 # under PREFIX. The generated subtree has one commit
79 # with subject and tag FILENAME with a single file "FILENAME.t"
80 #
81 # When this method returns:
82 # - the current branch of REPO will have file PREFIX/FILENAME.t
83 # - REPO will have a branch named ORPHAN with subtree history
84 #
85 # additional arguments are forwarded to "subtree add"
86 test_create_subtree_add () {
87 (
88 cd "$1" &&
89 orphan="$2" &&
90 prefix="$3" &&
91 filename="$4" &&
92 shift 4 &&
93 last="$(git branch --show-current)" &&
94 git switch --orphan "$orphan" &&
95 test_commit "$filename" &&
96 git checkout "$last" &&
97 git subtree add --prefix="$prefix" "$@" "$orphan"
98 )
99 }
100
101 test_expect_success 'shows short help text for -h' '
102 git subtree -h >out 2>err &&
103 test_must_be_empty err &&
104 grep -e "^ *or: git subtree pull" out &&
105 grep -F -e "--[no-]annotate" out
106 '
107
108 #
109 # Tests for 'git subtree add'
110 #
111
112 test_expect_success 'no merge from non-existent subtree' '
113 subtree_test_create_repo "$test_count" &&
114 subtree_test_create_repo "$test_count/sub proj" &&
115 test_create_commit "$test_count" main1 &&
116 test_create_commit "$test_count/sub proj" sub1 &&
117 (
118 cd "$test_count" &&
119 git fetch ./"sub proj" HEAD &&
120 test_must_fail git subtree merge --prefix="sub dir" FETCH_HEAD
121 )
122 '
123
124 test_expect_success 'no pull from non-existent subtree' '
125 subtree_test_create_repo "$test_count" &&
126 subtree_test_create_repo "$test_count/sub proj" &&
127 test_create_commit "$test_count" main1 &&
128 test_create_commit "$test_count/sub proj" sub1 &&
129 (
130 cd "$test_count" &&
131 git fetch ./"sub proj" HEAD &&
132 test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" HEAD
133 )
134 '
135
136 test_expect_success 'add rejects flags for split' '
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 test_wrong_flag git subtree add --prefix="sub dir" --annotate=foo FETCH_HEAD &&
145 test_wrong_flag git subtree add --prefix="sub dir" --branch=foo FETCH_HEAD &&
146 test_wrong_flag git subtree add --prefix="sub dir" --ignore-joins FETCH_HEAD &&
147 test_wrong_flag git subtree add --prefix="sub dir" --onto=foo FETCH_HEAD &&
148 test_wrong_flag git subtree add --prefix="sub dir" --rejoin FETCH_HEAD
149 )
150 '
151
152 test_expect_success 'add subproj as subtree into sub dir/ with --prefix' '
153 subtree_test_create_repo "$test_count" &&
154 subtree_test_create_repo "$test_count/sub proj" &&
155 test_create_commit "$test_count" main1 &&
156 test_create_commit "$test_count/sub proj" sub1 &&
157 (
158 cd "$test_count" &&
159 git fetch ./"sub proj" HEAD &&
160 git subtree add --prefix="sub dir" FETCH_HEAD &&
161 test "$(last_commit_subject)" = "Add '\''sub dir/'\'' from commit '\''$(git rev-parse FETCH_HEAD)'\''"
162 )
163 '
164
165 test_expect_success 'add subproj as subtree into sub dir/ with --prefix and --message' '
166 subtree_test_create_repo "$test_count" &&
167 subtree_test_create_repo "$test_count/sub proj" &&
168 test_create_commit "$test_count" main1 &&
169 test_create_commit "$test_count/sub proj" sub1 &&
170 (
171 cd "$test_count" &&
172 git fetch ./"sub proj" HEAD &&
173 git subtree add --prefix="sub dir" --message="Added subproject" FETCH_HEAD &&
174 test "$(last_commit_subject)" = "Added subproject"
175 )
176 '
177
178 test_expect_success 'add subproj as subtree into sub dir/ with --prefix as -P and --message as -m' '
179 subtree_test_create_repo "$test_count" &&
180 subtree_test_create_repo "$test_count/sub proj" &&
181 test_create_commit "$test_count" main1 &&
182 test_create_commit "$test_count/sub proj" sub1 &&
183 (
184 cd "$test_count" &&
185 git fetch ./"sub proj" HEAD &&
186 git subtree add -P "sub dir" -m "Added subproject" FETCH_HEAD &&
187 test "$(last_commit_subject)" = "Added subproject"
188 )
189 '
190
191 test_expect_success 'add subproj as subtree into sub dir/ with --squash and --prefix and --message' '
192 subtree_test_create_repo "$test_count" &&
193 subtree_test_create_repo "$test_count/sub proj" &&
194 test_create_commit "$test_count" main1 &&
195 test_create_commit "$test_count/sub proj" sub1 &&
196 (
197 cd "$test_count" &&
198 git fetch ./"sub proj" HEAD &&
199 git subtree add --prefix="sub dir" --message="Added subproject with squash" --squash FETCH_HEAD &&
200 test "$(last_commit_subject)" = "Added subproject with squash"
201 )
202 '
203
204 #
205 # Tests for 'git subtree merge'
206 #
207
208 test_expect_success 'merge rejects flags for split' '
209 subtree_test_create_repo "$test_count" &&
210 subtree_test_create_repo "$test_count/sub proj" &&
211 test_create_commit "$test_count" main1 &&
212 test_create_commit "$test_count/sub proj" sub1 &&
213 (
214 cd "$test_count" &&
215 git fetch ./"sub proj" HEAD &&
216 git subtree add --prefix="sub dir" FETCH_HEAD
217 ) &&
218 test_create_commit "$test_count/sub proj" sub2 &&
219 (
220 cd "$test_count" &&
221 git fetch ./"sub proj" HEAD &&
222 test_wrong_flag git subtree merge --prefix="sub dir" --annotate=foo FETCH_HEAD &&
223 test_wrong_flag git subtree merge --prefix="sub dir" --branch=foo FETCH_HEAD &&
224 test_wrong_flag git subtree merge --prefix="sub dir" --ignore-joins FETCH_HEAD &&
225 test_wrong_flag git subtree merge --prefix="sub dir" --onto=foo FETCH_HEAD &&
226 test_wrong_flag git subtree merge --prefix="sub dir" --rejoin FETCH_HEAD
227 )
228 '
229
230 test_expect_success 'merge new subproj history into sub dir/ with --prefix' '
231 subtree_test_create_repo "$test_count" &&
232 subtree_test_create_repo "$test_count/sub proj" &&
233 test_create_commit "$test_count" main1 &&
234 test_create_commit "$test_count/sub proj" sub1 &&
235 (
236 cd "$test_count" &&
237 git fetch ./"sub proj" HEAD &&
238 git subtree add --prefix="sub dir" FETCH_HEAD
239 ) &&
240 test_create_commit "$test_count/sub proj" sub2 &&
241 (
242 cd "$test_count" &&
243 git fetch ./"sub proj" HEAD &&
244 git subtree merge --prefix="sub dir" FETCH_HEAD &&
245 test "$(last_commit_subject)" = "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''"
246 )
247 '
248
249 test_expect_success 'merge new subproj history into sub dir/ with --prefix and --message' '
250 subtree_test_create_repo "$test_count" &&
251 subtree_test_create_repo "$test_count/sub proj" &&
252 test_create_commit "$test_count" main1 &&
253 test_create_commit "$test_count/sub proj" sub1 &&
254 (
255 cd "$test_count" &&
256 git fetch ./"sub proj" HEAD &&
257 git subtree add --prefix="sub dir" FETCH_HEAD
258 ) &&
259 test_create_commit "$test_count/sub proj" sub2 &&
260 (
261 cd "$test_count" &&
262 git fetch ./"sub proj" HEAD &&
263 git subtree merge --prefix="sub dir" --message="Merged changes from subproject" FETCH_HEAD &&
264 test "$(last_commit_subject)" = "Merged changes from subproject"
265 )
266 '
267
268 test_expect_success 'merge new subproj history into sub dir/ with --squash and --prefix and --message' '
269 subtree_test_create_repo "$test_count/sub proj" &&
270 subtree_test_create_repo "$test_count" &&
271 test_create_commit "$test_count" main1 &&
272 test_create_commit "$test_count/sub proj" sub1 &&
273 (
274 cd "$test_count" &&
275 git fetch ./"sub proj" HEAD &&
276 git subtree add --prefix="sub dir" FETCH_HEAD
277 ) &&
278 test_create_commit "$test_count/sub proj" sub2 &&
279 (
280 cd "$test_count" &&
281 git fetch ./"sub proj" HEAD &&
282 git subtree merge --prefix="sub dir" --message="Merged changes from subproject using squash" --squash FETCH_HEAD &&
283 test "$(last_commit_subject)" = "Merged changes from subproject using squash"
284 )
285 '
286
287 test_expect_success 'merge the added subproj again, should do nothing' '
288 subtree_test_create_repo "$test_count" &&
289 subtree_test_create_repo "$test_count/sub proj" &&
290 test_create_commit "$test_count" main1 &&
291 test_create_commit "$test_count/sub proj" sub1 &&
292 (
293 cd "$test_count" &&
294 git fetch ./"sub proj" HEAD &&
295 git subtree add --prefix="sub dir" FETCH_HEAD &&
296 # this shouldn not actually do anything, since FETCH_HEAD
297 # is already a parent
298 result=$(git merge -s ours -m "merge -s -ours" FETCH_HEAD) &&
299 test "${result}" = "Already up to date."
300 )
301 '
302
303 test_expect_success 'merge new subproj history into subdir/ with a slash appended to the argument of --prefix' '
304 subtree_test_create_repo "$test_count" &&
305 subtree_test_create_repo "$test_count/subproj" &&
306 test_create_commit "$test_count" main1 &&
307 test_create_commit "$test_count/subproj" sub1 &&
308 (
309 cd "$test_count" &&
310 git fetch ./subproj HEAD &&
311 git subtree add --prefix=subdir/ FETCH_HEAD
312 ) &&
313 test_create_commit "$test_count/subproj" sub2 &&
314 (
315 cd "$test_count" &&
316 git fetch ./subproj HEAD &&
317 git subtree merge --prefix=subdir/ FETCH_HEAD &&
318 test "$(last_commit_subject)" = "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''"
319 )
320 '
321
322 test_expect_success 'merge with --squash after annotated tag was added/merged with --squash pre-v2.32.0 ' '
323 test_create_pre2_32_repo "$test_count" &&
324 git -C "$test_count-clone" fetch "../$test_count-sub" sub2 &&
325 test_must_fail git -C "$test_count-clone" subtree merge --prefix="sub" --squash FETCH_HEAD &&
326 git -C "$test_count-clone" subtree merge --prefix="sub" --squash FETCH_HEAD "../$test_count-sub"
327 '
328
329 #
330 # Tests for 'git subtree split'
331 #
332
333 test_expect_success 'split requires option --prefix' '
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 echo "fatal: you must provide the --prefix option." >expected &&
343 test_must_fail git subtree split >actual 2>&1 &&
344 test_debug "printf '"expected: "'" &&
345 test_debug "cat expected" &&
346 test_debug "printf '"actual: "'" &&
347 test_debug "cat actual" &&
348 test_cmp expected actual
349 )
350 '
351
352 test_expect_success 'split requires path given by option --prefix must exist' '
353 subtree_test_create_repo "$test_count" &&
354 subtree_test_create_repo "$test_count/sub proj" &&
355 test_create_commit "$test_count" main1 &&
356 test_create_commit "$test_count/sub proj" sub1 &&
357 (
358 cd "$test_count" &&
359 git fetch ./"sub proj" HEAD &&
360 git subtree add --prefix="sub dir" FETCH_HEAD &&
361 echo "fatal: '\''non-existent-directory'\'' does not exist; use '\''git subtree add'\''" >expected &&
362 test_must_fail git subtree split --prefix=non-existent-directory >actual 2>&1 &&
363 test_debug "printf '"expected: "'" &&
364 test_debug "cat expected" &&
365 test_debug "printf '"actual: "'" &&
366 test_debug "cat actual" &&
367 test_cmp expected actual
368 )
369 '
370
371 test_expect_success 'split works when prefix exists in commit but not in working tree' '
372 subtree_test_create_repo "$test_count" &&
373 (
374 cd "$test_count" &&
375
376 # create subtree
377 mkdir pkg &&
378 echo ok >pkg/file &&
379 git add pkg &&
380 git commit -m "add pkg" &&
381 good=$(git rev-parse HEAD) &&
382
383 # remove it from working tree in later commit
384 git rm -r pkg &&
385 git commit -m "remove pkg" &&
386
387 # must still be able to split using the old commit
388 git subtree split --prefix=pkg "$good" >out &&
389 test -s out
390 )
391 '
392
393 test_expect_success 'split rejects flags for add' '
394 subtree_test_create_repo "$test_count" &&
395 subtree_test_create_repo "$test_count/sub proj" &&
396 test_create_commit "$test_count" main1 &&
397 test_create_commit "$test_count/sub proj" sub1 &&
398 (
399 cd "$test_count" &&
400 git fetch ./"sub proj" HEAD &&
401 git subtree add --prefix="sub dir" FETCH_HEAD
402 ) &&
403 test_create_commit "$test_count" "sub dir"/main-sub1 &&
404 test_create_commit "$test_count" main2 &&
405 test_create_commit "$test_count/sub proj" sub2 &&
406 test_create_commit "$test_count" "sub dir"/main-sub2 &&
407 (
408 cd "$test_count" &&
409 git fetch ./"sub proj" HEAD &&
410 git subtree merge --prefix="sub dir" FETCH_HEAD &&
411 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
412 test_wrong_flag git subtree split --prefix="sub dir" --squash &&
413 test_wrong_flag git subtree split --prefix="sub dir" --message=foo
414 )
415 '
416
417 test_expect_success 'split sub dir/ with --rejoin' '
418 subtree_test_create_repo "$test_count" &&
419 subtree_test_create_repo "$test_count/sub proj" &&
420 test_create_commit "$test_count" main1 &&
421 test_create_commit "$test_count/sub proj" sub1 &&
422 (
423 cd "$test_count" &&
424 git fetch ./"sub proj" HEAD &&
425 git subtree add --prefix="sub dir" FETCH_HEAD
426 ) &&
427 test_create_commit "$test_count" "sub dir"/main-sub1 &&
428 test_create_commit "$test_count" main2 &&
429 test_create_commit "$test_count/sub proj" sub2 &&
430 test_create_commit "$test_count" "sub dir"/main-sub2 &&
431 (
432 cd "$test_count" &&
433 git fetch ./"sub proj" HEAD &&
434 git subtree merge --prefix="sub dir" FETCH_HEAD &&
435 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
436 git subtree split --prefix="sub dir" --annotate="*" -b spl --rejoin &&
437 test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" &&
438 test "$(git rev-list --count spl)" -eq 5
439 )
440 '
441
442 test_expect_success 'split fail on RIIR git subtree data' '
443 subtree_test_create_repo "$test_count" &&
444 subtree_test_create_repo "$test_count/sub proj" &&
445 test_create_commit "$test_count" main1 &&
446 test_create_commit "$test_count/sub proj" sub1 &&
447 (
448 cd "$test_count" &&
449 git fetch ./"sub proj" HEAD &&
450 git subtree add --prefix="sub dir" FETCH_HEAD &&
451 # simulate RIIR git-subtree generated data
452 mkdir .git-subtree &&
453 echo "# sabotage" >.git-subtree/config &&
454 git add .git-subtree/config &&
455 git commit -m sabotage &&
456 test_must_fail git subtree split -P "sub dir" HEAD
457 )
458 '
459
460 # Tests that commits from other subtrees are not processed as
461 # part of a split.
462 #
463 # This test performs the following:
464 # - Creates Repo with subtrees 'subA' and 'subB'
465 # - Creates commits in the repo including changes to subtrees
466 # - Runs the following 'split' and commit' commands in order:
467 # - Perform 'split' on subtree A
468 # - Perform 'split' on subtree B
469 # - Create new commits with changes to subtree A and B
470 # - Perform split on subtree A
471 # - Check for expected history
472 test_expect_success 'split with multiple subtrees' '
473 subtree_test_create_repo "$test_count" &&
474 subtree_test_create_repo "$test_count/subA" &&
475 subtree_test_create_repo "$test_count/subB" &&
476 test_create_commit "$test_count" main1 &&
477 test_create_commit "$test_count/subA" subA1 &&
478 test_create_commit "$test_count/subA" subA2 &&
479 test_create_commit "$test_count/subA" subA3 &&
480 test_create_commit "$test_count/subB" subB1 &&
481 git -C "$test_count" fetch ./subA HEAD &&
482 git -C "$test_count" subtree add --prefix=subADir FETCH_HEAD &&
483 git -C "$test_count" fetch ./subB HEAD &&
484 git -C "$test_count" subtree add --prefix=subBDir FETCH_HEAD &&
485 test "$(git -C "$test_count" rev-list --count main)" -eq 7 &&
486 test_create_commit "$test_count" subADir/main-subA1 &&
487 test_create_commit "$test_count" subBDir/main-subB1 &&
488 git -C "$test_count" subtree split --prefix=subADir \
489 --squash --rejoin -m "Sub A Split 1" -b a1 &&
490 test "$(git -C "$test_count" rev-list --count main..a1)" -eq 1 &&
491 git -C "$test_count" subtree split --prefix=subBDir \
492 --squash --rejoin -m "Sub B Split 1" -b b1 &&
493 test "$(git -C "$test_count" rev-list --count main..b1)" -eq 1 &&
494 test_create_commit "$test_count" subADir/main-subA2 &&
495 test_create_commit "$test_count" subBDir/main-subB2 &&
496 git -C "$test_count" subtree split --prefix=subADir \
497 --squash --rejoin -m "Sub A Split 2" -b a2 &&
498 test "$(git -C "$test_count" rev-list --count main..a2)" -eq 2 &&
499 test "$(git -C "$test_count" rev-list --count a1..a2)" -eq 1 &&
500 git -C "$test_count" subtree split --prefix=subBDir \
501 --squash --rejoin -d -m "Sub B Split 1" -b b2 &&
502 test "$(git -C "$test_count" rev-list --count main..b2)" -eq 2 &&
503 test "$(git -C "$test_count" rev-list --count b1..b2)" -eq 1
504 '
505
506 # When subtree split-ing a directory that has other subtree
507 # *merges* underneath it, the split must include those subtrees.
508 # This test creates a nested subtree, `subA/subB`, and tests
509 # that the tree is correct after a subtree split of `subA/`.
510 # The test covers:
511 # - An initial `subtree add`; and
512 # - A follow-up `subtree merge`
513 # both with and without `--squashed`.
514 for is_squashed in '' 'y'
515 do
516 test_expect_success "split keeps nested ${is_squashed:+--squash }subtrees that are part of the split" '
517 subtree_test_create_repo "$test_count" &&
518 (
519 cd "$test_count" &&
520 mkdir subA &&
521 test_commit subA/file1 &&
522 test_create_subtree_add \
523 . mksubtree subA/subB file2 ${is_squashed:+--squash} &&
524 test_path_is_file subA/file1.t &&
525 test_path_is_file subA/subB/file2.t &&
526 git subtree split --prefix=subA --branch=bsplit &&
527 test "$(git rev-list --count bsplit)" -eq 2 &&
528 git checkout bsplit &&
529 test_path_is_file file1.t &&
530 test_path_is_file subB/file2.t &&
531 git checkout mksubtree &&
532 git branch -D bsplit &&
533 test_commit file3 &&
534 git checkout main &&
535 git subtree merge \
536 ${is_squashed:+--squash} \
537 --prefix=subA/subB mksubtree &&
538 test_path_is_file subA/subB/file3.t &&
539 git subtree split --prefix=subA --branch=bsplit &&
540 test "$(git rev-list --count bsplit)" -eq 3 &&
541 git checkout bsplit &&
542 test_path_is_file file1.t &&
543 test_path_is_file subB/file2.t &&
544 test_path_is_file subB/file3.t
545 )
546 '
547 done
548
549 # Usually,
550 #
551 # git subtree merge -P subA --squash f00...
552 #
553 # makes two commits, in this order:
554 #
555 # 1. Squashed 'subA/' content from commit f00...
556 # 2. Merge commit (1) as 'subA'
557 #
558 # Commit 1 updates the subtree but does *not* rewrite paths.
559 # Commit 2 rewrites all trees to start with `subA/`
560 #
561 # Commit 1 either has no parents or depends only on other
562 # "Squashed 'subA/' content" commits.
563 #
564 # For merge without --squash, subtree produces just one commit:
565 # a merge commit with git-subtree trailers.
566 #
567 # In either case, if the user rebases these commits, they will
568 # still have the git-subtree-* trailers… but will NOT have
569 # the layout described above.
570 #
571 # Test that subsequent `git subtree split` are not confused by this.
572 test_expect_success 'split with rebased subtree commit' '
573 subtree_test_create_repo "$test_count" &&
574 (
575 cd "$test_count" &&
576 test_commit file0 &&
577 test_create_subtree_add \
578 . mksubtree subA file1 --squash &&
579 test_path_is_file subA/file1.t &&
580 mkdir subB &&
581 test_commit subB/bfile &&
582 git commit --amend -F - <<'EOF' &&
583 Squashed '\''subB/'\'' content from commit '\''badf00da911bbe895347b4b236f5461d55dc9877'\''
584
585 Simulate a cherry-picked or rebased subtree commit.
586
587 git-subtree-dir: subB
588 git-subtree-split: badf00da911bbe895347b4b236f5461d55dc9877
589 EOF
590 test_commit subA/file2 &&
591 test_commit subB/bfile2 &&
592 git commit --amend -F - <<'EOF' &&
593 Split '\''subB/'\'' into commit '\''badf00da911bbe895347b4b236f5461d55dc9877'\''
594
595 Simulate a cherry-picked or rebased subtree commit.
596
597 git-subtree-dir: subB
598 git-subtree-mainline: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
599 git-subtree-split: badf00da911bbe895347b4b236f5461d55dc9877
600 EOF
601 git subtree split --prefix=subA --branch=bsplit &&
602 git checkout bsplit &&
603 test_path_is_file file1.t &&
604 test_path_is_file file2.t &&
605 test "$(last_commit_subject)" = "subA/file2" &&
606 test "$(git rev-list --count bsplit)" -eq 2
607 )
608 '
609
610 test_expect_success 'split sub dir/ with --rejoin from scratch' '
611 subtree_test_create_repo "$test_count" &&
612 test_create_commit "$test_count" main1 &&
613 (
614 cd "$test_count" &&
615 mkdir "sub dir" &&
616 echo file >"sub dir"/file &&
617 git add "sub dir/file" &&
618 git commit -m"sub dir file" &&
619 split_hash=$(git subtree split --prefix="sub dir" --rejoin) &&
620 git subtree split --prefix="sub dir" --rejoin &&
621 test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''"
622 )
623 '
624
625 test_expect_success 'split sub dir/ with --rejoin and --message' '
626 subtree_test_create_repo "$test_count" &&
627 subtree_test_create_repo "$test_count/sub proj" &&
628 test_create_commit "$test_count" main1 &&
629 test_create_commit "$test_count/sub proj" sub1 &&
630 (
631 cd "$test_count" &&
632 git fetch ./"sub proj" HEAD &&
633 git subtree add --prefix="sub dir" FETCH_HEAD
634 ) &&
635 test_create_commit "$test_count" "sub dir"/main-sub1 &&
636 test_create_commit "$test_count" main2 &&
637 test_create_commit "$test_count/sub proj" sub2 &&
638 test_create_commit "$test_count" "sub dir"/main-sub2 &&
639 (
640 cd "$test_count" &&
641 git fetch ./"sub proj" HEAD &&
642 git subtree merge --prefix="sub dir" FETCH_HEAD &&
643 git subtree split --prefix="sub dir" --message="Split & rejoin" --annotate="*" --rejoin &&
644 test "$(last_commit_subject)" = "Split & rejoin"
645 )
646 '
647
648 test_expect_success 'split "sub dir"/ with --rejoin and --squash' '
649 subtree_test_create_repo "$test_count" &&
650 subtree_test_create_repo "$test_count/sub proj" &&
651 test_create_commit "$test_count" main1 &&
652 test_create_commit "$test_count/sub proj" sub1 &&
653 (
654 cd "$test_count" &&
655 git fetch ./"sub proj" HEAD &&
656 git subtree add --prefix="sub dir" --squash FETCH_HEAD
657 ) &&
658 test_create_commit "$test_count" "sub dir"/main-sub1 &&
659 test_create_commit "$test_count" main2 &&
660 test_create_commit "$test_count/sub proj" sub2 &&
661 test_create_commit "$test_count" "sub dir"/main-sub2 &&
662 (
663 cd "$test_count" &&
664 git subtree pull --prefix="sub dir" --squash ./"sub proj" HEAD &&
665 MAIN=$(git rev-parse --verify HEAD) &&
666 SUB=$(git -C "sub proj" rev-parse --verify HEAD) &&
667
668 SPLIT=$(git subtree split --prefix="sub dir" --annotate="*" --rejoin --squash) &&
669
670 test_must_fail git merge-base --is-ancestor $SUB HEAD &&
671 test_must_fail git merge-base --is-ancestor $SPLIT HEAD &&
672 git rev-list HEAD ^$MAIN >commit-list &&
673 test_line_count = 2 commit-list &&
674 test "$(git rev-parse --verify HEAD:)" = "$(git rev-parse --verify $MAIN:)" &&
675 test "$(git rev-parse --verify HEAD:"sub dir")" = "$(git rev-parse --verify $SPLIT:)" &&
676 test "$(git rev-parse --verify HEAD^1)" = $MAIN &&
677 test "$(git rev-parse --verify HEAD^2)" != $SPLIT &&
678 test "$(git rev-parse --verify HEAD^2:)" = "$(git rev-parse --verify $SPLIT:)" &&
679 test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$SPLIT'\''"
680 )
681 '
682
683 test_expect_success 'split then pull "sub dir"/ with --rejoin and --squash' '
684 # 1. "add"
685 subtree_test_create_repo "$test_count" &&
686 subtree_test_create_repo "$test_count/sub proj" &&
687 test_create_commit "$test_count" main1 &&
688 test_create_commit "$test_count/sub proj" sub1 &&
689 git -C "$test_count" subtree --prefix="sub dir" add --squash ./"sub proj" HEAD &&
690
691 # 2. commit from parent
692 test_create_commit "$test_count" "sub dir"/main-sub1 &&
693
694 # 3. "split --rejoin --squash"
695 git -C "$test_count" subtree --prefix="sub dir" split --rejoin --squash &&
696
697 # 4. "pull --squash"
698 test_create_commit "$test_count/sub proj" sub2 &&
699 git -C "$test_count" subtree -d --prefix="sub dir" pull --squash ./"sub proj" HEAD &&
700
701 test_must_fail git merge-base HEAD FETCH_HEAD
702 '
703
704 test_expect_success 'split "sub dir"/ with --branch' '
705 subtree_test_create_repo "$test_count" &&
706 subtree_test_create_repo "$test_count/sub proj" &&
707 test_create_commit "$test_count" main1 &&
708 test_create_commit "$test_count/sub proj" sub1 &&
709 (
710 cd "$test_count" &&
711 git fetch ./"sub proj" HEAD &&
712 git subtree add --prefix="sub dir" FETCH_HEAD
713 ) &&
714 test_create_commit "$test_count" "sub dir"/main-sub1 &&
715 test_create_commit "$test_count" main2 &&
716 test_create_commit "$test_count/sub proj" sub2 &&
717 test_create_commit "$test_count" "sub dir"/main-sub2 &&
718 (
719 cd "$test_count" &&
720 git fetch ./"sub proj" HEAD &&
721 git subtree merge --prefix="sub dir" FETCH_HEAD &&
722 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
723 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
724 test "$(git rev-parse subproj-br)" = "$split_hash"
725 )
726 '
727
728 test_expect_success 'check hash of split' '
729 subtree_test_create_repo "$test_count" &&
730 subtree_test_create_repo "$test_count/sub proj" &&
731 test_create_commit "$test_count" main1 &&
732 test_create_commit "$test_count/sub proj" sub1 &&
733 (
734 cd "$test_count" &&
735 git fetch ./"sub proj" HEAD &&
736 git subtree add --prefix="sub dir" FETCH_HEAD
737 ) &&
738 test_create_commit "$test_count" "sub dir"/main-sub1 &&
739 test_create_commit "$test_count" main2 &&
740 test_create_commit "$test_count/sub proj" sub2 &&
741 test_create_commit "$test_count" "sub dir"/main-sub2 &&
742 (
743 cd "$test_count" &&
744 git fetch ./"sub proj" HEAD &&
745 git subtree merge --prefix="sub dir" FETCH_HEAD &&
746 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
747 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
748 test "$(git rev-parse subproj-br)" = "$split_hash" &&
749 # Check hash of split
750 new_hash=$(git rev-parse subproj-br^2) &&
751 (
752 cd ./"sub proj" &&
753 subdir_hash=$(git rev-parse HEAD) &&
754 test "$new_hash" = "$subdir_hash"
755 )
756 )
757 '
758
759 test_expect_success 'split "sub dir"/ with --branch for an existing branch' '
760 subtree_test_create_repo "$test_count" &&
761 subtree_test_create_repo "$test_count/sub proj" &&
762 test_create_commit "$test_count" main1 &&
763 test_create_commit "$test_count/sub proj" sub1 &&
764 (
765 cd "$test_count" &&
766 git fetch ./"sub proj" HEAD &&
767 git branch subproj-br FETCH_HEAD &&
768 git subtree add --prefix="sub dir" FETCH_HEAD
769 ) &&
770 test_create_commit "$test_count" "sub dir"/main-sub1 &&
771 test_create_commit "$test_count" main2 &&
772 test_create_commit "$test_count/sub proj" sub2 &&
773 test_create_commit "$test_count" "sub dir"/main-sub2 &&
774 (
775 cd "$test_count" &&
776 git fetch ./"sub proj" HEAD &&
777 git subtree merge --prefix="sub dir" FETCH_HEAD &&
778 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
779 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
780 test "$(git rev-parse subproj-br)" = "$split_hash"
781 )
782 '
783
784 test_expect_success 'split "sub dir"/ with --branch for an incompatible branch' '
785 subtree_test_create_repo "$test_count" &&
786 subtree_test_create_repo "$test_count/sub proj" &&
787 test_create_commit "$test_count" main1 &&
788 test_create_commit "$test_count/sub proj" sub1 &&
789 (
790 cd "$test_count" &&
791 git branch init HEAD &&
792 git fetch ./"sub proj" HEAD &&
793 git subtree add --prefix="sub dir" FETCH_HEAD
794 ) &&
795 test_create_commit "$test_count" "sub dir"/main-sub1 &&
796 test_create_commit "$test_count" main2 &&
797 test_create_commit "$test_count/sub proj" sub2 &&
798 test_create_commit "$test_count" "sub dir"/main-sub2 &&
799 (
800 cd "$test_count" &&
801 git fetch ./"sub proj" HEAD &&
802 git subtree merge --prefix="sub dir" FETCH_HEAD &&
803 test_must_fail git subtree split --prefix="sub dir" --branch init
804 )
805 '
806
807 test_expect_success 'split after annotated tag was added/merged with --squash pre-v2.32.0' '
808 test_create_pre2_32_repo "$test_count" &&
809 test_must_fail git -C "$test_count-clone" subtree split --prefix="sub" HEAD &&
810 git -C "$test_count-clone" subtree split --prefix="sub" HEAD "../$test_count-sub"
811 '
812
813 #
814 # Tests for 'git subtree pull'
815 #
816
817 test_expect_success 'pull requires option --prefix' '
818 subtree_test_create_repo "$test_count" &&
819 subtree_test_create_repo "$test_count/sub proj" &&
820 test_create_commit "$test_count" main1 &&
821 test_create_commit "$test_count/sub proj" sub1 &&
822 (
823 cd "$test_count" &&
824 git fetch ./"sub proj" HEAD &&
825 git subtree add --prefix="sub dir" FETCH_HEAD
826 ) &&
827 test_create_commit "$test_count/sub proj" sub2 &&
828 (
829 cd "$test_count" &&
830 test_must_fail git subtree pull ./"sub proj" HEAD >out 2>err &&
831
832 echo "fatal: you must provide the --prefix option." >expected &&
833 test_must_be_empty out &&
834 test_cmp expected err
835 )
836 '
837
838 test_expect_success 'pull requires path given by option --prefix must exist' '
839 subtree_test_create_repo "$test_count" &&
840 subtree_test_create_repo "$test_count/sub proj" &&
841 test_create_commit "$test_count" main1 &&
842 test_create_commit "$test_count/sub proj" sub1 &&
843 (
844 test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" HEAD >out 2>err &&
845
846 echo "fatal: '\''sub dir'\'' does not exist; use '\''git subtree add'\''" >expected &&
847 test_must_be_empty out &&
848 test_cmp expected err
849 )
850 '
851
852 test_expect_success 'pull basic operation' '
853 subtree_test_create_repo "$test_count" &&
854 subtree_test_create_repo "$test_count/sub proj" &&
855 test_create_commit "$test_count" main1 &&
856 test_create_commit "$test_count/sub proj" sub1 &&
857 (
858 cd "$test_count" &&
859 git fetch ./"sub proj" HEAD &&
860 git subtree add --prefix="sub dir" FETCH_HEAD
861 ) &&
862 test_create_commit "$test_count/sub proj" sub2 &&
863 (
864 cd "$test_count" &&
865 exp=$(git -C "sub proj" rev-parse --verify HEAD:) &&
866 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
867 act=$(git rev-parse --verify HEAD:"sub dir") &&
868 test "$act" = "$exp"
869 )
870 '
871
872 test_expect_success 'pull rejects flags for split' '
873 subtree_test_create_repo "$test_count" &&
874 subtree_test_create_repo "$test_count/sub proj" &&
875 test_create_commit "$test_count" main1 &&
876 test_create_commit "$test_count/sub proj" sub1 &&
877 (
878 cd "$test_count" &&
879 git fetch ./"sub proj" HEAD &&
880 git subtree add --prefix="sub dir" FETCH_HEAD
881 ) &&
882 test_create_commit "$test_count/sub proj" sub2 &&
883 (
884 test_must_fail git subtree pull --prefix="sub dir" --annotate=foo ./"sub proj" HEAD &&
885 test_must_fail git subtree pull --prefix="sub dir" --branch=foo ./"sub proj" HEAD &&
886 test_must_fail git subtree pull --prefix="sub dir" --ignore-joins ./"sub proj" HEAD &&
887 test_must_fail git subtree pull --prefix="sub dir" --onto=foo ./"sub proj" HEAD &&
888 test_must_fail git subtree pull --prefix="sub dir" --rejoin ./"sub proj" HEAD
889 )
890 '
891
892 test_expect_success 'pull with --squash after annotated tag was added/merged with --squash pre-v2.32.0 ' '
893 test_create_pre2_32_repo "$test_count" &&
894 git -C "$test_count-clone" subtree -d pull --prefix="sub" --squash "../$test_count-sub" sub2
895 '
896
897 #
898 # Tests for 'git subtree push'
899 #
900
901 test_expect_success 'push requires option --prefix' '
902 subtree_test_create_repo "$test_count" &&
903 subtree_test_create_repo "$test_count/sub proj" &&
904 test_create_commit "$test_count" main1 &&
905 test_create_commit "$test_count/sub proj" sub1 &&
906 (
907 cd "$test_count" &&
908 git fetch ./"sub proj" HEAD &&
909 git subtree add --prefix="sub dir" FETCH_HEAD &&
910 echo "fatal: you must provide the --prefix option." >expected &&
911 test_must_fail git subtree push "./sub proj" from-mainline >actual 2>&1 &&
912 test_debug "printf '"expected: "'" &&
913 test_debug "cat expected" &&
914 test_debug "printf '"actual: "'" &&
915 test_debug "cat actual" &&
916 test_cmp expected actual
917 )
918 '
919
920 test_expect_success 'push requires path given by option --prefix must exist' '
921 subtree_test_create_repo "$test_count" &&
922 subtree_test_create_repo "$test_count/sub proj" &&
923 test_create_commit "$test_count" main1 &&
924 test_create_commit "$test_count/sub proj" sub1 &&
925 (
926 cd "$test_count" &&
927 git fetch ./"sub proj" HEAD &&
928 git subtree add --prefix="sub dir" FETCH_HEAD &&
929 echo "fatal: '\''non-existent-directory'\'' does not exist; use '\''git subtree add'\''" >expected &&
930 test_must_fail git subtree push --prefix=non-existent-directory "./sub proj" from-mainline >actual 2>&1 &&
931 test_debug "printf '"expected: "'" &&
932 test_debug "cat expected" &&
933 test_debug "printf '"actual: "'" &&
934 test_debug "cat actual" &&
935 test_cmp expected actual
936 )
937 '
938
939 test_expect_success 'push rejects flags for add' '
940 subtree_test_create_repo "$test_count" &&
941 subtree_test_create_repo "$test_count/sub proj" &&
942 test_create_commit "$test_count" main1 &&
943 test_create_commit "$test_count/sub proj" sub1 &&
944 (
945 cd "$test_count" &&
946 git fetch ./"sub proj" HEAD &&
947 git subtree add --prefix="sub dir" FETCH_HEAD
948 ) &&
949 test_create_commit "$test_count" "sub dir"/main-sub1 &&
950 test_create_commit "$test_count" main2 &&
951 test_create_commit "$test_count/sub proj" sub2 &&
952 test_create_commit "$test_count" "sub dir"/main-sub2 &&
953 (
954 cd "$test_count" &&
955 git fetch ./"sub proj" HEAD &&
956 git subtree merge --prefix="sub dir" FETCH_HEAD &&
957 test_wrong_flag git subtree split --prefix="sub dir" --squash ./"sub proj" from-mainline &&
958 test_wrong_flag git subtree split --prefix="sub dir" --message=foo ./"sub proj" from-mainline
959 )
960 '
961
962 test_expect_success 'push basic operation' '
963 subtree_test_create_repo "$test_count" &&
964 subtree_test_create_repo "$test_count/sub proj" &&
965 test_create_commit "$test_count" main1 &&
966 test_create_commit "$test_count/sub proj" sub1 &&
967 (
968 cd "$test_count" &&
969 git fetch ./"sub proj" HEAD &&
970 git subtree add --prefix="sub dir" FETCH_HEAD
971 ) &&
972 test_create_commit "$test_count" "sub dir"/main-sub1 &&
973 test_create_commit "$test_count" main2 &&
974 test_create_commit "$test_count/sub proj" sub2 &&
975 test_create_commit "$test_count" "sub dir"/main-sub2 &&
976 (
977 cd "$test_count" &&
978 git fetch ./"sub proj" HEAD &&
979 git subtree merge --prefix="sub dir" FETCH_HEAD &&
980 before=$(git rev-parse --verify HEAD) &&
981 split_hash=$(git subtree split --prefix="sub dir") &&
982 git subtree push --prefix="sub dir" ./"sub proj" from-mainline &&
983 test "$before" = "$(git rev-parse --verify HEAD)" &&
984 test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
985 )
986 '
987
988 test_expect_success 'push sub dir/ with --rejoin' '
989 subtree_test_create_repo "$test_count" &&
990 subtree_test_create_repo "$test_count/sub proj" &&
991 test_create_commit "$test_count" main1 &&
992 test_create_commit "$test_count/sub proj" sub1 &&
993 (
994 cd "$test_count" &&
995 git fetch ./"sub proj" HEAD &&
996 git subtree add --prefix="sub dir" FETCH_HEAD
997 ) &&
998 test_create_commit "$test_count" "sub dir"/main-sub1 &&
999 test_create_commit "$test_count" main2 &&
1000 test_create_commit "$test_count/sub proj" sub2 &&
1001 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1002 (
1003 cd "$test_count" &&
1004 git fetch ./"sub proj" HEAD &&
1005 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1006 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
1007 git subtree push --prefix="sub dir" --annotate="*" --rejoin ./"sub proj" from-mainline &&
1008 test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" &&
1009 test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
1010 )
1011 '
1012
1013 test_expect_success 'push sub dir/ with --rejoin from scratch' '
1014 subtree_test_create_repo "$test_count" &&
1015 test_create_commit "$test_count" main1 &&
1016 (
1017 cd "$test_count" &&
1018 mkdir "sub dir" &&
1019 echo file >"sub dir"/file &&
1020 git add "sub dir/file" &&
1021 git commit -m"sub dir file" &&
1022 split_hash=$(git subtree split --prefix="sub dir" --rejoin) &&
1023 git init --bare "sub proj.git" &&
1024 git subtree push --prefix="sub dir" --rejoin ./"sub proj.git" from-mainline &&
1025 test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" &&
1026 test "$split_hash" = "$(git -C "sub proj.git" rev-parse --verify refs/heads/from-mainline)"
1027 )
1028 '
1029
1030 test_expect_success 'push sub dir/ with --rejoin and --message' '
1031 subtree_test_create_repo "$test_count" &&
1032 subtree_test_create_repo "$test_count/sub proj" &&
1033 test_create_commit "$test_count" main1 &&
1034 test_create_commit "$test_count/sub proj" sub1 &&
1035 (
1036 cd "$test_count" &&
1037 git fetch ./"sub proj" HEAD &&
1038 git subtree add --prefix="sub dir" FETCH_HEAD
1039 ) &&
1040 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1041 test_create_commit "$test_count" main2 &&
1042 test_create_commit "$test_count/sub proj" sub2 &&
1043 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1044 (
1045 cd "$test_count" &&
1046 git fetch ./"sub proj" HEAD &&
1047 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1048 git subtree push --prefix="sub dir" --message="Split & rejoin" --annotate="*" --rejoin ./"sub proj" from-mainline &&
1049 test "$(last_commit_subject)" = "Split & rejoin" &&
1050 split_hash="$(git rev-parse --verify HEAD^2)" &&
1051 test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
1052 )
1053 '
1054
1055 test_expect_success 'push "sub dir"/ with --rejoin and --squash' '
1056 subtree_test_create_repo "$test_count" &&
1057 subtree_test_create_repo "$test_count/sub proj" &&
1058 test_create_commit "$test_count" main1 &&
1059 test_create_commit "$test_count/sub proj" sub1 &&
1060 (
1061 cd "$test_count" &&
1062 git fetch ./"sub proj" HEAD &&
1063 git subtree add --prefix="sub dir" --squash FETCH_HEAD
1064 ) &&
1065 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1066 test_create_commit "$test_count" main2 &&
1067 test_create_commit "$test_count/sub proj" sub2 &&
1068 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1069 (
1070 cd "$test_count" &&
1071 git subtree pull --prefix="sub dir" --squash ./"sub proj" HEAD &&
1072 MAIN=$(git rev-parse --verify HEAD) &&
1073 SUB=$(git -C "sub proj" rev-parse --verify HEAD) &&
1074
1075 SPLIT=$(git subtree split --prefix="sub dir" --annotate="*") &&
1076 git subtree push --prefix="sub dir" --annotate="*" --rejoin --squash ./"sub proj" from-mainline &&
1077
1078 test_must_fail git merge-base --is-ancestor $SUB HEAD &&
1079 test_must_fail git merge-base --is-ancestor $SPLIT HEAD &&
1080 git rev-list HEAD ^$MAIN >commit-list &&
1081 test_line_count = 2 commit-list &&
1082 test "$(git rev-parse --verify HEAD:)" = "$(git rev-parse --verify $MAIN:)" &&
1083 test "$(git rev-parse --verify HEAD:"sub dir")" = "$(git rev-parse --verify $SPLIT:)" &&
1084 test "$(git rev-parse --verify HEAD^1)" = $MAIN &&
1085 test "$(git rev-parse --verify HEAD^2)" != $SPLIT &&
1086 test "$(git rev-parse --verify HEAD^2:)" = "$(git rev-parse --verify $SPLIT:)" &&
1087 test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$SPLIT'\''" &&
1088 test "$SPLIT" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
1089 )
1090 '
1091
1092 test_expect_success 'push "sub dir"/ with --branch' '
1093 subtree_test_create_repo "$test_count" &&
1094 subtree_test_create_repo "$test_count/sub proj" &&
1095 test_create_commit "$test_count" main1 &&
1096 test_create_commit "$test_count/sub proj" sub1 &&
1097 (
1098 cd "$test_count" &&
1099 git fetch ./"sub proj" HEAD &&
1100 git subtree add --prefix="sub dir" FETCH_HEAD
1101 ) &&
1102 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1103 test_create_commit "$test_count" main2 &&
1104 test_create_commit "$test_count/sub proj" sub2 &&
1105 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1106 (
1107 cd "$test_count" &&
1108 git fetch ./"sub proj" HEAD &&
1109 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1110 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
1111 git subtree push --prefix="sub dir" --annotate="*" --branch subproj-br ./"sub proj" from-mainline &&
1112 test "$(git rev-parse subproj-br)" = "$split_hash" &&
1113 test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
1114 )
1115 '
1116
1117 test_expect_success 'check hash of push' '
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 fetch ./"sub proj" HEAD &&
1125 git subtree add --prefix="sub dir" FETCH_HEAD
1126 ) &&
1127 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1128 test_create_commit "$test_count" main2 &&
1129 test_create_commit "$test_count/sub proj" sub2 &&
1130 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1131 (
1132 cd "$test_count" &&
1133 git fetch ./"sub proj" HEAD &&
1134 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1135 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
1136 git subtree push --prefix="sub dir" --annotate="*" --branch subproj-br ./"sub proj" from-mainline &&
1137 test "$(git rev-parse subproj-br)" = "$split_hash" &&
1138 # Check hash of split
1139 new_hash=$(git rev-parse subproj-br^2) &&
1140 (
1141 cd ./"sub proj" &&
1142 subdir_hash=$(git rev-parse HEAD) &&
1143 test "$new_hash" = "$subdir_hash"
1144 ) &&
1145 test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
1146 )
1147 '
1148
1149 test_expect_success 'push "sub dir"/ with --branch for an existing branch' '
1150 subtree_test_create_repo "$test_count" &&
1151 subtree_test_create_repo "$test_count/sub proj" &&
1152 test_create_commit "$test_count" main1 &&
1153 test_create_commit "$test_count/sub proj" sub1 &&
1154 (
1155 cd "$test_count" &&
1156 git fetch ./"sub proj" HEAD &&
1157 git branch subproj-br FETCH_HEAD &&
1158 git subtree add --prefix="sub dir" FETCH_HEAD
1159 ) &&
1160 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1161 test_create_commit "$test_count" main2 &&
1162 test_create_commit "$test_count/sub proj" sub2 &&
1163 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1164 (
1165 cd "$test_count" &&
1166 git fetch ./"sub proj" HEAD &&
1167 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1168 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
1169 git subtree push --prefix="sub dir" --annotate="*" --branch subproj-br ./"sub proj" from-mainline &&
1170 test "$(git rev-parse subproj-br)" = "$split_hash" &&
1171 test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
1172 )
1173 '
1174
1175 test_expect_success 'push "sub dir"/ with --branch for an incompatible branch' '
1176 subtree_test_create_repo "$test_count" &&
1177 subtree_test_create_repo "$test_count/sub proj" &&
1178 test_create_commit "$test_count" main1 &&
1179 test_create_commit "$test_count/sub proj" sub1 &&
1180 (
1181 cd "$test_count" &&
1182 git branch init HEAD &&
1183 git fetch ./"sub proj" HEAD &&
1184 git subtree add --prefix="sub dir" FETCH_HEAD
1185 ) &&
1186 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1187 test_create_commit "$test_count" main2 &&
1188 test_create_commit "$test_count/sub proj" sub2 &&
1189 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1190 (
1191 cd "$test_count" &&
1192 git fetch ./"sub proj" HEAD &&
1193 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1194 test_must_fail git subtree push --prefix="sub dir" --branch init "./sub proj" from-mainline
1195 )
1196 '
1197
1198 test_expect_success 'push "sub dir"/ with a local rev' '
1199 subtree_test_create_repo "$test_count" &&
1200 subtree_test_create_repo "$test_count/sub proj" &&
1201 test_create_commit "$test_count" main1 &&
1202 test_create_commit "$test_count/sub proj" sub1 &&
1203 (
1204 cd "$test_count" &&
1205 git fetch ./"sub proj" HEAD &&
1206 git subtree add --prefix="sub dir" FETCH_HEAD
1207 ) &&
1208 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1209 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1210 (
1211 cd "$test_count" &&
1212 bad_tree=$(git rev-parse --verify HEAD:"sub dir") &&
1213 good_tree=$(git rev-parse --verify HEAD^:"sub dir") &&
1214 git subtree push --prefix="sub dir" --annotate="*" ./"sub proj" HEAD^:from-mainline &&
1215 split_tree=$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline:) &&
1216 test "$split_tree" = "$good_tree"
1217 )
1218 '
1219
1220 test_expect_success 'push after annotated tag was added/merged with --squash pre-v2.32.0' '
1221 test_create_pre2_32_repo "$test_count" &&
1222 test_create_commit "$test_count-clone" sub/main-sub1 &&
1223 git -C "$test_count-clone" subtree push --prefix="sub" "../$test_count-sub" from-mainline
1224 '
1225
1226 #
1227 # Validity checking
1228 #
1229
1230 test_expect_success 'make sure exactly the right set of files ends up in the subproj' '
1231 subtree_test_create_repo "$test_count" &&
1232 subtree_test_create_repo "$test_count/sub proj" &&
1233 test_create_commit "$test_count" main1 &&
1234 test_create_commit "$test_count/sub proj" sub1 &&
1235 (
1236 cd "$test_count" &&
1237 git fetch ./"sub proj" HEAD &&
1238 git subtree add --prefix="sub dir" FETCH_HEAD
1239 ) &&
1240 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1241 test_create_commit "$test_count" main2 &&
1242 test_create_commit "$test_count/sub proj" sub2 &&
1243 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1244 (
1245 cd "$test_count" &&
1246 git fetch ./"sub proj" HEAD &&
1247 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1248 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1249 ) &&
1250 test_create_commit "$test_count/sub proj" sub3 &&
1251 test_create_commit "$test_count" "sub dir"/main-sub3 &&
1252 (
1253 cd "$test_count/sub proj" &&
1254 git fetch .. subproj-br &&
1255 git merge FETCH_HEAD
1256 ) &&
1257 test_create_commit "$test_count/sub proj" sub4 &&
1258 (
1259 cd "$test_count" &&
1260 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1261 ) &&
1262 test_create_commit "$test_count" "sub dir"/main-sub4 &&
1263 (
1264 cd "$test_count" &&
1265 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1266 ) &&
1267 (
1268 cd "$test_count/sub proj" &&
1269 git fetch .. subproj-br &&
1270 git merge FETCH_HEAD &&
1271
1272 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 \
1273 sub1 sub2 sub3 sub4 >expect &&
1274 git ls-files >actual &&
1275 test_cmp expect actual
1276 )
1277 '
1278
1279 test_expect_success 'make sure the subproj *only* contains commits that affect the "sub dir"' '
1280 subtree_test_create_repo "$test_count" &&
1281 subtree_test_create_repo "$test_count/sub proj" &&
1282 test_create_commit "$test_count" main1 &&
1283 test_create_commit "$test_count/sub proj" sub1 &&
1284 (
1285 cd "$test_count" &&
1286 git fetch ./"sub proj" HEAD &&
1287 git subtree add --prefix="sub dir" FETCH_HEAD
1288 ) &&
1289 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1290 test_create_commit "$test_count" main2 &&
1291 test_create_commit "$test_count/sub proj" sub2 &&
1292 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1293 (
1294 cd "$test_count" &&
1295 git fetch ./"sub proj" HEAD &&
1296 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1297 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1298 ) &&
1299 test_create_commit "$test_count/sub proj" sub3 &&
1300 test_create_commit "$test_count" "sub dir"/main-sub3 &&
1301 (
1302 cd "$test_count/sub proj" &&
1303 git fetch .. subproj-br &&
1304 git merge FETCH_HEAD
1305 ) &&
1306 test_create_commit "$test_count/sub proj" sub4 &&
1307 (
1308 cd "$test_count" &&
1309 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1310 ) &&
1311 test_create_commit "$test_count" "sub dir"/main-sub4 &&
1312 (
1313 cd "$test_count" &&
1314 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1315 ) &&
1316 (
1317 cd "$test_count/sub proj" &&
1318 git fetch .. subproj-br &&
1319 git merge FETCH_HEAD &&
1320
1321 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 \
1322 sub1 sub2 sub3 sub4 >expect &&
1323 git log --name-only --pretty=format:"" >log &&
1324 sort <log | sed "/^\$/ d" >actual &&
1325 test_cmp expect actual
1326 )
1327 '
1328
1329 test_expect_success 'make sure exactly the right set of files ends up in the mainline' '
1330 subtree_test_create_repo "$test_count" &&
1331 subtree_test_create_repo "$test_count/sub proj" &&
1332 test_create_commit "$test_count" main1 &&
1333 test_create_commit "$test_count/sub proj" sub1 &&
1334 (
1335 cd "$test_count" &&
1336 git fetch ./"sub proj" HEAD &&
1337 git subtree add --prefix="sub dir" FETCH_HEAD
1338 ) &&
1339 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1340 test_create_commit "$test_count" main2 &&
1341 test_create_commit "$test_count/sub proj" sub2 &&
1342 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1343 (
1344 cd "$test_count" &&
1345 git fetch ./"sub proj" HEAD &&
1346 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1347 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1348 ) &&
1349 test_create_commit "$test_count/sub proj" sub3 &&
1350 test_create_commit "$test_count" "sub dir"/main-sub3 &&
1351 (
1352 cd "$test_count/sub proj" &&
1353 git fetch .. subproj-br &&
1354 git merge FETCH_HEAD
1355 ) &&
1356 test_create_commit "$test_count/sub proj" sub4 &&
1357 (
1358 cd "$test_count" &&
1359 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1360 ) &&
1361 test_create_commit "$test_count" "sub dir"/main-sub4 &&
1362 (
1363 cd "$test_count" &&
1364 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1365 ) &&
1366 (
1367 cd "$test_count/sub proj" &&
1368 git fetch .. subproj-br &&
1369 git merge FETCH_HEAD
1370 ) &&
1371 (
1372 cd "$test_count" &&
1373 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
1374
1375 test_write_lines main1 main2 >chkm &&
1376 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 >chkms &&
1377 sed "s,^,sub dir/," chkms >chkms_sub &&
1378 test_write_lines sub1 sub2 sub3 sub4 >chks &&
1379 sed "s,^,sub dir/," chks >chks_sub &&
1380
1381 cat chkm chkms_sub chks_sub >expect &&
1382 git ls-files >actual &&
1383 test_cmp expect actual
1384 )
1385 '
1386
1387 test_expect_success 'make sure each filename changed exactly once in the entire history' '
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 config log.date relative &&
1395 git fetch ./"sub proj" HEAD &&
1396 git subtree add --prefix="sub dir" FETCH_HEAD
1397 ) &&
1398 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1399 test_create_commit "$test_count" main2 &&
1400 test_create_commit "$test_count/sub proj" sub2 &&
1401 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1402 (
1403 cd "$test_count" &&
1404 git fetch ./"sub proj" HEAD &&
1405 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1406 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1407 ) &&
1408 test_create_commit "$test_count/sub proj" sub3 &&
1409 test_create_commit "$test_count" "sub dir"/main-sub3 &&
1410 (
1411 cd "$test_count/sub proj" &&
1412 git fetch .. subproj-br &&
1413 git merge FETCH_HEAD
1414 ) &&
1415 test_create_commit "$test_count/sub proj" sub4 &&
1416 (
1417 cd "$test_count" &&
1418 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1419 ) &&
1420 test_create_commit "$test_count" "sub dir"/main-sub4 &&
1421 (
1422 cd "$test_count" &&
1423 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1424 ) &&
1425 (
1426 cd "$test_count/sub proj" &&
1427 git fetch .. subproj-br &&
1428 git merge FETCH_HEAD
1429 ) &&
1430 (
1431 cd "$test_count" &&
1432 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
1433
1434 test_write_lines main1 main2 >chkm &&
1435 test_write_lines sub1 sub2 sub3 sub4 >chks &&
1436 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 >chkms &&
1437 sed "s,^,sub dir/," chkms >chkms_sub &&
1438
1439 # main-sub?? and /"sub dir"/main-sub?? both change, because those are the
1440 # changes that were split into their own history. And "sub dir"/sub?? never
1441 # change, since they were *only* changed in the subtree branch.
1442 git log --name-only --pretty=format:"" >log &&
1443 sort <log >sorted-log &&
1444 sed "/^$/ d" sorted-log >actual &&
1445
1446 cat chkms chkm chks chkms_sub >expect-unsorted &&
1447 sort expect-unsorted >expect &&
1448 test_cmp expect actual
1449 )
1450 '
1451
1452 test_expect_success 'make sure the --rejoin commits never make it into subproj' '
1453 subtree_test_create_repo "$test_count" &&
1454 subtree_test_create_repo "$test_count/sub proj" &&
1455 test_create_commit "$test_count" main1 &&
1456 test_create_commit "$test_count/sub proj" sub1 &&
1457 (
1458 cd "$test_count" &&
1459 git fetch ./"sub proj" HEAD &&
1460 git subtree add --prefix="sub dir" FETCH_HEAD
1461 ) &&
1462 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1463 test_create_commit "$test_count" main2 &&
1464 test_create_commit "$test_count/sub proj" sub2 &&
1465 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1466 (
1467 cd "$test_count" &&
1468 git fetch ./"sub proj" HEAD &&
1469 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1470 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1471 ) &&
1472 test_create_commit "$test_count/sub proj" sub3 &&
1473 test_create_commit "$test_count" "sub dir"/main-sub3 &&
1474 (
1475 cd "$test_count/sub proj" &&
1476 git fetch .. subproj-br &&
1477 git merge FETCH_HEAD
1478 ) &&
1479 test_create_commit "$test_count/sub proj" sub4 &&
1480 (
1481 cd "$test_count" &&
1482 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1483 ) &&
1484 test_create_commit "$test_count" "sub dir"/main-sub4 &&
1485 (
1486 cd "$test_count" &&
1487 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1488 ) &&
1489 (
1490 cd "$test_count/sub proj" &&
1491 git fetch .. subproj-br &&
1492 git merge FETCH_HEAD
1493 ) &&
1494 (
1495 cd "$test_count" &&
1496 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
1497 test "$(git log --pretty=format:"%s" HEAD^2 | grep -i split)" = ""
1498 )
1499 '
1500
1501 test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' '
1502 subtree_test_create_repo "$test_count" &&
1503 subtree_test_create_repo "$test_count/sub proj" &&
1504 test_create_commit "$test_count" main1 &&
1505 test_create_commit "$test_count/sub proj" sub1 &&
1506 (
1507 cd "$test_count" &&
1508 git fetch ./"sub proj" HEAD &&
1509 git subtree add --prefix="sub dir" FETCH_HEAD
1510 ) &&
1511 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1512 test_create_commit "$test_count" main2 &&
1513 test_create_commit "$test_count/sub proj" sub2 &&
1514 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1515 (
1516 cd "$test_count" &&
1517 git fetch ./"sub proj" HEAD &&
1518 git subtree merge --prefix="sub dir" FETCH_HEAD &&
1519 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1520 ) &&
1521 test_create_commit "$test_count/sub proj" sub3 &&
1522 test_create_commit "$test_count" "sub dir"/main-sub3 &&
1523 (
1524 cd "$test_count/sub proj" &&
1525 git fetch .. subproj-br &&
1526 git merge FETCH_HEAD
1527 ) &&
1528 test_create_commit "$test_count/sub proj" sub4 &&
1529 (
1530 cd "$test_count" &&
1531 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1532 ) &&
1533 test_create_commit "$test_count" "sub dir"/main-sub4 &&
1534 (
1535 cd "$test_count" &&
1536 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1537 ) &&
1538 (
1539 cd "$test_count/sub proj" &&
1540 git fetch .. subproj-br &&
1541 git merge FETCH_HEAD
1542 ) &&
1543 (
1544 cd "$test_count" &&
1545 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
1546
1547 # They are meaningless to subproj since one side of the merge refers to the mainline
1548 test "$(git log --pretty=format:"%s%n%b" HEAD^2 | grep "git-subtree.*:")" = ""
1549 )
1550 '
1551
1552 #
1553 # A new set of tests
1554 #
1555
1556 test_expect_success 'make sure "git subtree split" find the correct parent' '
1557 subtree_test_create_repo "$test_count" &&
1558 subtree_test_create_repo "$test_count/sub proj" &&
1559 test_create_commit "$test_count" main1 &&
1560 test_create_commit "$test_count/sub proj" sub1 &&
1561 (
1562 cd "$test_count" &&
1563 git fetch ./"sub proj" HEAD &&
1564 git subtree add --prefix="sub dir" FETCH_HEAD
1565 ) &&
1566 test_create_commit "$test_count/sub proj" sub2 &&
1567 (
1568 cd "$test_count" &&
1569 git fetch ./"sub proj" HEAD &&
1570 git branch subproj-ref FETCH_HEAD &&
1571 git subtree merge --prefix="sub dir" FETCH_HEAD
1572 ) &&
1573 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1574 (
1575 cd "$test_count" &&
1576 git subtree split --prefix="sub dir" --branch subproj-br &&
1577
1578 # at this point, the new commit parent should be subproj-ref, if it is
1579 # not, something went wrong (the "newparent" of "HEAD~" commit should
1580 # have been sub2, but it was not, because its cache was not set to
1581 # itself)
1582 test "$(git log --pretty=format:%P -1 subproj-br)" = "$(git rev-parse subproj-ref)"
1583 )
1584 '
1585
1586 test_expect_success 'split a new subtree without --onto option' '
1587 subtree_test_create_repo "$test_count" &&
1588 subtree_test_create_repo "$test_count/sub proj" &&
1589 test_create_commit "$test_count" main1 &&
1590 test_create_commit "$test_count/sub proj" sub1 &&
1591 (
1592 cd "$test_count" &&
1593 git fetch ./"sub proj" HEAD &&
1594 git subtree add --prefix="sub dir" FETCH_HEAD
1595 ) &&
1596 test_create_commit "$test_count/sub proj" sub2 &&
1597 (
1598 cd "$test_count" &&
1599 git fetch ./"sub proj" HEAD &&
1600 git subtree merge --prefix="sub dir" FETCH_HEAD
1601 ) &&
1602 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1603 (
1604 cd "$test_count" &&
1605 git subtree split --prefix="sub dir" --branch subproj-br
1606 ) &&
1607 mkdir "$test_count"/"sub dir2" &&
1608 test_create_commit "$test_count" "sub dir2"/main-sub2 &&
1609 (
1610 cd "$test_count" &&
1611
1612 # also test that we still can split out an entirely new subtree
1613 # if the parent of the first commit in the tree is not empty,
1614 # then the new subtree has accidentally been attached to something
1615 git subtree split --prefix="sub dir2" --branch subproj2-br &&
1616 test "$(git log --pretty=format:%P -1 subproj2-br)" = ""
1617 )
1618 '
1619
1620 test_expect_success 'verify one file change per commit' '
1621 subtree_test_create_repo "$test_count" &&
1622 subtree_test_create_repo "$test_count/sub proj" &&
1623 test_create_commit "$test_count" main1 &&
1624 test_create_commit "$test_count/sub proj" sub1 &&
1625 (
1626 cd "$test_count" &&
1627 git fetch ./"sub proj" HEAD &&
1628 git branch sub1 FETCH_HEAD &&
1629 git subtree add --prefix="sub dir" sub1
1630 ) &&
1631 test_create_commit "$test_count/sub proj" sub2 &&
1632 (
1633 cd "$test_count" &&
1634 git fetch ./"sub proj" HEAD &&
1635 git subtree merge --prefix="sub dir" FETCH_HEAD
1636 ) &&
1637 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1638 (
1639 cd "$test_count" &&
1640 git subtree split --prefix="sub dir" --branch subproj-br
1641 ) &&
1642 mkdir "$test_count"/"sub dir2" &&
1643 test_create_commit "$test_count" "sub dir2"/main-sub2 &&
1644 (
1645 cd "$test_count" &&
1646 git subtree split --prefix="sub dir2" --branch subproj2-br &&
1647
1648 git log --format="%H" >commit-list &&
1649 while read commit
1650 do
1651 git log -n1 --format="" --name-only "$commit" >file-list &&
1652 test_line_count -le 1 file-list || return 1
1653 done <commit-list
1654 )
1655 '
1656
1657 test_expect_success 'push split to subproj' '
1658 subtree_test_create_repo "$test_count" &&
1659 subtree_test_create_repo "$test_count/sub proj" &&
1660 test_create_commit "$test_count" main1 &&
1661 test_create_commit "$test_count/sub proj" sub1 &&
1662 (
1663 cd "$test_count" &&
1664 git fetch ./"sub proj" HEAD &&
1665 git subtree add --prefix="sub dir" FETCH_HEAD
1666 ) &&
1667 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1668 test_create_commit "$test_count" main2 &&
1669 test_create_commit "$test_count/sub proj" sub2 &&
1670 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1671 (
1672 cd $test_count/"sub proj" &&
1673 git branch sub-branch-1 &&
1674 cd .. &&
1675 git fetch ./"sub proj" HEAD &&
1676 git subtree merge --prefix="sub dir" FETCH_HEAD
1677 ) &&
1678 test_create_commit "$test_count" "sub dir"/main-sub3 &&
1679 (
1680 cd "$test_count" &&
1681 git subtree push ./"sub proj" --prefix "sub dir" sub-branch-1 &&
1682 cd ./"sub proj" &&
1683 git checkout sub-branch-1 &&
1684 test "$(last_commit_subject)" = "sub dir/main-sub3"
1685 )
1686 '
1687
1688 # --ignore-joins must ignore mainline content outside of the
1689 # subtree. This test verifies that the logic in
1690 # `find_existing_splits()` correctly handles a `git subtree add`
1691 # In this test, the split history must not contain a commit titled
1692 #
1693 # Add 'sub/' from commit ...
1694 #
1695 # see: dd21d43b58 (subtree: make --ignore-joins pay
1696 # attention to adds, 2018-09-28)
1697 test_expect_success 'split --ignore-joins respects subtree add' '
1698 subtree_test_create_repo "$test_count" &&
1699 (
1700 cd "$test_count" &&
1701 test_commit main_must_not_be_in_subtree &&
1702 test_create_subtree_add . mksubtree sub sub1 &&
1703 test_commit sub/sub2 &&
1704 test_commit main_must_not_be_in_subtree2 &&
1705 git subtree split --prefix sub -b first_split --rejoin &&
1706 test_commit sub/sub3 &&
1707 no_ignore_joins="$(git subtree split --prefix sub -b no_ignore_joins)" &&
1708 ignore_joins="$(git subtree split --prefix sub --ignore-joins -b ignore_joins)" &&
1709 git checkout ignore_joins &&
1710 test_path_is_file sub1.t &&
1711 test_path_is_file sub2.t &&
1712 test_path_is_file sub3.t &&
1713 ! test_path_is_file main_must_not_be_in_subtree.t &&
1714 ! test_path_is_file main_must_not_be_in_subtree2.t &&
1715 test -z "$(git log -1 --grep "Add '''sub/''' from commit" ignore_joins)" &&
1716 test "$no_ignore_joins" = "$ignore_joins" &&
1717 test "$(git rev-list --count ignore_joins)" -eq 3;
1718 )
1719 '
1720
1721 # split excludes commits reachable from any previous --rejoin.
1722 # These ignored commits can still be the basis for new work
1723 # after the --rejoin. These commits must be processed, even
1724 # if they are excluded. Otherwise, the split history will be
1725 # incorrect.
1726 #
1727 # here, the merge
1728 #
1729 # git merge --no-ff new_work_based_on_prejoin
1730 #
1731 # doesn't contain any subtree changes and so should not end
1732 # up in the split history. this subtree should be flat,
1733 # with no merges.
1734 #
1735 # see: 315a84f9aa (subtree: use commits before rejoins for
1736 # splits, 2018-09-28)
1737 test_expect_success 'split links out-of-tree pre --rejoin commits with post --rejoin commits' '
1738 subtree_test_create_repo "$test_count" &&
1739 (
1740 cd "$test_count" &&
1741 test_commit main_must_not_be_in_subtree &&
1742 mkdir sub &&
1743 test_commit sub/sub1 &&
1744 test_commit sub/sub2 &&
1745 git subtree split --prefix sub --rejoin &&
1746 test "$(git rev-list --count HEAD)" -eq 6 &&
1747 git checkout sub/sub1 &&
1748 git checkout -b new_work_based_on_prejoin &&
1749 test_commit main_must_not_be_in_subtree2 &&
1750 git checkout main &&
1751 git merge --no-ff new_work_based_on_prejoin &&
1752 test_commit sub/sub3 &&
1753 git subtree split -d --prefix sub -b second_split &&
1754 git checkout second_split &&
1755 test_path_is_file sub1.t &&
1756 test_path_is_file sub2.t &&
1757 test_path_is_file sub3.t &&
1758 ! test_path_is_file main_must_not_be_in_subtree.t &&
1759 ! test_path_is_file main_must_not_be_in_subtree2.t &&
1760 test "$(git rev-list --count --merges second_split)" -eq 0 &&
1761 test "$(git rev-list --count second_split)" -eq 3;
1762 )
1763 '
1764
1765 # split must keep merge commits with unrelated histories, even
1766 # if both parents are treesame. When deciding whether or not
1767 # to eliminate a parent, copy_or_skip compares the merge-base
1768 # of each parent.
1769 #
1770 # in the split_of_merges branch:
1771 #
1772 # * expect 4 commits
1773 # * HEAD~ must be a merge
1774 #
1775 # see: 68f8ff8151 (subtree: improve decision on merges kept
1776 # in split, 2018-09-28)
1777 test_expect_success 'split preserves merges with unrelated history' '
1778 subtree_test_create_repo "$test_count" &&
1779 (
1780 cd "$test_count" &&
1781 test_commit main_must_not_be_in_subtree &&
1782 mkdir sub &&
1783 test_commit sub/sub1 &&
1784 git checkout --orphan new_history &&
1785 git checkout sub/sub1 -- . &&
1786 git add . &&
1787 git commit -m "treesame history but not a merge-base" &&
1788 git checkout main &&
1789 git merge --allow-unrelated-histories --no-ff new_history &&
1790 test "$(git rev-parse "HEAD^1^{tree}")" = "$(git rev-parse "HEAD^2^{tree}")" &&
1791 test_commit sub/sub2 &&
1792 git subtree split -d --prefix sub -b split_of_merges &&
1793 test "$(git rev-list --count split_of_merges)" -eq 4 &&
1794 test -n "$(git rev-list --merges HEAD~)";
1795 )
1796 '
1797
1798 #
1799 # This test covers 2 cases in subtree split copy_or_skip code
1800 # 1) Merges where one parent is a superset of the changes of the other
1801 # parent regarding changes to the subtree, in this case the merge
1802 # commit should be copied
1803 # 2) Merges where only one parent operate on the subtree, and the merge
1804 # commit should be skipped
1805 #
1806 # (1) is checked by ensuring subtree_tip is a descendent of subtree_branch
1807 # (2) should have a check added (not_a_subtree_change shouldn't be present
1808 # on the produced subtree)
1809 #
1810 # Other related cases which are not tested (or currently handled correctly)
1811 # - Case (1) where there are more than 2 parents, it will sometimes correctly copy
1812 # the merge, and sometimes not
1813 # - Merge commit where both parents have same tree as the merge, currently
1814 # will always be skipped, even if they reached that state via different
1815 # set of commits.
1816 #
1817
1818 test_expect_success 'subtree descendant check' '
1819 subtree_test_create_repo "$test_count" &&
1820 test_create_commit "$test_count" folder_subtree/a &&
1821 (
1822 cd "$test_count" &&
1823 git branch branch
1824 ) &&
1825 test_create_commit "$test_count" folder_subtree/0 &&
1826 test_create_commit "$test_count" folder_subtree/b &&
1827 cherry=$(cd "$test_count" && git rev-parse HEAD) &&
1828 (
1829 cd "$test_count" &&
1830 git checkout branch
1831 ) &&
1832 test_create_commit "$test_count" commit_on_branch &&
1833 (
1834 cd "$test_count" &&
1835 git cherry-pick $cherry &&
1836 git checkout main &&
1837 git merge -m "merge should be kept on subtree" branch &&
1838 git branch no_subtree_work_branch
1839 ) &&
1840 test_create_commit "$test_count" folder_subtree/d &&
1841 (
1842 cd "$test_count" &&
1843 git checkout no_subtree_work_branch
1844 ) &&
1845 test_create_commit "$test_count" not_a_subtree_change &&
1846 (
1847 cd "$test_count" &&
1848 git checkout main &&
1849 git merge -m "merge should be skipped on subtree" no_subtree_work_branch &&
1850
1851 git subtree split --prefix folder_subtree/ --branch subtree_tip main &&
1852 git subtree split --prefix folder_subtree/ --branch subtree_branch branch &&
1853 test $(git rev-list --count subtree_tip..subtree_branch) = 0
1854 )
1855 '
1856
1857 test_expect_success GPG 'add subproj with GPG signing using -S flag' '
1858 subtree_test_create_repo "$test_count" &&
1859 subtree_test_create_repo "$test_count/sub proj" &&
1860 test_create_commit "$test_count" main1 &&
1861 test_create_commit "$test_count/sub proj" sub1 &&
1862 (
1863 cd "$test_count" &&
1864 git fetch ./"sub proj" HEAD &&
1865 git subtree add --prefix="sub dir" -S FETCH_HEAD &&
1866 git verify-commit HEAD &&
1867 test "$(last_commit_subject)" = "Add '\''sub dir/'\'' from commit '\''$(git rev-parse FETCH_HEAD)'\''"
1868 )
1869 '
1870
1871 test_expect_success GPG 'add subproj with GPG signing using --gpg-sign flag' '
1872 subtree_test_create_repo "$test_count" &&
1873 subtree_test_create_repo "$test_count/sub proj" &&
1874 test_create_commit "$test_count" main1 &&
1875 test_create_commit "$test_count/sub proj" sub1 &&
1876 (
1877 cd "$test_count" &&
1878 git fetch ./"sub proj" HEAD &&
1879 git subtree add --prefix="sub dir" --gpg-sign FETCH_HEAD &&
1880 git verify-commit HEAD &&
1881 test "$(last_commit_subject)" = "Add '\''sub dir/'\'' from commit '\''$(git rev-parse FETCH_HEAD)'\''"
1882 )
1883 '
1884
1885 test_expect_success GPG 'add subproj with GPG signing using specific key ID' '
1886 subtree_test_create_repo "$test_count" &&
1887 subtree_test_create_repo "$test_count/sub proj" &&
1888 test_create_commit "$test_count" main1 &&
1889 test_create_commit "$test_count/sub proj" sub1 &&
1890 (
1891 cd "$test_count" &&
1892 git fetch ./"sub proj" HEAD &&
1893 git subtree add --prefix="sub dir" -S"$GIT_COMMITTER_EMAIL" FETCH_HEAD &&
1894 git verify-commit HEAD &&
1895 test "$(last_commit_subject)" = "Add '\''sub dir/'\'' from commit '\''$(git rev-parse FETCH_HEAD)'\''"
1896 )
1897 '
1898
1899 test_expect_success GPG 'merge with GPG signing' '
1900 subtree_test_create_repo "$test_count" &&
1901 subtree_test_create_repo "$test_count/sub proj" &&
1902 test_create_commit "$test_count" main1 &&
1903 test_create_commit "$test_count/sub proj" sub1 &&
1904 (
1905 cd "$test_count" &&
1906 git fetch ./"sub proj" HEAD &&
1907 git subtree add --prefix="sub dir" FETCH_HEAD
1908 ) &&
1909 test_create_commit "$test_count/sub proj" sub2 &&
1910 (
1911 cd "$test_count" &&
1912 git fetch ./"sub proj" HEAD &&
1913 git subtree merge --prefix="sub dir" -S FETCH_HEAD &&
1914 git verify-commit HEAD
1915 )
1916 '
1917
1918 test_expect_success GPG 'split with GPG signing and --rejoin' '
1919 subtree_test_create_repo "$test_count" &&
1920 subtree_test_create_repo "$test_count/sub proj" &&
1921 test_create_commit "$test_count" main1 &&
1922 test_create_commit "$test_count/sub proj" sub1 &&
1923 (
1924 cd "$test_count" &&
1925 git fetch ./"sub proj" HEAD &&
1926 git subtree add --prefix="sub dir" FETCH_HEAD
1927 ) &&
1928 test_create_commit "$test_count" "sub dir/main-sub1" &&
1929 (
1930 cd "$test_count" &&
1931 git subtree split --prefix="sub dir" --rejoin -S &&
1932 git verify-commit HEAD
1933 )
1934 '
1935
1936 test_expect_success GPG 'add with --squash and GPG signing' '
1937 subtree_test_create_repo "$test_count" &&
1938 subtree_test_create_repo "$test_count/sub proj" &&
1939 test_create_commit "$test_count" main1 &&
1940 test_create_commit "$test_count/sub proj" sub1 &&
1941 (
1942 cd "$test_count" &&
1943 git fetch ./"sub proj" HEAD &&
1944 git subtree add --prefix="sub dir" --squash -S FETCH_HEAD &&
1945 git verify-commit HEAD &&
1946 # With --squash, the commit subject should reference the squash commit (first parent of merge)
1947 squash_commit=$(git rev-parse HEAD^2) &&
1948 test "$(last_commit_subject)" = "Merge commit '\''$squash_commit'\'' as '\''sub dir'\''"
1949 )
1950 '
1951
1952 test_expect_success GPG 'pull with GPG signing' '
1953 subtree_test_create_repo "$test_count" &&
1954 subtree_test_create_repo "$test_count/sub proj" &&
1955 test_create_commit "$test_count" main1 &&
1956 test_create_commit "$test_count/sub proj" sub1 &&
1957 (
1958 cd "$test_count" &&
1959 git subtree add --prefix="sub dir" ./"sub proj" HEAD
1960 ) &&
1961 test_create_commit "$test_count/sub proj" sub2 &&
1962 (
1963 cd "$test_count" &&
1964 git subtree pull --prefix="sub dir" -S ./"sub proj" HEAD &&
1965 git verify-commit HEAD
1966 )
1967 '
1968
1969 test_done