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 ----+--------------------------------------
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 ----+----------------------------------------------
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 () {
51 if ! parent=$(git -C "$repo" rev-parse HEAD^{} 2>/dev/null)
55 T=$(git -C "$repo" write-tree) &&
63 oid=$(echo $name | git -C "$repo" commit-tree $T)
65 oid=$(echo $name | git -C "$repo" commit-tree -p $parent $T)
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
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.
88 pack=$(git -C "$repo/objects/pack" pack-objects -q pack) &&
90 eval P$pack=$name:$pack
95 -e "s#.*/pack-\(.*\)\.idx#\1#" \
96 -e "s#.*/pack-\(.*\)\.pack#\1#" |
100 if test -z "$(eval echo \${P$p})"
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' '
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 &&
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
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 &&
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
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 &&
221 cat >expect <<-EOF &&
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* | ! ! ! ! ! ! !
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 &&
258 cat >expect <<-EOF &&
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* | ! ! ! ! ! ! !
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 &&
292 cat >expect <<-EOF &&
298 git pack-redundant --all >out &&
299 format_packfiles <out >actual &&
300 test_cmp expect actual
304 test_expect_success 'master: clean loose objects' '
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' '
316 git pack-redundant --all | xargs rm &&
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" &&
329 printf "../../$master_repo/objects\n" >objects/info/alternates
333 test_expect_success 'shared: all packs are redundant, but no output without --alt-odb' '
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 |
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
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' '
369 cat >expect <<-EOF &&
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' '
385 cat >expect <<-EOF &&
386 fatal: Zero packs found!
388 git pack-redundant --all --alt-odb | xargs rm &&
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' '
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 |
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 # ----+----------------------------------------------
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' '
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 |
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 # ----+----------------------------------------------
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' '
482 cat >expect <<-EOF &&
486 git pack-redundant --all --alt-odb >out <<-EOF &&
491 format_packfiles <out >actual &&
492 test_cmp expect actual