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