Git 2.45
[git/gitster.git] / t / t5308-pack-detect-duplicates.sh
blob655cafa054121130945056643f2fb05a165f3ef1
1 #!/bin/sh
3 test_description='handling of duplicate objects in incoming packfiles'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7 . "$TEST_DIRECTORY"/lib-pack.sh
9 test_expect_success 'setup' '
10 test_oid_cache <<-EOF
11 lo_oid sha1:e68fe8129b546b101aee9510c5328e7f21ca1d18
12 lo_oid sha256:471819e8c52bf11513f100b2810a8aa0622d5cd3d1c913758a071dd4b3bad8fe
14 missing_oid sha1:e69d000000000000000000000000000000000000
15 missing_oid sha256:4720000000000000000000000000000000000000000000000000000000000000
16 EOF
19 # The sha1s we have in our pack. It's important that these have the same
20 # starting byte, so that they end up in the same fanout section of the index.
21 # That lets us make sure we are exercising the binary search with both sets.
22 LO_SHA1=$(test_oid lo_oid)
23 HI_SHA1=$EMPTY_BLOB
25 # And here's a "missing sha1" which will produce failed lookups. It must also
26 # be in the same fanout section, and should be between the two (so that during
27 # our binary search, we are sure to end up looking at one or the other of the
28 # duplicate runs).
29 MISSING_SHA1=$(test_oid missing_oid)
31 # git will never intentionally create packfiles with
32 # duplicate objects, so we have to construct them by hand.
34 # $1 is the name of the packfile to create
36 # $2 is the number of times to duplicate each object
37 create_pack () {
38 pack_header "$((2 * $2))" >"$1" &&
39 for i in $(test_seq 1 "$2"); do
40 pack_obj $LO_SHA1 &&
41 pack_obj $HI_SHA1
42 done >>"$1" &&
43 pack_trailer "$1"
46 # double-check that create_pack actually works
47 test_expect_success 'pack with no duplicates' '
48 create_pack no-dups.pack 1 &&
49 git index-pack --stdin <no-dups.pack
52 test_expect_success 'index-pack will allow duplicate objects by default' '
53 clear_packs &&
54 create_pack dups.pack 100 &&
55 git index-pack --stdin <dups.pack
58 test_expect_success 'create batch-check test vectors' '
59 cat >input <<-EOF &&
60 $LO_SHA1
61 $HI_SHA1
62 $MISSING_SHA1
63 EOF
64 cat >expect <<-EOF
65 $LO_SHA1 blob 2
66 $HI_SHA1 blob 0
67 $MISSING_SHA1 missing
68 EOF
71 test_expect_success 'lookup in duplicated pack' '
72 git cat-file --batch-check <input >actual &&
73 test_cmp expect actual
76 test_expect_success 'index-pack can reject packs with duplicates' '
77 clear_packs &&
78 create_pack dups.pack 2 &&
79 test_must_fail git index-pack --strict --stdin <dups.pack &&
80 test_expect_code 1 git cat-file -e $LO_SHA1
83 test_done