Revert "git-am: add am.threeWay config variable"
[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         output=$(git fsck --full)
24         case "$1" in
25         '')
26                 test -z "$output" ;;
27         *)
28                 echo "$output" | grep "$1" ;;
29         esac
30 }
31
32 corrupt () {
33         aa=${1%??????????????????????????????????????} zz=${1#??}
34         mv .git/objects/$aa/$zz .git/$aa$zz
35 }
36
37 recover () {
38         aa=${1%??????????????????????????????????????} zz=${1#??}
39         mkdir -p .git/objects/$aa
40         mv .git/$aa$zz .git/objects/$aa/$zz
41 }
42
43 check_dont_have () {
44         gaah= &&
45         for N in "$@"
46         do
47                 eval "o=\$$N"
48                 git cat-file -t $o && {
49                         echo Gaah $N
50                         gaah=$N
51                         break
52                 }
53         done
54         test -z "$gaah"
55 }
56
57 test_expect_success setup '
58         mkdir -p A/B &&
59         echo rat >C &&
60         echo ox >A/D &&
61         echo tiger >A/B/E &&
62         git add . &&
63
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 &&
72
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 &&
78
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 &&
89
90         rm -f A/G &&
91         test_tick && git commit -a -m monkey &&
92         K=`git rev-parse --verify HEAD` &&
93         check_fsck &&
94
95         check_have A B C D E F G H I J K L &&
96
97         git prune &&
98
99         check_have A B C D E F G H I J K L &&
100
101         check_fsck &&
102
103         test_line_count = 4 .git/logs/refs/heads/master
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         test_line_count = 5 .git/logs/refs/heads/master
120 '
121
122 test_expect_success 'corrupt and check' '
123
124         corrupt $F &&
125         check_fsck "missing blob $F"
126
127 '
128
129 test_expect_success 'reflog expire --dry-run should not touch reflog' '
130
131         git reflog expire --dry-run \
132                 --expire=$(($test_tick - 10000)) \
133                 --expire-unreachable=$(($test_tick - 10000)) \
134                 --stale-fix \
135                 --all &&
136
137         test_line_count = 5 .git/logs/refs/heads/master &&
138
139         check_fsck "missing blob $F"
140 '
141
142 test_expect_success 'reflog expire' '
143
144         git reflog expire --verbose \
145                 --expire=$(($test_tick - 10000)) \
146                 --expire-unreachable=$(($test_tick - 10000)) \
147                 --stale-fix \
148                 --all &&
149
150         test_line_count = 2 .git/logs/refs/heads/master &&
151
152         check_fsck "dangling commit $K"
153 '
154
155 test_expect_success 'prune and fsck' '
156
157         git prune &&
158         check_fsck &&
159
160         check_have A B C D E H L &&
161         check_dont_have F G I J K
162
163 '
164
165 test_expect_success 'recover and check' '
166
167         recover $F &&
168         check_fsck "dangling blob $F"
169
170 '
171
172 test_expect_success 'delete' '
173         echo 1 > C &&
174         test_tick &&
175         git commit -m rat C &&
176
177         echo 2 > C &&
178         test_tick &&
179         git commit -m ox C &&
180
181         echo 3 > C &&
182         test_tick &&
183         git commit -m tiger C &&
184
185         HEAD_entry_count=$(git reflog | wc -l) &&
186         master_entry_count=$(git reflog show master | wc -l) &&
187
188         test $HEAD_entry_count = 5 &&
189         test $master_entry_count = 5 &&
190
191
192         git reflog delete master@{1} &&
193         git reflog show master > output &&
194         test $(($master_entry_count - 1)) = $(wc -l < output) &&
195         test $HEAD_entry_count = $(git reflog | wc -l) &&
196         ! grep ox < output &&
197
198         master_entry_count=$(wc -l < output) &&
199
200         git reflog delete HEAD@{1} &&
201         test $(($HEAD_entry_count -1)) = $(git reflog | wc -l) &&
202         test $master_entry_count = $(git reflog show master | wc -l) &&
203
204         HEAD_entry_count=$(git reflog | wc -l) &&
205
206         git reflog delete master@{07.04.2005.15:15:00.-0700} &&
207         git reflog show master > output &&
208         test $(($master_entry_count - 1)) = $(wc -l < output) &&
209         ! grep dragon < output
210
211 '
212
213 test_expect_success 'rewind2' '
214
215         test_tick && git reset --hard HEAD~2 &&
216         test_line_count = 4 .git/logs/refs/heads/master
217 '
218
219 test_expect_success '--expire=never' '
220
221         git reflog expire --verbose \
222                 --expire=never \
223                 --expire-unreachable=never \
224                 --all &&
225         test_line_count = 4 .git/logs/refs/heads/master
226 '
227
228 test_expect_success 'gc.reflogexpire=never' '
229
230         git config gc.reflogexpire never &&
231         git config gc.reflogexpireunreachable never &&
232         git reflog expire --verbose --all &&
233         test_line_count = 4 .git/logs/refs/heads/master
234 '
235
236 test_expect_success 'gc.reflogexpire=false' '
237
238         git config gc.reflogexpire false &&
239         git config gc.reflogexpireunreachable false &&
240         git reflog expire --verbose --all &&
241         test_line_count = 4 .git/logs/refs/heads/master &&
242
243         git config --unset gc.reflogexpire &&
244         git config --unset gc.reflogexpireunreachable
245
246 '
247
248 test_expect_success 'checkout should not delete log for packed ref' '
249         test $(git reflog master | wc -l) = 4 &&
250         git branch foo &&
251         git pack-refs --all &&
252         git checkout foo &&
253         test $(git reflog master | wc -l) = 4
254 '
255
256 test_expect_success 'stale dirs do not cause d/f conflicts (reflogs on)' '
257         test_when_finished "git branch -d one || git branch -d one/two" &&
258
259         git branch one/two master &&
260         echo "one/two@{0} branch: Created from master" >expect &&
261         git log -g --format="%gd %gs" one/two >actual &&
262         test_cmp expect actual &&
263         git branch -d one/two &&
264
265         # now logs/refs/heads/one is a stale directory, but
266         # we should move it out of the way to create "one" reflog
267         git branch one master &&
268         echo "one@{0} branch: Created from master" >expect &&
269         git log -g --format="%gd %gs" one >actual &&
270         test_cmp expect actual
271 '
272
273 test_expect_success 'stale dirs do not cause d/f conflicts (reflogs off)' '
274         test_when_finished "git branch -d one || git branch -d one/two" &&
275
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 &&
281
282         # same as before, but we only create a reflog for "one" if
283         # it already exists, which it does not
284         git -c core.logallrefupdates=false branch one master &&
285         : >expect &&
286         git log -g --format="%gd %gs" one >actual &&
287         test_cmp expect actual
288 '
289
290 # Triggering the bug detected by this test requires a newline to fall
291 # exactly BUFSIZ-1 bytes from the end of the file. We don't know
292 # what that value is, since it's platform dependent. However, if
293 # we choose some value N, we also catch any D which divides N evenly
294 # (since we will read backwards in chunks of D). So we choose 8K,
295 # which catches glibc (with an 8K BUFSIZ) and *BSD (1K).
296 #
297 # Each line is 114 characters, so we need 75 to still have a few before the
298 # last 8K. The 89-character padding on the final entry lines up our
299 # newline exactly.
300 test_expect_success 'parsing reverse reflogs at BUFSIZ boundaries' '
301         git checkout -b reflogskip &&
302         z38=00000000000000000000000000000000000000 &&
303         ident="abc <xyz> 0000000001 +0000" &&
304         for i in $(test_seq 1 75); do
305                 printf "$z38%02d $z38%02d %s\t" $i $(($i+1)) "$ident" &&
306                 if test $i = 75; then
307                         for j in $(test_seq 1 89); do
308                                 printf X
309                         done
310                 else
311                         printf X
312                 fi &&
313                 printf "\n"
314         done >.git/logs/refs/heads/reflogskip &&
315         git rev-parse reflogskip@{73} >actual &&
316         echo ${z38}03 >expect &&
317         test_cmp expect actual
318 '
319
320 test_done