config: unify code paths to get global config paths
[git] / t / t7700-repack.sh
1 #!/bin/sh
2
3 test_description='git repack works correctly'
4
5 . ./test-lib.sh
6
7 commit_and_pack () {
8         test_commit "$@" 1>&2 &&
9         incrpackid=$(git pack-objects --all --unpacked --incremental .git/objects/pack/pack </dev/null) &&
10         echo pack-${incrpackid}.pack
11 }
12
13 test_no_missing_in_packs () {
14         myidx=$(ls -1 .git/objects/pack/*.idx) &&
15         test_path_is_file "$myidx" &&
16         git verify-pack -v alt_objects/pack/*.idx >orig.raw &&
17         sed -n -e "s/^\($OID_REGEX\).*/\1/p" orig.raw | sort >orig &&
18         git verify-pack -v $myidx >dest.raw &&
19         cut -d" " -f1 dest.raw | sort >dest &&
20         comm -23 orig dest >missing &&
21         test_must_be_empty missing
22 }
23
24 # we expect $packid and $oid to be defined
25 test_has_duplicate_object () {
26         want_duplicate_object="$1"
27         found_duplicate_object=false
28         for p in .git/objects/pack/*.idx
29         do
30                 idx=$(basename $p)
31                 test "pack-$packid.idx" = "$idx" && continue
32                 git verify-pack -v $p >packlist || return $?
33                 if grep "^$oid" packlist
34                 then
35                         found_duplicate_object=true
36                         echo "DUPLICATE OBJECT FOUND"
37                         break
38                 fi
39         done &&
40         test "$want_duplicate_object" = "$found_duplicate_object"
41 }
42
43 test_expect_success 'objects in packs marked .keep are not repacked' '
44         echo content1 >file1 &&
45         echo content2 >file2 &&
46         git add . &&
47         test_tick &&
48         git commit -m initial_commit &&
49         # Create two packs
50         # The first pack will contain all of the objects except one
51         git rev-list --objects --all >objs &&
52         grep -v file2 objs | git pack-objects pack &&
53         # The second pack will contain the excluded object
54         packid=$(grep file2 objs | git pack-objects pack) &&
55         >pack-$packid.keep &&
56         git verify-pack -v pack-$packid.idx >packlist &&
57         oid=$(head -n 1 packlist | sed -e "s/^\($OID_REGEX\).*/\1/") &&
58         mv pack-* .git/objects/pack/ &&
59         git repack -A -d -l &&
60         git prune-packed &&
61         test_has_duplicate_object false
62 '
63
64 test_expect_success 'writing bitmaps via command-line can duplicate .keep objects' '
65         # build on $oid, $packid, and .keep state from previous
66         git repack -Adbl &&
67         test_has_duplicate_object true
68 '
69
70 test_expect_success 'writing bitmaps via config can duplicate .keep objects' '
71         # build on $oid, $packid, and .keep state from previous
72         git -c repack.writebitmaps=true repack -Adl &&
73         test_has_duplicate_object true
74 '
75
76 test_expect_success 'loose objects in alternate ODB are not repacked' '
77         mkdir alt_objects &&
78         echo $(pwd)/alt_objects >.git/objects/info/alternates &&
79         echo content3 >file3 &&
80         oid=$(GIT_OBJECT_DIRECTORY=alt_objects git hash-object -w file3) &&
81         git add file3 &&
82         test_tick &&
83         git commit -m commit_file3 &&
84         git repack -a -d -l &&
85         git prune-packed &&
86         test_has_duplicate_object false
87 '
88
89 test_expect_success 'packed obs in alt ODB are repacked even when local repo is packless' '
90         mkdir alt_objects/pack &&
91         mv .git/objects/pack/* alt_objects/pack &&
92         git repack -a &&
93         test_no_missing_in_packs
94 '
95
96 test_expect_success 'packed obs in alt ODB are repacked when local repo has packs' '
97         rm -f .git/objects/pack/* &&
98         echo new_content >>file1 &&
99         git add file1 &&
100         test_tick &&
101         git commit -m more_content &&
102         git repack &&
103         git repack -a -d &&
104         test_no_missing_in_packs
105 '
106
107 test_expect_success 'packed obs in alternate ODB kept pack are repacked' '
108         # swap the .keep so the commit object is in the pack with .keep
109         for p in alt_objects/pack/*.pack
110         do
111                 base_name=$(basename $p .pack) &&
112                 if test_path_is_file alt_objects/pack/$base_name.keep
113                 then
114                         rm alt_objects/pack/$base_name.keep
115                 else
116                         touch alt_objects/pack/$base_name.keep
117                 fi
118         done &&
119         git repack -a -d &&
120         test_no_missing_in_packs
121 '
122
123 test_expect_success 'packed unreachable obs in alternate ODB are not loosened' '
124         rm -f alt_objects/pack/*.keep &&
125         mv .git/objects/pack/* alt_objects/pack/ &&
126         coid=$(git rev-parse HEAD^{commit}) &&
127         git reset --hard HEAD^ &&
128         test_tick &&
129         git reflog expire --expire=$test_tick --expire-unreachable=$test_tick --all &&
130         # The pack-objects call on the next line is equivalent to
131         # git repack -A -d without the call to prune-packed
132         git pack-objects --honor-pack-keep --non-empty --all --reflog \
133             --unpack-unreachable </dev/null pack &&
134         rm -f .git/objects/pack/* &&
135         mv pack-* .git/objects/pack/ &&
136         git verify-pack -v -- .git/objects/pack/*.idx >packlist &&
137         ! grep "^$coid " packlist &&
138         echo >.git/objects/info/alternates &&
139         test_must_fail git show $coid
140 '
141
142 test_expect_success 'local packed unreachable obs that exist in alternate ODB are not loosened' '
143         echo $(pwd)/alt_objects >.git/objects/info/alternates &&
144         echo "$coid" | git pack-objects --non-empty --all --reflog pack &&
145         rm -f .git/objects/pack/* &&
146         mv pack-* .git/objects/pack/ &&
147         # The pack-objects call on the next line is equivalent to
148         # git repack -A -d without the call to prune-packed
149         git pack-objects --honor-pack-keep --non-empty --all --reflog \
150             --unpack-unreachable </dev/null pack &&
151         rm -f .git/objects/pack/* &&
152         mv pack-* .git/objects/pack/ &&
153         git verify-pack -v -- .git/objects/pack/*.idx >packlist &&
154         ! grep "^$coid " &&
155         echo >.git/objects/info/alternates &&
156         test_must_fail git show $coid
157 '
158
159 test_expect_success 'objects made unreachable by grafts only are kept' '
160         test_tick &&
161         git commit --allow-empty -m "commit 4" &&
162         H0=$(git rev-parse HEAD) &&
163         H1=$(git rev-parse HEAD^) &&
164         H2=$(git rev-parse HEAD^^) &&
165         echo "$H0 $H2" >.git/info/grafts &&
166         git reflog expire --expire=$test_tick --expire-unreachable=$test_tick --all &&
167         git repack -a -d &&
168         git cat-file -t $H1
169 '
170
171 test_expect_success 'repack --keep-pack' '
172         test_create_repo keep-pack &&
173         (
174                 cd keep-pack &&
175                 P1=$(commit_and_pack 1) &&
176                 P2=$(commit_and_pack 2) &&
177                 P3=$(commit_and_pack 3) &&
178                 P4=$(commit_and_pack 4) &&
179                 ls .git/objects/pack/*.pack >old-counts &&
180                 test_line_count = 4 old-counts &&
181                 git repack -a -d --keep-pack $P1 --keep-pack $P4 &&
182                 ls .git/objects/pack/*.pack >new-counts &&
183                 grep -q $P1 new-counts &&
184                 grep -q $P4 new-counts &&
185                 test_line_count = 3 new-counts &&
186                 git fsck
187         )
188 '
189
190 test_expect_success 'bitmaps are created by default in bare repos' '
191         git clone --bare .git bare.git &&
192         git -C bare.git repack -ad &&
193         bitmap=$(ls bare.git/objects/pack/*.bitmap) &&
194         test_path_is_file "$bitmap"
195 '
196
197 test_expect_success 'incremental repack does not complain' '
198         git -C bare.git repack -q 2>repack.err &&
199         test_must_be_empty repack.err
200 '
201
202 test_expect_success 'bitmaps can be disabled on bare repos' '
203         git -c repack.writeBitmaps=false -C bare.git repack -ad &&
204         bitmap=$(ls bare.git/objects/pack/*.bitmap || :) &&
205         test -z "$bitmap"
206 '
207
208 test_expect_success 'no bitmaps created if .keep files present' '
209         pack=$(ls bare.git/objects/pack/*.pack) &&
210         test_path_is_file "$pack" &&
211         keep=${pack%.pack}.keep &&
212         test_when_finished "rm -f \"\$keep\"" &&
213         >"$keep" &&
214         git -C bare.git repack -ad 2>stderr &&
215         test_must_be_empty stderr &&
216         find bare.git/objects/pack/ -type f -name "*.bitmap" >actual &&
217         test_must_be_empty actual
218 '
219
220 test_expect_success 'auto-bitmaps do not complain if unavailable' '
221         test_config -C bare.git pack.packSizeLimit 1M &&
222         blob=$(test-tool genrandom big $((1024*1024)) |
223                git -C bare.git hash-object -w --stdin) &&
224         git -C bare.git update-ref refs/tags/big $blob &&
225         git -C bare.git repack -ad 2>stderr &&
226         test_must_be_empty stderr &&
227         find bare.git/objects/pack -type f -name "*.bitmap" >actual &&
228         test_must_be_empty actual
229 '
230
231 test_done