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.
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"
109 format_and_save_expect () {
110 sed -e 's/Z$//' >expect
113 # (C) (D, pull/1/head, topic/1)
116 # / \ o (H, topic/2) (M, tag:v2)
117 # / (F) \ / (N, tag:v3)
118 # / o --------- o (G, pull/2/head) o --- o --- o (release)
120 # o --- o --- o -------- o -- o ------------------ o ------- o --- o (main)
121 # (A) (B) (E, tag:v1) (I) (J) (K) (O) (P)
123 test_expect_success 'setup' '
124 # Try to make a stable fixed width for abbreviated commit ID,
125 # this fixed-width oid will be replaced with "<OID>".
126 git config core.abbrev 7 &&
128 # branch main: commit A & B
129 test_commit_setvar A "Commit A" main.txt &&
130 test_commit_setvar B "Commit B" main.txt &&
132 # branch topic/1: commit C & D, refs/pull/1/head
133 git checkout -b topic/1 &&
134 test_commit_setvar C "Commit C" topic-1.txt &&
135 test_commit_setvar D "Commit D" topic-1.txt &&
136 git update-ref refs/pull/1/head HEAD &&
138 # branch topic/1: commit E, tag v1
140 test_commit_setvar E "Commit E" main.txt &&
141 test_commit_setvar --tag TAG1 v1 &&
143 # branch topic/2: commit F & G, refs/pull/2/head
144 git checkout -b topic/2 &&
145 test_commit_setvar F "Commit F" topic-2.txt &&
146 test_commit_setvar G "Commit G" topic-2.txt &&
147 git update-ref refs/pull/2/head HEAD &&
148 test_commit_setvar H "Commit H" topic-2.txt &&
150 # branch main: merge commit I & J
152 test_commit_setvar --merge I topic/1 "Merge commit I" &&
153 test_commit_setvar --merge J refs/pull/2/head "Merge commit J" &&
155 # branch main: commit K
157 test_commit_setvar K "Commit K" main.txt &&
160 git checkout -b release &&
161 test_commit_setvar L "Commit L" release.txt &&
162 test_commit_setvar M "Commit M" release.txt &&
163 test_commit_setvar --tag TAG2 v2 &&
164 test_commit_setvar N "Commit N" release.txt &&
165 test_commit_setvar --tag TAG3 v3 &&
167 # branch main: merge commit O, commit P
169 test_commit_setvar --merge O tags/v2 "Merge commit O" &&
170 test_commit_setvar P "Commit P" main.txt
173 test_expect_success 'create bundle from special rev: main^!' '
174 git bundle create special-rev.bdl "main^!" &&
176 git bundle list-heads special-rev.bdl |
177 make_user_friendly_and_stable_output >actual &&
178 cat >expect <<-\EOF &&
179 <COMMIT-P> refs/heads/main
181 test_cmp expect actual &&
183 git bundle verify special-rev.bdl |
184 make_user_friendly_and_stable_output >actual &&
185 format_and_save_expect <<-\EOF &&
186 The bundle contains this ref:
187 <COMMIT-P> refs/heads/main
188 The bundle requires this ref:
191 test_cmp expect actual &&
193 test_bundle_object_count special-rev.bdl 3
196 test_expect_success 'create bundle with --max-count option' '
197 git bundle create max-count.bdl --max-count 1 \
204 git bundle verify max-count.bdl |
205 make_user_friendly_and_stable_output >actual &&
206 format_and_save_expect <<-\EOF &&
207 The bundle contains these 2 refs:
208 <COMMIT-P> refs/heads/main
210 The bundle requires this ref:
213 test_cmp expect actual &&
215 test_bundle_object_count max-count.bdl 4
218 test_expect_success 'create bundle with --since option' '
219 git log -1 --pretty="%ad" $M >actual &&
220 cat >expect <<-\EOF &&
221 Thu Apr 7 15:26:13 2005 -0700
223 test_cmp expect actual &&
225 git bundle create since.bdl \
226 --since "Thu Apr 7 15:27:00 2005 -0700" \
229 git bundle verify since.bdl |
230 make_user_friendly_and_stable_output >actual &&
231 format_and_save_expect <<-\EOF &&
232 The bundle contains these 5 refs:
233 <COMMIT-P> refs/heads/main
234 <COMMIT-N> refs/heads/release
238 The bundle requires these 2 refs:
242 test_cmp expect actual &&
244 test_bundle_object_count --thin since.bdl 13
247 test_expect_success 'create bundle 1 - no prerequisites' '
248 # create bundle from args
249 git bundle create 1.bdl topic/1 topic/2 &&
251 # create bundle from stdin
252 cat >input <<-\EOF &&
256 git bundle create stdin-1.bdl --stdin <input &&
258 cat >expect <<-\EOF &&
259 The bundle contains these 2 refs:
260 <COMMIT-D> refs/heads/topic/1
261 <COMMIT-H> refs/heads/topic/2
262 The bundle records a complete history.
265 # verify bundle, which has no prerequisites
266 git bundle verify 1.bdl |
267 make_user_friendly_and_stable_output >actual &&
268 test_cmp expect actual &&
270 git bundle verify stdin-1.bdl |
271 make_user_friendly_and_stable_output >actual &&
272 test_cmp expect actual &&
274 test_bundle_object_count 1.bdl 24 &&
275 test_bundle_object_count stdin-1.bdl 24
278 test_expect_success 'create bundle 2 - has prerequisites' '
279 # create bundle from args
280 git bundle create 2.bdl \
287 # create bundle from stdin
288 # input has a non-exist reference: "topic/deleted"
294 git bundle create stdin-2.bdl \
299 format_and_save_expect <<-\EOF &&
300 The bundle contains this ref:
301 <COMMIT-N> refs/heads/release
302 The bundle requires these 3 refs:
308 git bundle verify 2.bdl |
309 make_user_friendly_and_stable_output >actual &&
310 test_cmp expect actual &&
312 git bundle verify stdin-2.bdl |
313 make_user_friendly_and_stable_output >actual &&
314 test_cmp expect actual &&
316 test_bundle_object_count 2.bdl 16 &&
317 test_bundle_object_count stdin-2.bdl 16
320 test_expect_success 'fail to verify bundle without prerequisites' '
321 git init --bare test1.git &&
323 format_and_save_expect <<-\EOF &&
324 error: Repository lacks these prerequisite commits:
330 test_must_fail git -C test1.git bundle verify ../2.bdl 2>&1 |
331 make_user_friendly_and_stable_output >actual &&
332 test_cmp expect actual &&
334 test_must_fail git -C test1.git bundle verify ../stdin-2.bdl 2>&1 |
335 make_user_friendly_and_stable_output >actual &&
336 test_cmp expect actual
339 test_expect_success 'create bundle 3 - two refs, same object' '
340 # create bundle from args
341 git bundle create --version=3 3.bdl \
348 # create bundle from stdin
349 cat >input <<-\EOF &&
354 git bundle create --version=3 stdin-3.bdl \
358 format_and_save_expect <<-\EOF &&
359 The bundle contains these 2 refs:
360 <COMMIT-P> refs/heads/main
362 The bundle requires these 2 refs:
367 git bundle verify 3.bdl |
368 make_user_friendly_and_stable_output >actual &&
369 test_cmp expect actual &&
371 git bundle verify stdin-3.bdl |
372 make_user_friendly_and_stable_output >actual &&
373 test_cmp expect actual &&
375 test_bundle_object_count 3.bdl 4 &&
376 test_bundle_object_count stdin-3.bdl 4
379 test_expect_success 'create bundle 4 - with tags' '
380 # create bundle from args
381 git bundle create 4.bdl \
388 # create bundle from stdin
389 cat >input <<-\EOF &&
395 git bundle create stdin-4.bdl \
400 cat >expect <<-\EOF &&
401 The bundle contains these 3 refs:
405 The bundle records a complete history.
408 git bundle verify 4.bdl |
409 make_user_friendly_and_stable_output >actual &&
410 test_cmp expect actual &&
412 git bundle verify stdin-4.bdl |
413 make_user_friendly_and_stable_output >actual &&
414 test_cmp expect actual &&
416 test_bundle_object_count 4.bdl 3 &&
417 test_bundle_object_count stdin-4.bdl 3
420 test_expect_success 'clone from bundle' '
421 git clone --mirror 1.bdl mirror.git &&
422 git -C mirror.git show-ref |
423 make_user_friendly_and_stable_output >actual &&
424 cat >expect <<-\EOF &&
425 <COMMIT-D> refs/heads/topic/1
426 <COMMIT-H> refs/heads/topic/2
428 test_cmp expect actual &&
430 git -C mirror.git fetch ../2.bdl "+refs/*:refs/*" &&
431 git -C mirror.git show-ref |
432 make_user_friendly_and_stable_output >actual &&
433 cat >expect <<-\EOF &&
434 <COMMIT-N> refs/heads/release
435 <COMMIT-D> refs/heads/topic/1
436 <COMMIT-H> refs/heads/topic/2
438 test_cmp expect actual &&
440 git -C mirror.git fetch ../3.bdl "+refs/*:refs/*" &&
441 git -C mirror.git show-ref |
442 make_user_friendly_and_stable_output >actual &&
443 cat >expect <<-\EOF &&
444 <COMMIT-P> refs/heads/main
445 <COMMIT-N> refs/heads/release
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 ../4.bdl "+refs/*:refs/*" &&
452 git -C mirror.git show-ref |
453 make_user_friendly_and_stable_output >actual &&
454 cat >expect <<-\EOF &&
455 <COMMIT-P> refs/heads/main
456 <COMMIT-N> refs/heads/release
457 <COMMIT-D> refs/heads/topic/1
458 <COMMIT-H> refs/heads/topic/2
463 test_cmp expect actual