Merge branch 'es/worktree-repair-copied' into cw/worktrees-relative
[git/gitster.git] / t / t5304-prune.sh
blobe641df0116c24404e4892a0e30af4ef4bf8db493
1 #!/bin/sh
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
10 TEST_PASSES_SANITIZE_LEAK=true
11 . ./test-lib.sh
13 day=$((60*60*24))
14 week=$(($day*7))
16 add_blob() {
17 before=$(git count-objects | sed "s/ .*//") &&
18 BLOB=$(echo aleph_0 | git hash-object -w --stdin) &&
19 BLOB_FILE=.git/objects/$(echo $BLOB | sed "s/^../&\//") &&
20 test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
21 test_path_is_file $BLOB_FILE &&
22 test-tool chmtime =+0 $BLOB_FILE
25 test_expect_success setup '
26 >file &&
27 git add file &&
28 test_tick &&
29 git commit -m initial &&
30 git gc
33 test_expect_success 'bare repo prune is quiet without $GIT_DIR/objects/pack' '
34 git clone -q --shared --template= --bare . bare.git &&
35 rmdir bare.git/objects/pack &&
36 git --git-dir=bare.git prune --no-progress 2>prune.err &&
37 test_must_be_empty prune.err &&
38 rm -r bare.git prune.err
41 test_expect_success 'prune stale packs' '
42 orig_pack=$(echo .git/objects/pack/*.pack) &&
43 >.git/objects/tmp_1.pack &&
44 >.git/objects/tmp_2.pack &&
45 test-tool chmtime =-86501 .git/objects/tmp_1.pack &&
46 git prune --expire 1.day &&
47 test_path_is_file $orig_pack &&
48 test_path_is_file .git/objects/tmp_2.pack &&
49 test_path_is_missing .git/objects/tmp_1.pack
52 test_expect_success 'prune --expire' '
53 add_blob &&
54 git prune --expire=1.hour.ago &&
55 test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
56 test_path_is_file $BLOB_FILE &&
57 test-tool chmtime =-86500 $BLOB_FILE &&
58 git prune --expire 1.day &&
59 test $before = $(git count-objects | sed "s/ .*//") &&
60 test_path_is_missing $BLOB_FILE
63 test_expect_success 'gc: implicit prune --expire' '
64 add_blob &&
65 test-tool chmtime =-$((2*$week-30)) $BLOB_FILE &&
66 git gc --no-cruft &&
67 test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
68 test_path_is_file $BLOB_FILE &&
69 test-tool chmtime =-$((2*$week+1)) $BLOB_FILE &&
70 git gc --no-cruft &&
71 test $before = $(git count-objects | sed "s/ .*//") &&
72 test_path_is_missing $BLOB_FILE
75 test_expect_success 'gc: refuse to start with invalid gc.pruneExpire' '
76 test_when_finished "rm -rf repo" &&
77 git init repo &&
78 >repo/.git/config &&
79 git -C repo config gc.pruneExpire invalid &&
80 cat >expect <<-\EOF &&
81 error: Invalid gc.pruneexpire: '\''invalid'\''
82 fatal: bad config variable '\''gc.pruneexpire'\'' in file '\''.git/config'\'' at line 2
83 EOF
84 test_must_fail git -C repo gc 2>actual &&
85 test_cmp expect actual
88 test_expect_success 'gc: start with ok gc.pruneExpire' '
89 git config gc.pruneExpire 2.days.ago &&
90 git gc --no-cruft
93 test_expect_success 'prune: prune nonsense parameters' '
94 test_must_fail git prune garbage &&
95 test_must_fail git prune --- &&
96 test_must_fail git prune --no-such-option
99 test_expect_success 'prune: prune unreachable heads' '
100 git config core.logAllRefUpdates false &&
101 >file2 &&
102 git add file2 &&
103 git commit -m temporary &&
104 tmp_head=$(git rev-list -1 HEAD) &&
105 git reset HEAD^ &&
106 git reflog expire --all &&
107 git prune &&
108 test_must_fail git reset $tmp_head --
111 test_expect_success 'prune: do not prune detached HEAD with no reflog' '
112 git checkout --detach --quiet &&
113 git commit --allow-empty -m "detached commit" &&
114 git reflog expire --all &&
115 git prune -n >prune_actual &&
116 test_must_be_empty prune_actual
119 test_expect_success 'prune: prune former HEAD after checking out branch' '
120 head_oid=$(git rev-parse HEAD) &&
121 git checkout --quiet main &&
122 git reflog expire --all &&
123 git prune -v >prune_actual &&
124 grep "$head_oid" prune_actual
127 test_expect_success 'prune: do not prune heads listed as an argument' '
128 >file2 &&
129 git add file2 &&
130 git commit -m temporary &&
131 tmp_head=$(git rev-list -1 HEAD) &&
132 git reset HEAD^ &&
133 git prune -- $tmp_head &&
134 git reset $tmp_head --
137 test_expect_success 'gc --no-prune' '
138 add_blob &&
139 test-tool chmtime =-$((5001*$day)) $BLOB_FILE &&
140 git config gc.pruneExpire 2.days.ago &&
141 git gc --no-prune --no-cruft &&
142 test 1 = $(git count-objects | sed "s/ .*//") &&
143 test_path_is_file $BLOB_FILE
146 test_expect_success 'gc respects gc.pruneExpire' '
147 git config gc.pruneExpire 5002.days.ago &&
148 git gc --no-cruft &&
149 test_path_is_file $BLOB_FILE &&
150 git config gc.pruneExpire 5000.days.ago &&
151 git gc --no-cruft &&
152 test_path_is_missing $BLOB_FILE
155 test_expect_success 'gc --prune=<date>' '
156 add_blob &&
157 test-tool chmtime =-$((5001*$day)) $BLOB_FILE &&
158 git gc --prune=5002.days.ago --no-cruft &&
159 test_path_is_file $BLOB_FILE &&
160 git gc --prune=5000.days.ago --no-cruft &&
161 test_path_is_missing $BLOB_FILE
164 test_expect_success 'gc --prune=never' '
165 add_blob &&
166 git gc --prune=never --no-cruft &&
167 test_path_is_file $BLOB_FILE &&
168 git gc --prune=now --no-cruft &&
169 test_path_is_missing $BLOB_FILE
172 test_expect_success 'gc respects gc.pruneExpire=never' '
173 git config gc.pruneExpire never &&
174 add_blob &&
175 git gc --no-cruft &&
176 test_path_is_file $BLOB_FILE &&
177 git config gc.pruneExpire now &&
178 git gc --no-cruft &&
179 test_path_is_missing $BLOB_FILE
182 test_expect_success 'prune --expire=never' '
183 add_blob &&
184 git prune --expire=never &&
185 test_path_is_file $BLOB_FILE &&
186 git prune &&
187 test_path_is_missing $BLOB_FILE
190 test_expect_success 'gc: prune old objects after local clone' '
191 add_blob &&
192 test-tool chmtime =-$((2*$week+1)) $BLOB_FILE &&
193 git clone --no-hardlinks . aclone &&
195 cd aclone &&
196 test 1 = $(git count-objects | sed "s/ .*//") &&
197 test_path_is_file $BLOB_FILE &&
198 git gc --prune --no-cruft &&
199 test 0 = $(git count-objects | sed "s/ .*//") &&
200 test_path_is_missing $BLOB_FILE
204 test_expect_success 'garbage report in count-objects -v' '
205 test_when_finished "rm -f .git/objects/pack/fake*" &&
206 test_when_finished "rm -f .git/objects/pack/foo*" &&
207 >.git/objects/pack/foo &&
208 >.git/objects/pack/foo.bar &&
209 >.git/objects/pack/foo.keep &&
210 >.git/objects/pack/foo.pack &&
211 >.git/objects/pack/fake.bar &&
212 >.git/objects/pack/fake.keep &&
213 >.git/objects/pack/fake.pack &&
214 >.git/objects/pack/fake.idx &&
215 >.git/objects/pack/fake2.keep &&
216 >.git/objects/pack/fake3.idx &&
217 git count-objects -v 2>stderr &&
218 grep "index file .git/objects/pack/fake.idx is too small" stderr &&
219 grep "^warning:" stderr | sort >actual &&
220 cat >expected <<\EOF &&
221 warning: garbage found: .git/objects/pack/fake.bar
222 warning: garbage found: .git/objects/pack/foo
223 warning: garbage found: .git/objects/pack/foo.bar
224 warning: no corresponding .idx or .pack: .git/objects/pack/fake2.keep
225 warning: no corresponding .idx: .git/objects/pack/foo.keep
226 warning: no corresponding .idx: .git/objects/pack/foo.pack
227 warning: no corresponding .pack: .git/objects/pack/fake3.idx
229 test_cmp expected actual
232 test_expect_success 'clean pack garbage with gc' '
233 test_when_finished "rm -f .git/objects/pack/fake*" &&
234 test_when_finished "rm -f .git/objects/pack/foo*" &&
235 >.git/objects/pack/foo.keep &&
236 >.git/objects/pack/foo.pack &&
237 >.git/objects/pack/fake.idx &&
238 >.git/objects/pack/fake2.keep &&
239 >.git/objects/pack/fake2.idx &&
240 >.git/objects/pack/fake3.keep &&
241 git gc --no-cruft &&
242 git count-objects -v 2>stderr &&
243 grep "^warning:" stderr | sort >actual &&
244 cat >expected <<\EOF &&
245 warning: no corresponding .idx or .pack: .git/objects/pack/fake3.keep
246 warning: no corresponding .idx: .git/objects/pack/foo.keep
247 warning: no corresponding .idx: .git/objects/pack/foo.pack
249 test_cmp expected actual
252 test_expect_success 'prune .git/shallow' '
253 oid=$(echo hi|git commit-tree HEAD^{tree}) &&
254 echo $oid >.git/shallow &&
255 git prune --dry-run >out &&
256 grep $oid .git/shallow &&
257 grep $oid out &&
258 git prune &&
259 test_path_is_missing .git/shallow
262 test_expect_success 'prune .git/shallow when there are no loose objects' '
263 oid=$(echo hi|git commit-tree HEAD^{tree}) &&
264 echo $oid >.git/shallow &&
265 git update-ref refs/heads/shallow-tip $oid &&
266 git repack -ad &&
267 # verify assumption that all loose objects are gone
268 git count-objects | grep ^0 &&
269 git prune &&
270 echo $oid >expect &&
271 test_cmp expect .git/shallow
274 test_expect_success 'prune: handle alternate object database' '
275 test_create_repo A &&
276 git -C A commit --allow-empty -m "initial commit" &&
277 git clone --shared A B &&
278 git -C B commit --allow-empty -m "next commit" &&
279 git -C B prune
282 test_expect_success 'prune: handle index in multiple worktrees' '
283 git worktree add second-worktree &&
284 echo "new blob for second-worktree" >second-worktree/blob &&
285 git -C second-worktree add blob &&
286 git prune --expire=now &&
287 git -C second-worktree show :blob >actual &&
288 test_cmp second-worktree/blob actual
291 test_expect_success 'prune: handle HEAD in multiple worktrees' '
292 git worktree add --detach third-worktree &&
293 echo "new blob for third-worktree" >third-worktree/blob &&
294 git -C third-worktree add blob &&
295 git -C third-worktree commit -m "third" &&
296 rm .git/worktrees/third-worktree/index &&
297 test_must_fail git -C third-worktree show :blob &&
298 git prune --expire=now &&
299 git -C third-worktree show HEAD:blob >actual &&
300 test_cmp third-worktree/blob actual
303 test_expect_success 'prune: handle HEAD reflog in multiple worktrees' '
304 git config core.logAllRefUpdates true &&
305 echo "lost blob for third-worktree" >expected &&
307 cd third-worktree &&
308 cat ../expected >blob &&
309 git add blob &&
310 git commit -m "second commit in third" &&
311 git clean -f && # Remove untracked left behind by deleting index
312 git reset --hard HEAD^
313 ) &&
314 git prune --expire=now &&
315 oid=`git hash-object expected` &&
316 git -C third-worktree show "$oid" >actual &&
317 test_cmp expected actual
320 test_expect_success 'prune: handle expire option correctly' '
321 test_must_fail git prune --expire 2>error &&
322 test_grep "requires a value" error &&
324 test_must_fail git prune --expire=nyah 2>error &&
325 test_grep "malformed expiration" error &&
327 git prune --no-expire
330 test_expect_success 'trivial prune with bitmaps enabled' '
331 git repack -adb &&
332 blob=$(echo bitmap-unreachable-blob | git hash-object -w --stdin) &&
333 git prune --expire=now &&
334 git cat-file -e HEAD &&
335 test_must_fail git cat-file -e $blob
338 test_expect_success 'old reachable-from-recent retained with bitmaps' '
339 git repack -adb &&
340 to_drop=$(echo bitmap-from-recent-1 | git hash-object -w --stdin) &&
341 test-tool chmtime -86400 .git/objects/$(test_oid_to_path $to_drop) &&
342 to_save=$(echo bitmap-from-recent-2 | git hash-object -w --stdin) &&
343 test-tool chmtime -86400 .git/objects/$(test_oid_to_path $to_save) &&
344 tree=$(printf "100644 blob $to_save\tfile\n" | git mktree) &&
345 test-tool chmtime -86400 .git/objects/$(test_oid_to_path $tree) &&
346 commit=$(echo foo | git commit-tree $tree) &&
347 git prune --expire=12.hours.ago &&
348 git cat-file -e $commit &&
349 git cat-file -e $tree &&
350 git cat-file -e $to_save &&
351 test_must_fail git cat-file -e $to_drop
354 test_expect_success 'gc.recentObjectsHook' '
355 add_blob &&
356 test-tool chmtime =-86500 $BLOB_FILE &&
358 write_script precious-objects <<-EOF &&
359 echo $BLOB
361 test_config gc.recentObjectsHook ./precious-objects &&
363 git prune --expire=now &&
365 git cat-file -p $BLOB
368 test_done