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
)
83 # Format the output of git commands to make a user-friendly and stable
84 # text. We can easily prepare the expect text without having to worry
85 # about future changes of the commit ID and spaces of the output.
86 make_user_friendly_and_stable_output
() {
88 -e "s/${A%${A#???????}}[0-9a-f]*/<COMMIT-A>/g" \
89 -e "s/${B%${B#???????}}[0-9a-f]*/<COMMIT-B>/g" \
90 -e "s/${C%${C#???????}}[0-9a-f]*/<COMMIT-C>/g" \
91 -e "s/${D%${D#???????}}[0-9a-f]*/<COMMIT-D>/g" \
92 -e "s/${E%${E#???????}}[0-9a-f]*/<COMMIT-E>/g" \
93 -e "s/${F%${F#???????}}[0-9a-f]*/<COMMIT-F>/g" \
94 -e "s/${G%${G#???????}}[0-9a-f]*/<COMMIT-G>/g" \
95 -e "s/${H%${H#???????}}[0-9a-f]*/<COMMIT-H>/g" \
96 -e "s/${I%${I#???????}}[0-9a-f]*/<COMMIT-I>/g" \
97 -e "s/${J%${J#???????}}[0-9a-f]*/<COMMIT-J>/g" \
98 -e "s/${K%${K#???????}}[0-9a-f]*/<COMMIT-K>/g" \
99 -e "s/${L%${L#???????}}[0-9a-f]*/<COMMIT-L>/g" \
100 -e "s/${M%${M#???????}}[0-9a-f]*/<COMMIT-M>/g" \
101 -e "s/${N%${N#???????}}[0-9a-f]*/<COMMIT-N>/g" \
102 -e "s/${O%${O#???????}}[0-9a-f]*/<COMMIT-O>/g" \
103 -e "s/${P%${P#???????}}[0-9a-f]*/<COMMIT-P>/g" \
104 -e "s/${TAG1%${TAG1#???????}}[0-9a-f]*/<TAG-1>/g" \
105 -e "s/${TAG2%${TAG2#???????}}[0-9a-f]*/<TAG-2>/g" \
106 -e "s/${TAG3%${TAG3#???????}}[0-9a-f]*/<TAG-3>/g" \
110 # (C) (D, pull/1/head, topic/1)
113 # / \ o (H, topic/2) (M, tag:v2)
114 # / (F) \ / (N, tag:v3)
115 # / o --------- o (G, pull/2/head) o --- o --- o (release)
117 # o --- o --- o -------- o -- o ------------------ o ------- o --- o (main)
118 # (A) (B) (E, tag:v1) (I) (J) (K) (O) (P)
120 test_expect_success
'setup' '
121 # Try to make a stable fixed width for abbreviated commit ID,
122 # this fixed-width oid will be replaced with "<OID>".
123 git config core.abbrev 7 &&
125 # branch main: commit A & B
126 test_commit_setvar A "Commit A" main.txt &&
127 test_commit_setvar B "Commit B" main.txt &&
129 # branch topic/1: commit C & D, refs/pull/1/head
130 git checkout -b topic/1 &&
131 test_commit_setvar C "Commit C" topic-1.txt &&
132 test_commit_setvar D "Commit D" topic-1.txt &&
133 git update-ref refs/pull/1/head HEAD &&
135 # branch topic/1: commit E, tag v1
137 test_commit_setvar E "Commit E" main.txt &&
138 test_commit_setvar --tag TAG1 v1 &&
140 # branch topic/2: commit F & G, refs/pull/2/head
141 git checkout -b topic/2 &&
142 test_commit_setvar F "Commit F" topic-2.txt &&
143 test_commit_setvar G "Commit G" topic-2.txt &&
144 git update-ref refs/pull/2/head HEAD &&
145 test_commit_setvar H "Commit H" topic-2.txt &&
147 # branch main: merge commit I & J
149 test_commit_setvar --merge I topic/1 "Merge commit I" &&
150 test_commit_setvar --merge J refs/pull/2/head "Merge commit J" &&
152 # branch main: commit K
154 test_commit_setvar K "Commit K" main.txt &&
157 git checkout -b release &&
158 test_commit_setvar L "Commit L" release.txt &&
159 test_commit_setvar M "Commit M" release.txt &&
160 test_commit_setvar --tag TAG2 v2 &&
161 test_commit_setvar N "Commit N" release.txt &&
162 test_commit_setvar --tag TAG3 v3 &&
164 # branch main: merge commit O, commit P
166 test_commit_setvar --merge O tags/v2 "Merge commit O" &&
167 test_commit_setvar P "Commit P" main.txt
170 test_expect_success
'create bundle from special rev: main^!' '
171 git bundle create special-rev.bdl "main^!" &&
173 git bundle list-heads special-rev.bdl |
174 make_user_friendly_and_stable_output >actual &&
175 cat >expect <<-\EOF &&
176 <COMMIT-P> refs/heads/main
178 test_cmp expect actual &&
180 git bundle verify special-rev.bdl |
181 make_user_friendly_and_stable_output >actual &&
182 cat >expect <<-\EOF &&
183 The bundle contains this ref:
184 <COMMIT-P> refs/heads/main
185 The bundle requires this ref:
188 test_cmp expect actual &&
190 test_bundle_object_count special-rev.bdl 3
193 test_expect_success
'create bundle with --max-count option' '
194 git bundle create max-count.bdl --max-count 1 \
201 git bundle verify max-count.bdl |
202 make_user_friendly_and_stable_output >actual &&
203 cat >expect <<-\EOF &&
204 The bundle contains these 2 refs:
205 <COMMIT-P> refs/heads/main
207 The bundle requires this ref:
210 test_cmp expect actual &&
212 test_bundle_object_count max-count.bdl 4
215 test_expect_success
'create bundle with --since option' '
216 git log -1 --pretty="%ad" $M >actual &&
217 cat >expect <<-\EOF &&
218 Thu Apr 7 15:26:13 2005 -0700
220 test_cmp expect actual &&
222 git bundle create since.bdl \
223 --since "Thu Apr 7 15:27:00 2005 -0700" \
226 git bundle verify since.bdl |
227 make_user_friendly_and_stable_output >actual &&
228 cat >expect <<-\EOF &&
229 The bundle contains these 5 refs:
230 <COMMIT-P> refs/heads/main
231 <COMMIT-N> refs/heads/release
235 The bundle requires these 2 refs:
239 test_cmp expect actual &&
241 test_bundle_object_count --thin since.bdl 13
244 test_expect_success
'create bundle 1 - no prerequisites' '
245 # create bundle from args
246 git bundle create 1.bdl topic/1 topic/2 &&
248 # create bundle from stdin
249 cat >input <<-\EOF &&
253 git bundle create stdin-1.bdl --stdin <input &&
255 cat >expect <<-\EOF &&
256 The bundle contains these 2 refs:
257 <COMMIT-D> refs/heads/topic/1
258 <COMMIT-H> refs/heads/topic/2
259 The bundle records a complete history.
262 # verify bundle, which has no prerequisites
263 git bundle verify 1.bdl |
264 make_user_friendly_and_stable_output >actual &&
265 test_cmp expect actual &&
267 git bundle verify stdin-1.bdl |
268 make_user_friendly_and_stable_output >actual &&
269 test_cmp expect actual &&
271 test_bundle_object_count 1.bdl 24 &&
272 test_bundle_object_count stdin-1.bdl 24
275 test_expect_success
'create bundle 2 - has prerequisites' '
276 # create bundle from args
277 git bundle create 2.bdl \
284 # create bundle from stdin
285 # input has a non-exist reference: "topic/deleted"
291 git bundle create stdin-2.bdl \
296 cat >expect <<-\EOF &&
297 The bundle contains this ref:
298 <COMMIT-N> refs/heads/release
299 The bundle requires these 3 refs:
305 git bundle verify 2.bdl |
306 make_user_friendly_and_stable_output >actual &&
307 test_cmp expect actual &&
309 git bundle verify stdin-2.bdl |
310 make_user_friendly_and_stable_output >actual &&
311 test_cmp expect actual &&
313 test_bundle_object_count 2.bdl 16 &&
314 test_bundle_object_count stdin-2.bdl 16
317 test_expect_success
'fail to verify bundle without prerequisites' '
318 git init --bare test1.git &&
320 cat >expect <<-\EOF &&
321 error: Repository lacks these prerequisite commits:
327 test_must_fail git -C test1.git bundle verify ../2.bdl 2>&1 |
328 make_user_friendly_and_stable_output >actual &&
329 test_cmp expect actual &&
331 test_must_fail git -C test1.git bundle verify ../stdin-2.bdl 2>&1 |
332 make_user_friendly_and_stable_output >actual &&
333 test_cmp expect actual
336 test_expect_success
'create bundle 3 - two refs, same object' '
337 # create bundle from args
338 git bundle create --version=3 3.bdl \
345 # create bundle from stdin
346 cat >input <<-\EOF &&
351 git bundle create --version=3 stdin-3.bdl \
355 cat >expect <<-\EOF &&
356 The bundle contains these 2 refs:
357 <COMMIT-P> refs/heads/main
359 The bundle requires these 2 refs:
364 git bundle verify 3.bdl |
365 make_user_friendly_and_stable_output >actual &&
366 test_cmp expect actual &&
368 git bundle verify stdin-3.bdl |
369 make_user_friendly_and_stable_output >actual &&
370 test_cmp expect actual &&
372 test_bundle_object_count 3.bdl 4 &&
373 test_bundle_object_count stdin-3.bdl 4
376 test_expect_success
'create bundle 4 - with tags' '
377 # create bundle from args
378 git bundle create 4.bdl \
385 # create bundle from stdin
386 cat >input <<-\EOF &&
392 git bundle create stdin-4.bdl \
397 cat >expect <<-\EOF &&
398 The bundle contains these 3 refs:
402 The bundle records a complete history.
405 git bundle verify 4.bdl |
406 make_user_friendly_and_stable_output >actual &&
407 test_cmp expect actual &&
409 git bundle verify stdin-4.bdl |
410 make_user_friendly_and_stable_output >actual &&
411 test_cmp expect actual &&
413 test_bundle_object_count 4.bdl 3 &&
414 test_bundle_object_count stdin-4.bdl 3
417 test_expect_success
'clone from bundle' '
418 git clone --mirror 1.bdl mirror.git &&
419 git -C mirror.git show-ref |
420 make_user_friendly_and_stable_output >actual &&
421 cat >expect <<-\EOF &&
422 <COMMIT-D> refs/heads/topic/1
423 <COMMIT-H> refs/heads/topic/2
425 test_cmp expect actual &&
427 git -C mirror.git fetch ../2.bdl "+refs/*:refs/*" &&
428 git -C mirror.git show-ref |
429 make_user_friendly_and_stable_output >actual &&
430 cat >expect <<-\EOF &&
431 <COMMIT-N> refs/heads/release
432 <COMMIT-D> refs/heads/topic/1
433 <COMMIT-H> refs/heads/topic/2
435 test_cmp expect actual &&
437 git -C mirror.git fetch ../3.bdl "+refs/*:refs/*" &&
438 git -C mirror.git show-ref |
439 make_user_friendly_and_stable_output >actual &&
440 cat >expect <<-\EOF &&
441 <COMMIT-P> refs/heads/main
442 <COMMIT-N> refs/heads/release
443 <COMMIT-D> refs/heads/topic/1
444 <COMMIT-H> refs/heads/topic/2
446 test_cmp expect actual &&
448 git -C mirror.git fetch ../4.bdl "+refs/*:refs/*" &&
449 git -C mirror.git show-ref |
450 make_user_friendly_and_stable_output >actual &&
451 cat >expect <<-\EOF &&
452 <COMMIT-P> refs/heads/main
453 <COMMIT-N> refs/heads/release
454 <COMMIT-D> refs/heads/topic/1
455 <COMMIT-H> refs/heads/topic/2
460 test_cmp expect actual