use strpbrk(3) to search for characters from a given set
[git/debian.git] / t / t1410-reflog.sh
blob76d9b744a64b1b67e4093c262b7d6de302e29ff6
1 #!/bin/sh
3 # Copyright (c) 2007 Junio C Hamano
6 test_description='Test prune and reflog expiration'
7 . ./test-lib.sh
9 check_have () {
10 gaah= &&
11 for N in "$@"
13 eval "o=\$$N" && git cat-file -t $o || {
14 echo Gaah $N
15 gaah=$N
16 break
18 done &&
19 test -z "$gaah"
22 check_fsck () {
23 git fsck --full >fsck.output
24 case "$1" in
25 '')
26 test_must_be_empty fsck.output ;;
28 test_i18ngrep "$1" fsck.output ;;
29 esac
32 corrupt () {
33 mv .git/objects/$(test_oid_to_path $1) .git/$1
36 recover () {
37 aa=$(echo $1 | cut -c 1-2)
38 mkdir -p .git/objects/$aa
39 mv .git/$1 .git/objects/$(test_oid_to_path $1)
42 check_dont_have () {
43 gaah= &&
44 for N in "$@"
46 eval "o=\$$N"
47 git cat-file -t $o && {
48 echo Gaah $N
49 gaah=$N
50 break
52 done
53 test -z "$gaah"
56 test_expect_success setup '
57 test_oid_init &&
58 mkdir -p A/B &&
59 echo rat >C &&
60 echo ox >A/D &&
61 echo tiger >A/B/E &&
62 git add . &&
64 test_tick && git commit -m rabbit &&
65 H=$(git rev-parse --verify HEAD) &&
66 A=$(git rev-parse --verify HEAD:A) &&
67 B=$(git rev-parse --verify HEAD:A/B) &&
68 C=$(git rev-parse --verify HEAD:C) &&
69 D=$(git rev-parse --verify HEAD:A/D) &&
70 E=$(git rev-parse --verify HEAD:A/B/E) &&
71 check_fsck &&
73 test_chmod +x C &&
74 git add C &&
75 test_tick && git commit -m dragon &&
76 L=$(git rev-parse --verify HEAD) &&
77 check_fsck &&
79 rm -f C A/B/E &&
80 echo snake >F &&
81 echo horse >A/G &&
82 git add F A/G &&
83 test_tick && git commit -a -m sheep &&
84 F=$(git rev-parse --verify HEAD:F) &&
85 G=$(git rev-parse --verify HEAD:A/G) &&
86 I=$(git rev-parse --verify HEAD:A) &&
87 J=$(git rev-parse --verify HEAD) &&
88 check_fsck &&
90 rm -f A/G &&
91 test_tick && git commit -a -m monkey &&
92 K=$(git rev-parse --verify HEAD) &&
93 check_fsck &&
95 check_have A B C D E F G H I J K L &&
97 git prune &&
99 check_have A B C D E F G H I J K L &&
101 check_fsck &&
103 git reflog refs/heads/master >output &&
104 test_line_count = 4 output
107 test_expect_success rewind '
108 test_tick && git reset --hard HEAD~2 &&
109 test -f C &&
110 test -f A/B/E &&
111 ! test -f F &&
112 ! test -f A/G &&
114 check_have A B C D E F G H I J K L &&
116 git prune &&
118 check_have A B C D E F G H I J K L &&
120 git reflog refs/heads/master >output &&
121 test_line_count = 5 output
124 test_expect_success 'corrupt and check' '
126 corrupt $F &&
127 check_fsck "missing blob $F"
131 test_expect_success 'reflog expire --dry-run should not touch reflog' '
133 git reflog expire --dry-run \
134 --expire=$(($test_tick - 10000)) \
135 --expire-unreachable=$(($test_tick - 10000)) \
136 --stale-fix \
137 --all &&
139 git reflog refs/heads/master >output &&
140 test_line_count = 5 output &&
142 check_fsck "missing blob $F"
145 test_expect_success 'reflog expire' '
147 git reflog expire --verbose \
148 --expire=$(($test_tick - 10000)) \
149 --expire-unreachable=$(($test_tick - 10000)) \
150 --stale-fix \
151 --all &&
153 git reflog refs/heads/master >output &&
154 test_line_count = 2 output &&
156 check_fsck "dangling commit $K"
159 test_expect_success 'prune and fsck' '
161 git prune &&
162 check_fsck &&
164 check_have A B C D E H L &&
165 check_dont_have F G I J K
169 test_expect_success 'recover and check' '
171 recover $F &&
172 check_fsck "dangling blob $F"
176 test_expect_success 'delete' '
177 echo 1 > C &&
178 test_tick &&
179 git commit -m rat C &&
181 echo 2 > C &&
182 test_tick &&
183 git commit -m ox C &&
185 echo 3 > C &&
186 test_tick &&
187 git commit -m tiger C &&
189 HEAD_entry_count=$(git reflog | wc -l) &&
190 master_entry_count=$(git reflog show master | wc -l) &&
192 test $HEAD_entry_count = 5 &&
193 test $master_entry_count = 5 &&
196 git reflog delete master@{1} &&
197 git reflog show master > output &&
198 test_line_count = $(($master_entry_count - 1)) output &&
199 test $HEAD_entry_count = $(git reflog | wc -l) &&
200 ! grep ox < output &&
202 master_entry_count=$(wc -l < output) &&
204 git reflog delete HEAD@{1} &&
205 test $(($HEAD_entry_count -1)) = $(git reflog | wc -l) &&
206 test $master_entry_count = $(git reflog show master | wc -l) &&
208 HEAD_entry_count=$(git reflog | wc -l) &&
210 git reflog delete master@{07.04.2005.15:15:00.-0700} &&
211 git reflog show master > output &&
212 test_line_count = $(($master_entry_count - 1)) output &&
213 ! grep dragon < output
217 test_expect_success 'rewind2' '
219 test_tick && git reset --hard HEAD~2 &&
220 git reflog refs/heads/master >output &&
221 test_line_count = 4 output
224 test_expect_success '--expire=never' '
226 git reflog expire --verbose \
227 --expire=never \
228 --expire-unreachable=never \
229 --all &&
230 git reflog refs/heads/master >output &&
231 test_line_count = 4 output
234 test_expect_success 'gc.reflogexpire=never' '
235 test_config gc.reflogexpire never &&
236 test_config gc.reflogexpireunreachable never &&
238 git reflog expire --verbose --all >output &&
239 test_line_count = 9 output &&
241 git reflog refs/heads/master >output &&
242 test_line_count = 4 output
245 test_expect_success 'gc.reflogexpire=false' '
246 test_config gc.reflogexpire false &&
247 test_config gc.reflogexpireunreachable false &&
249 git reflog expire --verbose --all &&
250 git reflog refs/heads/master >output &&
251 test_line_count = 4 output
255 test_expect_success 'git reflog expire unknown reference' '
256 test_config gc.reflogexpire never &&
257 test_config gc.reflogexpireunreachable never &&
259 test_must_fail git reflog expire master@{123} 2>stderr &&
260 test_i18ngrep "points nowhere" stderr &&
261 test_must_fail git reflog expire does-not-exist 2>stderr &&
262 test_i18ngrep "points nowhere" stderr
265 test_expect_success 'checkout should not delete log for packed ref' '
266 test $(git reflog master | wc -l) = 4 &&
267 git branch foo &&
268 git pack-refs --all &&
269 git checkout foo &&
270 test $(git reflog master | wc -l) = 4
273 test_expect_success 'stale dirs do not cause d/f conflicts (reflogs on)' '
274 test_when_finished "git branch -d one || git branch -d one/two" &&
276 git branch one/two master &&
277 echo "one/two@{0} branch: Created from master" >expect &&
278 git log -g --format="%gd %gs" one/two >actual &&
279 test_cmp expect actual &&
280 git branch -d one/two &&
282 # now logs/refs/heads/one is a stale directory, but
283 # we should move it out of the way to create "one" reflog
284 git branch one master &&
285 echo "one@{0} branch: Created from master" >expect &&
286 git log -g --format="%gd %gs" one >actual &&
287 test_cmp expect actual
290 test_expect_success 'stale dirs do not cause d/f conflicts (reflogs off)' '
291 test_when_finished "git branch -d one || git branch -d one/two" &&
293 git branch one/two master &&
294 echo "one/two@{0} branch: Created from master" >expect &&
295 git log -g --format="%gd %gs" one/two >actual &&
296 test_cmp expect actual &&
297 git branch -d one/two &&
299 # same as before, but we only create a reflog for "one" if
300 # it already exists, which it does not
301 git -c core.logallrefupdates=false branch one master &&
302 git log -g --format="%gd %gs" one >actual &&
303 test_must_be_empty actual
306 # Triggering the bug detected by this test requires a newline to fall
307 # exactly BUFSIZ-1 bytes from the end of the file. We don't know
308 # what that value is, since it's platform dependent. However, if
309 # we choose some value N, we also catch any D which divides N evenly
310 # (since we will read backwards in chunks of D). So we choose 8K,
311 # which catches glibc (with an 8K BUFSIZ) and *BSD (1K).
313 # Each line is 114 characters, so we need 75 to still have a few before the
314 # last 8K. The 89-character padding on the final entry lines up our
315 # newline exactly.
316 test_expect_success SHA1 'parsing reverse reflogs at BUFSIZ boundaries' '
317 git checkout -b reflogskip &&
318 zf=$(test_oid zero_2) &&
319 ident="abc <xyz> 0000000001 +0000" &&
320 for i in $(test_seq 1 75); do
321 printf "$zf%02d $zf%02d %s\t" $i $(($i+1)) "$ident" &&
322 if test $i = 75; then
323 for j in $(test_seq 1 89); do
324 printf X
325 done
326 else
327 printf X
328 fi &&
329 printf "\n"
330 done >.git/logs/refs/heads/reflogskip &&
331 git rev-parse reflogskip@{73} >actual &&
332 echo ${zf}03 >expect &&
333 test_cmp expect actual
336 test_expect_success 'no segfaults for reflog containing non-commit sha1s' '
337 git update-ref --create-reflog -m "Creating ref" \
338 refs/tests/tree-in-reflog HEAD &&
339 git update-ref -m "Forcing tree" refs/tests/tree-in-reflog HEAD^{tree} &&
340 git update-ref -m "Restoring to commit" refs/tests/tree-in-reflog HEAD &&
341 git reflog refs/tests/tree-in-reflog
344 test_expect_failure 'reflog with non-commit entries displays all entries' '
345 git reflog refs/tests/tree-in-reflog >actual &&
346 test_line_count = 3 actual
349 test_expect_success 'reflog expire operates on symref not referrent' '
350 git branch --create-reflog the_symref &&
351 git branch --create-reflog referrent &&
352 git update-ref referrent HEAD &&
353 git symbolic-ref refs/heads/the_symref refs/heads/referrent &&
354 test_when_finished "rm -f .git/refs/heads/referrent.lock" &&
355 touch .git/refs/heads/referrent.lock &&
356 git reflog expire --expire=all the_symref
359 test_expect_success 'continue walking past root commits' '
360 git init orphanage &&
362 cd orphanage &&
363 cat >expect <<-\EOF &&
364 HEAD@{0} commit (initial): orphan2-1
365 HEAD@{1} commit: orphan1-2
366 HEAD@{2} commit (initial): orphan1-1
367 HEAD@{3} commit (initial): initial
369 test_commit initial &&
370 git checkout --orphan orphan1 &&
371 test_commit orphan1-1 &&
372 test_commit orphan1-2 &&
373 git checkout --orphan orphan2 &&
374 test_commit orphan2-1 &&
375 git log -g --format="%gd %gs" >actual &&
376 test_cmp expect actual
380 test_expect_success 'expire with multiple worktrees' '
381 git init main-wt &&
383 cd main-wt &&
384 test_tick &&
385 test_commit foo &&
386 git worktree add link-wt &&
387 test_tick &&
388 test_commit -C link-wt foobar &&
389 test_tick &&
390 git reflog expire --verbose --all --expire=$test_tick &&
391 test_must_be_empty .git/worktrees/link-wt/logs/HEAD
395 test_done