The second batch
[git] / t / t1410-reflog.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Junio C Hamano
4 #
5
6 test_description='Test prune and reflog expiration'
7 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
8 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
9
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 () {
26         git fsck --full >fsck.output
27         case "$1" in
28         '')
29                 test_must_be_empty fsck.output ;;
30         *)
31                 test_i18ngrep "$1" fsck.output ;;
32         esac
33 }
34
35 corrupt () {
36         mv .git/objects/$(test_oid_to_path $1) .git/$1
37 }
38
39 recover () {
40         aa=$(echo $1 | cut -c 1-2)
41         mkdir -p .git/objects/$aa
42         mv .git/$1 .git/objects/$(test_oid_to_path $1)
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 &&
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) &&
73         check_fsck &&
74
75         test_chmod +x C &&
76         git add C &&
77         test_tick && git commit -m dragon &&
78         L=$(git rev-parse --verify HEAD) &&
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 &&
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) &&
90         check_fsck &&
91
92         rm -f A/G &&
93         test_tick && git commit -a -m monkey &&
94         K=$(git rev-parse --verify HEAD) &&
95         check_fsck &&
96
97         check_have A B C D E F G H I J K L &&
98
99         git prune &&
100
101         check_have A B C D E F G H I J K L &&
102
103         check_fsck &&
104
105         git reflog refs/heads/main >output &&
106         test_line_count = 4 output
107 '
108
109 test_expect_success rewind '
110         test_tick && git reset --hard HEAD~2 &&
111         test -f C &&
112         test -f A/B/E &&
113         ! test -f F &&
114         ! test -f A/G &&
115
116         check_have A B C D E F G H I J K L &&
117
118         git prune &&
119
120         check_have A B C D E F G H I J K L &&
121
122         git reflog refs/heads/main >output &&
123         test_line_count = 5 output
124 '
125
126 test_expect_success 'corrupt and check' '
127
128         corrupt $F &&
129         check_fsck "missing blob $F"
130
131 '
132
133 test_expect_success 'reflog expire --dry-run should not touch reflog' '
134
135         git reflog expire --dry-run \
136                 --expire=$(($test_tick - 10000)) \
137                 --expire-unreachable=$(($test_tick - 10000)) \
138                 --stale-fix \
139                 --all &&
140
141         git reflog refs/heads/main >output &&
142         test_line_count = 5 output &&
143
144         check_fsck "missing blob $F"
145 '
146
147 test_expect_success 'reflog expire' '
148
149         git reflog expire --verbose \
150                 --expire=$(($test_tick - 10000)) \
151                 --expire-unreachable=$(($test_tick - 10000)) \
152                 --stale-fix \
153                 --all &&
154
155         git reflog refs/heads/main >output &&
156         test_line_count = 2 output &&
157
158         check_fsck "dangling commit $K"
159 '
160
161 test_expect_success '--stale-fix handles missing objects generously' '
162         git -c core.logAllRefUpdates=false fast-import --date-format=now <<-EOS &&
163         commit refs/heads/stale-fix
164         mark :1
165         committer Author <a@uth.or> now
166         data <<EOF
167         start stale fix
168         EOF
169         M 100644 inline file
170         data <<EOF
171         contents
172         EOF
173         commit refs/heads/stale-fix
174         committer Author <a@uth.or> now
175         data <<EOF
176         stale fix branch tip
177         EOF
178         from :1
179         EOS
180
181         parent_oid=$(git rev-parse stale-fix^) &&
182         test_when_finished "recover $parent_oid" &&
183         corrupt $parent_oid &&
184         git reflog expire --stale-fix
185 '
186
187 test_expect_success 'prune and fsck' '
188
189         git prune &&
190         check_fsck &&
191
192         check_have A B C D E H L &&
193         check_dont_have F G I J K
194
195 '
196
197 test_expect_success 'recover and check' '
198
199         recover $F &&
200         check_fsck "dangling blob $F"
201
202 '
203
204 test_expect_success 'delete' '
205         echo 1 > C &&
206         test_tick &&
207         git commit -m rat C &&
208
209         echo 2 > C &&
210         test_tick &&
211         git commit -m ox C &&
212
213         echo 3 > C &&
214         test_tick &&
215         git commit -m tiger C &&
216
217         HEAD_entry_count=$(git reflog | wc -l) &&
218         main_entry_count=$(git reflog show main | wc -l) &&
219
220         test $HEAD_entry_count = 5 &&
221         test $main_entry_count = 5 &&
222
223
224         git reflog delete main@{1} &&
225         git reflog show main > output &&
226         test_line_count = $(($main_entry_count - 1)) output &&
227         test $HEAD_entry_count = $(git reflog | wc -l) &&
228         ! grep ox < output &&
229
230         main_entry_count=$(wc -l < output) &&
231
232         git reflog delete HEAD@{1} &&
233         test $(($HEAD_entry_count -1)) = $(git reflog | wc -l) &&
234         test $main_entry_count = $(git reflog show main | wc -l) &&
235
236         HEAD_entry_count=$(git reflog | wc -l) &&
237
238         git reflog delete main@{07.04.2005.15:15:00.-0700} &&
239         git reflog show main > output &&
240         test_line_count = $(($main_entry_count - 1)) output &&
241         ! grep dragon < output
242
243 '
244
245 test_expect_success 'rewind2' '
246
247         test_tick && git reset --hard HEAD~2 &&
248         git reflog refs/heads/main >output &&
249         test_line_count = 4 output
250 '
251
252 test_expect_success '--expire=never' '
253
254         git reflog expire --verbose \
255                 --expire=never \
256                 --expire-unreachable=never \
257                 --all &&
258         git reflog refs/heads/main >output &&
259         test_line_count = 4 output
260 '
261
262 test_expect_success 'gc.reflogexpire=never' '
263         test_config gc.reflogexpire never &&
264         test_config gc.reflogexpireunreachable never &&
265
266         git reflog expire --verbose --all >output &&
267         test_line_count = 9 output &&
268
269         git reflog refs/heads/main >output &&
270         test_line_count = 4 output
271 '
272
273 test_expect_success 'gc.reflogexpire=false' '
274         test_config gc.reflogexpire false &&
275         test_config gc.reflogexpireunreachable false &&
276
277         git reflog expire --verbose --all &&
278         git reflog refs/heads/main >output &&
279         test_line_count = 4 output
280
281 '
282
283 test_expect_success 'git reflog expire unknown reference' '
284         test_config gc.reflogexpire never &&
285         test_config gc.reflogexpireunreachable never &&
286
287         test_must_fail git reflog expire main@{123} 2>stderr &&
288         test_i18ngrep "points nowhere" stderr &&
289         test_must_fail git reflog expire does-not-exist 2>stderr &&
290         test_i18ngrep "points nowhere" stderr
291 '
292
293 test_expect_success 'checkout should not delete log for packed ref' '
294         test $(git reflog main | wc -l) = 4 &&
295         git branch foo &&
296         git pack-refs --all &&
297         git checkout foo &&
298         test $(git reflog main | wc -l) = 4
299 '
300
301 test_expect_success 'stale dirs do not cause d/f conflicts (reflogs on)' '
302         test_when_finished "git branch -d one || git branch -d one/two" &&
303
304         git branch one/two main &&
305         echo "one/two@{0} branch: Created from main" >expect &&
306         git log -g --format="%gd %gs" one/two >actual &&
307         test_cmp expect actual &&
308         git branch -d one/two &&
309
310         # now logs/refs/heads/one is a stale directory, but
311         # we should move it out of the way to create "one" reflog
312         git branch one main &&
313         echo "one@{0} branch: Created from main" >expect &&
314         git log -g --format="%gd %gs" one >actual &&
315         test_cmp expect actual
316 '
317
318 test_expect_success 'stale dirs do not cause d/f conflicts (reflogs off)' '
319         test_when_finished "git branch -d one || git branch -d one/two" &&
320
321         git branch one/two main &&
322         echo "one/two@{0} branch: Created from main" >expect &&
323         git log -g --format="%gd %gs" one/two >actual &&
324         test_cmp expect actual &&
325         git branch -d one/two &&
326
327         # same as before, but we only create a reflog for "one" if
328         # it already exists, which it does not
329         git -c core.logallrefupdates=false branch one main &&
330         git log -g --format="%gd %gs" one >actual &&
331         test_must_be_empty actual
332 '
333
334 # Triggering the bug detected by this test requires a newline to fall
335 # exactly BUFSIZ-1 bytes from the end of the file. We don't know
336 # what that value is, since it's platform dependent. However, if
337 # we choose some value N, we also catch any D which divides N evenly
338 # (since we will read backwards in chunks of D). So we choose 8K,
339 # which catches glibc (with an 8K BUFSIZ) and *BSD (1K).
340 #
341 # Each line is 114 characters, so we need 75 to still have a few before the
342 # last 8K. The 89-character padding on the final entry lines up our
343 # newline exactly.
344 test_expect_success SHA1 'parsing reverse reflogs at BUFSIZ boundaries' '
345         git checkout -b reflogskip &&
346         zf=$(test_oid zero_2) &&
347         ident="abc <xyz> 0000000001 +0000" &&
348         for i in $(test_seq 1 75); do
349                 printf "$zf%02d $zf%02d %s\t" $i $(($i+1)) "$ident" &&
350                 if test $i = 75; then
351                         for j in $(test_seq 1 89); do
352                                 printf X
353                         done
354                 else
355                         printf X
356                 fi &&
357                 printf "\n"
358         done >.git/logs/refs/heads/reflogskip &&
359         git rev-parse reflogskip@{73} >actual &&
360         echo ${zf}03 >expect &&
361         test_cmp expect actual
362 '
363
364 test_expect_success 'no segfaults for reflog containing non-commit sha1s' '
365         git update-ref --create-reflog -m "Creating ref" \
366                 refs/tests/tree-in-reflog HEAD &&
367         git update-ref -m "Forcing tree" refs/tests/tree-in-reflog HEAD^{tree} &&
368         git update-ref -m "Restoring to commit" refs/tests/tree-in-reflog HEAD &&
369         git reflog refs/tests/tree-in-reflog
370 '
371
372 test_expect_failure 'reflog with non-commit entries displays all entries' '
373         git reflog refs/tests/tree-in-reflog >actual &&
374         test_line_count = 3 actual
375 '
376
377 test_expect_success 'reflog expire operates on symref not referrent' '
378         git branch --create-reflog the_symref &&
379         git branch --create-reflog referrent &&
380         git update-ref referrent HEAD &&
381         git symbolic-ref refs/heads/the_symref refs/heads/referrent &&
382         test_when_finished "rm -f .git/refs/heads/referrent.lock" &&
383         touch .git/refs/heads/referrent.lock &&
384         git reflog expire --expire=all the_symref
385 '
386
387 test_expect_success 'continue walking past root commits' '
388         git init orphanage &&
389         (
390                 cd orphanage &&
391                 cat >expect <<-\EOF &&
392                 HEAD@{0} commit (initial): orphan2-1
393                 HEAD@{1} commit: orphan1-2
394                 HEAD@{2} commit (initial): orphan1-1
395                 HEAD@{3} commit (initial): initial
396                 EOF
397                 test_commit initial &&
398                 git checkout --orphan orphan1 &&
399                 test_commit orphan1-1 &&
400                 test_commit orphan1-2 &&
401                 git checkout --orphan orphan2 &&
402                 test_commit orphan2-1 &&
403                 git log -g --format="%gd %gs" >actual &&
404                 test_cmp expect actual
405         )
406 '
407
408 test_expect_success 'expire with multiple worktrees' '
409         git init main-wt &&
410         (
411                 cd main-wt &&
412                 test_tick &&
413                 test_commit foo &&
414                 git  worktree add link-wt &&
415                 test_tick &&
416                 test_commit -C link-wt foobar &&
417                 test_tick &&
418                 git reflog expire --verbose --all --expire=$test_tick &&
419                 test_must_be_empty .git/worktrees/link-wt/logs/HEAD
420         )
421 '
422
423 test_done