| 1 | pack_refs=${pack_refs:-pack-refs} |
| 2 | |
| 3 | test_expect_success 'enable reflogs' ' |
| 4 | git config core.logallrefupdates true |
| 5 | ' |
| 6 | |
| 7 | test_expect_success 'prepare a trivial repository' ' |
| 8 | echo Hello > A && |
| 9 | git update-index --add A && |
| 10 | git commit -m "Initial commit." && |
| 11 | HEAD=$(git rev-parse --verify HEAD) |
| 12 | ' |
| 13 | |
| 14 | test_expect_success '${pack_refs} --prune --all' ' |
| 15 | test_path_is_missing .git/packed-refs && |
| 16 | git ${pack_refs} --no-prune --all && |
| 17 | test_path_is_file .git/packed-refs && |
| 18 | N=$(find .git/refs -type f | wc -l) && |
| 19 | test "$N" != 0 && |
| 20 | |
| 21 | git ${pack_refs} --prune --all && |
| 22 | test_path_is_file .git/packed-refs && |
| 23 | N=$(find .git/refs -type f) && |
| 24 | test -z "$N" |
| 25 | ' |
| 26 | |
| 27 | SHA1= |
| 28 | |
| 29 | test_expect_success 'see if git show-ref works as expected' ' |
| 30 | git branch a && |
| 31 | SHA1=$(cat .git/refs/heads/a) && |
| 32 | echo "$SHA1 refs/heads/a" >expect && |
| 33 | git show-ref a >result && |
| 34 | test_cmp expect result |
| 35 | ' |
| 36 | |
| 37 | test_expect_success 'see if a branch still exists when packed' ' |
| 38 | git branch b && |
| 39 | git ${pack_refs} --all && |
| 40 | rm -f .git/refs/heads/b && |
| 41 | echo "$SHA1 refs/heads/b" >expect && |
| 42 | git show-ref b >result && |
| 43 | test_cmp expect result |
| 44 | ' |
| 45 | |
| 46 | test_expect_success 'git branch c/d should barf if branch c exists' ' |
| 47 | git branch c && |
| 48 | git ${pack_refs} --all && |
| 49 | rm -f .git/refs/heads/c && |
| 50 | test_must_fail git branch c/d |
| 51 | ' |
| 52 | |
| 53 | test_expect_success 'see if a branch still exists after git ${pack_refs} --prune' ' |
| 54 | git branch e && |
| 55 | git ${pack_refs} --all --prune && |
| 56 | echo "$SHA1 refs/heads/e" >expect && |
| 57 | git show-ref e >result && |
| 58 | test_cmp expect result |
| 59 | ' |
| 60 | |
| 61 | test_expect_success 'see if git ${pack_refs} --prune remove ref files' ' |
| 62 | git branch f && |
| 63 | git ${pack_refs} --all --prune && |
| 64 | test_path_is_missing .git/refs/heads/f |
| 65 | ' |
| 66 | |
| 67 | test_expect_success 'see if git ${pack_refs} --prune removes empty dirs' ' |
| 68 | git branch r/s/t && |
| 69 | git ${pack_refs} --all --prune && |
| 70 | test_path_is_missing .git/refs/heads/r |
| 71 | ' |
| 72 | |
| 73 | test_expect_success 'git branch g should work when git branch g/h has been deleted' ' |
| 74 | git branch g/h && |
| 75 | git ${pack_refs} --all --prune && |
| 76 | git branch -d g/h && |
| 77 | git branch g && |
| 78 | git ${pack_refs} --all && |
| 79 | git branch -d g |
| 80 | ' |
| 81 | |
| 82 | test_expect_success 'git branch i/j/k should barf if branch i exists' ' |
| 83 | git branch i && |
| 84 | git ${pack_refs} --all --prune && |
| 85 | test_must_fail git branch i/j/k |
| 86 | ' |
| 87 | |
| 88 | test_expect_success 'test git branch k after branch k/l/m and k/lm have been deleted' ' |
| 89 | git branch k/l && |
| 90 | git branch k/lm && |
| 91 | git branch -d k/l && |
| 92 | git branch k/l/m && |
| 93 | git branch -d k/l/m && |
| 94 | git branch -d k/lm && |
| 95 | git branch k |
| 96 | ' |
| 97 | |
| 98 | test_expect_success 'test git branch n after some branch deletion and pruning' ' |
| 99 | git branch n/o && |
| 100 | git branch n/op && |
| 101 | git branch -d n/o && |
| 102 | git branch n/o/p && |
| 103 | git branch -d n/op && |
| 104 | git ${pack_refs} --all --prune && |
| 105 | git branch -d n/o/p && |
| 106 | git branch n |
| 107 | ' |
| 108 | |
| 109 | test_expect_success 'test excluded refs are not packed' ' |
| 110 | git branch dont_pack1 && |
| 111 | git branch dont_pack2 && |
| 112 | git branch pack_this && |
| 113 | git ${pack_refs} --all --exclude "refs/heads/dont_pack*" && |
| 114 | test_path_is_file .git/refs/heads/dont_pack1 && |
| 115 | test_path_is_file .git/refs/heads/dont_pack2 && |
| 116 | test_path_is_missing .git/refs/heads/pack_this' |
| 117 | |
| 118 | test_expect_success 'test --no-exclude refs clears excluded refs' ' |
| 119 | git branch dont_pack3 && |
| 120 | git branch dont_pack4 && |
| 121 | git ${pack_refs} --all --exclude "refs/heads/dont_pack*" --no-exclude && |
| 122 | test_path_is_missing .git/refs/heads/dont_pack3 && |
| 123 | test_path_is_missing .git/refs/heads/dont_pack4' |
| 124 | |
| 125 | test_expect_success 'test only included refs are packed' ' |
| 126 | git branch pack_this1 && |
| 127 | git branch pack_this2 && |
| 128 | git tag dont_pack5 && |
| 129 | git ${pack_refs} --include "refs/heads/pack_this*" && |
| 130 | test_path_is_file .git/refs/tags/dont_pack5 && |
| 131 | test_path_is_missing .git/refs/heads/pack_this1 && |
| 132 | test_path_is_missing .git/refs/heads/pack_this2' |
| 133 | |
| 134 | test_expect_success 'test --no-include refs clears included refs' ' |
| 135 | git branch pack1 && |
| 136 | git branch pack2 && |
| 137 | git ${pack_refs} --include "refs/heads/pack*" --no-include && |
| 138 | test_path_is_file .git/refs/heads/pack1 && |
| 139 | test_path_is_file .git/refs/heads/pack2' |
| 140 | |
| 141 | test_expect_success 'test --exclude takes precedence over --include' ' |
| 142 | git branch dont_pack5 && |
| 143 | git ${pack_refs} --include "refs/heads/pack*" --exclude "refs/heads/pack*" && |
| 144 | test_path_is_file .git/refs/heads/dont_pack5' |
| 145 | |
| 146 | test_expect_success 'see if up-to-date packed refs are preserved' ' |
| 147 | git branch q && |
| 148 | git ${pack_refs} --all --prune && |
| 149 | git update-ref refs/heads/q refs/heads/q && |
| 150 | test_path_is_missing .git/refs/heads/q |
| 151 | ' |
| 152 | |
| 153 | test_expect_success 'pack, prune and repack' ' |
| 154 | git tag foo && |
| 155 | git ${pack_refs} --all --prune && |
| 156 | git show-ref >all-of-them && |
| 157 | git ${pack_refs} && |
| 158 | git show-ref >again && |
| 159 | test_cmp all-of-them again |
| 160 | ' |
| 161 | |
| 162 | test_expect_success 'explicit ${pack_refs} with dangling packed reference' ' |
| 163 | git commit --allow-empty -m "soon to be garbage-collected" && |
| 164 | git ${pack_refs} --all && |
| 165 | git reset --hard HEAD^ && |
| 166 | git reflog expire --expire=all --all && |
| 167 | git prune --expire=all && |
| 168 | git ${pack_refs} --all 2>result && |
| 169 | test_must_be_empty result |
| 170 | ' |
| 171 | |
| 172 | test_expect_success 'delete ref with dangling packed version' ' |
| 173 | git checkout -b lamb && |
| 174 | git commit --allow-empty -m "future garbage" && |
| 175 | git ${pack_refs} --all && |
| 176 | git reset --hard HEAD^ && |
| 177 | git checkout main && |
| 178 | git reflog expire --expire=all --all && |
| 179 | git prune --expire=all && |
| 180 | git branch -d lamb 2>result && |
| 181 | test_must_be_empty result |
| 182 | ' |
| 183 | |
| 184 | test_expect_success 'delete ref while another dangling packed ref' ' |
| 185 | git branch lamb && |
| 186 | git commit --allow-empty -m "future garbage" && |
| 187 | git ${pack_refs} --all && |
| 188 | git reset --hard HEAD^ && |
| 189 | git reflog expire --expire=all --all && |
| 190 | git prune --expire=all && |
| 191 | git branch -d lamb 2>result && |
| 192 | test_must_be_empty result |
| 193 | ' |
| 194 | |
| 195 | test_expect_success 'pack ref directly below refs/' ' |
| 196 | git update-ref refs/top HEAD && |
| 197 | git ${pack_refs} --all --prune && |
| 198 | grep refs/top .git/packed-refs && |
| 199 | test_path_is_missing .git/refs/top |
| 200 | ' |
| 201 | |
| 202 | test_expect_success 'do not pack ref in refs/bisect' ' |
| 203 | git update-ref refs/bisect/local HEAD && |
| 204 | git ${pack_refs} --all --prune && |
| 205 | ! grep refs/bisect/local .git/packed-refs >/dev/null && |
| 206 | test_path_is_file .git/refs/bisect/local |
| 207 | ' |
| 208 | |
| 209 | test_expect_success 'disable reflogs' ' |
| 210 | git config core.logallrefupdates false && |
| 211 | rm -rf .git/logs |
| 212 | ' |
| 213 | |
| 214 | test_expect_success 'create packed foo/bar/baz branch' ' |
| 215 | git branch foo/bar/baz && |
| 216 | git ${pack_refs} --all --prune && |
| 217 | test_path_is_missing .git/refs/heads/foo/bar/baz && |
| 218 | test_must_fail git reflog exists refs/heads/foo/bar/baz |
| 219 | ' |
| 220 | |
| 221 | test_expect_success 'notice d/f conflict with existing directory' ' |
| 222 | test_must_fail git branch foo && |
| 223 | test_must_fail git branch foo/bar |
| 224 | ' |
| 225 | |
| 226 | test_expect_success 'existing directory reports concrete ref' ' |
| 227 | test_must_fail git branch foo 2>stderr && |
| 228 | test_grep refs/heads/foo/bar/baz stderr |
| 229 | ' |
| 230 | |
| 231 | test_expect_success 'notice d/f conflict with existing ref' ' |
| 232 | test_must_fail git branch foo/bar/baz/extra && |
| 233 | test_must_fail git branch foo/bar/baz/lots/of/extra/components |
| 234 | ' |
| 235 | |
| 236 | test_expect_success 'reject packed-refs with unterminated line' ' |
| 237 | cp .git/packed-refs .git/packed-refs.bak && |
| 238 | test_when_finished "mv .git/packed-refs.bak .git/packed-refs" && |
| 239 | printf "%s" "$HEAD refs/zzzzz" >>.git/packed-refs && |
| 240 | echo "fatal: unterminated line in .git/packed-refs: $HEAD refs/zzzzz" >expected_err && |
| 241 | test_must_fail git for-each-ref >out 2>err && |
| 242 | test_cmp expected_err err |
| 243 | ' |
| 244 | |
| 245 | test_expect_success 'reject packed-refs containing junk' ' |
| 246 | cp .git/packed-refs .git/packed-refs.bak && |
| 247 | test_when_finished "mv .git/packed-refs.bak .git/packed-refs" && |
| 248 | printf "%s\n" "bogus content" >>.git/packed-refs && |
| 249 | echo "fatal: unexpected line in .git/packed-refs: bogus content" >expected_err && |
| 250 | test_must_fail git for-each-ref >out 2>err && |
| 251 | test_cmp expected_err err |
| 252 | ' |
| 253 | |
| 254 | test_expect_success 'reject packed-refs with a short SHA-1' ' |
| 255 | cp .git/packed-refs .git/packed-refs.bak && |
| 256 | test_when_finished "mv .git/packed-refs.bak .git/packed-refs" && |
| 257 | printf "%.7s %s\n" $HEAD refs/zzzzz >>.git/packed-refs && |
| 258 | printf "fatal: unexpected line in .git/packed-refs: %.7s %s\n" $HEAD refs/zzzzz >expected_err && |
| 259 | test_must_fail git for-each-ref >out 2>err && |
| 260 | test_cmp expected_err err |
| 261 | ' |
| 262 | |
| 263 | test_expect_success 'timeout if packed-refs.lock exists' ' |
| 264 | LOCK=.git/packed-refs.lock && |
| 265 | >"$LOCK" && |
| 266 | test_when_finished "rm -f $LOCK" && |
| 267 | test_must_fail git ${pack_refs} --all --prune |
| 268 | ' |
| 269 | |
| 270 | test_expect_success 'retry acquiring packed-refs.lock' ' |
| 271 | LOCK=.git/packed-refs.lock && |
| 272 | >"$LOCK" && |
| 273 | test_when_finished "wait && rm -f $LOCK" && |
| 274 | { |
| 275 | ( sleep 1 && rm -f $LOCK ) & |
| 276 | } && |
| 277 | git -c core.packedrefstimeout=3000 ${pack_refs} --all --prune |
| 278 | ' |
| 279 | |
| 280 | test_expect_success SYMLINKS 'pack symlinked packed-refs' ' |
| 281 | # First make sure that symlinking works when reading: |
| 282 | git update-ref refs/heads/lossy refs/heads/main && |
| 283 | git for-each-ref >all-refs-before && |
| 284 | mv .git/packed-refs .git/my-deviant-packed-refs && |
| 285 | ln -s my-deviant-packed-refs .git/packed-refs && |
| 286 | git for-each-ref >all-refs-linked && |
| 287 | test_cmp all-refs-before all-refs-linked && |
| 288 | git ${pack_refs} --all --prune && |
| 289 | git for-each-ref >all-refs-packed && |
| 290 | test_cmp all-refs-before all-refs-packed && |
| 291 | test -h .git/packed-refs && |
| 292 | test "$(test_readlink .git/packed-refs)" = "my-deviant-packed-refs" |
| 293 | ' |
| 294 | |
| 295 | # The 'packed-refs' file is stored directly in .git/. This means it is global |
| 296 | # to the repository, and can only contain refs that are shared across all |
| 297 | # worktrees. |
| 298 | test_expect_success 'refs/worktree must not be packed' ' |
| 299 | test_commit initial && |
| 300 | test_commit wt1 && |
| 301 | test_commit wt2 && |
| 302 | git worktree add wt1 wt1 && |
| 303 | git worktree add wt2 wt2 && |
| 304 | git checkout initial && |
| 305 | git update-ref refs/worktree/foo HEAD && |
| 306 | git -C wt1 update-ref refs/worktree/foo HEAD && |
| 307 | git -C wt2 update-ref refs/worktree/foo HEAD && |
| 308 | git ${pack_refs} --all && |
| 309 | test_path_is_missing .git/refs/tags/wt1 && |
| 310 | test_path_is_file .git/refs/worktree/foo && |
| 311 | test_path_is_file .git/worktrees/wt1/refs/worktree/foo && |
| 312 | test_path_is_file .git/worktrees/wt2/refs/worktree/foo |
| 313 | ' |
| 314 | |
| 315 | # we do not want to count on running ${pack_refs} to |
| 316 | # actually pack it, as it is perfectly reasonable to |
| 317 | # skip processing a broken ref |
| 318 | test_expect_success 'create packed-refs file with broken ref' ' |
| 319 | test_tick && git commit --allow-empty -m one && |
| 320 | recoverable=$(git rev-parse HEAD) && |
| 321 | test_tick && git commit --allow-empty -m two && |
| 322 | missing=$(git rev-parse HEAD) && |
| 323 | rm -f .git/refs/heads/main && |
| 324 | cat >.git/packed-refs <<-EOF && |
| 325 | $missing refs/heads/main |
| 326 | $recoverable refs/heads/other |
| 327 | EOF |
| 328 | echo $missing >expect && |
| 329 | git rev-parse refs/heads/main >actual && |
| 330 | test_cmp expect actual |
| 331 | ' |
| 332 | |
| 333 | test_expect_success '${pack_refs} does not silently delete broken packed ref' ' |
| 334 | git ${pack_refs} --all --prune && |
| 335 | git rev-parse refs/heads/main >actual && |
| 336 | test_cmp expect actual |
| 337 | ' |
| 338 | |
| 339 | test_expect_success '${pack_refs} does not drop broken refs during deletion' ' |
| 340 | git update-ref -d refs/heads/other && |
| 341 | git rev-parse refs/heads/main >actual && |
| 342 | test_cmp expect actual |
| 343 | ' |
| 344 | |
| 345 | for command in "git ${pack_refs} --all --auto" "git maintenance run --task=${pack_refs} --auto" |
| 346 | do |
| 347 | test_expect_success "$command does not repack below 16 refs without packed-refs" ' |
| 348 | test_when_finished "rm -rf repo" && |
| 349 | git init repo && |
| 350 | ( |
| 351 | cd repo && |
| 352 | git config set maintenance.auto false && |
| 353 | git commit --allow-empty --message "initial" && |
| 354 | |
| 355 | # Create 14 additional references, which brings us to |
| 356 | # 15 together with the default branch. |
| 357 | test_seq -f "create refs/heads/loose-%d HEAD" 14 | |
| 358 | git update-ref --stdin && |
| 359 | test_path_is_missing .git/packed-refs && |
| 360 | git ${pack_refs} --auto --all && |
| 361 | test_path_is_missing .git/packed-refs && |
| 362 | |
| 363 | # Create the 16th reference, which should cause us to repack. |
| 364 | git update-ref refs/heads/loose-15 HEAD && |
| 365 | git ${pack_refs} --auto --all && |
| 366 | test_path_is_file .git/packed-refs |
| 367 | ) |
| 368 | ' |
| 369 | |
| 370 | test_expect_success "$command does not repack below 16 refs with small packed-refs" ' |
| 371 | test_when_finished "rm -rf repo" && |
| 372 | git init repo && |
| 373 | ( |
| 374 | cd repo && |
| 375 | git config set maintenance.auto false && |
| 376 | git commit --allow-empty --message "initial" && |
| 377 | |
| 378 | git ${pack_refs} --all && |
| 379 | test_line_count = 2 .git/packed-refs && |
| 380 | |
| 381 | # Create 15 loose references. |
| 382 | test_seq -f "create refs/heads/loose-%d HEAD" 15 | |
| 383 | git update-ref --stdin && |
| 384 | git ${pack_refs} --auto --all && |
| 385 | test_line_count = 2 .git/packed-refs && |
| 386 | |
| 387 | # Create the 16th loose reference, which should cause us to repack. |
| 388 | git update-ref refs/heads/loose-17 HEAD && |
| 389 | git ${pack_refs} --auto --all && |
| 390 | test_line_count = 18 .git/packed-refs |
| 391 | ) |
| 392 | ' |
| 393 | |
| 394 | test_expect_success "$command scales with size of packed-refs" ' |
| 395 | test_when_finished "rm -rf repo" && |
| 396 | git init repo && |
| 397 | ( |
| 398 | cd repo && |
| 399 | git config set maintenance.auto false && |
| 400 | git commit --allow-empty --message "initial" && |
| 401 | |
| 402 | # Create 99 packed refs. This should cause the heuristic |
| 403 | # to require more than the minimum amount of loose refs. |
| 404 | test_seq -f "create refs/heads/packed-%d HEAD" 99 | |
| 405 | git update-ref --stdin && |
| 406 | git ${pack_refs} --all && |
| 407 | test_line_count = 101 .git/packed-refs && |
| 408 | |
| 409 | # Create 24 loose refs, which should not yet cause us to repack. |
| 410 | test_seq -f "create refs/heads/loose-%d HEAD" 24 | |
| 411 | git update-ref --stdin && |
| 412 | git ${pack_refs} --auto --all && |
| 413 | test_line_count = 101 .git/packed-refs && |
| 414 | |
| 415 | # Create another handful of refs to cross the border. |
| 416 | # Note that we explicitly do not check for strict |
| 417 | # boundaries here, as this also depends on the size of |
| 418 | # the object hash. |
| 419 | test_seq -f "create refs/heads/addn-%d HEAD" 10 | |
| 420 | git update-ref --stdin && |
| 421 | git ${pack_refs} --auto --all && |
| 422 | test_line_count = 135 .git/packed-refs |
| 423 | ) |
| 424 | ' |
| 425 | done |
| 426 | |
| 427 | test_expect_success 'pack-refs does not store invalid peeled tag value' ' |
| 428 | test_when_finished rm -rf repo && |
| 429 | git init repo && |
| 430 | ( |
| 431 | cd repo && |
| 432 | git commit --allow-empty --message initial && |
| 433 | |
| 434 | echo garbage >blob-content && |
| 435 | blob_id=$(git hash-object -w -t blob blob-content) && |
| 436 | |
| 437 | # Write an invalid tag into the object database. The tag itself |
| 438 | # is well-formed, but the tagged object is a blob while we |
| 439 | # claim that it is a commit. |
| 440 | cat >tag-content <<-EOF && |
| 441 | object $blob_id |
| 442 | type commit |
| 443 | tag bad-tag |
| 444 | tagger C O Mitter <committer@example.com> 1112354055 +0200 |
| 445 | |
| 446 | annotated |
| 447 | EOF |
| 448 | tag_id=$(git hash-object -w -t tag tag-content) && |
| 449 | git update-ref refs/tags/bad-tag "$tag_id" && |
| 450 | |
| 451 | # The packed-refs file should not contain the peeled object ID. |
| 452 | # If it did this would cause commands that use the peeled value |
| 453 | # to not notice this corrupted tag. |
| 454 | git pack-refs --all && |
| 455 | test_grep ! "^\^" .git/packed-refs |
| 456 | ) |
| 457 | ' |