Raw
1 #!/bin/sh
2
3 test_description='handling of duplicate objects in incoming packfiles'
4
5 . ./test-lib.sh
6 . "$TEST_DIRECTORY"/lib-pack.sh
7
8 test_expect_success 'setup' '
9 test_oid_cache <<-EOF
10 lo_oid sha1:e68fe8129b546b101aee9510c5328e7f21ca1d18
11 lo_oid sha256:471819e8c52bf11513f100b2810a8aa0622d5cd3d1c913758a071dd4b3bad8fe
12
13 missing_oid sha1:e69d000000000000000000000000000000000000
14 missing_oid sha256:4720000000000000000000000000000000000000000000000000000000000000
15 EOF
16 '
17
18 # The sha1s we have in our pack. It's important that these have the same
19 # starting byte, so that they end up in the same fanout section of the index.
20 # That lets us make sure we are exercising the binary search with both sets.
21 LO_SHA1=$(test_oid lo_oid)
22 HI_SHA1=$EMPTY_BLOB
23
24 # And here's a "missing sha1" which will produce failed lookups. It must also
25 # be in the same fanout section, and should be between the two (so that during
26 # our binary search, we are sure to end up looking at one or the other of the
27 # duplicate runs).
28 MISSING_SHA1=$(test_oid missing_oid)
29
30 # Three distinct objects for tests where physical pack order matters.
31 A=$(test_oid packlib_7_0)
32 B=$LO_SHA1
33 C=$HI_SHA1
34
35 # git will never intentionally create packfiles with
36 # duplicate objects, so we have to construct them by hand.
37 #
38 # $1 is the name of the packfile to create
39 #
40 # $2 is the number of times to duplicate each object
41 create_pack () {
42 pack_header "$((2 * $2))" >"$1" &&
43 for i in $(test_seq 1 "$2"); do
44 pack_obj $LO_SHA1 &&
45 pack_obj $HI_SHA1
46 done >>"$1" &&
47 pack_trailer "$1"
48 }
49
50 # double-check that create_pack actually works
51 test_expect_success 'pack with no duplicates' '
52 create_pack no-dups.pack 1 &&
53 git index-pack --stdin <no-dups.pack
54 '
55
56 test_expect_success 'index-pack will allow duplicate objects by default' '
57 clear_packs &&
58 create_pack dups.pack 100 &&
59 git index-pack --stdin <dups.pack
60 '
61
62 test_expect_success 'bitmap writer rejects duplicate objects' '
63 pack=$(ls .git/objects/pack/pack-*.pack) &&
64 test_must_fail test-tool bitmap write "$(basename "$pack")" \
65 </dev/null 2>err &&
66 test_grep "fatal: pack contains duplicate object" err
67 '
68
69 test_expect_success 'create batch-check test vectors' '
70 cat >input <<-EOF &&
71 $LO_SHA1
72 $HI_SHA1
73 $MISSING_SHA1
74 EOF
75 cat >expect <<-EOF
76 $LO_SHA1 blob 2
77 $HI_SHA1 blob 0
78 $MISSING_SHA1 missing
79 EOF
80 '
81
82 test_expect_success 'lookup in duplicated pack' '
83 git cat-file --batch-check <input >actual &&
84 test_cmp expect actual
85 '
86
87 test_expect_success 'verify MIDX containing duplicated pack objects' '
88 git multi-pack-index write &&
89 test-tool read-midx --show-objects .git/objects >midx-objects &&
90 midx_offset=$(
91 awk -v oid="$LO_SHA1" "\$1 == oid { print \$2 }" <midx-objects
92 ) &&
93 lookup_offset=$(
94 test-tool find-pack --check-count=1 --show-offset "$LO_SHA1"
95 ) &&
96 test "$midx_offset" -ne "$lookup_offset" &&
97 git multi-pack-index verify &&
98 git fsck --full
99 '
100
101 test_expect_success 'duplicate entries remain in pack reverse index' '
102 clear_packs &&
103 {
104 pack_header 4 &&
105 pack_obj $A &&
106 pack_obj $B &&
107 pack_obj $A &&
108 pack_obj $C
109 } >physical-order.pack &&
110 pack_trailer physical-order.pack &&
111
112 test_must_fail git index-pack --rev-index --stdin --strict \
113 <physical-order.pack 2>err &&
114 test_grep "appears twice in the pack" err &&
115
116 git index-pack --rev-index --stdin <physical-order.pack &&
117 git show-index <"$(ls .git/objects/pack/pack-*.idx)" >offsets.raw &&
118
119 sort -n offsets.raw | grep -A1 "$B" | cut -d" " -f1 >adjacent &&
120 echo $(($(tail -n1 adjacent) - $(head -n1 adjacent))) >expect &&
121 echo "$B" >in &&
122
123 GIT_TEST_REV_INDEX_DIE_IN_MEMORY=1 \
124 git cat-file --batch-check="%(objectsize:disk)" \
125 <in >actual.disk &&
126 GIT_TEST_REV_INDEX_DIE_ON_DISK=1 \
127 git -c pack.readReverseIndex=false \
128 cat-file --batch-check="%(objectsize:disk)" \
129 <in >actual.mem &&
130
131 test_cmp expect actual.disk &&
132 test_cmp expect actual.mem
133 '
134
135 test_expect_success 'index-pack can reject packs with duplicates' '
136 clear_packs &&
137 create_pack dups.pack 2 &&
138 test_must_fail git index-pack --strict --stdin <dups.pack &&
139 test_expect_code 1 git cat-file -e $LO_SHA1
140 '
141
142 test_done