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