]>
Commit | Line | Data |
---|---|---|
1389d9dd JH |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2007 Junio C Hamano | |
4 | # | |
5 | ||
6 | test_description='Test prune and reflog expiration' | |
06d53148 | 7 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
334afbc7 JS |
8 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
9 | ||
1389d9dd JH |
10 | . ./test-lib.sh |
11 | ||
12 | check_have () { | |
13 | gaah= && | |
14 | for N in "$@" | |
15 | do | |
16 | eval "o=\$$N" && git cat-file -t $o || { | |
17 | echo Gaah $N | |
18 | gaah=$N | |
19 | break | |
20 | } | |
21 | done && | |
22 | test -z "$gaah" | |
23 | } | |
24 | ||
25 | check_fsck () { | |
674ba340 | 26 | git fsck --full >fsck.output |
1389d9dd JH |
27 | case "$1" in |
28 | '') | |
674ba340 | 29 | test_must_be_empty fsck.output ;; |
1389d9dd | 30 | *) |
674ba340 | 31 | test_i18ngrep "$1" fsck.output ;; |
1389d9dd JH |
32 | esac |
33 | } | |
34 | ||
35 | corrupt () { | |
ff4cb42e | 36 | mv .git/objects/$(test_oid_to_path $1) .git/$1 |
1389d9dd JH |
37 | } |
38 | ||
39 | recover () { | |
ff4cb42e | 40 | aa=$(echo $1 | cut -c 1-2) |
1389d9dd | 41 | mkdir -p .git/objects/$aa |
ff4cb42e | 42 | mv .git/$1 .git/objects/$(test_oid_to_path $1) |
1389d9dd JH |
43 | } |
44 | ||
45 | check_dont_have () { | |
46 | gaah= && | |
47 | for N in "$@" | |
48 | do | |
49 | eval "o=\$$N" | |
50 | git cat-file -t $o && { | |
51 | echo Gaah $N | |
52 | gaah=$N | |
53 | break | |
54 | } | |
55 | done | |
56 | test -z "$gaah" | |
57 | } | |
58 | ||
59 | test_expect_success setup ' | |
60 | mkdir -p A/B && | |
61 | echo rat >C && | |
62 | echo ox >A/D && | |
63 | echo tiger >A/B/E && | |
64 | git add . && | |
65 | ||
66 | test_tick && git commit -m rabbit && | |
2c25eaa1 EP |
67 | H=$(git rev-parse --verify HEAD) && |
68 | A=$(git rev-parse --verify HEAD:A) && | |
69 | B=$(git rev-parse --verify HEAD:A/B) && | |
70 | C=$(git rev-parse --verify HEAD:C) && | |
71 | D=$(git rev-parse --verify HEAD:A/D) && | |
72 | E=$(git rev-parse --verify HEAD:A/B/E) && | |
1389d9dd JH |
73 | check_fsck && |
74 | ||
1f553918 | 75 | test_chmod +x C && |
1389d9dd JH |
76 | git add C && |
77 | test_tick && git commit -m dragon && | |
2c25eaa1 | 78 | L=$(git rev-parse --verify HEAD) && |
1389d9dd JH |
79 | check_fsck && |
80 | ||
81 | rm -f C A/B/E && | |
82 | echo snake >F && | |
83 | echo horse >A/G && | |
84 | git add F A/G && | |
85 | test_tick && git commit -a -m sheep && | |
2c25eaa1 EP |
86 | F=$(git rev-parse --verify HEAD:F) && |
87 | G=$(git rev-parse --verify HEAD:A/G) && | |
88 | I=$(git rev-parse --verify HEAD:A) && | |
89 | J=$(git rev-parse --verify HEAD) && | |
1389d9dd JH |
90 | check_fsck && |
91 | ||
92 | rm -f A/G && | |
93 | test_tick && git commit -a -m monkey && | |
2c25eaa1 | 94 | K=$(git rev-parse --verify HEAD) && |
1389d9dd JH |
95 | check_fsck && |
96 | ||
97 | check_have A B C D E F G H I J K L && | |
98 | ||
026aa938 | 99 | git prune && |
1389d9dd JH |
100 | |
101 | check_have A B C D E F G H I J K L && | |
102 | ||
103 | check_fsck && | |
104 | ||
06d53148 | 105 | git reflog refs/heads/main >output && |
d0ab0584 | 106 | test_line_count = 4 output |
1389d9dd JH |
107 | ' |
108 | ||
e3c36758 ÆAB |
109 | test_expect_success 'correct usage on sub-command -h' ' |
110 | test_expect_code 129 git reflog expire -h >err && | |
111 | grep "git reflog expire" err | |
112 | ' | |
113 | ||
fbc15b13 ÆAB |
114 | test_expect_success 'correct usage on "git reflog show -h"' ' |
115 | test_expect_code 129 git reflog show -h >err && | |
116 | grep -F "git reflog [show]" err | |
117 | ' | |
118 | ||
e3c36758 ÆAB |
119 | test_expect_success 'pass through -- to sub-command' ' |
120 | test_when_finished "rm -rf repo" && | |
121 | git init repo && | |
122 | test_commit -C repo message --a-file contents dash-tag && | |
123 | ||
124 | git -C repo reflog show -- --does-not-exist >out && | |
125 | test_must_be_empty out && | |
126 | git -C repo reflog show >expect && | |
127 | git -C repo reflog show -- --a-file >actual && | |
128 | test_cmp expect actual | |
129 | ' | |
130 | ||
1389d9dd JH |
131 | test_expect_success rewind ' |
132 | test_tick && git reset --hard HEAD~2 && | |
133 | test -f C && | |
134 | test -f A/B/E && | |
135 | ! test -f F && | |
136 | ! test -f A/G && | |
137 | ||
138 | check_have A B C D E F G H I J K L && | |
139 | ||
026aa938 | 140 | git prune && |
1389d9dd JH |
141 | |
142 | check_have A B C D E F G H I J K L && | |
143 | ||
06d53148 | 144 | git reflog refs/heads/main >output && |
d0ab0584 | 145 | test_line_count = 5 output |
1389d9dd JH |
146 | ' |
147 | ||
148 | test_expect_success 'corrupt and check' ' | |
149 | ||
150 | corrupt $F && | |
151 | check_fsck "missing blob $F" | |
152 | ||
153 | ' | |
154 | ||
155 | test_expect_success 'reflog expire --dry-run should not touch reflog' ' | |
156 | ||
157 | git reflog expire --dry-run \ | |
158 | --expire=$(($test_tick - 10000)) \ | |
159 | --expire-unreachable=$(($test_tick - 10000)) \ | |
160 | --stale-fix \ | |
161 | --all && | |
162 | ||
06d53148 | 163 | git reflog refs/heads/main >output && |
d0ab0584 | 164 | test_line_count = 5 output && |
1389d9dd JH |
165 | |
166 | check_fsck "missing blob $F" | |
167 | ' | |
168 | ||
169 | test_expect_success 'reflog expire' ' | |
170 | ||
171 | git reflog expire --verbose \ | |
172 | --expire=$(($test_tick - 10000)) \ | |
173 | --expire-unreachable=$(($test_tick - 10000)) \ | |
174 | --stale-fix \ | |
175 | --all && | |
176 | ||
06d53148 | 177 | git reflog refs/heads/main >output && |
d0ab0584 | 178 | test_line_count = 2 output && |
1389d9dd JH |
179 | |
180 | check_fsck "dangling commit $K" | |
181 | ' | |
182 | ||
c809798b JS |
183 | test_expect_success '--stale-fix handles missing objects generously' ' |
184 | git -c core.logAllRefUpdates=false fast-import --date-format=now <<-EOS && | |
185 | commit refs/heads/stale-fix | |
186 | mark :1 | |
187 | committer Author <a@uth.or> now | |
188 | data <<EOF | |
189 | start stale fix | |
190 | EOF | |
191 | M 100644 inline file | |
192 | data <<EOF | |
193 | contents | |
194 | EOF | |
195 | commit refs/heads/stale-fix | |
196 | committer Author <a@uth.or> now | |
197 | data <<EOF | |
198 | stale fix branch tip | |
199 | EOF | |
200 | from :1 | |
201 | EOS | |
202 | ||
203 | parent_oid=$(git rev-parse stale-fix^) && | |
204 | test_when_finished "recover $parent_oid" && | |
205 | corrupt $parent_oid && | |
206 | git reflog expire --stale-fix | |
207 | ' | |
208 | ||
1389d9dd JH |
209 | test_expect_success 'prune and fsck' ' |
210 | ||
026aa938 | 211 | git prune && |
1389d9dd JH |
212 | check_fsck && |
213 | ||
214 | check_have A B C D E H L && | |
215 | check_dont_have F G I J K | |
216 | ||
217 | ' | |
218 | ||
219 | test_expect_success 'recover and check' ' | |
220 | ||
221 | recover $F && | |
222 | check_fsck "dangling blob $F" | |
223 | ||
224 | ' | |
225 | ||
55beff4f | 226 | test_expect_success 'delete' ' |
552cecc2 JS |
227 | echo 1 > C && |
228 | test_tick && | |
229 | git commit -m rat C && | |
230 | ||
231 | echo 2 > C && | |
232 | test_tick && | |
233 | git commit -m ox C && | |
234 | ||
235 | echo 3 > C && | |
236 | test_tick && | |
237 | git commit -m tiger C && | |
238 | ||
a48fcd83 | 239 | HEAD_entry_count=$(git reflog | wc -l) && |
06d53148 | 240 | main_entry_count=$(git reflog show main | wc -l) && |
38881a90 PB |
241 | |
242 | test $HEAD_entry_count = 5 && | |
06d53148 | 243 | test $main_entry_count = 5 && |
38881a90 | 244 | |
552cecc2 | 245 | |
06d53148 JS |
246 | git reflog delete main@{1} && |
247 | git reflog show main > output && | |
248 | test_line_count = $(($main_entry_count - 1)) output && | |
38881a90 | 249 | test $HEAD_entry_count = $(git reflog | wc -l) && |
552cecc2 JS |
250 | ! grep ox < output && |
251 | ||
06d53148 | 252 | main_entry_count=$(wc -l < output) && |
38881a90 PB |
253 | |
254 | git reflog delete HEAD@{1} && | |
255 | test $(($HEAD_entry_count -1)) = $(git reflog | wc -l) && | |
06d53148 | 256 | test $main_entry_count = $(git reflog show main | wc -l) && |
38881a90 | 257 | |
a48fcd83 | 258 | HEAD_entry_count=$(git reflog | wc -l) && |
38881a90 | 259 | |
06d53148 JS |
260 | git reflog delete main@{07.04.2005.15:15:00.-0700} && |
261 | git reflog show main > output && | |
262 | test_line_count = $(($main_entry_count - 1)) output && | |
552cecc2 | 263 | ! grep dragon < output |
50f3ac29 JH |
264 | |
265 | ' | |
266 | ||
4a9f4394 AS |
267 | test_expect_success 'rewind2' ' |
268 | ||
269 | test_tick && git reset --hard HEAD~2 && | |
06d53148 | 270 | git reflog refs/heads/main >output && |
d0ab0584 | 271 | test_line_count = 4 output |
4a9f4394 AS |
272 | ' |
273 | ||
274 | test_expect_success '--expire=never' ' | |
275 | ||
276 | git reflog expire --verbose \ | |
277 | --expire=never \ | |
278 | --expire-unreachable=never \ | |
279 | --all && | |
06d53148 | 280 | git reflog refs/heads/main >output && |
d0ab0584 | 281 | test_line_count = 4 output |
4a9f4394 AS |
282 | ' |
283 | ||
284 | test_expect_success 'gc.reflogexpire=never' ' | |
a65bf78c ÆAB |
285 | test_config gc.reflogexpire never && |
286 | test_config gc.reflogexpireunreachable never && | |
4a9f4394 | 287 | |
978f4307 ÆAB |
288 | git reflog expire --verbose --all >output && |
289 | test_line_count = 9 output && | |
290 | ||
06d53148 | 291 | git reflog refs/heads/main >output && |
d0ab0584 | 292 | test_line_count = 4 output |
4a9f4394 AS |
293 | ' |
294 | ||
295 | test_expect_success 'gc.reflogexpire=false' ' | |
a65bf78c ÆAB |
296 | test_config gc.reflogexpire false && |
297 | test_config gc.reflogexpireunreachable false && | |
4a9f4394 | 298 | |
4a9f4394 | 299 | git reflog expire --verbose --all && |
06d53148 | 300 | git reflog refs/heads/main >output && |
a65bf78c | 301 | test_line_count = 4 output |
4a9f4394 AS |
302 | |
303 | ' | |
304 | ||
fe66776d ÆAB |
305 | test_expect_success 'git reflog expire unknown reference' ' |
306 | test_config gc.reflogexpire never && | |
307 | test_config gc.reflogexpireunreachable never && | |
308 | ||
06d53148 | 309 | test_must_fail git reflog expire main@{123} 2>stderr && |
fe66776d ÆAB |
310 | test_i18ngrep "points nowhere" stderr && |
311 | test_must_fail git reflog expire does-not-exist 2>stderr && | |
312 | test_i18ngrep "points nowhere" stderr | |
313 | ' | |
314 | ||
482b8f32 | 315 | test_expect_success 'checkout should not delete log for packed ref' ' |
06d53148 | 316 | test $(git reflog main | wc -l) = 4 && |
482b8f32 RS |
317 | git branch foo && |
318 | git pack-refs --all && | |
319 | git checkout foo && | |
06d53148 | 320 | test $(git reflog main | wc -l) = 4 |
482b8f32 RS |
321 | ' |
322 | ||
9233887c | 323 | test_expect_success 'stale dirs do not cause d/f conflicts (reflogs on)' ' |
aae828b9 | 324 | test_when_finished "git branch -d one || git branch -d one/two" && |
9233887c | 325 | |
06d53148 JS |
326 | git branch one/two main && |
327 | echo "one/two@{0} branch: Created from main" >expect && | |
aae828b9 | 328 | git log -g --format="%gd %gs" one/two >actual && |
9233887c | 329 | test_cmp expect actual && |
aae828b9 | 330 | git branch -d one/two && |
9233887c | 331 | |
aae828b9 JK |
332 | # now logs/refs/heads/one is a stale directory, but |
333 | # we should move it out of the way to create "one" reflog | |
06d53148 JS |
334 | git branch one main && |
335 | echo "one@{0} branch: Created from main" >expect && | |
aae828b9 | 336 | git log -g --format="%gd %gs" one >actual && |
9233887c JK |
337 | test_cmp expect actual |
338 | ' | |
339 | ||
340 | test_expect_success 'stale dirs do not cause d/f conflicts (reflogs off)' ' | |
aae828b9 | 341 | test_when_finished "git branch -d one || git branch -d one/two" && |
9233887c | 342 | |
06d53148 JS |
343 | git branch one/two main && |
344 | echo "one/two@{0} branch: Created from main" >expect && | |
aae828b9 | 345 | git log -g --format="%gd %gs" one/two >actual && |
9233887c | 346 | test_cmp expect actual && |
aae828b9 | 347 | git branch -d one/two && |
9233887c | 348 | |
aae828b9 | 349 | # same as before, but we only create a reflog for "one" if |
9233887c | 350 | # it already exists, which it does not |
06d53148 | 351 | git -c core.logallrefupdates=false branch one main && |
aae828b9 | 352 | git log -g --format="%gd %gs" one >actual && |
1c5e94f4 | 353 | test_must_be_empty actual |
9233887c JK |
354 | ' |
355 | ||
e5e73ff2 JK |
356 | # Triggering the bug detected by this test requires a newline to fall |
357 | # exactly BUFSIZ-1 bytes from the end of the file. We don't know | |
358 | # what that value is, since it's platform dependent. However, if | |
359 | # we choose some value N, we also catch any D which divides N evenly | |
360 | # (since we will read backwards in chunks of D). So we choose 8K, | |
361 | # which catches glibc (with an 8K BUFSIZ) and *BSD (1K). | |
362 | # | |
363 | # Each line is 114 characters, so we need 75 to still have a few before the | |
364 | # last 8K. The 89-character padding on the final entry lines up our | |
365 | # newline exactly. | |
bcdff626 | 366 | test_expect_success REFFILES,SHA1 'parsing reverse reflogs at BUFSIZ boundaries' ' |
e5e73ff2 | 367 | git checkout -b reflogskip && |
ff4cb42e | 368 | zf=$(test_oid zero_2) && |
e5e73ff2 JK |
369 | ident="abc <xyz> 0000000001 +0000" && |
370 | for i in $(test_seq 1 75); do | |
ff4cb42e | 371 | printf "$zf%02d $zf%02d %s\t" $i $(($i+1)) "$ident" && |
e5e73ff2 JK |
372 | if test $i = 75; then |
373 | for j in $(test_seq 1 89); do | |
db5875aa | 374 | printf X || return 1 |
e5e73ff2 JK |
375 | done |
376 | else | |
377 | printf X | |
378 | fi && | |
db5875aa | 379 | printf "\n" || return 1 |
e5e73ff2 JK |
380 | done >.git/logs/refs/heads/reflogskip && |
381 | git rev-parse reflogskip@{73} >actual && | |
ff4cb42e | 382 | echo ${zf}03 >expect && |
e5e73ff2 JK |
383 | test_cmp expect actual |
384 | ' | |
385 | ||
aecad374 DK |
386 | test_expect_success 'no segfaults for reflog containing non-commit sha1s' ' |
387 | git update-ref --create-reflog -m "Creating ref" \ | |
388 | refs/tests/tree-in-reflog HEAD && | |
389 | git update-ref -m "Forcing tree" refs/tests/tree-in-reflog HEAD^{tree} && | |
390 | git update-ref -m "Restoring to commit" refs/tests/tree-in-reflog HEAD && | |
391 | git reflog refs/tests/tree-in-reflog | |
392 | ' | |
393 | ||
394 | test_expect_failure 'reflog with non-commit entries displays all entries' ' | |
395 | git reflog refs/tests/tree-in-reflog >actual && | |
396 | test_line_count = 3 actual | |
397 | ' | |
398 | ||
fe144315 HWN |
399 | # This test takes a lock on an individual ref; this is not supported in |
400 | # reftable. | |
401 | test_expect_success REFFILES 'reflog expire operates on symref not referrent' ' | |
7687f19e JK |
402 | git branch --create-reflog the_symref && |
403 | git branch --create-reflog referrent && | |
41d796ed DT |
404 | git update-ref referrent HEAD && |
405 | git symbolic-ref refs/heads/the_symref refs/heads/referrent && | |
406 | test_when_finished "rm -f .git/refs/heads/referrent.lock" && | |
407 | touch .git/refs/heads/referrent.lock && | |
408 | git reflog expire --expire=all the_symref | |
409 | ' | |
410 | ||
71abeb75 SG |
411 | test_expect_success 'continue walking past root commits' ' |
412 | git init orphanage && | |
413 | ( | |
414 | cd orphanage && | |
415 | cat >expect <<-\EOF && | |
416 | HEAD@{0} commit (initial): orphan2-1 | |
417 | HEAD@{1} commit: orphan1-2 | |
418 | HEAD@{2} commit (initial): orphan1-1 | |
419 | HEAD@{3} commit (initial): initial | |
420 | EOF | |
421 | test_commit initial && | |
71abeb75 SG |
422 | git checkout --orphan orphan1 && |
423 | test_commit orphan1-1 && | |
424 | test_commit orphan1-2 && | |
425 | git checkout --orphan orphan2 && | |
426 | test_commit orphan2-1 && | |
427 | git log -g --format="%gd %gs" >actual && | |
428 | test_cmp expect actual | |
429 | ) | |
430 | ' | |
431 | ||
c9ef0d95 NTND |
432 | test_expect_success 'expire with multiple worktrees' ' |
433 | git init main-wt && | |
434 | ( | |
435 | cd main-wt && | |
436 | test_tick && | |
437 | test_commit foo && | |
438 | git worktree add link-wt && | |
439 | test_tick && | |
440 | test_commit -C link-wt foobar && | |
441 | test_tick && | |
442 | git reflog expire --verbose --all --expire=$test_tick && | |
8c2d8d04 HWN |
443 | test-tool ref-store worktree:link-wt for-each-reflog-ent HEAD >actual && |
444 | test_must_be_empty actual | |
c9ef0d95 NTND |
445 | ) |
446 | ' | |
447 | ||
57be9c6d ÆAB |
448 | test_expect_success REFFILES 'empty reflog' ' |
449 | test_when_finished "rm -rf empty" && | |
450 | git init empty && | |
451 | test_commit -C empty A && | |
452 | >empty/.git/logs/refs/heads/foo && | |
453 | git -C empty reflog expire --all 2>err && | |
454 | test_must_be_empty err | |
455 | ' | |
456 | ||
1389d9dd | 457 | test_done |