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