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 #############################################################################
116 # Chart of packs and objects for this test case
118 # | T A B C D E F G H I J K L M N O P Q R
119 # ----+--------------------------------------
120 # P1 | x x x x x x x x
123 # ----+--------------------------------------
124 # ALL | x x x x x x x x x x x x x x x
126 #############################################################################
127 test_expect_success 'master: no redundant for pack 1, 2, 3' '
128 create_pack_in "$master_repo" P1 <<-EOF &&
138 create_pack_in "$master_repo" P2 <<-EOF &&
147 create_pack_in "$master_repo" P3 <<-EOF &&
157 git pack-redundant --all >out &&
158 test_must_be_empty out
162 #############################################################################
163 # Chart of packs and objects for this test case
165 # | T A B C D E F G H I J K L M N O P Q R
166 # ----+--------------------------------------
167 # P1 | x x x x x x x x
172 # ----+--------------------------------------
173 # ALL | x x x x x x x x x x x x x x x x x x
175 #############################################################################
176 test_expect_success 'master: one of pack-2/pack-3 is redundant' '
177 create_pack_in "$master_repo" P4 <<-EOF &&
184 create_pack_in "$master_repo" P5 <<-EOF &&
192 cat >expect <<-EOF &&
195 git pack-redundant --all >out &&
196 format_packfiles <out >actual &&
197 test_cmp expect actual
201 #############################################################################
202 # Chart of packs and objects for this test case
204 # | T A B C D E F G H I J K L M N O P Q R
205 # ----+--------------------------------------
206 # P1 | x x x x x x x x
207 # P2* | ! ! ! ! ! ! !
213 # ----+--------------------------------------
214 # ALL | x x x x x x x x x x x x x x x x x x x
216 #############################################################################
217 test_expect_success 'master: pack 2, 4, and 6 are redundant' '
218 create_pack_in "$master_repo" P6 <<-EOF &&
223 create_pack_in "$master_repo" P7 <<-EOF &&
229 cat >expect <<-EOF &&
234 git pack-redundant --all >out &&
235 format_packfiles <out >actual &&
236 test_cmp expect actual
240 #############################################################################
241 # Chart of packs and objects for this test case
243 # | T A B C D E F G H I J K L M N O P Q R
244 # ----+--------------------------------------
245 # P1 | x x x x x x x x
246 # P2* | ! ! ! ! ! ! !
253 # ----+--------------------------------------
254 # ALL | x x x x x x x x x x x x x x x x x x x
256 #############################################################################
257 test_expect_success 'master: pack-8 (subset of pack-1) is also redundant' '
258 create_pack_in "$master_repo" P8 <<-EOF &&
263 cat >expect <<-EOF &&
269 git pack-redundant --all >out &&
270 format_packfiles <out >actual &&
271 test_cmp expect actual
275 test_expect_success 'master: clean loose objects' '
279 find objects -type f | sed -e "/objects\/pack\//d" >out &&
280 test_must_be_empty out
284 test_expect_success 'master: remove redundant packs and pass fsck' '
287 git pack-redundant --all | xargs rm &&
289 git pack-redundant --all >out &&
290 test_must_be_empty out
294 # The following test cases will execute inside `shared.git`, instead of
295 # inside `master.git`.
296 test_expect_success 'setup shared.git' '
297 git clone --mirror "$master_repo" "$shared_repo" &&
300 printf "../../$master_repo/objects\n" >objects/info/alternates
304 test_expect_success 'shared: all packs are redundant, but no output without --alt-odb' '
307 git pack-redundant --all >out &&
308 test_must_be_empty out
312 #############################################################################
313 # Chart of packs and objects for this test case
315 # ================ master.git ===============
316 # | T A B C D E F G H I J K L M N O P Q R <----------+
317 # ----+-------------------------------------- |
318 # P1 | x x x x x x x x |
322 # ----+-------------------------------------- |
323 # ALL | x x x x x x x x x x x x x x x x x x x |
326 # ================ shared.git =============== |
327 # | T A B C D E F G H I J K L M N O P Q R <objects/info/alternates>
328 # ----+--------------------------------------
329 # P1* | s s s s s s s s
333 # ----+--------------------------------------
334 # ALL | x x x x x x x x x x x x x x x x x x x
336 #############################################################################
337 test_expect_success 'shared: show redundant packs in stderr for verbose mode' '
340 cat >expect <<-EOF &&
346 git pack-redundant --all --verbose >out 2>out.err &&
347 test_must_be_empty out &&
348 grep "pack$" out.err | format_packfiles >actual &&
349 test_cmp expect actual
353 test_expect_success 'shared: remove redundant packs, no packs left' '
356 cat >expect <<-EOF &&
357 fatal: Zero packs found!
359 git pack-redundant --all --alt-odb | xargs rm &&
361 test_must_fail git pack-redundant --all --alt-odb >actual 2>&1 &&
362 test_cmp expect actual
366 test_expect_success 'shared: create new objects and packs' '
367 create_commits_in "$shared_repo" X Y Z &&
368 create_pack_in "$shared_repo" Px1 <<-EOF &&
376 create_pack_in "$shared_repo" Px2 <<-EOF
386 test_expect_success 'shared: no redundant without --alt-odb' '
389 git pack-redundant --all >out &&
390 test_must_be_empty out
394 #############################################################################
395 # Chart of packs and objects for this test case
397 # ================ master.git ===============
398 # | T A B C D E F G H I J K L M N O P Q R <----------------+
399 # ----+-------------------------------------- |
400 # P1 | x x x x x x x x |
404 # ----+-------------------------------------- |
405 # ALL | x x x x x x x x x x x x x x x x x x x |
408 # ================ shared.git ======================= |
409 # | 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>
410 # ----+----------------------------------------------
413 # ----+----------------------------------------------
414 # ALL | s s s s s s s s s s s s s s s s s s s x x x
416 #############################################################################
417 test_expect_success 'shared: one pack is redundant with --alt-odb' '
420 git pack-redundant --all --alt-odb >out &&
421 format_packfiles <out >actual &&
422 test_line_count = 1 actual
426 #############################################################################
427 # Chart of packs and objects for this test case
429 # ================ master.git ===============
430 # | T A B C D E F G H I J K L M N O P Q R <----------------+
431 # ----+-------------------------------------- |
432 # P1 | x x x x x x x x |
436 # ----+-------------------------------------- |
437 # ALL | x x x x x x x x x x x x x x x x x x x |
440 # ================ shared.git ======================= |
441 # | 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>
442 # ----+----------------------------------------------
445 # ----+----------------------------------------------
446 # ALL | s s s s s s s s s s s s s s s s s s s i i i
447 # (ignored objects, marked with i)
449 #############################################################################
450 test_expect_success 'shared: ignore unique objects and all two packs are redundant' '
453 cat >expect <<-EOF &&
457 git pack-redundant --all --alt-odb >out <<-EOF &&
462 format_packfiles <out >actual &&
463 test_cmp expect actual