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