.github/workflows/main.yml: run static-analysis on bionic
[git/debian.git] / t / t5323-pack-redundant.sh
blob7e3340843fe570d7624d33390803d6820817ad4f
1 #!/bin/sh
3 # Copyright (c) 2018 Jiang Xin
6 test_description='Test git pack-redundant
8 In order to test git-pack-redundant, we will create a number of objects and
9 packs in the repository `master.git`. The relationship between packs (P1-P8)
10 and objects (T, A-R) is showed in the following chart. Objects of a pack will
11 be marked with letter x, while objects of redundant packs will be marked with
12 exclamation point, and redundant pack itself will be marked with asterisk.
14 | T A B C D E F G H I J K L M N O P Q R
15 ----+--------------------------------------
16 P1 | x x x x x x x x
17 P2* | ! ! ! ! ! ! !
18 P3 | x x x x x x
19 P4* | ! ! ! ! !
20 P5 | x x x x
21 P6* | ! ! !
22 P7 | x x
23 P8* | !
24 ----+--------------------------------------
25 ALL | x x x x x x x x x x x x x x x x x x x
27 Another repository `shared.git` has unique objects (X-Z), while other objects
28 (marked with letter s) are shared through alt-odb (of `master.git`). The
29 relationship between packs and objects is as follows:
31 | T A B C D E F G H I J K L M N O P Q R X Y Z
32 ----+----------------------------------------------
33 Px1 | s s s x x x
34 Px2 | s s s x x x
37 . ./test-lib.sh
39 master_repo=master.git
40 shared_repo=shared.git
42 # Create commits in <repo> and assign each commit's oid to shell variables
43 # given in the arguments (A, B, and C). E.g.:
45 # create_commits_in <repo> A B C
47 # NOTE: Avoid calling this function from a subshell since variable
48 # assignments will disappear when subshell exits.
49 create_commits_in () {
50 repo="$1" &&
51 if ! parent=$(git -C "$repo" rev-parse HEAD^{} 2>/dev/null)
52 then
53 parent=
54 fi &&
55 T=$(git -C "$repo" write-tree) &&
56 shift &&
57 while test $# -gt 0
59 name=$1 &&
60 test_tick &&
61 if test -z "$parent"
62 then
63 oid=$(echo $name | git -C "$repo" commit-tree $T)
64 else
65 oid=$(echo $name | git -C "$repo" commit-tree -p $parent $T)
66 fi &&
67 eval $name=$oid &&
68 parent=$oid &&
69 shift ||
70 return 1
71 done &&
72 git -C "$repo" update-ref refs/heads/master $oid
75 # Create pack in <repo> and assign pack id to variable given in the 2nd argument
76 # (<name>). Commits in the pack will be read from stdin. E.g.:
78 # create_pack_in <repo> <name> <<-EOF
79 # ...
80 # EOF
82 # NOTE: commits from stdin should be given using heredoc, not using pipe, and
83 # avoid calling this function from a subshell since variable assignments will
84 # disappear when subshell exits.
85 create_pack_in () {
86 repo="$1" &&
87 name="$2" &&
88 pack=$(git -C "$repo/objects/pack" pack-objects -q pack) &&
89 eval $name=$pack &&
90 eval P$pack=$name:$pack
93 format_packfiles () {
94 sed \
95 -e "s#.*/pack-\(.*\)\.idx#\1#" \
96 -e "s#.*/pack-\(.*\)\.pack#\1#" |
97 sort -u |
98 while read p
100 if test -z "$(eval echo \${P$p})"
101 then
102 echo $p
103 else
104 eval echo "\${P$p}"
106 done |
107 sort
110 test_expect_success 'setup master repo' '
111 git init --bare "$master_repo" &&
112 create_commits_in "$master_repo" A B C D E F G H I J K L M N O P Q R
115 test_expect_success 'master: pack-redundant works with no packfile' '
117 cd "$master_repo" &&
118 cat >expect <<-EOF &&
119 fatal: Zero packs found!
121 test_must_fail git pack-redundant --all >actual 2>&1 &&
122 test_cmp expect actual
126 #############################################################################
127 # Chart of packs and objects for this test case
129 # | T A B C D E F G H I J K L M N O P Q R
130 # ----+--------------------------------------
131 # P1 | x x x x x x x x
132 # ----+--------------------------------------
133 # ALL | x x x x x x x x
135 #############################################################################
136 test_expect_success 'master: pack-redundant works with one packfile' '
137 create_pack_in "$master_repo" P1 <<-EOF &&
148 cd "$master_repo" &&
149 git pack-redundant --all >out &&
150 test_must_be_empty out
154 #############################################################################
155 # Chart of packs and objects for this test case
157 # | T A B C D E F G H I J K L M N O P Q R
158 # ----+--------------------------------------
159 # P1 | x x x x x x x x
160 # P2 | x x x x x x x
161 # P3 | x x x x x x
162 # ----+--------------------------------------
163 # ALL | x x x x x x x x x x x x x x x
165 #############################################################################
166 test_expect_success 'master: no redundant for pack 1, 2, 3' '
167 create_pack_in "$master_repo" P2 <<-EOF &&
176 create_pack_in "$master_repo" P3 <<-EOF &&
185 cd "$master_repo" &&
186 git pack-redundant --all >out &&
187 test_must_be_empty out
191 #############################################################################
192 # Chart of packs and objects for this test case
194 # | T A B C D E F G H I J K L M N O P Q R
195 # ----+--------------------------------------
196 # P1 | x x x x x x x x
197 # P2 | x x x x x x x
198 # P3* | ! ! ! ! ! !
199 # P4 | x x x x x
200 # P5 | x x x x
201 # ----+--------------------------------------
202 # ALL | x x x x x x x x x x x x x x x x x x
204 #############################################################################
205 test_expect_success 'master: one of pack-2/pack-3 is redundant' '
206 create_pack_in "$master_repo" P4 <<-EOF &&
213 create_pack_in "$master_repo" P5 <<-EOF &&
220 cd "$master_repo" &&
221 cat >expect <<-EOF &&
222 P3:$P3
224 git pack-redundant --all >out &&
225 format_packfiles <out >actual &&
226 test_cmp expect actual
230 #############################################################################
231 # Chart of packs and objects for this test case
233 # | T A B C D E F G H I J K L M N O P Q R
234 # ----+--------------------------------------
235 # P1 | x x x x x x x x
236 # P2* | ! ! ! ! ! ! !
237 # P3 | x x x x x x
238 # P4* | ! ! ! ! !
239 # P5 | x x x x
240 # P6* | ! ! !
241 # P7 | x x
242 # ----+--------------------------------------
243 # ALL | x x x x x x x x x x x x x x x x x x x
245 #############################################################################
246 test_expect_success 'master: pack 2, 4, and 6 are redundant' '
247 create_pack_in "$master_repo" P6 <<-EOF &&
252 create_pack_in "$master_repo" P7 <<-EOF &&
257 cd "$master_repo" &&
258 cat >expect <<-EOF &&
259 P2:$P2
260 P4:$P4
261 P6:$P6
263 git pack-redundant --all >out &&
264 format_packfiles <out >actual &&
265 test_cmp expect actual
269 #############################################################################
270 # Chart of packs and objects for this test case
272 # | T A B C D E F G H I J K L M N O P Q R
273 # ----+--------------------------------------
274 # P1 | x x x x x x x x
275 # P2* | ! ! ! ! ! ! !
276 # P3 | x x x x x x
277 # P4* | ! ! ! ! !
278 # P5 | x x x x
279 # P6* | ! ! !
280 # P7 | x x
281 # P8* | !
282 # ----+--------------------------------------
283 # ALL | x x x x x x x x x x x x x x x x x x x
285 #############################################################################
286 test_expect_success 'master: pack-8 (subset of pack-1) is also redundant' '
287 create_pack_in "$master_repo" P8 <<-EOF &&
291 cd "$master_repo" &&
292 cat >expect <<-EOF &&
293 P2:$P2
294 P4:$P4
295 P6:$P6
296 P8:$P8
298 git pack-redundant --all >out &&
299 format_packfiles <out >actual &&
300 test_cmp expect actual
304 test_expect_success 'master: clean loose objects' '
306 cd "$master_repo" &&
307 git prune-packed &&
308 find objects -type f | sed -e "/objects\/pack\//d" >out &&
309 test_must_be_empty out
313 test_expect_success 'master: remove redundant packs and pass fsck' '
315 cd "$master_repo" &&
316 git pack-redundant --all | xargs rm &&
317 git fsck &&
318 git pack-redundant --all >out &&
319 test_must_be_empty out
323 # The following test cases will execute inside `shared.git`, instead of
324 # inside `master.git`.
325 test_expect_success 'setup shared.git' '
326 git clone --mirror "$master_repo" "$shared_repo" &&
328 cd "$shared_repo" &&
329 printf "../../$master_repo/objects\n" >objects/info/alternates
333 test_expect_success 'shared: all packs are redundant, but no output without --alt-odb' '
335 cd "$shared_repo" &&
336 git pack-redundant --all >out &&
337 test_must_be_empty out
341 #############################################################################
342 # Chart of packs and objects for this test case
344 # ================ master.git ===============
345 # | T A B C D E F G H I J K L M N O P Q R <----------+
346 # ----+-------------------------------------- |
347 # P1 | x x x x x x x x |
348 # P3 | x x x x x x |
349 # P5 | x x x x |
350 # P7 | x x |
351 # ----+-------------------------------------- |
352 # ALL | x x x x x x x x x x x x x x x x x x x |
355 # ================ shared.git =============== |
356 # | T A B C D E F G H I J K L M N O P Q R <objects/info/alternates>
357 # ----+--------------------------------------
358 # P1* | s s s s s s s s
359 # P3* | s s s s s s
360 # P5* | s s s s
361 # P7* | s s
362 # ----+--------------------------------------
363 # ALL | x x x x x x x x x x x x x x x x x x x
365 #############################################################################
366 test_expect_success 'shared: show redundant packs in stderr for verbose mode' '
368 cd "$shared_repo" &&
369 cat >expect <<-EOF &&
370 P1:$P1
371 P3:$P3
372 P5:$P5
373 P7:$P7
375 git pack-redundant --all --verbose >out 2>out.err &&
376 test_must_be_empty out &&
377 grep "pack$" out.err | format_packfiles >actual &&
378 test_cmp expect actual
382 test_expect_success 'shared: remove redundant packs, no packs left' '
384 cd "$shared_repo" &&
385 cat >expect <<-EOF &&
386 fatal: Zero packs found!
388 git pack-redundant --all --alt-odb | xargs rm &&
389 git fsck &&
390 test_must_fail git pack-redundant --all --alt-odb >actual 2>&1 &&
391 test_cmp expect actual
395 test_expect_success 'shared: create new objects and packs' '
396 create_commits_in "$shared_repo" X Y Z &&
397 create_pack_in "$shared_repo" Px1 <<-EOF &&
405 create_pack_in "$shared_repo" Px2 <<-EOF
415 test_expect_success 'shared: no redundant without --alt-odb' '
417 cd "$shared_repo" &&
418 git pack-redundant --all >out &&
419 test_must_be_empty out
423 #############################################################################
424 # Chart of packs and objects for this test case
426 # ================ master.git ===============
427 # | T A B C D E F G H I J K L M N O P Q R <----------------+
428 # ----+-------------------------------------- |
429 # P1 | x x x x x x x x |
430 # P3 | x x x x x x |
431 # P5 | x x x x |
432 # P7 | x x |
433 # ----+-------------------------------------- |
434 # ALL | x x x x x x x x x x x x x x x x x x x |
437 # ================ shared.git ======================= |
438 # | T A B C D E F G H I J K L M N O P Q R X Y Z <objects/info/alternates>
439 # ----+----------------------------------------------
440 # Px1 | s s s x x x
441 # Px2*| s s s ! ! !
442 # ----+----------------------------------------------
443 # ALL | s s s s s s s s s s s s s s s s s s s x x x
445 #############################################################################
446 test_expect_success 'shared: one pack is redundant with --alt-odb' '
448 cd "$shared_repo" &&
449 git pack-redundant --all --alt-odb >out &&
450 format_packfiles <out >actual &&
451 test_line_count = 1 actual
455 #############################################################################
456 # Chart of packs and objects for this test case
458 # ================ master.git ===============
459 # | T A B C D E F G H I J K L M N O P Q R <----------------+
460 # ----+-------------------------------------- |
461 # P1 | x x x x x x x x |
462 # P3 | x x x x x x |
463 # P5 | x x x x |
464 # P7 | x x |
465 # ----+-------------------------------------- |
466 # ALL | x x x x x x x x x x x x x x x x x x x |
469 # ================ shared.git ======================= |
470 # | T A B C D E F G H I J K L M N O P Q R X Y Z <objects/info/alternates>
471 # ----+----------------------------------------------
472 # Px1*| s s s i i i
473 # Px2*| s s s i i i
474 # ----+----------------------------------------------
475 # ALL | s s s s s s s s s s s s s s s s s s s i i i
476 # (ignored objects, marked with i)
478 #############################################################################
479 test_expect_success 'shared: ignore unique objects and all two packs are redundant' '
481 cd "$shared_repo" &&
482 cat >expect <<-EOF &&
483 Px1:$Px1
484 Px2:$Px2
486 git pack-redundant --all --alt-odb >out <<-EOF &&
491 format_packfiles <out >actual &&
492 test_cmp expect actual
496 test_done