Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 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 () { |
Junio C Hamano | df391b1 | 2007-01-28 16:33:58 -0800 | [diff] [blame] | 23 | output=$(git fsck --full) |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 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 && |
Elia Pinto | 2c25eaa | 2015-12-22 16:05:48 +0100 | [diff] [blame] | 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) && |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 71 | check_fsck && |
| 72 | |
Johannes Sixt | 1f55391 | 2009-02-28 21:12:57 +0100 | [diff] [blame] | 73 | test_chmod +x C && |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 74 | git add C && |
| 75 | test_tick && git commit -m dragon && |
Elia Pinto | 2c25eaa | 2015-12-22 16:05:48 +0100 | [diff] [blame] | 76 | L=$(git rev-parse --verify HEAD) && |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 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 && |
Elia Pinto | 2c25eaa | 2015-12-22 16:05:48 +0100 | [diff] [blame] | 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) && |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 88 | check_fsck && |
| 89 | |
| 90 | rm -f A/G && |
| 91 | test_tick && git commit -a -m monkey && |
Elia Pinto | 2c25eaa | 2015-12-22 16:05:48 +0100 | [diff] [blame] | 92 | K=$(git rev-parse --verify HEAD) && |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 93 | check_fsck && |
| 94 | |
| 95 | check_have A B C D E F G H I J K L && |
| 96 | |
Junio C Hamano | 026aa93 | 2007-01-21 21:29:44 -0800 | [diff] [blame] | 97 | git prune && |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 98 | |
| 99 | check_have A B C D E F G H I J K L && |
| 100 | |
| 101 | check_fsck && |
| 102 | |
David Turner | d0ab058 | 2015-07-27 18:57:08 -0400 | [diff] [blame] | 103 | git reflog refs/heads/master >output && |
| 104 | test_line_count = 4 output |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 105 | ' |
| 106 | |
| 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 && |
| 113 | |
| 114 | check_have A B C D E F G H I J K L && |
| 115 | |
Junio C Hamano | 026aa93 | 2007-01-21 21:29:44 -0800 | [diff] [blame] | 116 | git prune && |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 117 | |
| 118 | check_have A B C D E F G H I J K L && |
| 119 | |
David Turner | d0ab058 | 2015-07-27 18:57:08 -0400 | [diff] [blame] | 120 | git reflog refs/heads/master >output && |
| 121 | test_line_count = 5 output |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 122 | ' |
| 123 | |
| 124 | test_expect_success 'corrupt and check' ' |
| 125 | |
| 126 | corrupt $F && |
| 127 | check_fsck "missing blob $F" |
| 128 | |
| 129 | ' |
| 130 | |
| 131 | test_expect_success 'reflog expire --dry-run should not touch reflog' ' |
| 132 | |
| 133 | git reflog expire --dry-run \ |
| 134 | --expire=$(($test_tick - 10000)) \ |
| 135 | --expire-unreachable=$(($test_tick - 10000)) \ |
| 136 | --stale-fix \ |
| 137 | --all && |
| 138 | |
David Turner | d0ab058 | 2015-07-27 18:57:08 -0400 | [diff] [blame] | 139 | git reflog refs/heads/master >output && |
| 140 | test_line_count = 5 output && |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 141 | |
| 142 | check_fsck "missing blob $F" |
| 143 | ' |
| 144 | |
| 145 | test_expect_success 'reflog expire' ' |
| 146 | |
| 147 | git reflog expire --verbose \ |
| 148 | --expire=$(($test_tick - 10000)) \ |
| 149 | --expire-unreachable=$(($test_tick - 10000)) \ |
| 150 | --stale-fix \ |
| 151 | --all && |
| 152 | |
David Turner | d0ab058 | 2015-07-27 18:57:08 -0400 | [diff] [blame] | 153 | git reflog refs/heads/master >output && |
| 154 | test_line_count = 2 output && |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 155 | |
| 156 | check_fsck "dangling commit $K" |
| 157 | ' |
| 158 | |
| 159 | test_expect_success 'prune and fsck' ' |
| 160 | |
Junio C Hamano | 026aa93 | 2007-01-21 21:29:44 -0800 | [diff] [blame] | 161 | git prune && |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 162 | check_fsck && |
| 163 | |
| 164 | check_have A B C D E H L && |
| 165 | check_dont_have F G I J K |
| 166 | |
| 167 | ' |
| 168 | |
| 169 | test_expect_success 'recover and check' ' |
| 170 | |
| 171 | recover $F && |
| 172 | check_fsck "dangling blob $F" |
| 173 | |
| 174 | ' |
| 175 | |
Junio C Hamano | 55beff4 | 2008-08-10 23:21:25 -0700 | [diff] [blame] | 176 | test_expect_success 'delete' ' |
Johannes Schindelin | 552cecc | 2007-10-17 02:50:45 +0100 | [diff] [blame] | 177 | echo 1 > C && |
| 178 | test_tick && |
| 179 | git commit -m rat C && |
| 180 | |
| 181 | echo 2 > C && |
| 182 | test_tick && |
| 183 | git commit -m ox C && |
| 184 | |
| 185 | echo 3 > C && |
| 186 | test_tick && |
| 187 | git commit -m tiger C && |
| 188 | |
Jonathan Nieder | a48fcd8 | 2010-10-30 20:46:54 -0500 | [diff] [blame] | 189 | HEAD_entry_count=$(git reflog | wc -l) && |
| 190 | master_entry_count=$(git reflog show master | wc -l) && |
Pieter de Bie | 38881a9 | 2008-08-10 01:33:29 +0200 | [diff] [blame] | 191 | |
| 192 | test $HEAD_entry_count = 5 && |
| 193 | test $master_entry_count = 5 && |
| 194 | |
Johannes Schindelin | 552cecc | 2007-10-17 02:50:45 +0100 | [diff] [blame] | 195 | |
| 196 | git reflog delete master@{1} && |
| 197 | git reflog show master > output && |
Pieter de Bie | 38881a9 | 2008-08-10 01:33:29 +0200 | [diff] [blame] | 198 | test $(($master_entry_count - 1)) = $(wc -l < output) && |
| 199 | test $HEAD_entry_count = $(git reflog | wc -l) && |
Johannes Schindelin | 552cecc | 2007-10-17 02:50:45 +0100 | [diff] [blame] | 200 | ! grep ox < output && |
| 201 | |
Jonathan Nieder | a48fcd8 | 2010-10-30 20:46:54 -0500 | [diff] [blame] | 202 | master_entry_count=$(wc -l < output) && |
Pieter de Bie | 38881a9 | 2008-08-10 01:33:29 +0200 | [diff] [blame] | 203 | |
| 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) && |
| 207 | |
Jonathan Nieder | a48fcd8 | 2010-10-30 20:46:54 -0500 | [diff] [blame] | 208 | HEAD_entry_count=$(git reflog | wc -l) && |
Pieter de Bie | 38881a9 | 2008-08-10 01:33:29 +0200 | [diff] [blame] | 209 | |
Johannes Schindelin | 552cecc | 2007-10-17 02:50:45 +0100 | [diff] [blame] | 210 | git reflog delete master@{07.04.2005.15:15:00.-0700} && |
| 211 | git reflog show master > output && |
Pieter de Bie | 38881a9 | 2008-08-10 01:33:29 +0200 | [diff] [blame] | 212 | test $(($master_entry_count - 1)) = $(wc -l < output) && |
Johannes Schindelin | 552cecc | 2007-10-17 02:50:45 +0100 | [diff] [blame] | 213 | ! grep dragon < output |
Junio C Hamano | 50f3ac2 | 2008-02-22 22:54:37 -0800 | [diff] [blame] | 214 | |
| 215 | ' |
| 216 | |
Adam Simpkins | 4a9f439 | 2010-02-26 19:50:03 -0800 | [diff] [blame] | 217 | test_expect_success 'rewind2' ' |
| 218 | |
| 219 | test_tick && git reset --hard HEAD~2 && |
David Turner | d0ab058 | 2015-07-27 18:57:08 -0400 | [diff] [blame] | 220 | git reflog refs/heads/master >output && |
| 221 | test_line_count = 4 output |
Adam Simpkins | 4a9f439 | 2010-02-26 19:50:03 -0800 | [diff] [blame] | 222 | ' |
| 223 | |
| 224 | test_expect_success '--expire=never' ' |
| 225 | |
| 226 | git reflog expire --verbose \ |
| 227 | --expire=never \ |
| 228 | --expire-unreachable=never \ |
| 229 | --all && |
David Turner | d0ab058 | 2015-07-27 18:57:08 -0400 | [diff] [blame] | 230 | git reflog refs/heads/master >output && |
| 231 | test_line_count = 4 output |
Adam Simpkins | 4a9f439 | 2010-02-26 19:50:03 -0800 | [diff] [blame] | 232 | ' |
| 233 | |
| 234 | test_expect_success 'gc.reflogexpire=never' ' |
| 235 | |
| 236 | git config gc.reflogexpire never && |
| 237 | git config gc.reflogexpireunreachable never && |
| 238 | git reflog expire --verbose --all && |
David Turner | d0ab058 | 2015-07-27 18:57:08 -0400 | [diff] [blame] | 239 | git reflog refs/heads/master >output && |
| 240 | test_line_count = 4 output |
Adam Simpkins | 4a9f439 | 2010-02-26 19:50:03 -0800 | [diff] [blame] | 241 | ' |
| 242 | |
| 243 | test_expect_success 'gc.reflogexpire=false' ' |
| 244 | |
| 245 | git config gc.reflogexpire false && |
| 246 | git config gc.reflogexpireunreachable false && |
| 247 | git reflog expire --verbose --all && |
David Turner | d0ab058 | 2015-07-27 18:57:08 -0400 | [diff] [blame] | 248 | git reflog refs/heads/master >output && |
| 249 | test_line_count = 4 output && |
Adam Simpkins | 4a9f439 | 2010-02-26 19:50:03 -0800 | [diff] [blame] | 250 | |
| 251 | git config --unset gc.reflogexpire && |
| 252 | git config --unset gc.reflogexpireunreachable |
| 253 | |
| 254 | ' |
| 255 | |
Ronnie Sahlberg | 482b8f3 | 2014-05-06 15:45:53 -0700 | [diff] [blame] | 256 | test_expect_success 'checkout should not delete log for packed ref' ' |
| 257 | test $(git reflog master | wc -l) = 4 && |
| 258 | git branch foo && |
| 259 | git pack-refs --all && |
| 260 | git checkout foo && |
| 261 | test $(git reflog master | wc -l) = 4 |
| 262 | ' |
| 263 | |
Jeff King | 9233887 | 2014-11-04 08:24:53 -0500 | [diff] [blame] | 264 | test_expect_success 'stale dirs do not cause d/f conflicts (reflogs on)' ' |
Jeff King | aae828b | 2014-11-08 20:59:18 -0500 | [diff] [blame] | 265 | test_when_finished "git branch -d one || git branch -d one/two" && |
Jeff King | 9233887 | 2014-11-04 08:24:53 -0500 | [diff] [blame] | 266 | |
Jeff King | aae828b | 2014-11-08 20:59:18 -0500 | [diff] [blame] | 267 | git branch one/two master && |
| 268 | echo "one/two@{0} branch: Created from master" >expect && |
| 269 | git log -g --format="%gd %gs" one/two >actual && |
Jeff King | 9233887 | 2014-11-04 08:24:53 -0500 | [diff] [blame] | 270 | test_cmp expect actual && |
Jeff King | aae828b | 2014-11-08 20:59:18 -0500 | [diff] [blame] | 271 | git branch -d one/two && |
Jeff King | 9233887 | 2014-11-04 08:24:53 -0500 | [diff] [blame] | 272 | |
Jeff King | aae828b | 2014-11-08 20:59:18 -0500 | [diff] [blame] | 273 | # now logs/refs/heads/one is a stale directory, but |
| 274 | # we should move it out of the way to create "one" reflog |
| 275 | git branch one master && |
| 276 | echo "one@{0} branch: Created from master" >expect && |
| 277 | git log -g --format="%gd %gs" one >actual && |
Jeff King | 9233887 | 2014-11-04 08:24:53 -0500 | [diff] [blame] | 278 | test_cmp expect actual |
| 279 | ' |
| 280 | |
| 281 | test_expect_success 'stale dirs do not cause d/f conflicts (reflogs off)' ' |
Jeff King | aae828b | 2014-11-08 20:59:18 -0500 | [diff] [blame] | 282 | test_when_finished "git branch -d one || git branch -d one/two" && |
Jeff King | 9233887 | 2014-11-04 08:24:53 -0500 | [diff] [blame] | 283 | |
Jeff King | aae828b | 2014-11-08 20:59:18 -0500 | [diff] [blame] | 284 | git branch one/two master && |
| 285 | echo "one/two@{0} branch: Created from master" >expect && |
| 286 | git log -g --format="%gd %gs" one/two >actual && |
Jeff King | 9233887 | 2014-11-04 08:24:53 -0500 | [diff] [blame] | 287 | test_cmp expect actual && |
Jeff King | aae828b | 2014-11-08 20:59:18 -0500 | [diff] [blame] | 288 | git branch -d one/two && |
Jeff King | 9233887 | 2014-11-04 08:24:53 -0500 | [diff] [blame] | 289 | |
Jeff King | aae828b | 2014-11-08 20:59:18 -0500 | [diff] [blame] | 290 | # same as before, but we only create a reflog for "one" if |
Jeff King | 9233887 | 2014-11-04 08:24:53 -0500 | [diff] [blame] | 291 | # it already exists, which it does not |
Jeff King | aae828b | 2014-11-08 20:59:18 -0500 | [diff] [blame] | 292 | git -c core.logallrefupdates=false branch one master && |
Jeff King | 9233887 | 2014-11-04 08:24:53 -0500 | [diff] [blame] | 293 | : >expect && |
Jeff King | aae828b | 2014-11-08 20:59:18 -0500 | [diff] [blame] | 294 | git log -g --format="%gd %gs" one >actual && |
Jeff King | 9233887 | 2014-11-04 08:24:53 -0500 | [diff] [blame] | 295 | test_cmp expect actual |
| 296 | ' |
| 297 | |
Jeff King | e5e73ff | 2014-12-04 20:28:54 -0500 | [diff] [blame] | 298 | # Triggering the bug detected by this test requires a newline to fall |
| 299 | # exactly BUFSIZ-1 bytes from the end of the file. We don't know |
| 300 | # what that value is, since it's platform dependent. However, if |
| 301 | # we choose some value N, we also catch any D which divides N evenly |
| 302 | # (since we will read backwards in chunks of D). So we choose 8K, |
| 303 | # which catches glibc (with an 8K BUFSIZ) and *BSD (1K). |
| 304 | # |
| 305 | # Each line is 114 characters, so we need 75 to still have a few before the |
| 306 | # last 8K. The 89-character padding on the final entry lines up our |
| 307 | # newline exactly. |
| 308 | test_expect_success 'parsing reverse reflogs at BUFSIZ boundaries' ' |
| 309 | git checkout -b reflogskip && |
| 310 | z38=00000000000000000000000000000000000000 && |
| 311 | ident="abc <xyz> 0000000001 +0000" && |
| 312 | for i in $(test_seq 1 75); do |
| 313 | printf "$z38%02d $z38%02d %s\t" $i $(($i+1)) "$ident" && |
| 314 | if test $i = 75; then |
| 315 | for j in $(test_seq 1 89); do |
| 316 | printf X |
| 317 | done |
| 318 | else |
| 319 | printf X |
| 320 | fi && |
| 321 | printf "\n" |
| 322 | done >.git/logs/refs/heads/reflogskip && |
| 323 | git rev-parse reflogskip@{73} >actual && |
| 324 | echo ${z38}03 >expect && |
| 325 | test_cmp expect actual |
| 326 | ' |
| 327 | |
Dennis Kaarsemaker | aecad37 | 2016-01-05 22:12:10 +0100 | [diff] [blame] | 328 | test_expect_success 'no segfaults for reflog containing non-commit sha1s' ' |
| 329 | git update-ref --create-reflog -m "Creating ref" \ |
| 330 | refs/tests/tree-in-reflog HEAD && |
| 331 | git update-ref -m "Forcing tree" refs/tests/tree-in-reflog HEAD^{tree} && |
| 332 | git update-ref -m "Restoring to commit" refs/tests/tree-in-reflog HEAD && |
| 333 | git reflog refs/tests/tree-in-reflog |
| 334 | ' |
| 335 | |
| 336 | test_expect_failure 'reflog with non-commit entries displays all entries' ' |
| 337 | git reflog refs/tests/tree-in-reflog >actual && |
| 338 | test_line_count = 3 actual |
| 339 | ' |
| 340 | |
David Turner | 41d796e | 2016-04-07 15:03:11 -0400 | [diff] [blame] | 341 | test_expect_success 'reflog expire operates on symref not referrent' ' |
| 342 | git branch -l the_symref && |
| 343 | git branch -l referrent && |
| 344 | git update-ref referrent HEAD && |
| 345 | git symbolic-ref refs/heads/the_symref refs/heads/referrent && |
| 346 | test_when_finished "rm -f .git/refs/heads/referrent.lock" && |
| 347 | touch .git/refs/heads/referrent.lock && |
| 348 | git reflog expire --expire=all the_symref |
| 349 | ' |
| 350 | |
SZEDER Gábor | 71abeb7 | 2016-06-03 22:42:35 +0200 | [diff] [blame] | 351 | test_expect_success 'continue walking past root commits' ' |
| 352 | git init orphanage && |
| 353 | ( |
| 354 | cd orphanage && |
| 355 | cat >expect <<-\EOF && |
| 356 | HEAD@{0} commit (initial): orphan2-1 |
| 357 | HEAD@{1} commit: orphan1-2 |
| 358 | HEAD@{2} commit (initial): orphan1-1 |
| 359 | HEAD@{3} commit (initial): initial |
| 360 | EOF |
| 361 | test_commit initial && |
SZEDER Gábor | 71abeb7 | 2016-06-03 22:42:35 +0200 | [diff] [blame] | 362 | git checkout --orphan orphan1 && |
| 363 | test_commit orphan1-1 && |
| 364 | test_commit orphan1-2 && |
| 365 | git checkout --orphan orphan2 && |
| 366 | test_commit orphan2-1 && |
| 367 | git log -g --format="%gd %gs" >actual && |
| 368 | test_cmp expect actual |
| 369 | ) |
| 370 | ' |
| 371 | |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 372 | test_done |