3 # Copyright (c) 2008 Johannes E. Schindelin
6 test_description
='prune'
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-chmtime
=+0 $BLOB_FILE
21 test_expect_success setup
'
26 git commit -m initial &&
31 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-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
44 test_expect_success
'prune --expire' '
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-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
57 test_expect_success
'gc: implicit prune --expire' '
60 test-chmtime =-$((2*$week-30)) $BLOB_FILE &&
62 verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
63 test_path_is_file $BLOB_FILE &&
64 test-chmtime =-$((2*$week+1)) $BLOB_FILE &&
66 verbose test $before = $(git count-objects | sed "s/ .*//") &&
67 test_path_is_missing $BLOB_FILE
71 test_expect_success
'gc: refuse to start with invalid gc.pruneExpire' '
73 git config gc.pruneExpire invalid &&
78 test_expect_success
'gc: start with ok gc.pruneExpire' '
80 git config gc.pruneExpire 2.days.ago &&
85 test_expect_success
'prune: prune nonsense parameters' '
87 test_must_fail git prune garbage &&
88 test_must_fail git prune --- &&
89 test_must_fail git prune --no-such-option
93 test_expect_success
'prune: prune unreachable heads' '
95 git config core.logAllRefUpdates false &&
96 mv .git/logs .git/logs.old &&
99 git commit -m temporary &&
100 tmp_head=$(git rev-list -1 HEAD) &&
103 test_must_fail git reset $tmp_head --
107 test_expect_success
'prune: do not prune detached HEAD with no reflog' '
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 &&
116 test_cmp prune_actual prune_expected
120 test_expect_success
'prune: prune former HEAD after checking out branch' '
122 head_sha1=$(git rev-parse HEAD) &&
123 git checkout --quiet master &&
124 git prune -v >prune_actual &&
125 grep "$head_sha1" prune_actual
129 test_expect_success
'prune: do not prune heads listed as an argument' '
133 git commit -m temporary &&
134 tmp_head=$(git rev-list -1 HEAD) &&
136 git prune -- $tmp_head &&
137 git reset $tmp_head --
141 test_expect_success
'gc --no-prune' '
144 test-chmtime =-$((5001*$day)) $BLOB_FILE &&
145 git config gc.pruneExpire 2.days.ago &&
147 verbose test 1 = $(git count-objects | sed "s/ .*//") &&
148 test_path_is_file $BLOB_FILE
152 test_expect_success
'gc respects gc.pruneExpire' '
154 git config gc.pruneExpire 5002.days.ago &&
156 test_path_is_file $BLOB_FILE &&
157 git config gc.pruneExpire 5000.days.ago &&
159 test_path_is_missing $BLOB_FILE
163 test_expect_success
'gc --prune=<date>' '
166 test-chmtime =-$((5001*$day)) $BLOB_FILE &&
167 git gc --prune=5002.days.ago &&
168 test_path_is_file $BLOB_FILE &&
169 git gc --prune=5000.days.ago &&
170 test_path_is_missing $BLOB_FILE
174 test_expect_success
'gc --prune=never' '
177 git gc --prune=never &&
178 test_path_is_file $BLOB_FILE &&
179 git gc --prune=now &&
180 test_path_is_missing $BLOB_FILE
184 test_expect_success
'gc respects gc.pruneExpire=never' '
186 git config gc.pruneExpire never &&
189 test_path_is_file $BLOB_FILE &&
190 git config gc.pruneExpire now &&
192 test_path_is_missing $BLOB_FILE
196 test_expect_success
'prune --expire=never' '
199 git prune --expire=never &&
200 test_path_is_file $BLOB_FILE &&
202 test_path_is_missing $BLOB_FILE
206 test_expect_success
'gc: prune old objects after local clone' '
208 test-chmtime =-$((2*$week+1)) $BLOB_FILE &&
209 git clone --no-hardlinks . aclone &&
212 verbose test 1 = $(git count-objects | sed "s/ .*//") &&
213 test_path_is_file $BLOB_FILE &&
215 verbose test 0 = $(git count-objects | sed "s/ .*//") &&
216 test_path_is_missing $BLOB_FILE
220 test_expect_success
'garbage report in count-objects -v' '
221 test_when_finished "rm -f .git/objects/pack/fake*" &&
222 test_when_finished "rm -f .git/objects/pack/foo*" &&
223 : >.git/objects/pack/foo &&
224 : >.git/objects/pack/foo.bar &&
225 : >.git/objects/pack/foo.keep &&
226 : >.git/objects/pack/foo.pack &&
227 : >.git/objects/pack/fake.bar &&
228 : >.git/objects/pack/fake.keep &&
229 : >.git/objects/pack/fake.pack &&
230 : >.git/objects/pack/fake.idx &&
231 : >.git/objects/pack/fake2.keep &&
232 : >.git/objects/pack/fake3.idx &&
233 git count-objects -v 2>stderr &&
234 grep "index file .git/objects/pack/fake.idx is too small" stderr &&
235 grep "^warning:" stderr | sort >actual &&
236 cat >expected <<\EOF &&
237 warning: garbage found: .git/objects/pack/fake.bar
238 warning: garbage found: .git/objects/pack/foo
239 warning: garbage found: .git/objects/pack/foo.bar
240 warning: no corresponding .idx or .pack: .git/objects/pack/fake2.keep
241 warning: no corresponding .idx: .git/objects/pack/foo.keep
242 warning: no corresponding .idx: .git/objects/pack/foo.pack
243 warning: no corresponding .pack: .git/objects/pack/fake3.idx
245 test_cmp expected actual
248 test_expect_success
'clean pack garbage with gc' '
249 test_when_finished "rm -f .git/objects/pack/fake*" &&
250 test_when_finished "rm -f .git/objects/pack/foo*" &&
251 : >.git/objects/pack/foo.keep &&
252 : >.git/objects/pack/foo.pack &&
253 : >.git/objects/pack/fake.idx &&
254 : >.git/objects/pack/fake2.keep &&
255 : >.git/objects/pack/fake2.idx &&
256 : >.git/objects/pack/fake3.keep &&
258 git count-objects -v 2>stderr &&
259 grep "^warning:" stderr | sort >actual &&
260 cat >expected <<\EOF &&
261 warning: no corresponding .idx or .pack: .git/objects/pack/fake3.keep
262 warning: no corresponding .idx: .git/objects/pack/foo.keep
263 warning: no corresponding .idx: .git/objects/pack/foo.pack
265 test_cmp expected actual
268 test_expect_success
'prune .git/shallow' '
269 SHA1=$(echo hi|git commit-tree HEAD^{tree}) &&
270 echo $SHA1 >.git/shallow &&
271 git prune --dry-run >out &&
272 grep $SHA1 .git/shallow &&
275 test_path_is_missing .git/shallow
278 test_expect_success
'prune: handle alternate object database' '
279 test_create_repo A &&
280 git -C A commit --allow-empty -m "initial commit" &&
281 git clone --shared A B &&
282 git -C B commit --allow-empty -m "next commit" &&
286 test_expect_success
'prune: handle index in multiple worktrees' '
287 git worktree add second-worktree &&
288 echo "new blob for second-worktree" >second-worktree/blob &&
289 git -C second-worktree add blob &&
290 git prune --expire=now &&
291 git -C second-worktree show :blob >actual &&
292 test_cmp second-worktree/blob actual
295 test_expect_success
'prune: handle HEAD in multiple worktrees' '
296 git worktree add --detach third-worktree &&
297 echo "new blob for third-worktree" >third-worktree/blob &&
298 git -C third-worktree add blob &&
299 git -C third-worktree commit -m "third" &&
300 rm .git/worktrees/third-worktree/index &&
301 test_must_fail git -C third-worktree show :blob &&
302 git prune --expire=now &&
303 git -C third-worktree show HEAD:blob >actual &&
304 test_cmp third-worktree/blob actual
307 test_expect_success
'prune: handle HEAD reflog in multiple worktrees' '
308 git config core.logAllRefUpdates true &&
309 echo "lost blob for third-worktree" >expected &&
312 cat ../expected >blob &&
314 git commit -m "second commit in third" &&
315 git reset --hard HEAD^
317 git prune --expire=now &&
318 SHA1=`git hash-object expected` &&
319 git -C third-worktree show "$SHA1" >actual &&
320 test_cmp expected actual