pack-objects: consider packs in multi-pack-index
[git/git.git] / t / t5319-multi-pack-index.sh
CommitLineData
a3407730
DS
1#!/bin/sh
2
3test_description='multi-pack-indexes'
4. ./test-lib.sh
5
c4d25228
DS
6objdir=.git/objects
7
4d80560c 8midx_read_expect () {
396f2570 9 NUM_PACKS=$1
d7cacf29 10 NUM_OBJECTS=$2
662148c4
DS
11 NUM_CHUNKS=$3
12 OBJECT_DIR=$4
13 EXTRA_CHUNKS="$5"
3227565c
DS
14 {
15 cat <<-EOF &&
662148c4
DS
16 header: 4d494458 1 $NUM_CHUNKS $NUM_PACKS
17 chunks: pack-names oid-fanout oid-lookup object-offsets$EXTRA_CHUNKS
d7cacf29 18 num_objects: $NUM_OBJECTS
3227565c
DS
19 packs:
20 EOF
21 if test $NUM_PACKS -ge 1
22 then
662148c4 23 ls $OBJECT_DIR/pack/ | grep idx | sort
3227565c 24 fi &&
662148c4 25 printf "object-dir: $OBJECT_DIR\n"
3227565c 26 } >expect &&
662148c4 27 test-tool read-midx $OBJECT_DIR >actual &&
4d80560c
DS
28 test_cmp expect actual
29}
30
a3407730 31test_expect_success 'write midx with no packs' '
fc59e748
DS
32 test_when_finished rm -f pack/multi-pack-index &&
33 git multi-pack-index --object-dir=. write &&
662148c4 34 midx_read_expect 0 0 4 .
a3407730
DS
35'
36
2c381335
DS
37generate_objects () {
38 i=$1
39 iii=$(printf '%03i' $i)
40 {
41 test-tool genrandom "bar" 200 &&
42 test-tool genrandom "baz $iii" 50
43 } >wide_delta_$iii &&
44 {
45 test-tool genrandom "foo"$i 100 &&
46 test-tool genrandom "foo"$(( $i + 1 )) 100 &&
47 test-tool genrandom "foo"$(( $i + 2 )) 100
48 } >deep_delta_$iii &&
49 {
50 echo $iii &&
51 test-tool genrandom "$iii" 8192
52 } >file_$iii &&
53 git update-index --add file_$iii deep_delta_$iii wide_delta_$iii
54}
55
56commit_and_list_objects () {
57 {
58 echo 101 &&
59 test-tool genrandom 100 8192;
60 } >file_101 &&
61 git update-index --add file_101 &&
62 tree=$(git write-tree) &&
63 commit=$(git commit-tree $tree -p HEAD</dev/null) &&
64 {
65 echo $tree &&
66 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
67 } >obj-list &&
68 git reset --hard $commit
69}
70
71test_expect_success 'create objects' '
72 test_commit initial &&
73 for i in $(test_seq 1 5)
74 do
75 generate_objects $i
76 done &&
77 commit_and_list_objects
78'
79
80test_expect_success 'write midx with one v1 pack' '
c4d25228
DS
81 pack=$(git pack-objects --index-version=1 $objdir/pack/test <obj-list) &&
82 test_when_finished rm $objdir/pack/test-$pack.pack \
83 $objdir/pack/test-$pack.idx $objdir/pack/multi-pack-index &&
84 git multi-pack-index --object-dir=$objdir write &&
85 midx_read_expect 1 18 4 $objdir
2c381335
DS
86'
87
c4d25228 88midx_git_two_modes () {
e9ab2ed7
DS
89 if [ "$2" = "sorted" ]
90 then
91 git -c core.multiPackIndex=false $1 | sort >expect &&
92 git -c core.multiPackIndex=true $1 | sort >actual
93 else
94 git -c core.multiPackIndex=false $1 >expect &&
95 git -c core.multiPackIndex=true $1 >actual
96 fi &&
c4d25228
DS
97 test_cmp expect actual
98}
99
100compare_results_with_midx () {
101 MSG=$1
102 test_expect_success "check normal git operations: $MSG" '
103 midx_git_two_modes "rev-list --objects --all" &&
e9ab2ed7
DS
104 midx_git_two_modes "log --raw" &&
105 midx_git_two_modes "count-objects --verbose" &&
106 midx_git_two_modes "cat-file --batch-all-objects --buffer --batch-check" &&
107 midx_git_two_modes "cat-file --batch-all-objects --buffer --batch-check --unsorted" sorted
c4d25228
DS
108 '
109}
110
2c381335 111test_expect_success 'write midx with one v2 pack' '
c4d25228
DS
112 git pack-objects --index-version=2,0x40 $objdir/pack/test <obj-list &&
113 git multi-pack-index --object-dir=$objdir write &&
114 midx_read_expect 1 18 4 $objdir
2c381335
DS
115'
116
c4d25228
DS
117compare_results_with_midx "one v2 pack"
118
2c381335
DS
119test_expect_success 'add more objects' '
120 for i in $(test_seq 6 10)
121 do
122 generate_objects $i
123 done &&
124 commit_and_list_objects
125'
126
127test_expect_success 'write midx with two packs' '
c4d25228
DS
128 git pack-objects --index-version=1 $objdir/pack/test-2 <obj-list &&
129 git multi-pack-index --object-dir=$objdir write &&
130 midx_read_expect 2 34 4 $objdir
2c381335
DS
131'
132
c4d25228
DS
133compare_results_with_midx "two packs"
134
2c381335
DS
135test_expect_success 'add more packs' '
136 for j in $(test_seq 11 20)
137 do
138 generate_objects $j &&
139 commit_and_list_objects &&
c4d25228 140 git pack-objects --index-version=2 $objdir/pack/test-pack <obj-list
2c381335
DS
141 done
142'
143
c4d25228
DS
144compare_results_with_midx "mixed mode (two packs + extra)"
145
2c381335 146test_expect_success 'write midx with twelve packs' '
c4d25228
DS
147 git multi-pack-index --object-dir=$objdir write &&
148 midx_read_expect 12 74 4 $objdir
662148c4
DS
149'
150
c4d25228
DS
151compare_results_with_midx "twelve packs"
152
525e18c0
DS
153test_expect_success 'repack removes multi-pack-index' '
154 test_path_is_file $objdir/pack/multi-pack-index &&
155 git repack -adf &&
156 test_path_is_missing $objdir/pack/multi-pack-index
157'
158
159compare_results_with_midx "after repack"
160
e9ab2ed7
DS
161test_expect_success 'multi-pack-index and pack-bitmap' '
162 git -c repack.writeBitmaps=true repack -ad &&
163 git multi-pack-index write &&
164 git rev-list --test-bitmap HEAD
165'
166
29e2016b
DS
167test_expect_success 'multi-pack-index and alternates' '
168 git init --bare alt.git &&
169 echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
170 echo content1 >file1 &&
171 altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
172 git cat-file blob $altblob &&
173 git rev-list --all
174'
175
176compare_results_with_midx "with alternate (local midx)"
177
178test_expect_success 'multi-pack-index in an alternate' '
6a22d521
DS
179 mv .git/objects/pack/* alt.git/objects/pack &&
180 test_commit add_local_objects &&
181 git repack --local &&
182 git multi-pack-index write &&
183 midx_read_expect 1 3 4 $objdir &&
184 git reset --hard HEAD~1 &&
185 rm -f .git/objects/pack/*
29e2016b
DS
186'
187
188compare_results_with_midx "with alternate (remote midx)"
189
525e18c0 190
662148c4
DS
191# usage: corrupt_data <file> <pos> [<data>]
192corrupt_data () {
193 file=$1
194 pos=$2
195 data="${3:-\0}"
196 printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
197}
198
199# Force 64-bit offsets by manipulating the idx file.
200# This makes the IDX file _incorrect_ so be careful to clean up after!
201test_expect_success 'force some 64-bit offsets with pack-objects' '
202 mkdir objects64 &&
203 mkdir objects64/pack &&
204 for i in $(test_seq 1 11)
205 do
206 generate_objects 11
207 done &&
208 commit_and_list_objects &&
209 pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
210 idx64=objects64/pack/test-64-$pack64.idx &&
211 chmod u+w $idx64 &&
212 corrupt_data $idx64 2999 "\02" &&
213 midx64=$(git multi-pack-index --object-dir=objects64 write) &&
214 midx_read_expect 1 63 5 objects64 " large-offsets"
2c381335
DS
215'
216
a3407730 217test_done