]>
Commit | Line | Data |
---|---|---|
8464010f DST |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2008 Johannes E. Schindelin | |
4 | # | |
5 | ||
6 | test_description='prune' | |
7 | . ./test-lib.sh | |
8 | ||
98df233e CB |
9 | day=$((60*60*24)) |
10 | week=$(($day*7)) | |
11 | ||
12 | add_blob() { | |
13 | before=$(git count-objects | sed "s/ .*//") && | |
14 | BLOB=$(echo aleph_0 | git hash-object -w --stdin) && | |
15 | BLOB_FILE=.git/objects/$(echo $BLOB | sed "s/^../&\//") && | |
8ad16524 | 16 | verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") && |
f1dd90bd | 17 | test_path_is_file $BLOB_FILE && |
0e496492 | 18 | test-tool chmtime =+0 $BLOB_FILE |
98df233e CB |
19 | } |
20 | ||
8464010f DST |
21 | test_expect_success setup ' |
22 | ||
23 | : > file && | |
24 | git add file && | |
25 | test_tick && | |
26 | git commit -m initial && | |
27 | git gc | |
28 | ||
29 | ' | |
30 | ||
31 | test_expect_success 'prune stale packs' ' | |
32 | ||
33 | orig_pack=$(echo .git/objects/pack/*.pack) && | |
34 | : > .git/objects/tmp_1.pack && | |
35 | : > .git/objects/tmp_2.pack && | |
0e496492 | 36 | test-tool chmtime =-86501 .git/objects/tmp_1.pack && |
8464010f | 37 | git prune --expire 1.day && |
f1dd90bd JK |
38 | test_path_is_file $orig_pack && |
39 | test_path_is_file .git/objects/tmp_2.pack && | |
40 | test_path_is_missing .git/objects/tmp_1.pack | |
8464010f DST |
41 | |
42 | ' | |
43 | ||
25ee9731 JS |
44 | test_expect_success 'prune --expire' ' |
45 | ||
98df233e | 46 | add_blob && |
25ee9731 | 47 | git prune --expire=1.hour.ago && |
8ad16524 | 48 | verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") && |
f1dd90bd | 49 | test_path_is_file $BLOB_FILE && |
0e496492 | 50 | test-tool chmtime =-86500 $BLOB_FILE && |
25ee9731 | 51 | git prune --expire 1.day && |
8ad16524 | 52 | verbose test $before = $(git count-objects | sed "s/ .*//") && |
f1dd90bd | 53 | test_path_is_missing $BLOB_FILE |
25ee9731 JS |
54 | |
55 | ' | |
56 | ||
57 | test_expect_success 'gc: implicit prune --expire' ' | |
58 | ||
98df233e | 59 | add_blob && |
0e496492 | 60 | test-tool chmtime =-$((2*$week-30)) $BLOB_FILE && |
25ee9731 | 61 | git gc && |
8ad16524 | 62 | verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") && |
f1dd90bd | 63 | test_path_is_file $BLOB_FILE && |
0e496492 | 64 | test-tool chmtime =-$((2*$week+1)) $BLOB_FILE && |
25ee9731 | 65 | git gc && |
8ad16524 | 66 | verbose test $before = $(git count-objects | sed "s/ .*//") && |
f1dd90bd | 67 | test_path_is_missing $BLOB_FILE |
25ee9731 JS |
68 | |
69 | ' | |
70 | ||
71 | test_expect_success 'gc: refuse to start with invalid gc.pruneExpire' ' | |
72 | ||
73 | git config gc.pruneExpire invalid && | |
74 | test_must_fail git gc | |
75 | ||
76 | ' | |
77 | ||
78 | test_expect_success 'gc: start with ok gc.pruneExpire' ' | |
79 | ||
80 | git config gc.pruneExpire 2.days.ago && | |
81 | git gc | |
82 | ||
83 | ' | |
84 | ||
0c62705a MB |
85 | test_expect_success 'prune: prune nonsense parameters' ' |
86 | ||
87 | test_must_fail git prune garbage && | |
88 | test_must_fail git prune --- && | |
89 | test_must_fail git prune --no-such-option | |
90 | ||
91 | ' | |
92 | ||
93 | test_expect_success 'prune: prune unreachable heads' ' | |
94 | ||
95 | git config core.logAllRefUpdates false && | |
96 | mv .git/logs .git/logs.old && | |
97 | : > file2 && | |
98 | git add file2 && | |
99 | git commit -m temporary && | |
100 | tmp_head=$(git rev-list -1 HEAD) && | |
101 | git reset HEAD^ && | |
102 | git prune && | |
103 | test_must_fail git reset $tmp_head -- | |
104 | ||
105 | ' | |
106 | ||
c40fdd01 MK |
107 | test_expect_success 'prune: do not prune detached HEAD with no reflog' ' |
108 | ||
109 | git checkout --detach --quiet && | |
110 | git commit --allow-empty -m "detached commit" && | |
111 | # verify that there is no reflogs | |
112 | # (should be removed and disabled by previous test) | |
f1dd90bd | 113 | test_path_is_missing .git/logs && |
c40fdd01 | 114 | git prune -n >prune_actual && |
1c5e94f4 | 115 | test_must_be_empty prune_actual |
c40fdd01 MK |
116 | |
117 | ' | |
118 | ||
119 | test_expect_success 'prune: prune former HEAD after checking out branch' ' | |
120 | ||
cc80c95f | 121 | head_oid=$(git rev-parse HEAD) && |
c40fdd01 MK |
122 | git checkout --quiet master && |
123 | git prune -v >prune_actual && | |
cc80c95f | 124 | grep "$head_oid" prune_actual |
c40fdd01 MK |
125 | |
126 | ' | |
127 | ||
fe308f53 | 128 | test_expect_success 'prune: do not prune heads listed as an argument' ' |
0c62705a MB |
129 | |
130 | : > file2 && | |
131 | git add file2 && | |
132 | git commit -m temporary && | |
133 | tmp_head=$(git rev-list -1 HEAD) && | |
134 | git reset HEAD^ && | |
135 | git prune -- $tmp_head && | |
136 | git reset $tmp_head -- | |
137 | ||
138 | ' | |
139 | ||
58e9d9d4 JS |
140 | test_expect_success 'gc --no-prune' ' |
141 | ||
98df233e | 142 | add_blob && |
0e496492 | 143 | test-tool chmtime =-$((5001*$day)) $BLOB_FILE && |
58e9d9d4 JS |
144 | git config gc.pruneExpire 2.days.ago && |
145 | git gc --no-prune && | |
8ad16524 | 146 | verbose test 1 = $(git count-objects | sed "s/ .*//") && |
f1dd90bd | 147 | test_path_is_file $BLOB_FILE |
58e9d9d4 JS |
148 | |
149 | ' | |
150 | ||
151 | test_expect_success 'gc respects gc.pruneExpire' ' | |
152 | ||
153 | git config gc.pruneExpire 5002.days.ago && | |
154 | git gc && | |
f1dd90bd | 155 | test_path_is_file $BLOB_FILE && |
58e9d9d4 JS |
156 | git config gc.pruneExpire 5000.days.ago && |
157 | git gc && | |
f1dd90bd | 158 | test_path_is_missing $BLOB_FILE |
58e9d9d4 JS |
159 | |
160 | ' | |
161 | ||
162 | test_expect_success 'gc --prune=<date>' ' | |
163 | ||
98df233e | 164 | add_blob && |
0e496492 | 165 | test-tool chmtime =-$((5001*$day)) $BLOB_FILE && |
58e9d9d4 | 166 | git gc --prune=5002.days.ago && |
f1dd90bd | 167 | test_path_is_file $BLOB_FILE && |
58e9d9d4 | 168 | git gc --prune=5000.days.ago && |
f1dd90bd | 169 | test_path_is_missing $BLOB_FILE |
58e9d9d4 JS |
170 | |
171 | ' | |
172 | ||
cbf731ed AS |
173 | test_expect_success 'gc --prune=never' ' |
174 | ||
175 | add_blob && | |
176 | git gc --prune=never && | |
f1dd90bd | 177 | test_path_is_file $BLOB_FILE && |
cbf731ed | 178 | git gc --prune=now && |
f1dd90bd | 179 | test_path_is_missing $BLOB_FILE |
cbf731ed AS |
180 | |
181 | ' | |
182 | ||
183 | test_expect_success 'gc respects gc.pruneExpire=never' ' | |
184 | ||
185 | git config gc.pruneExpire never && | |
186 | add_blob && | |
187 | git gc && | |
f1dd90bd | 188 | test_path_is_file $BLOB_FILE && |
cbf731ed AS |
189 | git config gc.pruneExpire now && |
190 | git gc && | |
f1dd90bd | 191 | test_path_is_missing $BLOB_FILE |
cbf731ed AS |
192 | |
193 | ' | |
194 | ||
195 | test_expect_success 'prune --expire=never' ' | |
196 | ||
197 | add_blob && | |
198 | git prune --expire=never && | |
f1dd90bd | 199 | test_path_is_file $BLOB_FILE && |
cbf731ed | 200 | git prune && |
f1dd90bd | 201 | test_path_is_missing $BLOB_FILE |
cbf731ed AS |
202 | |
203 | ' | |
204 | ||
98df233e CB |
205 | test_expect_success 'gc: prune old objects after local clone' ' |
206 | add_blob && | |
0e496492 | 207 | test-tool chmtime =-$((2*$week+1)) $BLOB_FILE && |
98df233e CB |
208 | git clone --no-hardlinks . aclone && |
209 | ( | |
210 | cd aclone && | |
8ad16524 | 211 | verbose test 1 = $(git count-objects | sed "s/ .*//") && |
f1dd90bd | 212 | test_path_is_file $BLOB_FILE && |
98df233e | 213 | git gc --prune && |
8ad16524 | 214 | verbose test 0 = $(git count-objects | sed "s/ .*//") && |
f1dd90bd | 215 | test_path_is_missing $BLOB_FILE |
98df233e CB |
216 | ) |
217 | ' | |
218 | ||
543c5caa | 219 | test_expect_success 'garbage report in count-objects -v' ' |
f813e9ea | 220 | test_when_finished "rm -f .git/objects/pack/fake*" && |
e6d65c9a | 221 | test_when_finished "rm -f .git/objects/pack/foo*" && |
543c5caa NTND |
222 | : >.git/objects/pack/foo && |
223 | : >.git/objects/pack/foo.bar && | |
224 | : >.git/objects/pack/foo.keep && | |
225 | : >.git/objects/pack/foo.pack && | |
226 | : >.git/objects/pack/fake.bar && | |
227 | : >.git/objects/pack/fake.keep && | |
228 | : >.git/objects/pack/fake.pack && | |
229 | : >.git/objects/pack/fake.idx && | |
230 | : >.git/objects/pack/fake2.keep && | |
231 | : >.git/objects/pack/fake3.idx && | |
232 | git count-objects -v 2>stderr && | |
233 | grep "index file .git/objects/pack/fake.idx is too small" stderr && | |
234 | grep "^warning:" stderr | sort >actual && | |
235 | cat >expected <<\EOF && | |
236 | warning: garbage found: .git/objects/pack/fake.bar | |
237 | warning: garbage found: .git/objects/pack/foo | |
238 | warning: garbage found: .git/objects/pack/foo.bar | |
235e8d59 | 239 | warning: no corresponding .idx or .pack: .git/objects/pack/fake2.keep |
543c5caa NTND |
240 | warning: no corresponding .idx: .git/objects/pack/foo.keep |
241 | warning: no corresponding .idx: .git/objects/pack/foo.pack | |
242 | warning: no corresponding .pack: .git/objects/pack/fake3.idx | |
243 | EOF | |
244 | test_cmp expected actual | |
245 | ' | |
246 | ||
478f34d2 | 247 | test_expect_success 'clean pack garbage with gc' ' |
e6d65c9a DK |
248 | test_when_finished "rm -f .git/objects/pack/fake*" && |
249 | test_when_finished "rm -f .git/objects/pack/foo*" && | |
250 | : >.git/objects/pack/foo.keep && | |
251 | : >.git/objects/pack/foo.pack && | |
252 | : >.git/objects/pack/fake.idx && | |
253 | : >.git/objects/pack/fake2.keep && | |
254 | : >.git/objects/pack/fake2.idx && | |
255 | : >.git/objects/pack/fake3.keep && | |
256 | git gc && | |
257 | git count-objects -v 2>stderr && | |
258 | grep "^warning:" stderr | sort >actual && | |
259 | cat >expected <<\EOF && | |
260 | warning: no corresponding .idx or .pack: .git/objects/pack/fake3.keep | |
261 | warning: no corresponding .idx: .git/objects/pack/foo.keep | |
262 | warning: no corresponding .idx: .git/objects/pack/foo.pack | |
263 | EOF | |
264 | test_cmp expected actual | |
265 | ' | |
266 | ||
eab3296c | 267 | test_expect_success 'prune .git/shallow' ' |
cc80c95f JK |
268 | oid=$(echo hi|git commit-tree HEAD^{tree}) && |
269 | echo $oid >.git/shallow && | |
eab3296c | 270 | git prune --dry-run >out && |
cc80c95f JK |
271 | grep $oid .git/shallow && |
272 | grep $oid out && | |
eab3296c | 273 | git prune && |
f1dd90bd | 274 | test_path_is_missing .git/shallow |
eab3296c NTND |
275 | ' |
276 | ||
d55a30bb | 277 | test_expect_success 'prune .git/shallow when there are no loose objects' ' |
cc80c95f JK |
278 | oid=$(echo hi|git commit-tree HEAD^{tree}) && |
279 | echo $oid >.git/shallow && | |
280 | git update-ref refs/heads/shallow-tip $oid && | |
d55a30bb JK |
281 | git repack -ad && |
282 | # verify assumption that all loose objects are gone | |
283 | git count-objects | grep ^0 && | |
284 | git prune && | |
cc80c95f | 285 | echo $oid >expect && |
d55a30bb JK |
286 | test_cmp expect .git/shallow |
287 | ' | |
288 | ||
b0a42642 JM |
289 | test_expect_success 'prune: handle alternate object database' ' |
290 | test_create_repo A && | |
291 | git -C A commit --allow-empty -m "initial commit" && | |
292 | git clone --shared A B && | |
293 | git -C B commit --allow-empty -m "next commit" && | |
294 | git -C B prune | |
295 | ' | |
296 | ||
be489d02 NTND |
297 | test_expect_success 'prune: handle index in multiple worktrees' ' |
298 | git worktree add second-worktree && | |
299 | echo "new blob for second-worktree" >second-worktree/blob && | |
300 | git -C second-worktree add blob && | |
301 | git prune --expire=now && | |
302 | git -C second-worktree show :blob >actual && | |
303 | test_cmp second-worktree/blob actual | |
304 | ' | |
305 | ||
d0c39a49 NTND |
306 | test_expect_success 'prune: handle HEAD in multiple worktrees' ' |
307 | git worktree add --detach third-worktree && | |
308 | echo "new blob for third-worktree" >third-worktree/blob && | |
309 | git -C third-worktree add blob && | |
310 | git -C third-worktree commit -m "third" && | |
311 | rm .git/worktrees/third-worktree/index && | |
312 | test_must_fail git -C third-worktree show :blob && | |
313 | git prune --expire=now && | |
314 | git -C third-worktree show HEAD:blob >actual && | |
315 | test_cmp third-worktree/blob actual | |
316 | ' | |
317 | ||
acd9544a NTND |
318 | test_expect_success 'prune: handle HEAD reflog in multiple worktrees' ' |
319 | git config core.logAllRefUpdates true && | |
320 | echo "lost blob for third-worktree" >expected && | |
321 | ( | |
322 | cd third-worktree && | |
323 | cat ../expected >blob && | |
324 | git add blob && | |
325 | git commit -m "second commit in third" && | |
326 | git reset --hard HEAD^ | |
327 | ) && | |
328 | git prune --expire=now && | |
cc80c95f JK |
329 | oid=`git hash-object expected` && |
330 | git -C third-worktree show "$oid" >actual && | |
acd9544a NTND |
331 | test_cmp expected actual |
332 | ' | |
333 | ||
8ab5aa4b JH |
334 | test_expect_success 'prune: handle expire option correctly' ' |
335 | test_must_fail git prune --expire 2>error && | |
336 | test_i18ngrep "requires a value" error && | |
337 | ||
338 | test_must_fail git prune --expire=nyah 2>error && | |
339 | test_i18ngrep "malformed expiration" error && | |
340 | ||
341 | git prune --no-expire | |
342 | ' | |
343 | ||
fe6f2b08 JK |
344 | test_expect_success 'trivial prune with bitmaps enabled' ' |
345 | git repack -adb && | |
346 | blob=$(echo bitmap-unreachable-blob | git hash-object -w --stdin) && | |
347 | git prune --expire=now && | |
348 | git cat-file -e HEAD && | |
349 | test_must_fail git cat-file -e $blob | |
350 | ' | |
351 | ||
8464010f | 352 | test_done |