3 # Copyright (c) 2021 Jiang Xin
6 test_description
='Test git-bundle'
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
12 .
"$TEST_DIRECTORY"/lib-bundle.sh
14 # Create a commit or tag and set the variable with the object ID.
15 test_commit_setvar
() {
43 echo >&2 "error: unknown option $1"
54 echo >&2 "error: test_commit_setvar must have at least 2 arguments"
59 indir
=${indir:+"$indir"/}
66 git
${indir:+ -C "$indir"} merge
--no-edit --no-ff \
68 oid
=$
(git
${indir:+ -C "$indir"} rev-parse HEAD
)
71 git
${indir:+ -C "$indir"} tag
-m "$1" "$1" "${2:-HEAD}" &&
72 oid
=$
(git
${indir:+ -C "$indir"} rev-parse
"$1")
75 echo "${3-$1}" >"$indir$file" &&
76 git
${indir:+ -C "$indir"} add
"$file" &&
77 git
${indir:+ -C "$indir"} commit
$signoff -m "$1" &&
78 oid
=$
(git
${indir:+ -C "$indir"} rev-parse HEAD
)
85 suffix
=${oid#???????} &&
95 # Format the output of git commands to make a user-friendly and stable
96 # text. We can easily prepare the expect text without having to worry
97 # about future changes of the commit ID.
98 make_user_friendly_and_stable_output
() {
100 -e "s/$(get_abbrev_oid $A)[0-9a-f]*/<COMMIT-A>/g" \
101 -e "s/$(get_abbrev_oid $B)[0-9a-f]*/<COMMIT-B>/g" \
102 -e "s/$(get_abbrev_oid $C)[0-9a-f]*/<COMMIT-C>/g" \
103 -e "s/$(get_abbrev_oid $D)[0-9a-f]*/<COMMIT-D>/g" \
104 -e "s/$(get_abbrev_oid $E)[0-9a-f]*/<COMMIT-E>/g" \
105 -e "s/$(get_abbrev_oid $F)[0-9a-f]*/<COMMIT-F>/g" \
106 -e "s/$(get_abbrev_oid $G)[0-9a-f]*/<COMMIT-G>/g" \
107 -e "s/$(get_abbrev_oid $H)[0-9a-f]*/<COMMIT-H>/g" \
108 -e "s/$(get_abbrev_oid $I)[0-9a-f]*/<COMMIT-I>/g" \
109 -e "s/$(get_abbrev_oid $J)[0-9a-f]*/<COMMIT-J>/g" \
110 -e "s/$(get_abbrev_oid $K)[0-9a-f]*/<COMMIT-K>/g" \
111 -e "s/$(get_abbrev_oid $L)[0-9a-f]*/<COMMIT-L>/g" \
112 -e "s/$(get_abbrev_oid $M)[0-9a-f]*/<COMMIT-M>/g" \
113 -e "s/$(get_abbrev_oid $N)[0-9a-f]*/<COMMIT-N>/g" \
114 -e "s/$(get_abbrev_oid $O)[0-9a-f]*/<COMMIT-O>/g" \
115 -e "s/$(get_abbrev_oid $P)[0-9a-f]*/<COMMIT-P>/g" \
116 -e "s/$(get_abbrev_oid $TAG1)[0-9a-f]*/<TAG-1>/g" \
117 -e "s/$(get_abbrev_oid $TAG2)[0-9a-f]*/<TAG-2>/g" \
118 -e "s/$(get_abbrev_oid $TAG3)[0-9a-f]*/<TAG-3>/g"
121 format_and_save_expect
() {
122 sed -e 's/Z$//' >expect
125 HASH_MESSAGE
="The bundle uses this hash algorithm: $GIT_DEFAULT_HASH"
127 # (C) (D, pull/1/head, topic/1)
130 # / \ o (H, topic/2) (M, tag:v2)
131 # / (F) \ / (N, tag:v3)
132 # / o --------- o (G, pull/2/head) o --- o --- o (release)
134 # o --- o --- o -------- o -- o ------------------ o ------- o --- o (main)
135 # (A) (B) (E, tag:v1) (I) (J) (K) (O) (P)
137 test_expect_success
'setup' '
138 # Try to make a stable fixed width for abbreviated commit ID,
139 # this fixed-width oid will be replaced with "<OID>".
140 git config core.abbrev 7 &&
142 # branch main: commit A & B
143 test_commit_setvar A "Commit A" main.txt &&
144 test_commit_setvar B "Commit B" main.txt &&
146 # branch topic/1: commit C & D, refs/pull/1/head
147 git checkout -b topic/1 &&
148 test_commit_setvar C "Commit C" topic-1.txt &&
149 test_commit_setvar D "Commit D" topic-1.txt &&
150 git update-ref refs/pull/1/head HEAD &&
152 # branch topic/1: commit E, tag v1
154 test_commit_setvar E "Commit E" main.txt &&
155 test_commit_setvar --tag TAG1 v1 &&
157 # branch topic/2: commit F & G, refs/pull/2/head
158 git checkout -b topic/2 &&
159 test_commit_setvar F "Commit F" topic-2.txt &&
160 test_commit_setvar G "Commit G" topic-2.txt &&
161 git update-ref refs/pull/2/head HEAD &&
162 test_commit_setvar H "Commit H" topic-2.txt &&
164 # branch main: merge commit I & J
166 test_commit_setvar --merge I topic/1 "Merge commit I" &&
167 test_commit_setvar --merge J refs/pull/2/head "Merge commit J" &&
169 # branch main: commit K
171 test_commit_setvar K "Commit K" main.txt &&
174 git checkout -b release &&
175 test_commit_setvar L "Commit L" release.txt &&
176 test_commit_setvar M "Commit M" release.txt &&
177 test_commit_setvar --tag TAG2 v2 &&
178 test_commit_setvar N "Commit N" release.txt &&
179 test_commit_setvar --tag TAG3 v3 &&
181 # branch main: merge commit O, commit P
183 test_commit_setvar --merge O tags/v2 "Merge commit O" &&
184 test_commit_setvar P "Commit P" main.txt
187 test_expect_success
'create bundle from special rev: main^!' '
188 git bundle create special-rev.bdl "main^!" &&
190 git bundle list-heads special-rev.bdl |
191 make_user_friendly_and_stable_output >actual &&
192 cat >expect <<-\EOF &&
193 <COMMIT-P> refs/heads/main
195 test_cmp expect actual &&
197 git bundle verify special-rev.bdl |
198 make_user_friendly_and_stable_output >actual &&
199 format_and_save_expect <<-EOF &&
200 The bundle contains this ref:
201 <COMMIT-P> refs/heads/main
202 The bundle requires this ref:
206 test_cmp expect actual &&
208 test_bundle_object_count special-rev.bdl 3
211 test_expect_success
'create bundle with --max-count option' '
212 git bundle create max-count.bdl --max-count 1 \
219 git bundle verify max-count.bdl |
220 make_user_friendly_and_stable_output >actual &&
221 format_and_save_expect <<-EOF &&
222 The bundle contains these 2 refs:
223 <COMMIT-P> refs/heads/main
225 The bundle requires this ref:
229 test_cmp expect actual &&
231 test_bundle_object_count max-count.bdl 4
234 test_expect_success
'create bundle with --since option' '
235 git log -1 --pretty="%ad" $M >actual &&
236 cat >expect <<-\EOF &&
237 Thu Apr 7 15:26:13 2005 -0700
239 test_cmp expect actual &&
241 git bundle create since.bdl \
242 --since "Thu Apr 7 15:27:00 2005 -0700" \
245 git bundle verify since.bdl |
246 make_user_friendly_and_stable_output >actual &&
247 format_and_save_expect <<-EOF &&
248 The bundle contains these 5 refs:
249 <COMMIT-P> refs/heads/main
250 <COMMIT-N> refs/heads/release
254 The bundle requires these 2 refs:
259 test_cmp expect actual &&
261 test_bundle_object_count --thin since.bdl 13
264 test_expect_success
'create bundle 1 - no prerequisites' '
265 # create bundle from args
266 git bundle create 1.bdl topic/1 topic/2 &&
268 # create bundle from stdin
269 cat >input <<-\EOF &&
273 git bundle create stdin-1.bdl --stdin <input &&
275 format_and_save_expect <<-EOF &&
276 The bundle contains these 2 refs:
277 <COMMIT-D> refs/heads/topic/1
278 <COMMIT-H> refs/heads/topic/2
279 The bundle records a complete history.
283 # verify bundle, which has no prerequisites
284 git bundle verify 1.bdl |
285 make_user_friendly_and_stable_output >actual &&
286 test_cmp expect actual &&
288 git bundle verify stdin-1.bdl |
289 make_user_friendly_and_stable_output >actual &&
290 test_cmp expect actual &&
292 test_bundle_object_count 1.bdl 24 &&
293 test_bundle_object_count stdin-1.bdl 24
296 test_expect_success
'create bundle 2 - has prerequisites' '
297 # create bundle from args
298 git bundle create 2.bdl \
305 # create bundle from stdin
306 # input has a non-exist reference: "topic/deleted"
312 git bundle create stdin-2.bdl \
317 format_and_save_expect <<-EOF &&
318 The bundle contains this ref:
319 <COMMIT-N> refs/heads/release
320 The bundle requires these 3 refs:
327 git bundle verify 2.bdl |
328 make_user_friendly_and_stable_output >actual &&
329 test_cmp expect actual &&
331 git bundle verify stdin-2.bdl |
332 make_user_friendly_and_stable_output >actual &&
333 test_cmp expect actual &&
335 test_bundle_object_count 2.bdl 16 &&
336 test_bundle_object_count stdin-2.bdl 16
339 test_expect_success
'fail to verify bundle without prerequisites' '
340 git init --bare test1.git &&
342 format_and_save_expect <<-\EOF &&
343 error: Repository lacks these prerequisite commits:
349 test_must_fail git -C test1.git bundle verify ../2.bdl 2>&1 |
350 make_user_friendly_and_stable_output >actual &&
351 test_cmp expect actual &&
353 test_must_fail git -C test1.git bundle verify ../stdin-2.bdl 2>&1 |
354 make_user_friendly_and_stable_output >actual &&
355 test_cmp expect actual
358 test_expect_success
'create bundle 3 - two refs, same object' '
359 # create bundle from args
360 git bundle create --version=3 3.bdl \
367 # create bundle from stdin
368 cat >input <<-\EOF &&
373 git bundle create --version=3 stdin-3.bdl \
377 format_and_save_expect <<-EOF &&
378 The bundle contains these 2 refs:
379 <COMMIT-P> refs/heads/main
381 The bundle requires these 2 refs:
387 git bundle verify 3.bdl |
388 make_user_friendly_and_stable_output >actual &&
389 test_cmp expect actual &&
391 git bundle verify stdin-3.bdl |
392 make_user_friendly_and_stable_output >actual &&
393 test_cmp expect actual &&
395 test_bundle_object_count 3.bdl 4 &&
396 test_bundle_object_count stdin-3.bdl 4
399 test_expect_success
'create bundle 4 - with tags' '
400 # create bundle from args
401 git bundle create 4.bdl \
408 # create bundle from stdin
409 cat >input <<-\EOF &&
415 git bundle create stdin-4.bdl \
420 cat >expect <<-EOF &&
421 The bundle contains these 3 refs:
425 The bundle records a complete history.
429 git bundle verify 4.bdl |
430 make_user_friendly_and_stable_output >actual &&
431 test_cmp expect actual &&
433 git bundle verify stdin-4.bdl |
434 make_user_friendly_and_stable_output >actual &&
435 test_cmp expect actual &&
437 test_bundle_object_count 4.bdl 3 &&
438 test_bundle_object_count stdin-4.bdl 3
441 test_expect_success
'clone from bundle' '
442 git clone --mirror 1.bdl mirror.git &&
443 git -C mirror.git show-ref |
444 make_user_friendly_and_stable_output >actual &&
445 cat >expect <<-\EOF &&
446 <COMMIT-D> refs/heads/topic/1
447 <COMMIT-H> refs/heads/topic/2
449 test_cmp expect actual &&
451 git -C mirror.git fetch ../2.bdl "+refs/*:refs/*" &&
452 git -C mirror.git show-ref |
453 make_user_friendly_and_stable_output >actual &&
454 cat >expect <<-\EOF &&
455 <COMMIT-N> refs/heads/release
456 <COMMIT-D> refs/heads/topic/1
457 <COMMIT-H> refs/heads/topic/2
459 test_cmp expect actual &&
461 git -C mirror.git fetch ../3.bdl "+refs/*:refs/*" &&
462 git -C mirror.git show-ref |
463 make_user_friendly_and_stable_output >actual &&
464 cat >expect <<-\EOF &&
465 <COMMIT-P> refs/heads/main
466 <COMMIT-N> refs/heads/release
467 <COMMIT-D> refs/heads/topic/1
468 <COMMIT-H> refs/heads/topic/2
470 test_cmp expect actual &&
472 git -C mirror.git fetch ../4.bdl "+refs/*:refs/*" &&
473 git -C mirror.git show-ref |
474 make_user_friendly_and_stable_output >actual &&
475 cat >expect <<-\EOF &&
476 <COMMIT-P> refs/heads/main
477 <COMMIT-N> refs/heads/release
478 <COMMIT-D> refs/heads/topic/1
479 <COMMIT-H> refs/heads/topic/2
484 test_cmp expect actual
487 test_expect_success
'unfiltered bundle with --objects' '
488 git bundle create all-objects.bdl \
490 git bundle create all.bdl \
493 # Compare the headers of these files.
494 sed -n -e "/^$/q" -e "p" all.bdl >expect &&
495 sed -n -e "/^$/q" -e "p" all-objects.bdl >actual &&
496 test_cmp expect actual
499 for filter
in "blob:none" "tree:0" "tree:1" "blob:limit=100"
501 test_expect_success
"filtered bundle: $filter" '
502 test_when_finished rm -rf .git/objects/pack cloned unbundled &&
503 git bundle create partial.bdl \
507 git bundle verify partial.bdl >unfiltered &&
508 make_user_friendly_and_stable_output <unfiltered >actual &&
510 cat >expect <<-EOF &&
511 The bundle contains these 10 refs:
512 <COMMIT-P> refs/heads/main
513 <COMMIT-N> refs/heads/release
514 <COMMIT-D> refs/heads/topic/1
515 <COMMIT-H> refs/heads/topic/2
516 <COMMIT-D> refs/pull/1/head
517 <COMMIT-G> refs/pull/2/head
522 The bundle records a complete history.
524 The bundle uses this filter: $filter
526 test_cmp expect actual &&
528 test_config uploadpack.allowfilter 1 &&
529 test_config uploadpack.allowanysha1inwant 1 &&
530 git clone --no-local --filter=$filter --bare "file://$(pwd)" cloned &&
532 git init unbundled &&
533 git -C unbundled bundle unbundle ../partial.bdl >ref-list.txt &&
534 ls unbundled/.git/objects/pack/pack-*.promisor >promisor &&
535 test_line_count = 1 promisor &&
537 # Count the same number of reachable objects.
538 reflist=$(git for-each-ref --format="%(objectname)") &&
539 git rev-list --objects --filter=$filter --missing=allow-any \
541 for repo in cloned unbundled
543 git -C $repo rev-list --objects --missing=allow-any \
545 test_cmp expect actual || return 1
550 # NEEDSWORK: 'git clone --bare' should be able to clone from a filtered
551 # bundle, but that requires a change to promisor/filter config options.
552 # For now, we fail gracefully with a helpful error. This behavior can be
553 # changed in the future to succeed as much as possible.
554 test_expect_success
'cloning from filtered bundle has useful error' '
555 git bundle create partial.bdl \
557 --filter=blob:none &&
558 test_must_fail git clone --bare partial.bdl partial 2>err &&
559 grep "cannot clone from filtered bundle" err
562 test_expect_success
'verify catches unreachable, broken prerequisites' '
563 test_when_finished rm -rf clone-from clone-to &&
564 git init clone-from &&
567 git checkout -b base &&
569 git checkout -b tip &&
570 git commit --allow-empty -m "will drop by shallow" &&
571 git commit --allow-empty -m "will keep by shallow" &&
572 git commit --allow-empty -m "for bundle, not clone" &&
573 git bundle create tip.bundle tip~1..tip &&
574 git reset --hard HEAD~1 &&
577 BAD_OID=$(git -C clone-from rev-parse tip~1) &&
578 TIP_OID=$(git -C clone-from rev-parse tip) &&
579 git clone --depth=1 --no-single-branch \
580 "file://$(pwd)/clone-from" clone-to &&
584 # Set up broken history by removing shallow markers
585 git update-ref -d refs/remotes/origin/tip &&
589 test_must_fail git bundle verify \
590 ../clone-from/tip.bundle 2>err &&
591 grep "Could not read $BAD_OID" err &&
592 grep "Failed to traverse parents of commit $TIP_OID" err &&
594 # Unbundling should fail
595 test_must_fail git bundle unbundle \
596 ../clone-from/tip.bundle 2>err &&
597 grep "Could not read $BAD_OID" err &&
598 grep "Failed to traverse parents of commit $TIP_OID" err