]>
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' | |
334afbc7 JS |
7 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master |
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 | ||
d0ab0584 DT |
105 | git reflog refs/heads/master >output && |
106 | test_line_count = 4 output | |
1389d9dd JH |
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 | ||
026aa938 | 118 | git prune && |
1389d9dd JH |
119 | |
120 | check_have A B C D E F G H I J K L && | |
121 | ||
d0ab0584 DT |
122 | git reflog refs/heads/master >output && |
123 | test_line_count = 5 output | |
1389d9dd JH |
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 | ||
d0ab0584 DT |
141 | git reflog refs/heads/master >output && |
142 | test_line_count = 5 output && | |
1389d9dd JH |
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 | ||
d0ab0584 DT |
155 | git reflog refs/heads/master >output && |
156 | test_line_count = 2 output && | |
1389d9dd JH |
157 | |
158 | check_fsck "dangling commit $K" | |
159 | ' | |
160 | ||
161 | test_expect_success 'prune and fsck' ' | |
162 | ||
026aa938 | 163 | git prune && |
1389d9dd JH |
164 | check_fsck && |
165 | ||
166 | check_have A B C D E H L && | |
167 | check_dont_have F G I J K | |
168 | ||
169 | ' | |
170 | ||
171 | test_expect_success 'recover and check' ' | |
172 | ||
173 | recover $F && | |
174 | check_fsck "dangling blob $F" | |
175 | ||
176 | ' | |
177 | ||
55beff4f | 178 | test_expect_success 'delete' ' |
552cecc2 JS |
179 | echo 1 > C && |
180 | test_tick && | |
181 | git commit -m rat C && | |
182 | ||
183 | echo 2 > C && | |
184 | test_tick && | |
185 | git commit -m ox C && | |
186 | ||
187 | echo 3 > C && | |
188 | test_tick && | |
189 | git commit -m tiger C && | |
190 | ||
a48fcd83 JN |
191 | HEAD_entry_count=$(git reflog | wc -l) && |
192 | master_entry_count=$(git reflog show master | wc -l) && | |
38881a90 PB |
193 | |
194 | test $HEAD_entry_count = 5 && | |
195 | test $master_entry_count = 5 && | |
196 | ||
552cecc2 JS |
197 | |
198 | git reflog delete master@{1} && | |
199 | git reflog show master > output && | |
6e4826ea | 200 | test_line_count = $(($master_entry_count - 1)) output && |
38881a90 | 201 | test $HEAD_entry_count = $(git reflog | wc -l) && |
552cecc2 JS |
202 | ! grep ox < output && |
203 | ||
a48fcd83 | 204 | master_entry_count=$(wc -l < output) && |
38881a90 PB |
205 | |
206 | git reflog delete HEAD@{1} && | |
207 | test $(($HEAD_entry_count -1)) = $(git reflog | wc -l) && | |
208 | test $master_entry_count = $(git reflog show master | wc -l) && | |
209 | ||
a48fcd83 | 210 | HEAD_entry_count=$(git reflog | wc -l) && |
38881a90 | 211 | |
552cecc2 JS |
212 | git reflog delete master@{07.04.2005.15:15:00.-0700} && |
213 | git reflog show master > output && | |
6e4826ea | 214 | test_line_count = $(($master_entry_count - 1)) output && |
552cecc2 | 215 | ! grep dragon < output |
50f3ac29 JH |
216 | |
217 | ' | |
218 | ||
4a9f4394 AS |
219 | test_expect_success 'rewind2' ' |
220 | ||
221 | test_tick && git reset --hard HEAD~2 && | |
d0ab0584 DT |
222 | git reflog refs/heads/master >output && |
223 | test_line_count = 4 output | |
4a9f4394 AS |
224 | ' |
225 | ||
226 | test_expect_success '--expire=never' ' | |
227 | ||
228 | git reflog expire --verbose \ | |
229 | --expire=never \ | |
230 | --expire-unreachable=never \ | |
231 | --all && | |
d0ab0584 DT |
232 | git reflog refs/heads/master >output && |
233 | test_line_count = 4 output | |
4a9f4394 AS |
234 | ' |
235 | ||
236 | test_expect_success 'gc.reflogexpire=never' ' | |
a65bf78c ÆAB |
237 | test_config gc.reflogexpire never && |
238 | test_config gc.reflogexpireunreachable never && | |
4a9f4394 | 239 | |
978f4307 ÆAB |
240 | git reflog expire --verbose --all >output && |
241 | test_line_count = 9 output && | |
242 | ||
d0ab0584 DT |
243 | git reflog refs/heads/master >output && |
244 | test_line_count = 4 output | |
4a9f4394 AS |
245 | ' |
246 | ||
247 | test_expect_success 'gc.reflogexpire=false' ' | |
a65bf78c ÆAB |
248 | test_config gc.reflogexpire false && |
249 | test_config gc.reflogexpireunreachable false && | |
4a9f4394 | 250 | |
4a9f4394 | 251 | git reflog expire --verbose --all && |
d0ab0584 | 252 | git reflog refs/heads/master >output && |
a65bf78c | 253 | test_line_count = 4 output |
4a9f4394 AS |
254 | |
255 | ' | |
256 | ||
fe66776d ÆAB |
257 | test_expect_success 'git reflog expire unknown reference' ' |
258 | test_config gc.reflogexpire never && | |
259 | test_config gc.reflogexpireunreachable never && | |
260 | ||
261 | test_must_fail git reflog expire master@{123} 2>stderr && | |
262 | test_i18ngrep "points nowhere" stderr && | |
263 | test_must_fail git reflog expire does-not-exist 2>stderr && | |
264 | test_i18ngrep "points nowhere" stderr | |
265 | ' | |
266 | ||
482b8f32 RS |
267 | test_expect_success 'checkout should not delete log for packed ref' ' |
268 | test $(git reflog master | wc -l) = 4 && | |
269 | git branch foo && | |
270 | git pack-refs --all && | |
271 | git checkout foo && | |
272 | test $(git reflog master | wc -l) = 4 | |
273 | ' | |
274 | ||
9233887c | 275 | test_expect_success 'stale dirs do not cause d/f conflicts (reflogs on)' ' |
aae828b9 | 276 | test_when_finished "git branch -d one || git branch -d one/two" && |
9233887c | 277 | |
aae828b9 JK |
278 | git branch one/two master && |
279 | echo "one/two@{0} branch: Created from master" >expect && | |
280 | git log -g --format="%gd %gs" one/two >actual && | |
9233887c | 281 | test_cmp expect actual && |
aae828b9 | 282 | git branch -d one/two && |
9233887c | 283 | |
aae828b9 JK |
284 | # now logs/refs/heads/one is a stale directory, but |
285 | # we should move it out of the way to create "one" reflog | |
286 | git branch one master && | |
287 | echo "one@{0} branch: Created from master" >expect && | |
288 | git log -g --format="%gd %gs" one >actual && | |
9233887c JK |
289 | test_cmp expect actual |
290 | ' | |
291 | ||
292 | test_expect_success 'stale dirs do not cause d/f conflicts (reflogs off)' ' | |
aae828b9 | 293 | test_when_finished "git branch -d one || git branch -d one/two" && |
9233887c | 294 | |
aae828b9 JK |
295 | git branch one/two master && |
296 | echo "one/two@{0} branch: Created from master" >expect && | |
297 | git log -g --format="%gd %gs" one/two >actual && | |
9233887c | 298 | test_cmp expect actual && |
aae828b9 | 299 | git branch -d one/two && |
9233887c | 300 | |
aae828b9 | 301 | # same as before, but we only create a reflog for "one" if |
9233887c | 302 | # it already exists, which it does not |
aae828b9 | 303 | git -c core.logallrefupdates=false branch one master && |
aae828b9 | 304 | git log -g --format="%gd %gs" one >actual && |
1c5e94f4 | 305 | test_must_be_empty actual |
9233887c JK |
306 | ' |
307 | ||
e5e73ff2 JK |
308 | # Triggering the bug detected by this test requires a newline to fall |
309 | # exactly BUFSIZ-1 bytes from the end of the file. We don't know | |
310 | # what that value is, since it's platform dependent. However, if | |
311 | # we choose some value N, we also catch any D which divides N evenly | |
312 | # (since we will read backwards in chunks of D). So we choose 8K, | |
313 | # which catches glibc (with an 8K BUFSIZ) and *BSD (1K). | |
314 | # | |
315 | # Each line is 114 characters, so we need 75 to still have a few before the | |
316 | # last 8K. The 89-character padding on the final entry lines up our | |
317 | # newline exactly. | |
ff4cb42e | 318 | test_expect_success SHA1 'parsing reverse reflogs at BUFSIZ boundaries' ' |
e5e73ff2 | 319 | git checkout -b reflogskip && |
ff4cb42e | 320 | zf=$(test_oid zero_2) && |
e5e73ff2 JK |
321 | ident="abc <xyz> 0000000001 +0000" && |
322 | for i in $(test_seq 1 75); do | |
ff4cb42e | 323 | printf "$zf%02d $zf%02d %s\t" $i $(($i+1)) "$ident" && |
e5e73ff2 JK |
324 | if test $i = 75; then |
325 | for j in $(test_seq 1 89); do | |
326 | printf X | |
327 | done | |
328 | else | |
329 | printf X | |
330 | fi && | |
331 | printf "\n" | |
332 | done >.git/logs/refs/heads/reflogskip && | |
333 | git rev-parse reflogskip@{73} >actual && | |
ff4cb42e | 334 | echo ${zf}03 >expect && |
e5e73ff2 JK |
335 | test_cmp expect actual |
336 | ' | |
337 | ||
aecad374 DK |
338 | test_expect_success 'no segfaults for reflog containing non-commit sha1s' ' |
339 | git update-ref --create-reflog -m "Creating ref" \ | |
340 | refs/tests/tree-in-reflog HEAD && | |
341 | git update-ref -m "Forcing tree" refs/tests/tree-in-reflog HEAD^{tree} && | |
342 | git update-ref -m "Restoring to commit" refs/tests/tree-in-reflog HEAD && | |
343 | git reflog refs/tests/tree-in-reflog | |
344 | ' | |
345 | ||
346 | test_expect_failure 'reflog with non-commit entries displays all entries' ' | |
347 | git reflog refs/tests/tree-in-reflog >actual && | |
348 | test_line_count = 3 actual | |
349 | ' | |
350 | ||
41d796ed | 351 | test_expect_success 'reflog expire operates on symref not referrent' ' |
7687f19e JK |
352 | git branch --create-reflog the_symref && |
353 | git branch --create-reflog referrent && | |
41d796ed DT |
354 | git update-ref referrent HEAD && |
355 | git symbolic-ref refs/heads/the_symref refs/heads/referrent && | |
356 | test_when_finished "rm -f .git/refs/heads/referrent.lock" && | |
357 | touch .git/refs/heads/referrent.lock && | |
358 | git reflog expire --expire=all the_symref | |
359 | ' | |
360 | ||
71abeb75 SG |
361 | test_expect_success 'continue walking past root commits' ' |
362 | git init orphanage && | |
363 | ( | |
364 | cd orphanage && | |
365 | cat >expect <<-\EOF && | |
366 | HEAD@{0} commit (initial): orphan2-1 | |
367 | HEAD@{1} commit: orphan1-2 | |
368 | HEAD@{2} commit (initial): orphan1-1 | |
369 | HEAD@{3} commit (initial): initial | |
370 | EOF | |
371 | test_commit initial && | |
71abeb75 SG |
372 | git checkout --orphan orphan1 && |
373 | test_commit orphan1-1 && | |
374 | test_commit orphan1-2 && | |
375 | git checkout --orphan orphan2 && | |
376 | test_commit orphan2-1 && | |
377 | git log -g --format="%gd %gs" >actual && | |
378 | test_cmp expect actual | |
379 | ) | |
380 | ' | |
381 | ||
c9ef0d95 NTND |
382 | test_expect_success 'expire with multiple worktrees' ' |
383 | git init main-wt && | |
384 | ( | |
385 | cd main-wt && | |
386 | test_tick && | |
387 | test_commit foo && | |
388 | git worktree add link-wt && | |
389 | test_tick && | |
390 | test_commit -C link-wt foobar && | |
391 | test_tick && | |
392 | git reflog expire --verbose --all --expire=$test_tick && | |
393 | test_must_be_empty .git/worktrees/link-wt/logs/HEAD | |
394 | ) | |
395 | ' | |
396 | ||
1389d9dd | 397 | test_done |