3 # Copyright (c) 2008 Johannes E. Schindelin
6 test_description
='prune'
7 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
8 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
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
24 test_expect_success setup
'
28 git commit -m initial &&
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
43 test_expect_success
'prune --expire' '
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
54 test_expect_success
'gc: implicit prune --expire' '
56 test-tool chmtime =-$((2*$week-30)) $BLOB_FILE &&
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 &&
62 verbose test $before = $(git count-objects | sed "s/ .*//") &&
63 test_path_is_missing $BLOB_FILE
66 test_expect_success
'gc: refuse to start with invalid gc.pruneExpire' '
67 git config gc.pruneExpire invalid &&
71 test_expect_success
'gc: start with ok gc.pruneExpire' '
72 git config gc.pruneExpire 2.days.ago &&
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
82 test_expect_success
'prune: prune unreachable heads' '
83 git config core.logAllRefUpdates false &&
86 git commit -m temporary &&
87 tmp_head=$(git rev-list -1 HEAD) &&
89 git reflog expire --all &&
91 test_must_fail git reset $tmp_head --
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
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
110 test_expect_success
'prune: do not prune heads listed as an argument' '
113 git commit -m temporary &&
114 tmp_head=$(git rev-list -1 HEAD) &&
116 git prune -- $tmp_head &&
117 git reset $tmp_head --
120 test_expect_success
'gc --no-prune' '
122 test-tool chmtime =-$((5001*$day)) $BLOB_FILE &&
123 git config gc.pruneExpire 2.days.ago &&
125 verbose test 1 = $(git count-objects | sed "s/ .*//") &&
126 test_path_is_file $BLOB_FILE
129 test_expect_success
'gc respects gc.pruneExpire' '
130 git config gc.pruneExpire 5002.days.ago &&
132 test_path_is_file $BLOB_FILE &&
133 git config gc.pruneExpire 5000.days.ago &&
135 test_path_is_missing $BLOB_FILE
138 test_expect_success
'gc --prune=<date>' '
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
147 test_expect_success
'gc --prune=never' '
149 git gc --prune=never &&
150 test_path_is_file $BLOB_FILE &&
151 git gc --prune=now &&
152 test_path_is_missing $BLOB_FILE
155 test_expect_success
'gc respects gc.pruneExpire=never' '
156 git config gc.pruneExpire never &&
159 test_path_is_file $BLOB_FILE &&
160 git config gc.pruneExpire now &&
162 test_path_is_missing $BLOB_FILE
165 test_expect_success
'prune --expire=never' '
167 git prune --expire=never &&
168 test_path_is_file $BLOB_FILE &&
170 test_path_is_missing $BLOB_FILE
173 test_expect_success
'gc: prune old objects after local clone' '
175 test-tool chmtime =-$((2*$week+1)) $BLOB_FILE &&
176 git clone --no-hardlinks . aclone &&
179 verbose test 1 = $(git count-objects | sed "s/ .*//") &&
180 test_path_is_file $BLOB_FILE &&
182 verbose test 0 = $(git count-objects | sed "s/ .*//") &&
183 test_path_is_missing $BLOB_FILE
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
212 test_cmp expected actual
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 &&
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
232 test_cmp expected actual
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 &&
242 test_path_is_missing .git/shallow
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 &&
250 # verify assumption that all loose objects are gone
251 git count-objects | grep ^0 &&
254 test_cmp expect .git/shallow
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" &&
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
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
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 &&
291 cat ../expected >blob &&
293 git commit -m "second commit in third" &&
294 git clean -f && # Remove untracked left behind by deleting index
295 git reset --hard HEAD^
297 git prune --expire=now &&
298 oid=`git hash-object expected` &&
299 git -C third-worktree show "$oid" >actual &&
300 test_cmp expected actual
303 test_expect_success
'prune: handle expire option correctly' '
304 test_must_fail git prune --expire 2>error &&
305 test_i18ngrep "requires a value" error &&
307 test_must_fail git prune --expire=nyah 2>error &&
308 test_i18ngrep "malformed expiration" error &&
310 git prune --no-expire
313 test_expect_success
'trivial prune with bitmaps enabled' '
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
321 test_expect_success
'old reachable-from-recent retained with bitmaps' '
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