midx: write object offsets
[git] / t / t5319-multi-pack-index.sh
1 #!/bin/sh
2
3 test_description='multi-pack-indexes'
4 . ./test-lib.sh
5
6 midx_read_expect () {
7         NUM_PACKS=$1
8         NUM_OBJECTS=$2
9         NUM_CHUNKS=$3
10         OBJECT_DIR=$4
11         EXTRA_CHUNKS="$5"
12         {
13                 cat <<-EOF &&
14                 header: 4d494458 1 $NUM_CHUNKS $NUM_PACKS
15                 chunks: pack-names oid-fanout oid-lookup object-offsets$EXTRA_CHUNKS
16                 num_objects: $NUM_OBJECTS
17                 packs:
18                 EOF
19                 if test $NUM_PACKS -ge 1
20                 then
21                         ls $OBJECT_DIR/pack/ | grep idx | sort
22                 fi &&
23                 printf "object-dir: $OBJECT_DIR\n"
24         } >expect &&
25         test-tool read-midx $OBJECT_DIR >actual &&
26         test_cmp expect actual
27 }
28
29 test_expect_success 'write midx with no packs' '
30         test_when_finished rm -f pack/multi-pack-index &&
31         git multi-pack-index --object-dir=. write &&
32         midx_read_expect 0 0 4 .
33 '
34
35 generate_objects () {
36         i=$1
37         iii=$(printf '%03i' $i)
38         {
39                 test-tool genrandom "bar" 200 &&
40                 test-tool genrandom "baz $iii" 50
41         } >wide_delta_$iii &&
42         {
43                 test-tool genrandom "foo"$i 100 &&
44                 test-tool genrandom "foo"$(( $i + 1 )) 100 &&
45                 test-tool genrandom "foo"$(( $i + 2 )) 100
46         } >deep_delta_$iii &&
47         {
48                 echo $iii &&
49                 test-tool genrandom "$iii" 8192
50         } >file_$iii &&
51         git update-index --add file_$iii deep_delta_$iii wide_delta_$iii
52 }
53
54 commit_and_list_objects () {
55         {
56                 echo 101 &&
57                 test-tool genrandom 100 8192;
58         } >file_101 &&
59         git update-index --add file_101 &&
60         tree=$(git write-tree) &&
61         commit=$(git commit-tree $tree -p HEAD</dev/null) &&
62         {
63                 echo $tree &&
64                 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\)        .*/\\1/"
65         } >obj-list &&
66         git reset --hard $commit
67 }
68
69 test_expect_success 'create objects' '
70         test_commit initial &&
71         for i in $(test_seq 1 5)
72         do
73                 generate_objects $i
74         done &&
75         commit_and_list_objects
76 '
77
78 test_expect_success 'write midx with one v1 pack' '
79         pack=$(git pack-objects --index-version=1 pack/test <obj-list) &&
80         test_when_finished rm pack/test-$pack.pack pack/test-$pack.idx pack/multi-pack-index &&
81         git multi-pack-index --object-dir=. write &&
82         midx_read_expect 1 18 4 .
83 '
84
85 test_expect_success 'write midx with one v2 pack' '
86         git pack-objects --index-version=2,0x40 pack/test <obj-list &&
87         git multi-pack-index --object-dir=. write &&
88         midx_read_expect 1 18 4 .
89 '
90
91 test_expect_success 'add more objects' '
92         for i in $(test_seq 6 10)
93         do
94                 generate_objects $i
95         done &&
96         commit_and_list_objects
97 '
98
99 test_expect_success 'write midx with two packs' '
100         git pack-objects --index-version=1 pack/test-2 <obj-list &&
101         git multi-pack-index --object-dir=. write &&
102         midx_read_expect 2 34 4 .
103 '
104
105 test_expect_success 'add more packs' '
106         for j in $(test_seq 11 20)
107         do
108                 generate_objects $j &&
109                 commit_and_list_objects &&
110                 git pack-objects --index-version=2 pack/test-pack <obj-list
111         done
112 '
113
114 test_expect_success 'write midx with twelve packs' '
115         git multi-pack-index --object-dir=. write &&
116         midx_read_expect 12 74 4 .
117 '
118
119 # usage: corrupt_data <file> <pos> [<data>]
120 corrupt_data () {
121         file=$1
122         pos=$2
123         data="${3:-\0}"
124         printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
125 }
126
127 # Force 64-bit offsets by manipulating the idx file.
128 # This makes the IDX file _incorrect_ so be careful to clean up after!
129 test_expect_success 'force some 64-bit offsets with pack-objects' '
130         mkdir objects64 &&
131         mkdir objects64/pack &&
132         for i in $(test_seq 1 11)
133         do
134                 generate_objects 11
135         done &&
136         commit_and_list_objects &&
137         pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
138         idx64=objects64/pack/test-64-$pack64.idx &&
139         chmod u+w $idx64 &&
140         corrupt_data $idx64 2999 "\02" &&
141         midx64=$(git multi-pack-index --object-dir=objects64 write) &&
142         midx_read_expect 1 63 5 objects64 " large-offsets"
143 '
144
145 test_done