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 # (C) (D, pull/1/head, topic/1)
128 # / \ o (H, topic/2) (M, tag:v2)
129 # / (F) \ / (N, tag:v3)
130 # / o --------- o (G, pull/2/head) o --- o --- o (release)
132 # o --- o --- o -------- o -- o ------------------ o ------- o --- o (main)
133 # (A) (B) (E, tag:v1) (I) (J) (K) (O) (P)
135 test_expect_success
'setup' '
136 # Try to make a stable fixed width for abbreviated commit ID,
137 # this fixed-width oid will be replaced with "<OID>".
138 git config core.abbrev 7 &&
140 # branch main: commit A & B
141 test_commit_setvar A "Commit A" main.txt &&
142 test_commit_setvar B "Commit B" main.txt &&
144 # branch topic/1: commit C & D, refs/pull/1/head
145 git checkout -b topic/1 &&
146 test_commit_setvar C "Commit C" topic-1.txt &&
147 test_commit_setvar D "Commit D" topic-1.txt &&
148 git update-ref refs/pull/1/head HEAD &&
150 # branch topic/1: commit E, tag v1
152 test_commit_setvar E "Commit E" main.txt &&
153 test_commit_setvar --tag TAG1 v1 &&
155 # branch topic/2: commit F & G, refs/pull/2/head
156 git checkout -b topic/2 &&
157 test_commit_setvar F "Commit F" topic-2.txt &&
158 test_commit_setvar G "Commit G" topic-2.txt &&
159 git update-ref refs/pull/2/head HEAD &&
160 test_commit_setvar H "Commit H" topic-2.txt &&
162 # branch main: merge commit I & J
164 test_commit_setvar --merge I topic/1 "Merge commit I" &&
165 test_commit_setvar --merge J refs/pull/2/head "Merge commit J" &&
167 # branch main: commit K
169 test_commit_setvar K "Commit K" main.txt &&
172 git checkout -b release &&
173 test_commit_setvar L "Commit L" release.txt &&
174 test_commit_setvar M "Commit M" release.txt &&
175 test_commit_setvar --tag TAG2 v2 &&
176 test_commit_setvar N "Commit N" release.txt &&
177 test_commit_setvar --tag TAG3 v3 &&
179 # branch main: merge commit O, commit P
181 test_commit_setvar --merge O tags/v2 "Merge commit O" &&
182 test_commit_setvar P "Commit P" main.txt
185 test_expect_success
'create bundle from special rev: main^!' '
186 git bundle create special-rev.bdl "main^!" &&
188 git bundle list-heads special-rev.bdl |
189 make_user_friendly_and_stable_output >actual &&
190 cat >expect <<-\EOF &&
191 <COMMIT-P> refs/heads/main
193 test_cmp expect actual &&
195 git bundle verify special-rev.bdl |
196 make_user_friendly_and_stable_output >actual &&
197 format_and_save_expect <<-\EOF &&
198 The bundle contains this ref:
199 <COMMIT-P> refs/heads/main
200 The bundle requires this ref:
203 test_cmp expect actual &&
205 test_bundle_object_count special-rev.bdl 3
208 test_expect_success
'create bundle with --max-count option' '
209 git bundle create max-count.bdl --max-count 1 \
216 git bundle verify max-count.bdl |
217 make_user_friendly_and_stable_output >actual &&
218 format_and_save_expect <<-\EOF &&
219 The bundle contains these 2 refs:
220 <COMMIT-P> refs/heads/main
222 The bundle requires this ref:
225 test_cmp expect actual &&
227 test_bundle_object_count max-count.bdl 4
230 test_expect_success
'create bundle with --since option' '
231 git log -1 --pretty="%ad" $M >actual &&
232 cat >expect <<-\EOF &&
233 Thu Apr 7 15:26:13 2005 -0700
235 test_cmp expect actual &&
237 git bundle create since.bdl \
238 --since "Thu Apr 7 15:27:00 2005 -0700" \
241 git bundle verify since.bdl |
242 make_user_friendly_and_stable_output >actual &&
243 format_and_save_expect <<-\EOF &&
244 The bundle contains these 5 refs:
245 <COMMIT-P> refs/heads/main
246 <COMMIT-N> refs/heads/release
250 The bundle requires these 2 refs:
254 test_cmp expect actual &&
256 test_bundle_object_count --thin since.bdl 13
259 test_expect_success
'create bundle 1 - no prerequisites' '
260 # create bundle from args
261 git bundle create 1.bdl topic/1 topic/2 &&
263 # create bundle from stdin
264 cat >input <<-\EOF &&
268 git bundle create stdin-1.bdl --stdin <input &&
270 cat >expect <<-\EOF &&
271 The bundle contains these 2 refs:
272 <COMMIT-D> refs/heads/topic/1
273 <COMMIT-H> refs/heads/topic/2
274 The bundle records a complete history.
277 # verify bundle, which has no prerequisites
278 git bundle verify 1.bdl |
279 make_user_friendly_and_stable_output >actual &&
280 test_cmp expect actual &&
282 git bundle verify stdin-1.bdl |
283 make_user_friendly_and_stable_output >actual &&
284 test_cmp expect actual &&
286 test_bundle_object_count 1.bdl 24 &&
287 test_bundle_object_count stdin-1.bdl 24
290 test_expect_success
'create bundle 2 - has prerequisites' '
291 # create bundle from args
292 git bundle create 2.bdl \
299 # create bundle from stdin
300 # input has a non-exist reference: "topic/deleted"
306 git bundle create stdin-2.bdl \
311 format_and_save_expect <<-\EOF &&
312 The bundle contains this ref:
313 <COMMIT-N> refs/heads/release
314 The bundle requires these 3 refs:
320 git bundle verify 2.bdl |
321 make_user_friendly_and_stable_output >actual &&
322 test_cmp expect actual &&
324 git bundle verify stdin-2.bdl |
325 make_user_friendly_and_stable_output >actual &&
326 test_cmp expect actual &&
328 test_bundle_object_count 2.bdl 16 &&
329 test_bundle_object_count stdin-2.bdl 16
332 test_expect_success
'fail to verify bundle without prerequisites' '
333 git init --bare test1.git &&
335 format_and_save_expect <<-\EOF &&
336 error: Repository lacks these prerequisite commits:
342 test_must_fail git -C test1.git bundle verify ../2.bdl 2>&1 |
343 make_user_friendly_and_stable_output >actual &&
344 test_cmp expect actual &&
346 test_must_fail git -C test1.git bundle verify ../stdin-2.bdl 2>&1 |
347 make_user_friendly_and_stable_output >actual &&
348 test_cmp expect actual
351 test_expect_success
'create bundle 3 - two refs, same object' '
352 # create bundle from args
353 git bundle create --version=3 3.bdl \
360 # create bundle from stdin
361 cat >input <<-\EOF &&
366 git bundle create --version=3 stdin-3.bdl \
370 format_and_save_expect <<-\EOF &&
371 The bundle contains these 2 refs:
372 <COMMIT-P> refs/heads/main
374 The bundle requires these 2 refs:
379 git bundle verify 3.bdl |
380 make_user_friendly_and_stable_output >actual &&
381 test_cmp expect actual &&
383 git bundle verify stdin-3.bdl |
384 make_user_friendly_and_stable_output >actual &&
385 test_cmp expect actual &&
387 test_bundle_object_count 3.bdl 4 &&
388 test_bundle_object_count stdin-3.bdl 4
391 test_expect_success
'create bundle 4 - with tags' '
392 # create bundle from args
393 git bundle create 4.bdl \
400 # create bundle from stdin
401 cat >input <<-\EOF &&
407 git bundle create stdin-4.bdl \
412 cat >expect <<-\EOF &&
413 The bundle contains these 3 refs:
417 The bundle records a complete history.
420 git bundle verify 4.bdl |
421 make_user_friendly_and_stable_output >actual &&
422 test_cmp expect actual &&
424 git bundle verify stdin-4.bdl |
425 make_user_friendly_and_stable_output >actual &&
426 test_cmp expect actual &&
428 test_bundle_object_count 4.bdl 3 &&
429 test_bundle_object_count stdin-4.bdl 3
432 test_expect_success
'clone from bundle' '
433 git clone --mirror 1.bdl mirror.git &&
434 git -C mirror.git show-ref |
435 make_user_friendly_and_stable_output >actual &&
436 cat >expect <<-\EOF &&
437 <COMMIT-D> refs/heads/topic/1
438 <COMMIT-H> refs/heads/topic/2
440 test_cmp expect actual &&
442 git -C mirror.git fetch ../2.bdl "+refs/*:refs/*" &&
443 git -C mirror.git show-ref |
444 make_user_friendly_and_stable_output >actual &&
445 cat >expect <<-\EOF &&
446 <COMMIT-N> refs/heads/release
447 <COMMIT-D> refs/heads/topic/1
448 <COMMIT-H> refs/heads/topic/2
450 test_cmp expect actual &&
452 git -C mirror.git fetch ../3.bdl "+refs/*:refs/*" &&
453 git -C mirror.git show-ref |
454 make_user_friendly_and_stable_output >actual &&
455 cat >expect <<-\EOF &&
456 <COMMIT-P> refs/heads/main
457 <COMMIT-N> refs/heads/release
458 <COMMIT-D> refs/heads/topic/1
459 <COMMIT-H> refs/heads/topic/2
461 test_cmp expect actual &&
463 git -C mirror.git fetch ../4.bdl "+refs/*:refs/*" &&
464 git -C mirror.git show-ref |
465 make_user_friendly_and_stable_output >actual &&
466 cat >expect <<-\EOF &&
467 <COMMIT-P> refs/heads/main
468 <COMMIT-N> refs/heads/release
469 <COMMIT-D> refs/heads/topic/1
470 <COMMIT-H> refs/heads/topic/2
475 test_cmp expect actual