Use the final_oid_fn to finalize hashing of object IDs
[alt-git.git] / t / t5304-prune.sh
blobb447ce56a9b2f4d92e2bea095e05a673fecb83a3
1 #!/bin/sh
3 # Copyright (c) 2008 Johannes E. Schindelin
6 test_description='prune'
7 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
8 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10 . ./test-lib.sh
12 day=$((60*60*24))
13 week=$(($day*7))
15 add_blob() {
16 before=$(git count-objects | sed "s/ .*//") &&
17 BLOB=$(echo aleph_0 | git hash-object -w --stdin) &&
18 BLOB_FILE=.git/objects/$(echo $BLOB | sed "s/^../&\//") &&
19 verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
20 test_path_is_file $BLOB_FILE &&
21 test-tool chmtime =+0 $BLOB_FILE
24 test_expect_success setup '
26 : > file &&
27 git add file &&
28 test_tick &&
29 git commit -m initial &&
30 git gc
34 test_expect_success 'prune stale packs' '
36 orig_pack=$(echo .git/objects/pack/*.pack) &&
37 : > .git/objects/tmp_1.pack &&
38 : > .git/objects/tmp_2.pack &&
39 test-tool chmtime =-86501 .git/objects/tmp_1.pack &&
40 git prune --expire 1.day &&
41 test_path_is_file $orig_pack &&
42 test_path_is_file .git/objects/tmp_2.pack &&
43 test_path_is_missing .git/objects/tmp_1.pack
47 test_expect_success 'prune --expire' '
49 add_blob &&
50 git prune --expire=1.hour.ago &&
51 verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
52 test_path_is_file $BLOB_FILE &&
53 test-tool chmtime =-86500 $BLOB_FILE &&
54 git prune --expire 1.day &&
55 verbose test $before = $(git count-objects | sed "s/ .*//") &&
56 test_path_is_missing $BLOB_FILE
60 test_expect_success 'gc: implicit prune --expire' '
62 add_blob &&
63 test-tool chmtime =-$((2*$week-30)) $BLOB_FILE &&
64 git gc &&
65 verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
66 test_path_is_file $BLOB_FILE &&
67 test-tool chmtime =-$((2*$week+1)) $BLOB_FILE &&
68 git gc &&
69 verbose test $before = $(git count-objects | sed "s/ .*//") &&
70 test_path_is_missing $BLOB_FILE
74 test_expect_success 'gc: refuse to start with invalid gc.pruneExpire' '
76 git config gc.pruneExpire invalid &&
77 test_must_fail git gc
81 test_expect_success 'gc: start with ok gc.pruneExpire' '
83 git config gc.pruneExpire 2.days.ago &&
84 git gc
88 test_expect_success 'prune: prune nonsense parameters' '
90 test_must_fail git prune garbage &&
91 test_must_fail git prune --- &&
92 test_must_fail git prune --no-such-option
96 test_expect_success 'prune: prune unreachable heads' '
98 git config core.logAllRefUpdates false &&
99 mv .git/logs .git/logs.old &&
100 : > file2 &&
101 git add file2 &&
102 git commit -m temporary &&
103 tmp_head=$(git rev-list -1 HEAD) &&
104 git reset HEAD^ &&
105 git prune &&
106 test_must_fail git reset $tmp_head --
110 test_expect_success 'prune: do not prune detached HEAD with no reflog' '
112 git checkout --detach --quiet &&
113 git commit --allow-empty -m "detached commit" &&
114 # verify that there is no reflogs
115 # (should be removed and disabled by previous test)
116 test_path_is_missing .git/logs &&
117 git prune -n >prune_actual &&
118 test_must_be_empty prune_actual
122 test_expect_success 'prune: prune former HEAD after checking out branch' '
124 head_oid=$(git rev-parse HEAD) &&
125 git checkout --quiet main &&
126 git prune -v >prune_actual &&
127 grep "$head_oid" prune_actual
131 test_expect_success 'prune: do not prune heads listed as an argument' '
133 : > file2 &&
134 git add file2 &&
135 git commit -m temporary &&
136 tmp_head=$(git rev-list -1 HEAD) &&
137 git reset HEAD^ &&
138 git prune -- $tmp_head &&
139 git reset $tmp_head --
143 test_expect_success 'gc --no-prune' '
145 add_blob &&
146 test-tool chmtime =-$((5001*$day)) $BLOB_FILE &&
147 git config gc.pruneExpire 2.days.ago &&
148 git gc --no-prune &&
149 verbose test 1 = $(git count-objects | sed "s/ .*//") &&
150 test_path_is_file $BLOB_FILE
154 test_expect_success 'gc respects gc.pruneExpire' '
156 git config gc.pruneExpire 5002.days.ago &&
157 git gc &&
158 test_path_is_file $BLOB_FILE &&
159 git config gc.pruneExpire 5000.days.ago &&
160 git gc &&
161 test_path_is_missing $BLOB_FILE
165 test_expect_success 'gc --prune=<date>' '
167 add_blob &&
168 test-tool chmtime =-$((5001*$day)) $BLOB_FILE &&
169 git gc --prune=5002.days.ago &&
170 test_path_is_file $BLOB_FILE &&
171 git gc --prune=5000.days.ago &&
172 test_path_is_missing $BLOB_FILE
176 test_expect_success 'gc --prune=never' '
178 add_blob &&
179 git gc --prune=never &&
180 test_path_is_file $BLOB_FILE &&
181 git gc --prune=now &&
182 test_path_is_missing $BLOB_FILE
186 test_expect_success 'gc respects gc.pruneExpire=never' '
188 git config gc.pruneExpire never &&
189 add_blob &&
190 git gc &&
191 test_path_is_file $BLOB_FILE &&
192 git config gc.pruneExpire now &&
193 git gc &&
194 test_path_is_missing $BLOB_FILE
198 test_expect_success 'prune --expire=never' '
200 add_blob &&
201 git prune --expire=never &&
202 test_path_is_file $BLOB_FILE &&
203 git prune &&
204 test_path_is_missing $BLOB_FILE
208 test_expect_success 'gc: prune old objects after local clone' '
209 add_blob &&
210 test-tool chmtime =-$((2*$week+1)) $BLOB_FILE &&
211 git clone --no-hardlinks . aclone &&
213 cd aclone &&
214 verbose test 1 = $(git count-objects | sed "s/ .*//") &&
215 test_path_is_file $BLOB_FILE &&
216 git gc --prune &&
217 verbose test 0 = $(git count-objects | sed "s/ .*//") &&
218 test_path_is_missing $BLOB_FILE
222 test_expect_success 'garbage report in count-objects -v' '
223 test_when_finished "rm -f .git/objects/pack/fake*" &&
224 test_when_finished "rm -f .git/objects/pack/foo*" &&
225 : >.git/objects/pack/foo &&
226 : >.git/objects/pack/foo.bar &&
227 : >.git/objects/pack/foo.keep &&
228 : >.git/objects/pack/foo.pack &&
229 : >.git/objects/pack/fake.bar &&
230 : >.git/objects/pack/fake.keep &&
231 : >.git/objects/pack/fake.pack &&
232 : >.git/objects/pack/fake.idx &&
233 : >.git/objects/pack/fake2.keep &&
234 : >.git/objects/pack/fake3.idx &&
235 git count-objects -v 2>stderr &&
236 grep "index file .git/objects/pack/fake.idx is too small" stderr &&
237 grep "^warning:" stderr | sort >actual &&
238 cat >expected <<\EOF &&
239 warning: garbage found: .git/objects/pack/fake.bar
240 warning: garbage found: .git/objects/pack/foo
241 warning: garbage found: .git/objects/pack/foo.bar
242 warning: no corresponding .idx or .pack: .git/objects/pack/fake2.keep
243 warning: no corresponding .idx: .git/objects/pack/foo.keep
244 warning: no corresponding .idx: .git/objects/pack/foo.pack
245 warning: no corresponding .pack: .git/objects/pack/fake3.idx
247 test_cmp expected actual
250 test_expect_success 'clean pack garbage with gc' '
251 test_when_finished "rm -f .git/objects/pack/fake*" &&
252 test_when_finished "rm -f .git/objects/pack/foo*" &&
253 : >.git/objects/pack/foo.keep &&
254 : >.git/objects/pack/foo.pack &&
255 : >.git/objects/pack/fake.idx &&
256 : >.git/objects/pack/fake2.keep &&
257 : >.git/objects/pack/fake2.idx &&
258 : >.git/objects/pack/fake3.keep &&
259 git gc &&
260 git count-objects -v 2>stderr &&
261 grep "^warning:" stderr | sort >actual &&
262 cat >expected <<\EOF &&
263 warning: no corresponding .idx or .pack: .git/objects/pack/fake3.keep
264 warning: no corresponding .idx: .git/objects/pack/foo.keep
265 warning: no corresponding .idx: .git/objects/pack/foo.pack
267 test_cmp expected actual
270 test_expect_success 'prune .git/shallow' '
271 oid=$(echo hi|git commit-tree HEAD^{tree}) &&
272 echo $oid >.git/shallow &&
273 git prune --dry-run >out &&
274 grep $oid .git/shallow &&
275 grep $oid out &&
276 git prune &&
277 test_path_is_missing .git/shallow
280 test_expect_success 'prune .git/shallow when there are no loose objects' '
281 oid=$(echo hi|git commit-tree HEAD^{tree}) &&
282 echo $oid >.git/shallow &&
283 git update-ref refs/heads/shallow-tip $oid &&
284 git repack -ad &&
285 # verify assumption that all loose objects are gone
286 git count-objects | grep ^0 &&
287 git prune &&
288 echo $oid >expect &&
289 test_cmp expect .git/shallow
292 test_expect_success 'prune: handle alternate object database' '
293 test_create_repo A &&
294 git -C A commit --allow-empty -m "initial commit" &&
295 git clone --shared A B &&
296 git -C B commit --allow-empty -m "next commit" &&
297 git -C B prune
300 test_expect_success 'prune: handle index in multiple worktrees' '
301 git worktree add second-worktree &&
302 echo "new blob for second-worktree" >second-worktree/blob &&
303 git -C second-worktree add blob &&
304 git prune --expire=now &&
305 git -C second-worktree show :blob >actual &&
306 test_cmp second-worktree/blob actual
309 test_expect_success 'prune: handle HEAD in multiple worktrees' '
310 git worktree add --detach third-worktree &&
311 echo "new blob for third-worktree" >third-worktree/blob &&
312 git -C third-worktree add blob &&
313 git -C third-worktree commit -m "third" &&
314 rm .git/worktrees/third-worktree/index &&
315 test_must_fail git -C third-worktree show :blob &&
316 git prune --expire=now &&
317 git -C third-worktree show HEAD:blob >actual &&
318 test_cmp third-worktree/blob actual
321 test_expect_success 'prune: handle HEAD reflog in multiple worktrees' '
322 git config core.logAllRefUpdates true &&
323 echo "lost blob for third-worktree" >expected &&
325 cd third-worktree &&
326 cat ../expected >blob &&
327 git add blob &&
328 git commit -m "second commit in third" &&
329 git reset --hard HEAD^
330 ) &&
331 git prune --expire=now &&
332 oid=`git hash-object expected` &&
333 git -C third-worktree show "$oid" >actual &&
334 test_cmp expected actual
337 test_expect_success 'prune: handle expire option correctly' '
338 test_must_fail git prune --expire 2>error &&
339 test_i18ngrep "requires a value" error &&
341 test_must_fail git prune --expire=nyah 2>error &&
342 test_i18ngrep "malformed expiration" error &&
344 git prune --no-expire
347 test_expect_success 'trivial prune with bitmaps enabled' '
348 git repack -adb &&
349 blob=$(echo bitmap-unreachable-blob | git hash-object -w --stdin) &&
350 git prune --expire=now &&
351 git cat-file -e HEAD &&
352 test_must_fail git cat-file -e $blob
355 test_done