blob: 8ae314af585482ec240fed353613faf6b23ebb67 [file] [log] [blame]
David Steven Tweed84640102008-02-07 02:55:14 +00001#!/bin/sh
2#
3# Copyright (c) 2008 Johannes E. Schindelin
4#
5
6test_description='prune'
Johannes Schindelin966b4be2020-11-18 23:44:29 +00007GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +00008export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
9
David Steven Tweed84640102008-02-07 02:55:14 +000010. ./test-lib.sh
11
Clemens Buchacher98df2332009-09-13 12:49:45 +020012day=$((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/^../&\//") &&
Jeff King8ad16522014-10-10 02:11:14 -040019 verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
Jeff Kingf1dd90b2014-10-10 02:07:00 -040020 test_path_is_file $BLOB_FILE &&
Nguyễn Thái Ngọc Duy0e496492018-03-24 08:44:31 +010021 test-tool chmtime =+0 $BLOB_FILE
Clemens Buchacher98df2332009-09-13 12:49:45 +020022}
23
David Steven Tweed84640102008-02-07 02:55:14 +000024test_expect_success setup '
Han-Wen Nienhuys1fa9cf62021-05-31 16:56:27 +000025 >file &&
David Steven Tweed84640102008-02-07 02:55:14 +000026 git add file &&
27 test_tick &&
28 git commit -m initial &&
29 git gc
David Steven Tweed84640102008-02-07 02:55:14 +000030'
31
32test_expect_success 'prune stale packs' '
David Steven Tweed84640102008-02-07 02:55:14 +000033 orig_pack=$(echo .git/objects/pack/*.pack) &&
Han-Wen Nienhuys1fa9cf62021-05-31 16:56:27 +000034 >.git/objects/tmp_1.pack &&
35 >.git/objects/tmp_2.pack &&
Nguyễn Thái Ngọc Duy0e496492018-03-24 08:44:31 +010036 test-tool chmtime =-86501 .git/objects/tmp_1.pack &&
David Steven Tweed84640102008-02-07 02:55:14 +000037 git prune --expire 1.day &&
Jeff Kingf1dd90b2014-10-10 02:07:00 -040038 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
David Steven Tweed84640102008-02-07 02:55:14 +000041'
42
Johannes Schindelin25ee9732008-03-12 21:55:47 +010043test_expect_success 'prune --expire' '
Clemens Buchacher98df2332009-09-13 12:49:45 +020044 add_blob &&
Johannes Schindelin25ee9732008-03-12 21:55:47 +010045 git prune --expire=1.hour.ago &&
Jeff King8ad16522014-10-10 02:11:14 -040046 verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
Jeff Kingf1dd90b2014-10-10 02:07:00 -040047 test_path_is_file $BLOB_FILE &&
Nguyễn Thái Ngọc Duy0e496492018-03-24 08:44:31 +010048 test-tool chmtime =-86500 $BLOB_FILE &&
Johannes Schindelin25ee9732008-03-12 21:55:47 +010049 git prune --expire 1.day &&
Jeff King8ad16522014-10-10 02:11:14 -040050 verbose test $before = $(git count-objects | sed "s/ .*//") &&
Jeff Kingf1dd90b2014-10-10 02:07:00 -040051 test_path_is_missing $BLOB_FILE
Johannes Schindelin25ee9732008-03-12 21:55:47 +010052'
53
54test_expect_success 'gc: implicit prune --expire' '
Clemens Buchacher98df2332009-09-13 12:49:45 +020055 add_blob &&
Nguyễn Thái Ngọc Duy0e496492018-03-24 08:44:31 +010056 test-tool chmtime =-$((2*$week-30)) $BLOB_FILE &&
Johannes Schindelin25ee9732008-03-12 21:55:47 +010057 git gc &&
Jeff King8ad16522014-10-10 02:11:14 -040058 verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
Jeff Kingf1dd90b2014-10-10 02:07:00 -040059 test_path_is_file $BLOB_FILE &&
Nguyễn Thái Ngọc Duy0e496492018-03-24 08:44:31 +010060 test-tool chmtime =-$((2*$week+1)) $BLOB_FILE &&
Johannes Schindelin25ee9732008-03-12 21:55:47 +010061 git gc &&
Jeff King8ad16522014-10-10 02:11:14 -040062 verbose test $before = $(git count-objects | sed "s/ .*//") &&
Jeff Kingf1dd90b2014-10-10 02:07:00 -040063 test_path_is_missing $BLOB_FILE
Johannes Schindelin25ee9732008-03-12 21:55:47 +010064'
65
66test_expect_success 'gc: refuse to start with invalid gc.pruneExpire' '
Johannes Schindelin25ee9732008-03-12 21:55:47 +010067 git config gc.pruneExpire invalid &&
68 test_must_fail git gc
Johannes Schindelin25ee9732008-03-12 21:55:47 +010069'
70
71test_expect_success 'gc: start with ok gc.pruneExpire' '
Johannes Schindelin25ee9732008-03-12 21:55:47 +010072 git config gc.pruneExpire 2.days.ago &&
73 git gc
Johannes Schindelin25ee9732008-03-12 21:55:47 +010074'
75
Michele Ballabio0c627052008-03-23 22:34:34 +010076test_expect_success 'prune: prune nonsense parameters' '
Michele Ballabio0c627052008-03-23 22:34:34 +010077 test_must_fail git prune garbage &&
78 test_must_fail git prune --- &&
79 test_must_fail git prune --no-such-option
Michele Ballabio0c627052008-03-23 22:34:34 +010080'
81
82test_expect_success 'prune: prune unreachable heads' '
Michele Ballabio0c627052008-03-23 22:34:34 +010083 git config core.logAllRefUpdates false &&
Han-Wen Nienhuysd491f5e2021-05-31 16:56:28 +000084 >file2 &&
Michele Ballabio0c627052008-03-23 22:34:34 +010085 git add file2 &&
86 git commit -m temporary &&
87 tmp_head=$(git rev-list -1 HEAD) &&
88 git reset HEAD^ &&
Han-Wen Nienhuysd491f5e2021-05-31 16:56:28 +000089 git reflog expire --all &&
Michele Ballabio0c627052008-03-23 22:34:34 +010090 git prune &&
91 test_must_fail git reset $tmp_head --
Michele Ballabio0c627052008-03-23 22:34:34 +010092'
93
Max Kirillovc40fdd02014-09-03 19:14:10 +030094test_expect_success 'prune: do not prune detached HEAD with no reflog' '
Max Kirillovc40fdd02014-09-03 19:14:10 +030095 git checkout --detach --quiet &&
96 git commit --allow-empty -m "detached commit" &&
Han-Wen Nienhuysd491f5e2021-05-31 16:56:28 +000097 git reflog expire --all &&
Max Kirillovc40fdd02014-09-03 19:14:10 +030098 git prune -n >prune_actual &&
SZEDER Gábor1c5e94f2018-08-19 23:57:25 +020099 test_must_be_empty prune_actual
Max Kirillovc40fdd02014-09-03 19:14:10 +0300100'
101
102test_expect_success 'prune: prune former HEAD after checking out branch' '
Jeff Kingcc80c952019-02-14 06:07:36 -0500103 head_oid=$(git rev-parse HEAD) &&
Johannes Schindelin966b4be2020-11-18 23:44:29 +0000104 git checkout --quiet main &&
Han-Wen Nienhuysd491f5e2021-05-31 16:56:28 +0000105 git reflog expire --all &&
Max Kirillovc40fdd02014-09-03 19:14:10 +0300106 git prune -v >prune_actual &&
Jeff Kingcc80c952019-02-14 06:07:36 -0500107 grep "$head_oid" prune_actual
Max Kirillovc40fdd02014-09-03 19:14:10 +0300108'
109
Junio C Hamanofe308f52008-03-24 23:20:51 -0700110test_expect_success 'prune: do not prune heads listed as an argument' '
Han-Wen Nienhuys1fa9cf62021-05-31 16:56:27 +0000111 >file2 &&
Michele Ballabio0c627052008-03-23 22:34:34 +0100112 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 --
Michele Ballabio0c627052008-03-23 22:34:34 +0100118'
119
Johannes Schindelin58e9d9d2009-02-14 23:10:10 +0100120test_expect_success 'gc --no-prune' '
Clemens Buchacher98df2332009-09-13 12:49:45 +0200121 add_blob &&
Nguyễn Thái Ngọc Duy0e496492018-03-24 08:44:31 +0100122 test-tool chmtime =-$((5001*$day)) $BLOB_FILE &&
Johannes Schindelin58e9d9d2009-02-14 23:10:10 +0100123 git config gc.pruneExpire 2.days.ago &&
124 git gc --no-prune &&
Jeff King8ad16522014-10-10 02:11:14 -0400125 verbose test 1 = $(git count-objects | sed "s/ .*//") &&
Jeff Kingf1dd90b2014-10-10 02:07:00 -0400126 test_path_is_file $BLOB_FILE
Johannes Schindelin58e9d9d2009-02-14 23:10:10 +0100127'
128
129test_expect_success 'gc respects gc.pruneExpire' '
Johannes Schindelin58e9d9d2009-02-14 23:10:10 +0100130 git config gc.pruneExpire 5002.days.ago &&
131 git gc &&
Jeff Kingf1dd90b2014-10-10 02:07:00 -0400132 test_path_is_file $BLOB_FILE &&
Johannes Schindelin58e9d9d2009-02-14 23:10:10 +0100133 git config gc.pruneExpire 5000.days.ago &&
134 git gc &&
Jeff Kingf1dd90b2014-10-10 02:07:00 -0400135 test_path_is_missing $BLOB_FILE
Johannes Schindelin58e9d9d2009-02-14 23:10:10 +0100136'
137
138test_expect_success 'gc --prune=<date>' '
Clemens Buchacher98df2332009-09-13 12:49:45 +0200139 add_blob &&
Nguyễn Thái Ngọc Duy0e496492018-03-24 08:44:31 +0100140 test-tool chmtime =-$((5001*$day)) $BLOB_FILE &&
Johannes Schindelin58e9d9d2009-02-14 23:10:10 +0100141 git gc --prune=5002.days.ago &&
Jeff Kingf1dd90b2014-10-10 02:07:00 -0400142 test_path_is_file $BLOB_FILE &&
Johannes Schindelin58e9d9d2009-02-14 23:10:10 +0100143 git gc --prune=5000.days.ago &&
Jeff Kingf1dd90b2014-10-10 02:07:00 -0400144 test_path_is_missing $BLOB_FILE
Johannes Schindelin58e9d9d2009-02-14 23:10:10 +0100145'
146
Adam Simpkinscbf731e2010-02-26 19:50:02 -0800147test_expect_success 'gc --prune=never' '
Adam Simpkinscbf731e2010-02-26 19:50:02 -0800148 add_blob &&
149 git gc --prune=never &&
Jeff Kingf1dd90b2014-10-10 02:07:00 -0400150 test_path_is_file $BLOB_FILE &&
Adam Simpkinscbf731e2010-02-26 19:50:02 -0800151 git gc --prune=now &&
Jeff Kingf1dd90b2014-10-10 02:07:00 -0400152 test_path_is_missing $BLOB_FILE
Adam Simpkinscbf731e2010-02-26 19:50:02 -0800153'
154
155test_expect_success 'gc respects gc.pruneExpire=never' '
Adam Simpkinscbf731e2010-02-26 19:50:02 -0800156 git config gc.pruneExpire never &&
157 add_blob &&
158 git gc &&
Jeff Kingf1dd90b2014-10-10 02:07:00 -0400159 test_path_is_file $BLOB_FILE &&
Adam Simpkinscbf731e2010-02-26 19:50:02 -0800160 git config gc.pruneExpire now &&
161 git gc &&
Jeff Kingf1dd90b2014-10-10 02:07:00 -0400162 test_path_is_missing $BLOB_FILE
Adam Simpkinscbf731e2010-02-26 19:50:02 -0800163'
164
165test_expect_success 'prune --expire=never' '
Adam Simpkinscbf731e2010-02-26 19:50:02 -0800166 add_blob &&
167 git prune --expire=never &&
Jeff Kingf1dd90b2014-10-10 02:07:00 -0400168 test_path_is_file $BLOB_FILE &&
Adam Simpkinscbf731e2010-02-26 19:50:02 -0800169 git prune &&
Jeff Kingf1dd90b2014-10-10 02:07:00 -0400170 test_path_is_missing $BLOB_FILE
Adam Simpkinscbf731e2010-02-26 19:50:02 -0800171'
172
Clemens Buchacher98df2332009-09-13 12:49:45 +0200173test_expect_success 'gc: prune old objects after local clone' '
174 add_blob &&
Nguyễn Thái Ngọc Duy0e496492018-03-24 08:44:31 +0100175 test-tool chmtime =-$((2*$week+1)) $BLOB_FILE &&
Clemens Buchacher98df2332009-09-13 12:49:45 +0200176 git clone --no-hardlinks . aclone &&
177 (
178 cd aclone &&
Jeff King8ad16522014-10-10 02:11:14 -0400179 verbose test 1 = $(git count-objects | sed "s/ .*//") &&
Jeff Kingf1dd90b2014-10-10 02:07:00 -0400180 test_path_is_file $BLOB_FILE &&
Clemens Buchacher98df2332009-09-13 12:49:45 +0200181 git gc --prune &&
Jeff King8ad16522014-10-10 02:11:14 -0400182 verbose test 0 = $(git count-objects | sed "s/ .*//") &&
Jeff Kingf1dd90b2014-10-10 02:07:00 -0400183 test_path_is_missing $BLOB_FILE
Clemens Buchacher98df2332009-09-13 12:49:45 +0200184 )
185'
186
Nguyễn Thái Ngọc Duy543c5ca2013-02-15 19:07:10 +0700187test_expect_success 'garbage report in count-objects -v' '
Jeff Kingf813e9e2015-06-22 06:40:50 -0400188 test_when_finished "rm -f .git/objects/pack/fake*" &&
Doug Kellye6d65c92015-11-03 21:05:07 -0600189 test_when_finished "rm -f .git/objects/pack/foo*" &&
Han-Wen Nienhuys1fa9cf62021-05-31 16:56:27 +0000190 >.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 &&
Nguyễn Thái Ngọc Duy543c5ca2013-02-15 19:07:10 +0700200 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
Justin Lebar235e8d52014-03-31 15:11:47 -0700207warning: no corresponding .idx or .pack: .git/objects/pack/fake2.keep
Nguyễn Thái Ngọc Duy543c5ca2013-02-15 19:07:10 +0700208warning: 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
Doug Kelly478f34d2015-11-03 21:05:08 -0600215test_expect_success 'clean pack garbage with gc' '
Doug Kellye6d65c92015-11-03 21:05:07 -0600216 test_when_finished "rm -f .git/objects/pack/fake*" &&
217 test_when_finished "rm -f .git/objects/pack/foo*" &&
Han-Wen Nienhuys1fa9cf62021-05-31 16:56:27 +0000218 >.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 &&
Doug Kellye6d65c92015-11-03 21:05:07 -0600224 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
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700235test_expect_success 'prune .git/shallow' '
Jeff Kingcc80c952019-02-14 06:07:36 -0500236 oid=$(echo hi|git commit-tree HEAD^{tree}) &&
237 echo $oid >.git/shallow &&
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700238 git prune --dry-run >out &&
Jeff Kingcc80c952019-02-14 06:07:36 -0500239 grep $oid .git/shallow &&
240 grep $oid out &&
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700241 git prune &&
Jeff Kingf1dd90b2014-10-10 02:07:00 -0400242 test_path_is_missing .git/shallow
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700243'
244
Jeff Kingd55a30b2019-02-13 23:35:22 -0500245test_expect_success 'prune .git/shallow when there are no loose objects' '
Jeff Kingcc80c952019-02-14 06:07:36 -0500246 oid=$(echo hi|git commit-tree HEAD^{tree}) &&
247 echo $oid >.git/shallow &&
248 git update-ref refs/heads/shallow-tip $oid &&
Jeff Kingd55a30b2019-02-13 23:35:22 -0500249 git repack -ad &&
250 # verify assumption that all loose objects are gone
251 git count-objects | grep ^0 &&
252 git prune &&
Jeff Kingcc80c952019-02-14 06:07:36 -0500253 echo $oid >expect &&
Jeff Kingd55a30b2019-02-13 23:35:22 -0500254 test_cmp expect .git/shallow
255'
256
Jonathon Mahb0a42642015-02-08 20:15:39 -0500257test_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
Nguyễn Thái Ngọc Duybe489d02017-08-23 19:36:52 +0700265test_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
Nguyễn Thái Ngọc Duyd0c39a42017-08-23 19:36:59 +0700274test_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
Nguyễn Thái Ngọc Duyacd95442017-08-23 19:37:01 +0700286test_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" &&
Elijah Newrenf222bd32021-09-11 17:08:42 +0000294 git clean -f && # Remove untracked left behind by deleting index
Nguyễn Thái Ngọc Duyacd95442017-08-23 19:37:01 +0700295 git reset --hard HEAD^
296 ) &&
297 git prune --expire=now &&
Jeff Kingcc80c952019-02-14 06:07:36 -0500298 oid=`git hash-object expected` &&
299 git -C third-worktree show "$oid" >actual &&
Nguyễn Thái Ngọc Duyacd95442017-08-23 19:37:01 +0700300 test_cmp expected actual
301'
302
Junio C Hamano8ab5aa42018-04-21 12:13:13 +0900303test_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
Jeff Kingfe6f2b02019-04-18 16:08:27 -0400313test_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
Jeff King2ba582b2021-04-28 11:42:43 -0400321test_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
David Steven Tweed84640102008-02-07 02:55:14 +0000337test_done