Merge branch 'ab/describe-tests-fix' into jch
[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 'prune stale packs' '
33         orig_pack=$(echo .git/objects/pack/*.pack) &&
34         >.git/objects/tmp_1.pack &&
35         >.git/objects/tmp_2.pack &&
36         test-tool chmtime =-86501 .git/objects/tmp_1.pack &&
37         git prune --expire 1.day &&
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
41 '
42
43 test_expect_success 'prune --expire' '
44         add_blob &&
45         git prune --expire=1.hour.ago &&
46         verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
47         test_path_is_file $BLOB_FILE &&
48         test-tool chmtime =-86500 $BLOB_FILE &&
49         git prune --expire 1.day &&
50         verbose test $before = $(git count-objects | sed "s/ .*//") &&
51         test_path_is_missing $BLOB_FILE
52 '
53
54 test_expect_success 'gc: implicit prune --expire' '
55         add_blob &&
56         test-tool chmtime =-$((2*$week-30)) $BLOB_FILE &&
57         git gc &&
58         verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
59         test_path_is_file $BLOB_FILE &&
60         test-tool chmtime =-$((2*$week+1)) $BLOB_FILE &&
61         git gc &&
62         verbose test $before = $(git count-objects | sed "s/ .*//") &&
63         test_path_is_missing $BLOB_FILE
64 '
65
66 test_expect_success 'gc: refuse to start with invalid gc.pruneExpire' '
67         git config gc.pruneExpire invalid &&
68         test_must_fail git gc
69 '
70
71 test_expect_success 'gc: start with ok gc.pruneExpire' '
72         git config gc.pruneExpire 2.days.ago &&
73         git gc
74 '
75
76 test_expect_success 'prune: prune nonsense parameters' '
77         test_must_fail git prune garbage &&
78         test_must_fail git prune --- &&
79         test_must_fail git prune --no-such-option
80 '
81
82 test_expect_success 'prune: prune unreachable heads' '
83         git config core.logAllRefUpdates false &&
84         >file2 &&
85         git add file2 &&
86         git commit -m temporary &&
87         tmp_head=$(git rev-list -1 HEAD) &&
88         git reset HEAD^ &&
89         git reflog expire --all &&
90         git prune &&
91         test_must_fail git reset $tmp_head --
92 '
93
94 test_expect_success 'prune: do not prune detached HEAD with no reflog' '
95         git checkout --detach --quiet &&
96         git commit --allow-empty -m "detached commit" &&
97         git reflog expire --all &&
98         git prune -n >prune_actual &&
99         test_must_be_empty prune_actual
100 '
101
102 test_expect_success 'prune: prune former HEAD after checking out branch' '
103         head_oid=$(git rev-parse HEAD) &&
104         git checkout --quiet main &&
105         git reflog expire --all &&
106         git prune -v >prune_actual &&
107         grep "$head_oid" prune_actual
108 '
109
110 test_expect_success 'prune: do not prune heads listed as an argument' '
111         >file2 &&
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 --
118 '
119
120 test_expect_success 'gc --no-prune' '
121         add_blob &&
122         test-tool chmtime =-$((5001*$day)) $BLOB_FILE &&
123         git config gc.pruneExpire 2.days.ago &&
124         git gc --no-prune &&
125         verbose test 1 = $(git count-objects | sed "s/ .*//") &&
126         test_path_is_file $BLOB_FILE
127 '
128
129 test_expect_success 'gc respects gc.pruneExpire' '
130         git config gc.pruneExpire 5002.days.ago &&
131         git gc &&
132         test_path_is_file $BLOB_FILE &&
133         git config gc.pruneExpire 5000.days.ago &&
134         git gc &&
135         test_path_is_missing $BLOB_FILE
136 '
137
138 test_expect_success 'gc --prune=<date>' '
139         add_blob &&
140         test-tool chmtime =-$((5001*$day)) $BLOB_FILE &&
141         git gc --prune=5002.days.ago &&
142         test_path_is_file $BLOB_FILE &&
143         git gc --prune=5000.days.ago &&
144         test_path_is_missing $BLOB_FILE
145 '
146
147 test_expect_success 'gc --prune=never' '
148         add_blob &&
149         git gc --prune=never &&
150         test_path_is_file $BLOB_FILE &&
151         git gc --prune=now &&
152         test_path_is_missing $BLOB_FILE
153 '
154
155 test_expect_success 'gc respects gc.pruneExpire=never' '
156         git config gc.pruneExpire never &&
157         add_blob &&
158         git gc &&
159         test_path_is_file $BLOB_FILE &&
160         git config gc.pruneExpire now &&
161         git gc &&
162         test_path_is_missing $BLOB_FILE
163 '
164
165 test_expect_success 'prune --expire=never' '
166         add_blob &&
167         git prune --expire=never &&
168         test_path_is_file $BLOB_FILE &&
169         git prune &&
170         test_path_is_missing $BLOB_FILE
171 '
172
173 test_expect_success 'gc: prune old objects after local clone' '
174         add_blob &&
175         test-tool chmtime =-$((2*$week+1)) $BLOB_FILE &&
176         git clone --no-hardlinks . aclone &&
177         (
178                 cd aclone &&
179                 verbose test 1 = $(git count-objects | sed "s/ .*//") &&
180                 test_path_is_file $BLOB_FILE &&
181                 git gc --prune &&
182                 verbose test 0 = $(git count-objects | sed "s/ .*//") &&
183                 test_path_is_missing $BLOB_FILE
184         )
185 '
186
187 test_expect_success 'garbage report in count-objects -v' '
188         test_when_finished "rm -f .git/objects/pack/fake*" &&
189         test_when_finished "rm -f .git/objects/pack/foo*" &&
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 &&
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 &&
204 warning: garbage found: .git/objects/pack/fake.bar
205 warning: garbage found: .git/objects/pack/foo
206 warning: garbage found: .git/objects/pack/foo.bar
207 warning: no corresponding .idx or .pack: .git/objects/pack/fake2.keep
208 warning: no corresponding .idx: .git/objects/pack/foo.keep
209 warning: no corresponding .idx: .git/objects/pack/foo.pack
210 warning: no corresponding .pack: .git/objects/pack/fake3.idx
211 EOF
212         test_cmp expected actual
213 '
214
215 test_expect_success 'clean pack garbage with gc' '
216         test_when_finished "rm -f .git/objects/pack/fake*" &&
217         test_when_finished "rm -f .git/objects/pack/foo*" &&
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 &&
224         git gc &&
225         git count-objects -v 2>stderr &&
226         grep "^warning:" stderr | sort >actual &&
227         cat >expected <<\EOF &&
228 warning: no corresponding .idx or .pack: .git/objects/pack/fake3.keep
229 warning: no corresponding .idx: .git/objects/pack/foo.keep
230 warning: no corresponding .idx: .git/objects/pack/foo.pack
231 EOF
232         test_cmp expected actual
233 '
234
235 test_expect_success 'prune .git/shallow' '
236         oid=$(echo hi|git commit-tree HEAD^{tree}) &&
237         echo $oid >.git/shallow &&
238         git prune --dry-run >out &&
239         grep $oid .git/shallow &&
240         grep $oid out &&
241         git prune &&
242         test_path_is_missing .git/shallow
243 '
244
245 test_expect_success 'prune .git/shallow when there are no loose objects' '
246         oid=$(echo hi|git commit-tree HEAD^{tree}) &&
247         echo $oid >.git/shallow &&
248         git update-ref refs/heads/shallow-tip $oid &&
249         git repack -ad &&
250         # verify assumption that all loose objects are gone
251         git count-objects | grep ^0 &&
252         git prune &&
253         echo $oid >expect &&
254         test_cmp expect .git/shallow
255 '
256
257 test_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
265 test_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
274 test_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
286 test_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" &&
294                 git reset --hard HEAD^
295         ) &&
296         git prune --expire=now &&
297         oid=`git hash-object expected` &&
298         git -C third-worktree show "$oid" >actual &&
299         test_cmp expected actual
300 '
301
302 test_expect_success 'prune: handle expire option correctly' '
303         test_must_fail git prune --expire 2>error &&
304         test_i18ngrep "requires a value" error &&
305
306         test_must_fail git prune --expire=nyah 2>error &&
307         test_i18ngrep "malformed expiration" error &&
308
309         git prune --no-expire
310 '
311
312 test_expect_success 'trivial prune with bitmaps enabled' '
313         git repack -adb &&
314         blob=$(echo bitmap-unreachable-blob | git hash-object -w --stdin) &&
315         git prune --expire=now &&
316         git cat-file -e HEAD &&
317         test_must_fail git cat-file -e $blob
318 '
319
320 test_expect_success 'old reachable-from-recent retained with bitmaps' '
321         git repack -adb &&
322         to_drop=$(echo bitmap-from-recent-1 | git hash-object -w --stdin) &&
323         test-tool chmtime -86400 .git/objects/$(test_oid_to_path $to_drop) &&
324         to_save=$(echo bitmap-from-recent-2 | git hash-object -w --stdin) &&
325         test-tool chmtime -86400 .git/objects/$(test_oid_to_path $to_save) &&
326         tree=$(printf "100644 blob $to_save\tfile\n" | git mktree) &&
327         test-tool chmtime -86400 .git/objects/$(test_oid_to_path $tree) &&
328         commit=$(echo foo | git commit-tree $tree) &&
329         git prune --expire=12.hours.ago &&
330         git cat-file -e $commit &&
331         git cat-file -e $tree &&
332         git cat-file -e $to_save &&
333         test_must_fail git cat-file -e $to_drop
334 '
335
336 test_done