]> git.ipfire.org Git - thirdparty/git.git/blob - contrib/subtree/t/t7900-subtree.sh
format-patch: fix leak of empty header string
[thirdparty/git.git] / contrib / subtree / t / t7900-subtree.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2012 Avery Pennaraum
4 # Copyright (c) 2015 Alexey Shumkin
5 #
6 test_description='Basic porcelain support for subtrees
7
8 This test verifies the basic operation of the add, merge, split, pull,
9 and push subcommands of git subtree.
10 '
11
12 TEST_DIRECTORY=$(pwd)/../../../t
13 . "$TEST_DIRECTORY"/test-lib.sh
14
15 # Use our own wrapper around test-lib.sh's test_create_repo, in order
16 # to set log.date=relative. `git subtree` parses the output of `git
17 # log`, and so it must be careful to not be affected by settings that
18 # change the `git log` output. We test this by setting
19 # log.date=relative for every repo in the tests.
20 subtree_test_create_repo () {
21 test_create_repo "$1" &&
22 git -C "$1" config log.date relative
23 }
24
25 test_create_commit () (
26 repo=$1 &&
27 commit=$2 &&
28 cd "$repo" &&
29 mkdir -p "$(dirname "$commit")" \
30 || error "Could not create directory for commit"
31 echo "$commit" >"$commit" &&
32 git add "$commit" || error "Could not add commit"
33 git commit -m "$commit" || error "Could not commit"
34 )
35
36 test_wrong_flag() {
37 test_must_fail "$@" >out 2>err &&
38 test_must_be_empty out &&
39 grep "flag does not make sense with" err
40 }
41
42 last_commit_subject () {
43 git log --pretty=format:%s -1
44 }
45
46 # 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 immitate this behaviour below using a replace ref.
51 # This function creates 3 repositories:
52 # - $1
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=$(cat msg | sed -e "s/$commit/$tag/" | git -C "$1-clone" commit-tree HEAD^2^{tree}) &&
67 git -C "$1-clone" replace HEAD^2 $new_commit
68 }
69
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
75 '
76
77 #
78 # Tests for 'git subtree add'
79 #
80
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 &&
86 (
87 cd "$test_count" &&
88 git fetch ./"sub proj" HEAD &&
89 test_must_fail git subtree merge --prefix="sub dir" FETCH_HEAD
90 )
91 '
92
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 &&
98 (
99 cd "$test_count" &&
100 git fetch ./"sub proj" HEAD &&
101 test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" HEAD
102 )
103 '
104
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 &&
110 (
111 cd "$test_count" &&
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
118 )
119 '
120
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 &&
126 (
127 cd "$test_count" &&
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)'\''"
131 )
132 '
133
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 &&
139 (
140 cd "$test_count" &&
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"
144 )
145 '
146
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 &&
152 (
153 cd "$test_count" &&
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"
157 )
158 '
159
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 &&
165 (
166 cd "$test_count" &&
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"
170 )
171 '
172
173 #
174 # Tests for 'git subtree merge'
175 #
176
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 &&
182 (
183 cd "$test_count" &&
184 git fetch ./"sub proj" HEAD &&
185 git subtree add --prefix="sub dir" FETCH_HEAD
186 ) &&
187 test_create_commit "$test_count/sub proj" sub2 &&
188 (
189 cd "$test_count" &&
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
196 )
197 '
198
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 &&
204 (
205 cd "$test_count" &&
206 git fetch ./"sub proj" HEAD &&
207 git subtree add --prefix="sub dir" FETCH_HEAD
208 ) &&
209 test_create_commit "$test_count/sub proj" sub2 &&
210 (
211 cd "$test_count" &&
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)'\''"
215 )
216 '
217
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 &&
223 (
224 cd "$test_count" &&
225 git fetch ./"sub proj" HEAD &&
226 git subtree add --prefix="sub dir" FETCH_HEAD
227 ) &&
228 test_create_commit "$test_count/sub proj" sub2 &&
229 (
230 cd "$test_count" &&
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"
234 )
235 '
236
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 &&
242 (
243 cd "$test_count" &&
244 git fetch ./"sub proj" HEAD &&
245 git subtree add --prefix="sub dir" FETCH_HEAD
246 ) &&
247 test_create_commit "$test_count/sub proj" sub2 &&
248 (
249 cd "$test_count" &&
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"
253 )
254 '
255
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 &&
261 (
262 cd "$test_count" &&
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."
269 )
270 '
271
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 &&
277 (
278 cd "$test_count" &&
279 git fetch ./subproj HEAD &&
280 git subtree add --prefix=subdir/ FETCH_HEAD
281 ) &&
282 test_create_commit "$test_count/subproj" sub2 &&
283 (
284 cd "$test_count" &&
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)'\''"
288 )
289 '
290
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"
296 '
297
298 #
299 # Tests for 'git subtree split'
300 #
301
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 &&
307 (
308 cd "$test_count" &&
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
318 )
319 '
320
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 &&
326 (
327 cd "$test_count" &&
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
337 )
338 '
339
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 &&
345 (
346 cd "$test_count" &&
347 git fetch ./"sub proj" HEAD &&
348 git subtree add --prefix="sub dir" FETCH_HEAD
349 ) &&
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 &&
354 (
355 cd "$test_count" &&
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
361 )
362 '
363
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 &&
369 (
370 cd "$test_count" &&
371 git fetch ./"sub proj" HEAD &&
372 git subtree add --prefix="sub dir" FETCH_HEAD
373 ) &&
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 &&
378 (
379 cd "$test_count" &&
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'\''"
385 )
386 '
387
388 # Tests that commits from other subtrees are not processed as
389 # part of a split.
390 #
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\]")" = ""
426 '
427
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 &&
431 (
432 cd "$test_count" &&
433 mkdir "sub dir" &&
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'\''"
440 )
441 '
442
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 &&
448 (
449 cd "$test_count" &&
450 git fetch ./"sub proj" HEAD &&
451 git subtree add --prefix="sub dir" FETCH_HEAD
452 ) &&
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 &&
457 (
458 cd "$test_count" &&
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"
463 )
464 '
465
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 &&
471 (
472 cd "$test_count" &&
473 git fetch ./"sub proj" HEAD &&
474 git subtree add --prefix="sub dir" --squash FETCH_HEAD
475 ) &&
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 &&
480 (
481 cd "$test_count" &&
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) &&
485
486 SPLIT=$(git subtree split --prefix="sub dir" --annotate="*" --rejoin --squash) &&
487
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'\''"
498 )
499 '
500
501 test_expect_success 'split then pull "sub dir"/ with --rejoin and --squash' '
502 # 1. "add"
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 &&
508
509 # 2. commit from parent
510 test_create_commit "$test_count" "sub dir"/main-sub1 &&
511
512 # 3. "split --rejoin --squash"
513 git -C "$test_count" subtree --prefix="sub dir" split --rejoin --squash &&
514
515 # 4. "pull --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 &&
518
519 test_must_fail git merge-base HEAD FETCH_HEAD
520 '
521
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 &&
527 (
528 cd "$test_count" &&
529 git fetch ./"sub proj" HEAD &&
530 git subtree add --prefix="sub dir" FETCH_HEAD
531 ) &&
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 &&
536 (
537 cd "$test_count" &&
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"
543 )
544 '
545
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 &&
551 (
552 cd "$test_count" &&
553 git fetch ./"sub proj" HEAD &&
554 git subtree add --prefix="sub dir" FETCH_HEAD
555 ) &&
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 &&
560 (
561 cd "$test_count" &&
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) &&
569 (
570 cd ./"sub proj" &&
571 subdir_hash=$(git rev-parse HEAD) &&
572 test "$new_hash" = "$subdir_hash"
573 )
574 )
575 '
576
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 &&
582 (
583 cd "$test_count" &&
584 git fetch ./"sub proj" HEAD &&
585 git branch subproj-br FETCH_HEAD &&
586 git subtree add --prefix="sub dir" FETCH_HEAD
587 ) &&
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 &&
592 (
593 cd "$test_count" &&
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"
599 )
600 '
601
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 &&
607 (
608 cd "$test_count" &&
609 git branch init HEAD &&
610 git fetch ./"sub proj" HEAD &&
611 git subtree add --prefix="sub dir" FETCH_HEAD
612 ) &&
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 &&
617 (
618 cd "$test_count" &&
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
622 )
623 '
624
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"
629 '
630
631 #
632 # Tests for 'git subtree pull'
633 #
634
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 &&
640 (
641 cd "$test_count" &&
642 git fetch ./"sub proj" HEAD &&
643 git subtree add --prefix="sub dir" FETCH_HEAD
644 ) &&
645 test_create_commit "$test_count/sub proj" sub2 &&
646 (
647 cd "$test_count" &&
648 test_must_fail git subtree pull ./"sub proj" HEAD >out 2>err &&
649
650 echo "fatal: you must provide the --prefix option." >expected &&
651 test_must_be_empty out &&
652 test_cmp expected err
653 )
654 '
655
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 &&
661 (
662 test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" HEAD >out 2>err &&
663
664 echo "fatal: '\''sub dir'\'' does not exist; use '\''git subtree add'\''" >expected &&
665 test_must_be_empty out &&
666 test_cmp expected err
667 )
668 '
669
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 &&
675 (
676 cd "$test_count" &&
677 git fetch ./"sub proj" HEAD &&
678 git subtree add --prefix="sub dir" FETCH_HEAD
679 ) &&
680 test_create_commit "$test_count/sub proj" sub2 &&
681 (
682 cd "$test_count" &&
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") &&
686 test "$act" = "$exp"
687 )
688 '
689
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 &&
695 (
696 cd "$test_count" &&
697 git fetch ./"sub proj" HEAD &&
698 git subtree add --prefix="sub dir" FETCH_HEAD
699 ) &&
700 test_create_commit "$test_count/sub proj" sub2 &&
701 (
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
707 )
708 '
709
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
713 '
714
715 #
716 # Tests for 'git subtree push'
717 #
718
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 &&
724 (
725 cd "$test_count" &&
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
735 )
736 '
737
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 &&
743 (
744 cd "$test_count" &&
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
754 )
755 '
756
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 &&
762 (
763 cd "$test_count" &&
764 git fetch ./"sub proj" HEAD &&
765 git subtree add --prefix="sub dir" FETCH_HEAD
766 ) &&
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 &&
771 (
772 cd "$test_count" &&
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
777 )
778 '
779
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 &&
785 (
786 cd "$test_count" &&
787 git fetch ./"sub proj" HEAD &&
788 git subtree add --prefix="sub dir" FETCH_HEAD
789 ) &&
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 &&
794 (
795 cd "$test_count" &&
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)"
803 )
804 '
805
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 &&
811 (
812 cd "$test_count" &&
813 git fetch ./"sub proj" HEAD &&
814 git subtree add --prefix="sub dir" FETCH_HEAD
815 ) &&
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 &&
820 (
821 cd "$test_count" &&
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)"
828 )
829 '
830
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 &&
834 (
835 cd "$test_count" &&
836 mkdir "sub dir" &&
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)"
845 )
846 '
847
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 &&
853 (
854 cd "$test_count" &&
855 git fetch ./"sub proj" HEAD &&
856 git subtree add --prefix="sub dir" FETCH_HEAD
857 ) &&
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 &&
862 (
863 cd "$test_count" &&
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)"
870 )
871 '
872
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 &&
878 (
879 cd "$test_count" &&
880 git fetch ./"sub proj" HEAD &&
881 git subtree add --prefix="sub dir" --squash FETCH_HEAD
882 ) &&
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 &&
887 (
888 cd "$test_count" &&
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) &&
892
893 SPLIT=$(git subtree split --prefix="sub dir" --annotate="*") &&
894 git subtree push --prefix="sub dir" --annotate="*" --rejoin --squash ./"sub proj" from-mainline &&
895
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)"
907 )
908 '
909
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 &&
915 (
916 cd "$test_count" &&
917 git fetch ./"sub proj" HEAD &&
918 git subtree add --prefix="sub dir" FETCH_HEAD
919 ) &&
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 &&
924 (
925 cd "$test_count" &&
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)"
932 )
933 '
934
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 &&
940 (
941 cd "$test_count" &&
942 git fetch ./"sub proj" HEAD &&
943 git subtree add --prefix="sub dir" FETCH_HEAD
944 ) &&
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 &&
949 (
950 cd "$test_count" &&
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) &&
958 (
959 cd ./"sub proj" &&
960 subdir_hash=$(git rev-parse HEAD) &&
961 test "$new_hash" = "$subdir_hash"
962 ) &&
963 test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)"
964 )
965 '
966
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 &&
972 (
973 cd "$test_count" &&
974 git fetch ./"sub proj" HEAD &&
975 git branch subproj-br FETCH_HEAD &&
976 git subtree add --prefix="sub dir" FETCH_HEAD
977 ) &&
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 &&
982 (
983 cd "$test_count" &&
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)"
990 )
991 '
992
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 &&
998 (
999 cd "$test_count" &&
1000 git branch init HEAD &&
1001 git fetch ./"sub proj" HEAD &&
1002 git subtree add --prefix="sub dir" FETCH_HEAD
1003 ) &&
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 &&
1008 (
1009 cd "$test_count" &&
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
1013 )
1014 '
1015
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 &&
1021 (
1022 cd "$test_count" &&
1023 git fetch ./"sub proj" HEAD &&
1024 git subtree add --prefix="sub dir" FETCH_HEAD
1025 ) &&
1026 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1027 test_create_commit "$test_count" "sub dir"/main-sub2 &&
1028 (
1029 cd "$test_count" &&
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"
1035 )
1036 '
1037
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
1042 '
1043
1044 #
1045 # Validity checking
1046 #
1047
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 &&
1053 (
1054 cd "$test_count" &&
1055 git fetch ./"sub proj" HEAD &&
1056 git subtree add --prefix="sub dir" FETCH_HEAD
1057 ) &&
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 &&
1062 (
1063 cd "$test_count" &&
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
1067 ) &&
1068 test_create_commit "$test_count/sub proj" sub3 &&
1069 test_create_commit "$test_count" "sub dir"/main-sub3 &&
1070 (
1071 cd "$test_count/sub proj" &&
1072 git fetch .. subproj-br &&
1073 git merge FETCH_HEAD
1074 ) &&
1075 test_create_commit "$test_count/sub proj" sub4 &&
1076 (
1077 cd "$test_count" &&
1078 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1079 ) &&
1080 test_create_commit "$test_count" "sub dir"/main-sub4 &&
1081 (
1082 cd "$test_count" &&
1083 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1084 ) &&
1085 (
1086 cd "$test_count/sub proj" &&
1087 git fetch .. subproj-br &&
1088 git merge FETCH_HEAD &&
1089
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
1094 )
1095 '
1096
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 &&
1102 (
1103 cd "$test_count" &&
1104 git fetch ./"sub proj" HEAD &&
1105 git subtree add --prefix="sub dir" FETCH_HEAD
1106 ) &&
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 &&
1111 (
1112 cd "$test_count" &&
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
1116 ) &&
1117 test_create_commit "$test_count/sub proj" sub3 &&
1118 test_create_commit "$test_count" "sub dir"/main-sub3 &&
1119 (
1120 cd "$test_count/sub proj" &&
1121 git fetch .. subproj-br &&
1122 git merge FETCH_HEAD
1123 ) &&
1124 test_create_commit "$test_count/sub proj" sub4 &&
1125 (
1126 cd "$test_count" &&
1127 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1128 ) &&
1129 test_create_commit "$test_count" "sub dir"/main-sub4 &&
1130 (
1131 cd "$test_count" &&
1132 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1133 ) &&
1134 (
1135 cd "$test_count/sub proj" &&
1136 git fetch .. subproj-br &&
1137 git merge FETCH_HEAD &&
1138
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
1144 )
1145 '
1146
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 &&
1152 (
1153 cd "$test_count" &&
1154 git fetch ./"sub proj" HEAD &&
1155 git subtree add --prefix="sub dir" FETCH_HEAD
1156 ) &&
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 &&
1161 (
1162 cd "$test_count" &&
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
1166 ) &&
1167 test_create_commit "$test_count/sub proj" sub3 &&
1168 test_create_commit "$test_count" "sub dir"/main-sub3 &&
1169 (
1170 cd "$test_count/sub proj" &&
1171 git fetch .. subproj-br &&
1172 git merge FETCH_HEAD
1173 ) &&
1174 test_create_commit "$test_count/sub proj" sub4 &&
1175 (
1176 cd "$test_count" &&
1177 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1178 ) &&
1179 test_create_commit "$test_count" "sub dir"/main-sub4 &&
1180 (
1181 cd "$test_count" &&
1182 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1183 ) &&
1184 (
1185 cd "$test_count/sub proj" &&
1186 git fetch .. subproj-br &&
1187 git merge FETCH_HEAD
1188 ) &&
1189 (
1190 cd "$test_count" &&
1191 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
1192
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 &&
1198
1199 cat chkm chkms_sub chks_sub >expect &&
1200 git ls-files >actual &&
1201 test_cmp expect actual
1202 )
1203 '
1204
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 &&
1210 (
1211 cd "$test_count" &&
1212 git config log.date relative &&
1213 git fetch ./"sub proj" HEAD &&
1214 git subtree add --prefix="sub dir" FETCH_HEAD
1215 ) &&
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 &&
1220 (
1221 cd "$test_count" &&
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
1225 ) &&
1226 test_create_commit "$test_count/sub proj" sub3 &&
1227 test_create_commit "$test_count" "sub dir"/main-sub3 &&
1228 (
1229 cd "$test_count/sub proj" &&
1230 git fetch .. subproj-br &&
1231 git merge FETCH_HEAD
1232 ) &&
1233 test_create_commit "$test_count/sub proj" sub4 &&
1234 (
1235 cd "$test_count" &&
1236 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1237 ) &&
1238 test_create_commit "$test_count" "sub dir"/main-sub4 &&
1239 (
1240 cd "$test_count" &&
1241 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1242 ) &&
1243 (
1244 cd "$test_count/sub proj" &&
1245 git fetch .. subproj-br &&
1246 git merge FETCH_HEAD
1247 ) &&
1248 (
1249 cd "$test_count" &&
1250 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
1251
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 &&
1256
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 &&
1263
1264 cat chkms chkm chks chkms_sub >expect-unsorted &&
1265 sort expect-unsorted >expect &&
1266 test_cmp expect actual
1267 )
1268 '
1269
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 &&
1275 (
1276 cd "$test_count" &&
1277 git fetch ./"sub proj" HEAD &&
1278 git subtree add --prefix="sub dir" FETCH_HEAD
1279 ) &&
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 &&
1284 (
1285 cd "$test_count" &&
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
1289 ) &&
1290 test_create_commit "$test_count/sub proj" sub3 &&
1291 test_create_commit "$test_count" "sub dir"/main-sub3 &&
1292 (
1293 cd "$test_count/sub proj" &&
1294 git fetch .. subproj-br &&
1295 git merge FETCH_HEAD
1296 ) &&
1297 test_create_commit "$test_count/sub proj" sub4 &&
1298 (
1299 cd "$test_count" &&
1300 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1301 ) &&
1302 test_create_commit "$test_count" "sub dir"/main-sub4 &&
1303 (
1304 cd "$test_count" &&
1305 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1306 ) &&
1307 (
1308 cd "$test_count/sub proj" &&
1309 git fetch .. subproj-br &&
1310 git merge FETCH_HEAD
1311 ) &&
1312 (
1313 cd "$test_count" &&
1314 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
1315 test "$(git log --pretty=format:"%s" HEAD^2 | grep -i split)" = ""
1316 )
1317 '
1318
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 &&
1324 (
1325 cd "$test_count" &&
1326 git fetch ./"sub proj" HEAD &&
1327 git subtree add --prefix="sub dir" FETCH_HEAD
1328 ) &&
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 &&
1333 (
1334 cd "$test_count" &&
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
1338 ) &&
1339 test_create_commit "$test_count/sub proj" sub3 &&
1340 test_create_commit "$test_count" "sub dir"/main-sub3 &&
1341 (
1342 cd "$test_count/sub proj" &&
1343 git fetch .. subproj-br &&
1344 git merge FETCH_HEAD
1345 ) &&
1346 test_create_commit "$test_count/sub proj" sub4 &&
1347 (
1348 cd "$test_count" &&
1349 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1350 ) &&
1351 test_create_commit "$test_count" "sub dir"/main-sub4 &&
1352 (
1353 cd "$test_count" &&
1354 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
1355 ) &&
1356 (
1357 cd "$test_count/sub proj" &&
1358 git fetch .. subproj-br &&
1359 git merge FETCH_HEAD
1360 ) &&
1361 (
1362 cd "$test_count" &&
1363 git subtree pull --prefix="sub dir" ./"sub proj" HEAD &&
1364
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.*:")" = ""
1367 )
1368 '
1369
1370 #
1371 # A new set of tests
1372 #
1373
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 &&
1379 (
1380 cd "$test_count" &&
1381 git fetch ./"sub proj" HEAD &&
1382 git subtree add --prefix="sub dir" FETCH_HEAD
1383 ) &&
1384 test_create_commit "$test_count/sub proj" sub2 &&
1385 (
1386 cd "$test_count" &&
1387 git fetch ./"sub proj" HEAD &&
1388 git branch subproj-ref FETCH_HEAD &&
1389 git subtree merge --prefix="sub dir" FETCH_HEAD
1390 ) &&
1391 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1392 (
1393 cd "$test_count" &&
1394 git subtree split --prefix="sub dir" --branch subproj-br &&
1395
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
1399 # itself)
1400 test "$(git log --pretty=format:%P -1 subproj-br)" = "$(git rev-parse subproj-ref)"
1401 )
1402 '
1403
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 &&
1409 (
1410 cd "$test_count" &&
1411 git fetch ./"sub proj" HEAD &&
1412 git subtree add --prefix="sub dir" FETCH_HEAD
1413 ) &&
1414 test_create_commit "$test_count/sub proj" sub2 &&
1415 (
1416 cd "$test_count" &&
1417 git fetch ./"sub proj" HEAD &&
1418 git subtree merge --prefix="sub dir" FETCH_HEAD
1419 ) &&
1420 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1421 (
1422 cd "$test_count" &&
1423 git subtree split --prefix="sub dir" --branch subproj-br
1424 ) &&
1425 mkdir "$test_count"/"sub dir2" &&
1426 test_create_commit "$test_count" "sub dir2"/main-sub2 &&
1427 (
1428 cd "$test_count" &&
1429
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)" = ""
1435 )
1436 '
1437
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 &&
1443 (
1444 cd "$test_count" &&
1445 git fetch ./"sub proj" HEAD &&
1446 git branch sub1 FETCH_HEAD &&
1447 git subtree add --prefix="sub dir" sub1
1448 ) &&
1449 test_create_commit "$test_count/sub proj" sub2 &&
1450 (
1451 cd "$test_count" &&
1452 git fetch ./"sub proj" HEAD &&
1453 git subtree merge --prefix="sub dir" FETCH_HEAD
1454 ) &&
1455 test_create_commit "$test_count" "sub dir"/main-sub1 &&
1456 (
1457 cd "$test_count" &&
1458 git subtree split --prefix="sub dir" --branch subproj-br
1459 ) &&
1460 mkdir "$test_count"/"sub dir2" &&
1461 test_create_commit "$test_count" "sub dir2"/main-sub2 &&
1462 (
1463 cd "$test_count" &&
1464 git subtree split --prefix="sub dir2" --branch subproj2-br &&
1465
1466 git log --format="%H" >commit-list &&
1467 while read commit
1468 do
1469 git log -n1 --format="" --name-only "$commit" >file-list &&
1470 test_line_count -le 1 file-list || return 1
1471 done <commit-list
1472 )
1473 '
1474
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 &&
1480 (
1481 cd "$test_count" &&
1482 git fetch ./"sub proj" HEAD &&
1483 git subtree add --prefix="sub dir" FETCH_HEAD
1484 ) &&
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 &&
1489 (
1490 cd $test_count/"sub proj" &&
1491 git branch sub-branch-1 &&
1492 cd .. &&
1493 git fetch ./"sub proj" HEAD &&
1494 git subtree merge --prefix="sub dir" FETCH_HEAD
1495 ) &&
1496 test_create_commit "$test_count" "sub dir"/main-sub3 &&
1497 (
1498 cd "$test_count" &&
1499 git subtree push ./"sub proj" --prefix "sub dir" sub-branch-1 &&
1500 cd ./"sub proj" &&
1501 git checkout sub-branch-1 &&
1502 test "$(last_commit_subject)" = "sub dir/main-sub3"
1503 )
1504 '
1505
1506 #
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
1513 #
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)
1517 #
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
1523 # set of commits.
1524 #
1525
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 &&
1530 (
1531 cd "$test_count" &&
1532 git branch branch
1533 ) &&
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) &&
1537 (
1538 cd "$test_count" &&
1539 git checkout branch
1540 ) &&
1541 test_create_commit "$test_count" commit_on_branch &&
1542 (
1543 cd "$test_count" &&
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
1548 ) &&
1549 test_create_commit "$test_count" folder_subtree/d &&
1550 (
1551 cd "$test_count" &&
1552 git checkout no_subtree_work_branch
1553 ) &&
1554 test_create_commit "$test_count" not_a_subtree_change &&
1555 (
1556 cd "$test_count" &&
1557 git checkout $defaultBranch &&
1558 git merge -m "merge should be skipped on subtree" no_subtree_work_branch &&
1559
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
1563 )
1564 '
1565
1566 test_done