git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
git / t / t7900-maintenance.sh
| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='git maintenance builtin' |
| 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
| 7 | GIT_TEST_COMMIT_GRAPH=0 |
| 8 | GIT_TEST_MULTI_PACK_INDEX=0 |
| 9 | |
| 10 | test_lazy_prereq XMLLINT ' |
| 11 | xmllint --version |
| 12 | ' |
| 13 | |
| 14 | test_xmllint () { |
| 15 | if test_have_prereq XMLLINT |
| 16 | then |
| 17 | xmllint --noout "$@" |
| 18 | else |
| 19 | true |
| 20 | fi |
| 21 | } |
| 22 | |
| 23 | test_maintenance_tasks () { |
| 24 | cat >expect && |
| 25 | sed -ne "s/.*\"region_enter\".*\"category\":\"maintenance\([^\"]*\)\".*\"label\":\"\([^\"][^\"]*\)\".*/\2\1/p" "$1" >actual && |
| 26 | test_cmp expect actual |
| 27 | } |
| 28 | |
| 29 | test_lazy_prereq SYSTEMD_ANALYZE ' |
| 30 | systemd-analyze verify /lib/systemd/system/basic.target |
| 31 | ' |
| 32 | |
| 33 | test_systemd_analyze_verify () { |
| 34 | if test_have_prereq SYSTEMD_ANALYZE |
| 35 | then |
| 36 | systemd-analyze verify "$@" |
| 37 | fi |
| 38 | } |
| 39 | |
| 40 | test_expect_success 'help text' ' |
| 41 | git maintenance -h >actual && |
| 42 | test_grep "usage: git maintenance <subcommand>" actual && |
| 43 | test_expect_code 129 git maintenance barf 2>err && |
| 44 | test_grep "unknown subcommand: \`barf'\''" err && |
| 45 | test_grep "usage: git maintenance" err && |
| 46 | test_expect_code 129 git maintenance 2>err && |
| 47 | test_grep "error: need a subcommand" err && |
| 48 | test_grep "usage: git maintenance" err |
| 49 | ' |
| 50 | |
| 51 | test_expect_success 'run [--auto|--quiet] with gc strategy' ' |
| 52 | test_config maintenance.strategy gc && |
| 53 | GIT_TRACE2_EVENT="$(pwd)/run-no-auto.txt" \ |
| 54 | git maintenance run 2>/dev/null && |
| 55 | GIT_TRACE2_EVENT="$(pwd)/run-auto.txt" \ |
| 56 | git maintenance run --auto 2>/dev/null && |
| 57 | GIT_TRACE2_EVENT="$(pwd)/run-no-quiet.txt" \ |
| 58 | git maintenance run --no-quiet 2>/dev/null && |
| 59 | git maintenance is-needed && |
| 60 | test_subcommand git gc --quiet --no-detach --skip-foreground-tasks <run-no-auto.txt && |
| 61 | ! git maintenance is-needed --auto && |
| 62 | test_subcommand ! git gc --auto --quiet --no-detach --skip-foreground-tasks <run-auto.txt && |
| 63 | test_subcommand git gc --no-quiet --no-detach --skip-foreground-tasks <run-no-quiet.txt |
| 64 | ' |
| 65 | |
| 66 | test_expect_success 'maintenance.auto config option' ' |
| 67 | test_when_finished "rm -rf repo" && |
| 68 | git init repo && |
| 69 | ( |
| 70 | cd repo && |
| 71 | sane_unset GIT_TEST_MAINT_AUTO_DETACH && |
| 72 | |
| 73 | GIT_TRACE2_EVENT="$(pwd)/default" git commit --quiet --allow-empty -m 1 && |
| 74 | test_subcommand git maintenance run --auto --quiet --detach <default && |
| 75 | GIT_TRACE2_EVENT="$(pwd)/true" \ |
| 76 | git -c maintenance.auto=true \ |
| 77 | commit --quiet --allow-empty -m 2 && |
| 78 | test_subcommand git maintenance run --auto --quiet --detach <true && |
| 79 | GIT_TRACE2_EVENT="$(pwd)/false" \ |
| 80 | git -c maintenance.auto=false \ |
| 81 | commit --quiet --allow-empty -m 3 && |
| 82 | test_subcommand ! git maintenance run --auto --quiet --detach <false |
| 83 | ) |
| 84 | ' |
| 85 | |
| 86 | test_expect_success 'gc.auto config option' ' |
| 87 | test_when_finished "rm -rf repo" && |
| 88 | git init repo && |
| 89 | ( |
| 90 | cd repo && |
| 91 | sane_unset GIT_TEST_MAINT_AUTO_DETACH && |
| 92 | |
| 93 | GIT_TRACE2_EVENT="$(pwd)/default" git commit --quiet --allow-empty -m 1 && |
| 94 | test_subcommand git maintenance run --auto --quiet --detach <default && |
| 95 | GIT_TRACE2_EVENT="$(pwd)/true" \ |
| 96 | git -c gc.auto=1 commit --quiet --allow-empty -m 2 && |
| 97 | test_subcommand git maintenance run --auto --quiet --detach <true && |
| 98 | GIT_TRACE2_EVENT="$(pwd)/false" \ |
| 99 | git -c gc.auto=0 commit --quiet --allow-empty -m 3 && |
| 100 | test_subcommand ! git maintenance run --auto --quiet --detach <false |
| 101 | ) |
| 102 | ' |
| 103 | |
| 104 | test_expect_success 'maintenance.auto overrides gc.auto' ' |
| 105 | test_when_finished "rm -rf repo" && |
| 106 | git init repo && |
| 107 | ( |
| 108 | cd repo && |
| 109 | sane_unset GIT_TEST_MAINT_AUTO_DETACH && |
| 110 | |
| 111 | git config set maintenance.auto false && |
| 112 | git config set gc.auto 1 && |
| 113 | GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 && |
| 114 | test_subcommand ! git maintenance run --auto --quiet --detach <trace && |
| 115 | |
| 116 | git config set maintenance.auto true && |
| 117 | git config set gc.auto 0 && |
| 118 | GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 && |
| 119 | test_subcommand git maintenance run --auto --quiet --detach <trace |
| 120 | ) |
| 121 | ' |
| 122 | |
| 123 | for cfg in maintenance.autoDetach gc.autoDetach |
| 124 | do |
| 125 | test_expect_success "$cfg=true config option" ' |
| 126 | test_when_finished "rm -f trace" && |
| 127 | test_config $cfg true && |
| 128 | GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 && |
| 129 | test_subcommand git maintenance run --auto --quiet --detach <trace |
| 130 | ' |
| 131 | |
| 132 | test_expect_success "$cfg=false config option" ' |
| 133 | test_when_finished "rm -f trace" && |
| 134 | test_config $cfg false && |
| 135 | GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 && |
| 136 | test_subcommand git maintenance run --auto --quiet --no-detach <trace |
| 137 | ' |
| 138 | done |
| 139 | |
| 140 | test_expect_success "maintenance.autoDetach overrides gc.autoDetach" ' |
| 141 | test_when_finished "rm -f trace" && |
| 142 | test_config maintenance.autoDetach false && |
| 143 | test_config gc.autoDetach true && |
| 144 | GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 && |
| 145 | test_subcommand git maintenance run --auto --quiet --no-detach <trace |
| 146 | ' |
| 147 | |
| 148 | test_expect_success 'register uses XDG_CONFIG_HOME config if it exists' ' |
| 149 | test_when_finished rm -r .config/git/config && |
| 150 | ( |
| 151 | # Override HOME so that .gitconfig (which test-lib.sh may |
| 152 | # have created, e.g. to set safe.bareRepository) does not |
| 153 | # take precedence over the XDG location. |
| 154 | HOME=$PWD/must-not-exist && |
| 155 | XDG_CONFIG_HOME=.config && |
| 156 | export HOME XDG_CONFIG_HOME && |
| 157 | mkdir -p $XDG_CONFIG_HOME/git && |
| 158 | >$XDG_CONFIG_HOME/git/config && |
| 159 | git maintenance register && |
| 160 | git config --file=$XDG_CONFIG_HOME/git/config --get maintenance.repo >actual && |
| 161 | pwd >expect && |
| 162 | test_cmp expect actual |
| 163 | ) |
| 164 | ' |
| 165 | |
| 166 | test_expect_success 'register does not need XDG_CONFIG_HOME config to exist' ' |
| 167 | test_when_finished git maintenance unregister && |
| 168 | test_path_is_missing $XDG_CONFIG_HOME/git/config && |
| 169 | git maintenance register && |
| 170 | git config --global --get maintenance.repo >actual && |
| 171 | pwd >expect && |
| 172 | test_cmp expect actual |
| 173 | ' |
| 174 | |
| 175 | test_expect_success 'unregister uses XDG_CONFIG_HOME config if it exists' ' |
| 176 | test_when_finished rm -r .config/git/config && |
| 177 | ( |
| 178 | # Override HOME so that .gitconfig (which test-lib.sh may |
| 179 | # have created, e.g. to set safe.bareRepository) does not |
| 180 | # take precedence over the XDG location. |
| 181 | HOME=$PWD/must-not-exist && |
| 182 | XDG_CONFIG_HOME=.config && |
| 183 | export HOME XDG_CONFIG_HOME && |
| 184 | mkdir -p $XDG_CONFIG_HOME/git && |
| 185 | >$XDG_CONFIG_HOME/git/config && |
| 186 | git maintenance register && |
| 187 | git maintenance unregister && |
| 188 | test_must_fail git config --file=$XDG_CONFIG_HOME/git/config --get maintenance.repo >actual && |
| 189 | test_must_be_empty actual |
| 190 | ) |
| 191 | ' |
| 192 | |
| 193 | test_expect_success 'unregister does not need XDG_CONFIG_HOME config to exist' ' |
| 194 | test_path_is_missing $XDG_CONFIG_HOME/git/config && |
| 195 | git maintenance register && |
| 196 | git maintenance unregister && |
| 197 | test_must_fail git config --global --get maintenance.repo >actual && |
| 198 | test_must_be_empty actual |
| 199 | ' |
| 200 | |
| 201 | test_expect_success 'maintenance.<task>.enabled' ' |
| 202 | git config maintenance.gc.enabled false && |
| 203 | git config maintenance.commit-graph.enabled true && |
| 204 | GIT_TRACE2_EVENT="$(pwd)/run-config.txt" git maintenance run 2>err && |
| 205 | test_maintenance_tasks run-config.txt <<-\EOF |
| 206 | commit-graph |
| 207 | EOF |
| 208 | ' |
| 209 | |
| 210 | test_expect_success 'run --task=<task>' ' |
| 211 | GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" \ |
| 212 | git maintenance run --task=commit-graph 2>/dev/null && |
| 213 | GIT_TRACE2_EVENT="$(pwd)/run-gc.txt" \ |
| 214 | git maintenance run --task=gc 2>/dev/null && |
| 215 | GIT_TRACE2_EVENT="$(pwd)/run-both.txt" \ |
| 216 | git maintenance run --task=commit-graph --task=gc 2>/dev/null && |
| 217 | test_maintenance_tasks run-commit-graph.txt <<-\EOF && |
| 218 | commit-graph |
| 219 | EOF |
| 220 | test_maintenance_tasks run-gc.txt <<-\EOF && |
| 221 | gc foreground |
| 222 | gc |
| 223 | EOF |
| 224 | test_maintenance_tasks run-both.txt <<-\EOF |
| 225 | gc foreground |
| 226 | commit-graph |
| 227 | gc |
| 228 | EOF |
| 229 | ' |
| 230 | |
| 231 | test_expect_success 'core.commitGraph=false prevents write process' ' |
| 232 | GIT_TRACE2_EVENT="$(pwd)/no-commit-graph.txt" \ |
| 233 | git -c core.commitGraph=false maintenance run \ |
| 234 | --task=commit-graph 2>/dev/null && |
| 235 | test_subcommand ! git commit-graph write --split --reachable --no-progress \ |
| 236 | <no-commit-graph.txt |
| 237 | ' |
| 238 | |
| 239 | test_expect_success 'commit-graph auto condition' ' |
| 240 | COMMAND="maintenance run --task=commit-graph --auto --quiet" && |
| 241 | |
| 242 | GIT_TRACE2_EVENT="$(pwd)/cg-no.txt" \ |
| 243 | git -c maintenance.commit-graph.auto=1 $COMMAND && |
| 244 | GIT_TRACE2_EVENT="$(pwd)/cg-negative-means-yes.txt" \ |
| 245 | git -c maintenance.commit-graph.auto="-1" $COMMAND && |
| 246 | |
| 247 | test_commit first && |
| 248 | |
| 249 | ! git -c maintenance.commit-graph.auto=0 \ |
| 250 | maintenance is-needed --auto --task=commit-graph && |
| 251 | git -c maintenance.commit-graph.auto=1 \ |
| 252 | maintenance is-needed --auto --task=commit-graph && |
| 253 | |
| 254 | GIT_TRACE2_EVENT="$(pwd)/cg-zero-means-no.txt" \ |
| 255 | git -c maintenance.commit-graph.auto=0 $COMMAND && |
| 256 | GIT_TRACE2_EVENT="$(pwd)/cg-one-satisfied.txt" \ |
| 257 | git -c maintenance.commit-graph.auto=1 $COMMAND && |
| 258 | |
| 259 | git commit --allow-empty -m "second" && |
| 260 | git commit --allow-empty -m "third" && |
| 261 | |
| 262 | GIT_TRACE2_EVENT="$(pwd)/cg-two-satisfied.txt" \ |
| 263 | git -c maintenance.commit-graph.auto=2 $COMMAND && |
| 264 | |
| 265 | test_maintenance_tasks cg-no.txt <<-\EOF && |
| 266 | EOF |
| 267 | test_maintenance_tasks cg-negative-means-yes.txt <<-\EOF && |
| 268 | commit-graph |
| 269 | EOF |
| 270 | test_maintenance_tasks cg-zero-means-no.txt <<-\EOF && |
| 271 | EOF |
| 272 | test_maintenance_tasks cg-one-satisfied.txt <<-\EOF && |
| 273 | commit-graph |
| 274 | EOF |
| 275 | test_maintenance_tasks cg-two-satisfied.txt <<-\EOF |
| 276 | commit-graph |
| 277 | EOF |
| 278 | ' |
| 279 | |
| 280 | test_expect_success 'commit-graph auto condition with merges' ' |
| 281 | test_when_finished "rm -rf repo" && |
| 282 | git init repo && |
| 283 | ( |
| 284 | cd repo && |
| 285 | git config set maintenance.auto false && |
| 286 | test_commit initial && |
| 287 | git switch --create feature && |
| 288 | test_commit feature-1 && |
| 289 | test_commit feature-2 && |
| 290 | git switch - && |
| 291 | test_commit main-1 && |
| 292 | test_commit main-2 && |
| 293 | git merge feature && |
| 294 | |
| 295 | # We have 6 commits, none of which are covered by a commit |
| 296 | # graph. So this must be the boundary at which we start to |
| 297 | # perform maintenance. |
| 298 | test_must_fail git -c maintenance.commit-graph.auto=7 \ |
| 299 | maintenance is-needed --auto --task=commit-graph && |
| 300 | git -c maintenance.commit-graph.auto=6 \ |
| 301 | maintenance is-needed --auto --task=commit-graph |
| 302 | ) |
| 303 | ' |
| 304 | |
| 305 | test_expect_success 'run --task=bogus' ' |
| 306 | test_must_fail git maintenance run --task=bogus 2>err && |
| 307 | test_grep "is not a valid task" err |
| 308 | ' |
| 309 | |
| 310 | test_expect_success 'run --task duplicate' ' |
| 311 | test_must_fail git maintenance run --task=gc --task=gc 2>err && |
| 312 | test_grep "cannot be selected multiple times" err |
| 313 | ' |
| 314 | |
| 315 | test_expect_success 'run --task=prefetch with no remotes' ' |
| 316 | git maintenance run --task=prefetch 2>err && |
| 317 | test_must_be_empty err |
| 318 | ' |
| 319 | |
| 320 | test_expect_success 'prefetch multiple remotes' ' |
| 321 | git clone . clone1 && |
| 322 | git clone . clone2 && |
| 323 | git remote add remote1 "file://$(pwd)/clone1" && |
| 324 | git remote add remote2 "file://$(pwd)/clone2" && |
| 325 | git -C clone1 switch -c one && |
| 326 | git -C clone2 switch -c two && |
| 327 | test_commit -C clone1 one && |
| 328 | test_commit -C clone2 two && |
| 329 | GIT_TRACE2_EVENT="$(pwd)/run-prefetch.txt" git maintenance run --task=prefetch 2>/dev/null && |
| 330 | fetchargs="--prefetch --prune --no-tags --no-write-fetch-head --recurse-submodules=no --quiet" && |
| 331 | test_subcommand git fetch remote1 $fetchargs <run-prefetch.txt && |
| 332 | test_subcommand git fetch remote2 $fetchargs <run-prefetch.txt && |
| 333 | git for-each-ref refs/remotes >actual && |
| 334 | test_must_be_empty actual && |
| 335 | git log prefetch/remotes/remote1/one && |
| 336 | git log prefetch/remotes/remote2/two && |
| 337 | git fetch --all && |
| 338 | test_cmp_rev refs/remotes/remote1/one refs/prefetch/remotes/remote1/one && |
| 339 | test_cmp_rev refs/remotes/remote2/two refs/prefetch/remotes/remote2/two && |
| 340 | |
| 341 | git log --oneline --decorate --all >log && |
| 342 | test_grep ! "prefetch" log && |
| 343 | |
| 344 | test_when_finished git config --unset remote.remote1.skipFetchAll && |
| 345 | git config remote.remote1.skipFetchAll true && |
| 346 | GIT_TRACE2_EVENT="$(pwd)/skip-remote1.txt" git maintenance run --task=prefetch 2>/dev/null && |
| 347 | test_subcommand ! git fetch remote1 $fetchargs <skip-remote1.txt && |
| 348 | test_subcommand git fetch remote2 $fetchargs <skip-remote1.txt |
| 349 | ' |
| 350 | |
| 351 | test_expect_success 'loose-objects task' ' |
| 352 | # Repack everything so we know the state of the object dir |
| 353 | git repack -adk && |
| 354 | |
| 355 | # Hack to stop maintenance from running during "git commit" |
| 356 | echo in use >.git/objects/maintenance.lock && |
| 357 | |
| 358 | # Assuming that "git commit" creates at least one loose object |
| 359 | test_commit create-loose-object && |
| 360 | rm .git/objects/maintenance.lock && |
| 361 | |
| 362 | ls .git/objects >obj-dir-before && |
| 363 | test_file_not_empty obj-dir-before && |
| 364 | ls .git/objects/pack/*.pack >packs-before && |
| 365 | test_line_count = 1 packs-before && |
| 366 | |
| 367 | # The first run creates a pack-file |
| 368 | # but does not delete loose objects. |
| 369 | git maintenance run --task=loose-objects && |
| 370 | ls .git/objects >obj-dir-between && |
| 371 | test_cmp obj-dir-before obj-dir-between && |
| 372 | ls .git/objects/pack/*.pack >packs-between && |
| 373 | test_line_count = 2 packs-between && |
| 374 | ls .git/objects/pack/loose-*.pack >loose-packs && |
| 375 | test_line_count = 1 loose-packs && |
| 376 | |
| 377 | # The second run deletes loose objects |
| 378 | # but does not create a pack-file. |
| 379 | git maintenance run --task=loose-objects && |
| 380 | ls .git/objects >obj-dir-after && |
| 381 | cat >expect <<-\EOF && |
| 382 | info |
| 383 | pack |
| 384 | EOF |
| 385 | test_cmp expect obj-dir-after && |
| 386 | ls .git/objects/pack/*.pack >packs-after && |
| 387 | test_cmp packs-between packs-after |
| 388 | ' |
| 389 | |
| 390 | test_expect_success 'maintenance.loose-objects.auto' ' |
| 391 | git repack -adk && |
| 392 | GIT_TRACE2_EVENT="$(pwd)/trace-lo1.txt" \ |
| 393 | git -c maintenance.loose-objects.auto=1 maintenance \ |
| 394 | run --auto --task=loose-objects 2>/dev/null && |
| 395 | test_subcommand ! git prune-packed --quiet <trace-lo1.txt && |
| 396 | |
| 397 | printf data-A | git hash-object -t blob --stdin -w && |
| 398 | ! git -c maintenance.loose-objects.auto=2 \ |
| 399 | maintenance is-needed --auto --task=loose-objects && |
| 400 | GIT_TRACE2_EVENT="$(pwd)/trace-loA" \ |
| 401 | git -c maintenance.loose-objects.auto=2 \ |
| 402 | maintenance run --auto --task=loose-objects 2>/dev/null && |
| 403 | test_subcommand ! git prune-packed --quiet <trace-loA && |
| 404 | |
| 405 | printf data-B | git hash-object -t blob --stdin -w && |
| 406 | git -c maintenance.loose-objects.auto=2 \ |
| 407 | maintenance is-needed --auto --task=loose-objects && |
| 408 | GIT_TRACE2_EVENT="$(pwd)/trace-loB" \ |
| 409 | git -c maintenance.loose-objects.auto=2 \ |
| 410 | maintenance run --auto --task=loose-objects 2>/dev/null && |
| 411 | test_subcommand git prune-packed --quiet <trace-loB && |
| 412 | |
| 413 | GIT_TRACE2_EVENT="$(pwd)/trace-loC" \ |
| 414 | git -c maintenance.loose-objects.auto=2 \ |
| 415 | maintenance run --auto --task=loose-objects 2>/dev/null && |
| 416 | test_subcommand git prune-packed --quiet <trace-loC |
| 417 | ' |
| 418 | |
| 419 | test_expect_success 'maintenance.loose-objects.batchSize' ' |
| 420 | git init loose-batch && |
| 421 | |
| 422 | # This creates three objects per commit. |
| 423 | test_commit_bulk -C loose-batch 34 && |
| 424 | pack=$(ls loose-batch/.git/objects/pack/pack-*.pack) && |
| 425 | index="${pack%pack}idx" && |
| 426 | rm "$index" && |
| 427 | git -C loose-batch unpack-objects <"$pack" && |
| 428 | git -C loose-batch config maintenance.loose-objects.batchSize 50 && |
| 429 | |
| 430 | GIT_PROGRESS_DELAY=0 \ |
| 431 | git -C loose-batch maintenance run --no-quiet --task=loose-objects 2>err && |
| 432 | test_grep "Enumerating objects: 50, done." err && |
| 433 | |
| 434 | GIT_PROGRESS_DELAY=0 \ |
| 435 | git -C loose-batch maintenance run --no-quiet --task=loose-objects 2>err && |
| 436 | test_grep "Enumerating objects: 50, done." err && |
| 437 | |
| 438 | GIT_PROGRESS_DELAY=0 \ |
| 439 | git -C loose-batch maintenance run --no-quiet --task=loose-objects 2>err && |
| 440 | test_grep "Enumerating objects: 2, done." err && |
| 441 | |
| 442 | GIT_PROGRESS_DELAY=0 \ |
| 443 | git -C loose-batch maintenance run --no-quiet --task=loose-objects 2>err && |
| 444 | test_must_be_empty err |
| 445 | ' |
| 446 | |
| 447 | test_expect_success 'incremental-repack task' ' |
| 448 | packDir=.git/objects/pack && |
| 449 | for i in $(test_seq 1 5) |
| 450 | do |
| 451 | test_commit $i || return 1 |
| 452 | done && |
| 453 | |
| 454 | # Create three disjoint pack-files with size BIG, small, small. |
| 455 | echo HEAD~2 | git pack-objects --revs $packDir/test-1 && |
| 456 | test_tick && |
| 457 | git pack-objects --revs $packDir/test-2 <<-\EOF && |
| 458 | HEAD~1 |
| 459 | ^HEAD~2 |
| 460 | EOF |
| 461 | test_tick && |
| 462 | git pack-objects --revs $packDir/test-3 <<-\EOF && |
| 463 | HEAD |
| 464 | ^HEAD~1 |
| 465 | EOF |
| 466 | |
| 467 | # Delete refs that have not been repacked in these packs. |
| 468 | git for-each-ref --format="delete %(refname)" \ |
| 469 | refs/prefetch refs/tags refs/remotes \ |
| 470 | --exclude=refs/remotes/*/HEAD >refs && |
| 471 | git update-ref --stdin <refs && |
| 472 | |
| 473 | # Replace the object directory with this pack layout. |
| 474 | rm -f $packDir/pack-* && |
| 475 | rm -f $packDir/loose-* && |
| 476 | ls $packDir/*.pack >packs-before && |
| 477 | test_line_count = 3 packs-before && |
| 478 | |
| 479 | # make sure we do not have any broken refs that were |
| 480 | # missed in the deletion above |
| 481 | git for-each-ref && |
| 482 | |
| 483 | # the job repacks the two into a new pack, but does not |
| 484 | # delete the old ones. |
| 485 | git maintenance run --task=incremental-repack && |
| 486 | ls $packDir/*.pack >packs-between && |
| 487 | test_line_count = 4 packs-between && |
| 488 | |
| 489 | # the job deletes the two old packs, and does not write |
| 490 | # a new one because the batch size is not high enough to |
| 491 | # pack the largest pack-file. |
| 492 | git maintenance run --task=incremental-repack && |
| 493 | ls .git/objects/pack/*.pack >packs-after && |
| 494 | test_line_count = 2 packs-after |
| 495 | ' |
| 496 | |
| 497 | test_expect_success EXPENSIVE 'incremental-repack 2g limit' ' |
| 498 | test_when_finished rm -rf expensive-repo && |
| 499 | git init expensive-repo && |
| 500 | ( |
| 501 | cd expensive-repo && |
| 502 | git config set core.compression 0 && |
| 503 | git config set maintenance.auto false && |
| 504 | |
| 505 | for i in $(test_seq 1 5) |
| 506 | do |
| 507 | test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big || |
| 508 | return 1 |
| 509 | done && |
| 510 | git add big && |
| 511 | git commit -qm "Add big file (1)" && |
| 512 | |
| 513 | # ensure any possible loose objects are in a pack-file |
| 514 | git maintenance run --task=loose-objects && |
| 515 | |
| 516 | rm big && |
| 517 | for i in $(test_seq 6 10) |
| 518 | do |
| 519 | test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big || |
| 520 | return 1 |
| 521 | done && |
| 522 | git add big && |
| 523 | git commit -qm "Add big file (2)" && |
| 524 | |
| 525 | # ensure any possible loose objects are in a pack-file |
| 526 | git maintenance run --task=loose-objects && |
| 527 | |
| 528 | # Now run the incremental-repack task and check the batch-size |
| 529 | GIT_TRACE2_EVENT="$(pwd)/run-2g.txt" git maintenance run \ |
| 530 | --task=incremental-repack 2>/dev/null && |
| 531 | test_subcommand git multi-pack-index repack \ |
| 532 | --no-progress --batch-size=2147483647 <run-2g.txt |
| 533 | ) |
| 534 | ' |
| 535 | |
| 536 | run_incremental_repack_and_verify () { |
| 537 | test_commit A && |
| 538 | git repack -adk && |
| 539 | git multi-pack-index write && |
| 540 | ! git -c maintenance.incremental-repack.auto=1 \ |
| 541 | maintenance is-needed --auto --task=incremental-repack && |
| 542 | GIT_TRACE2_EVENT="$(pwd)/midx-init.txt" git \ |
| 543 | -c maintenance.incremental-repack.auto=1 \ |
| 544 | maintenance run --auto --task=incremental-repack 2>/dev/null && |
| 545 | test_subcommand ! git multi-pack-index write --no-progress <midx-init.txt && |
| 546 | |
| 547 | test_commit B && |
| 548 | git pack-objects --revs .git/objects/pack/pack <<-\EOF && |
| 549 | HEAD |
| 550 | ^HEAD~1 |
| 551 | EOF |
| 552 | GIT_TRACE2_EVENT=$(pwd)/trace-A git \ |
| 553 | -c maintenance.incremental-repack.auto=2 \ |
| 554 | maintenance run --auto --task=incremental-repack 2>/dev/null && |
| 555 | test_subcommand ! git multi-pack-index write --no-progress <trace-A && |
| 556 | |
| 557 | test_commit C && |
| 558 | git pack-objects --revs .git/objects/pack/pack <<-\EOF && |
| 559 | HEAD |
| 560 | ^HEAD~1 |
| 561 | EOF |
| 562 | git -c maintenance.incremental-repack.auto=2 \ |
| 563 | maintenance is-needed --auto --task=incremental-repack && |
| 564 | GIT_TRACE2_EVENT=$(pwd)/trace-B git \ |
| 565 | -c maintenance.incremental-repack.auto=2 \ |
| 566 | maintenance run --auto --task=incremental-repack 2>/dev/null && |
| 567 | test_subcommand git multi-pack-index write --no-progress <trace-B |
| 568 | } |
| 569 | |
| 570 | test_expect_success 'maintenance.incremental-repack.auto' ' |
| 571 | rm -rf incremental-repack-true && |
| 572 | git init incremental-repack-true && |
| 573 | ( |
| 574 | cd incremental-repack-true && |
| 575 | git config core.multiPackIndex true && |
| 576 | git config maintenance.auto false && |
| 577 | run_incremental_repack_and_verify |
| 578 | ) |
| 579 | ' |
| 580 | |
| 581 | test_expect_success 'maintenance.incremental-repack.auto (when config is unset)' ' |
| 582 | rm -rf incremental-repack-unset && |
| 583 | git init incremental-repack-unset && |
| 584 | ( |
| 585 | cd incremental-repack-unset && |
| 586 | test_unconfig core.multiPackIndex && |
| 587 | git config maintenance.auto false && |
| 588 | run_incremental_repack_and_verify |
| 589 | ) |
| 590 | ' |
| 591 | |
| 592 | run_and_verify_geometric_pack () { |
| 593 | EXPECTED_PACKS="$1" && |
| 594 | |
| 595 | # Verify that we perform a geometric repack. |
| 596 | rm -f "trace2.txt" && |
| 597 | GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \ |
| 598 | git maintenance run --task=geometric-repack 2>/dev/null && |
| 599 | test_subcommand git repack -d -l -q --geometric=2 \ |
| 600 | --write-midx <trace2.txt && |
| 601 | |
| 602 | # Verify that the number of packfiles matches our expectation. |
| 603 | ls -l .git/objects/pack/*.pack >packfiles && |
| 604 | test_line_count = "$EXPECTED_PACKS" packfiles && |
| 605 | |
| 606 | # And verify that there are no loose objects anymore. |
| 607 | git count-objects -v >count && |
| 608 | test_grep '^count: 0$' count && |
| 609 | |
| 610 | # Verify that no orphaned .idx files were left behind. On |
| 611 | # Windows, a missing odb_to_close causes the parent to hold |
| 612 | # mmap handles on .idx files, silently preventing their |
| 613 | # deletion by the child git-repack process. |
| 614 | ls .git/objects/pack/pack-*.idx .git/objects/pack/pack-*.pack | |
| 615 | sed "s/\.pack$/.idx/" | |
| 616 | sort | uniq -u >orphaned-idx && |
| 617 | test_must_be_empty orphaned-idx |
| 618 | } |
| 619 | |
| 620 | test_expect_success 'geometric repacking task' ' |
| 621 | test_when_finished "rm -rf repo" && |
| 622 | git init repo && |
| 623 | ( |
| 624 | cd repo && |
| 625 | git config set maintenance.auto false && |
| 626 | test_commit initial && |
| 627 | |
| 628 | # The initial repack causes an all-into-one repack. |
| 629 | GIT_TRACE2_EVENT="$(pwd)/initial-repack.txt" \ |
| 630 | git maintenance run --task=geometric-repack 2>/dev/null && |
| 631 | test_subcommand git repack -d -l -q --cruft --cruft-expiration=2.weeks.ago \ |
| 632 | --write-midx <initial-repack.txt && |
| 633 | |
| 634 | # Repacking should now cause a no-op geometric repack because |
| 635 | # no packfiles need to be combined. |
| 636 | ls -l .git/objects/pack/*.pack >before && |
| 637 | run_and_verify_geometric_pack 1 && |
| 638 | ls -l .git/objects/pack/*.pack >after && |
| 639 | test_cmp before after && |
| 640 | |
| 641 | # This incremental change creates a new packfile that only |
| 642 | # soaks up loose objects. The packfiles are not getting merged |
| 643 | # at this point. |
| 644 | test_commit loose && |
| 645 | run_and_verify_geometric_pack 2 && |
| 646 | |
| 647 | # Both packfiles have 3 objects, so the next run would cause us |
| 648 | # to merge all packfiles together. This should be turned into |
| 649 | # an all-into-one-repack. |
| 650 | GIT_TRACE2_EVENT="$(pwd)/all-into-one-repack.txt" \ |
| 651 | git maintenance run --task=geometric-repack 2>/dev/null && |
| 652 | test_subcommand git repack -d -l -q --cruft --cruft-expiration=2.weeks.ago \ |
| 653 | --write-midx <all-into-one-repack.txt && |
| 654 | |
| 655 | # The geometric repack soaks up unreachable objects. |
| 656 | echo blob-1 | git hash-object -w --stdin -t blob && |
| 657 | run_and_verify_geometric_pack 2 && |
| 658 | |
| 659 | # A second unreachable object should be written into another packfile. |
| 660 | echo blob-2 | git hash-object -w --stdin -t blob && |
| 661 | run_and_verify_geometric_pack 3 && |
| 662 | |
| 663 | # And these two small packs should now be merged via the |
| 664 | # geometric repack. The large packfile should remain intact. |
| 665 | cp -R .git/objects .git/objects.save && |
| 666 | run_and_verify_geometric_pack 2 && |
| 667 | |
| 668 | # On Windows, verify the same with legacy delete semantics |
| 669 | # that reject deletion of mmap-held .idx files. |
| 670 | if test_have_prereq MINGW |
| 671 | then |
| 672 | rm -rf .git/objects && |
| 673 | mv .git/objects.save .git/objects && |
| 674 | test_env GIT_TEST_LEGACY_DELETE=1 \ |
| 675 | run_and_verify_geometric_pack 2 |
| 676 | fi && |
| 677 | |
| 678 | # If we now add two more objects and repack twice we should |
| 679 | # then see another all-into-one repack. This time around |
| 680 | # though, as we have unreachable objects, we should also see a |
| 681 | # cruft pack. |
| 682 | echo blob-3 | git hash-object -w --stdin -t blob && |
| 683 | echo blob-4 | git hash-object -w --stdin -t blob && |
| 684 | run_and_verify_geometric_pack 3 && |
| 685 | GIT_TRACE2_EVENT="$(pwd)/cruft-repack.txt" \ |
| 686 | git maintenance run --task=geometric-repack 2>/dev/null && |
| 687 | test_subcommand git repack -d -l -q --cruft --cruft-expiration=2.weeks.ago \ |
| 688 | --write-midx <cruft-repack.txt && |
| 689 | ls .git/objects/pack/*.pack >packs && |
| 690 | test_line_count = 2 packs && |
| 691 | ls .git/objects/pack/*.mtimes >cruft && |
| 692 | test_line_count = 1 cruft |
| 693 | ) |
| 694 | ' |
| 695 | |
| 696 | test_geometric_repack_needed () { |
| 697 | NEEDED="$1" |
| 698 | GEOMETRIC_CONFIG="$2" && |
| 699 | rm -f trace2.txt && |
| 700 | GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \ |
| 701 | git ${GEOMETRIC_CONFIG:+-c maintenance.geometric-repack.$GEOMETRIC_CONFIG} \ |
| 702 | maintenance run --auto --task=geometric-repack 2>/dev/null && |
| 703 | case "$NEEDED" in |
| 704 | true) |
| 705 | test_grep "\[\"git\",\"repack\"," trace2.txt;; |
| 706 | false) |
| 707 | ! test_grep "\[\"git\",\"repack\"," trace2.txt;; |
| 708 | *) |
| 709 | BUG "invalid parameter: $NEEDED";; |
| 710 | esac |
| 711 | } |
| 712 | |
| 713 | test_expect_success 'geometric repacking with --auto' ' |
| 714 | test_when_finished "rm -rf repo" && |
| 715 | git init repo && |
| 716 | ( |
| 717 | cd repo && |
| 718 | git config set maintenance.auto false && |
| 719 | |
| 720 | # An empty repository does not need repacking, except when |
| 721 | # explicitly told to do it. |
| 722 | test_geometric_repack_needed false && |
| 723 | test_geometric_repack_needed false auto=0 && |
| 724 | test_geometric_repack_needed false auto=1 && |
| 725 | test_geometric_repack_needed true auto=-1 && |
| 726 | |
| 727 | test_oid_init && |
| 728 | |
| 729 | # Loose objects cause a repack when crossing the limit. Note |
| 730 | # that the number of objects gets extrapolated by having a look |
| 731 | # at the "objects/17/" shard. |
| 732 | test_commit "$(test_oid blob17_1)" && |
| 733 | test_geometric_repack_needed false && |
| 734 | test_commit "$(test_oid blob17_2)" && |
| 735 | test_geometric_repack_needed false auto=257 && |
| 736 | test_geometric_repack_needed true auto=256 && |
| 737 | |
| 738 | # Force another repack. |
| 739 | test_commit first && |
| 740 | test_commit second && |
| 741 | test_geometric_repack_needed true auto=-1 && |
| 742 | |
| 743 | # We now have two packfiles that would be merged together. As |
| 744 | # such, the repack should always happen unless the user has |
| 745 | # disabled the auto task. |
| 746 | test_geometric_repack_needed false auto=0 && |
| 747 | test_geometric_repack_needed true auto=9000 |
| 748 | ) |
| 749 | ' |
| 750 | |
| 751 | test_expect_success 'geometric repacking honors configured split factor' ' |
| 752 | test_when_finished "rm -rf repo" && |
| 753 | git init repo && |
| 754 | ( |
| 755 | cd repo && |
| 756 | git config set maintenance.auto false && |
| 757 | |
| 758 | # Create three different packs with 9, 2 and 1 object, respectively. |
| 759 | # This is done so that only a subset of packs would be merged |
| 760 | # together so that we can verify that `git repack` receives the |
| 761 | # correct geometric factor. |
| 762 | for i in $(test_seq 9) |
| 763 | do |
| 764 | echo first-$i | git hash-object -w --stdin -t blob || return 1 |
| 765 | done && |
| 766 | git repack --geometric=2 -d && |
| 767 | |
| 768 | for i in $(test_seq 2) |
| 769 | do |
| 770 | echo second-$i | git hash-object -w --stdin -t blob || return 1 |
| 771 | done && |
| 772 | git repack --geometric=2 -d && |
| 773 | |
| 774 | echo third | git hash-object -w --stdin -t blob && |
| 775 | git repack --geometric=2 -d && |
| 776 | |
| 777 | test_geometric_repack_needed false splitFactor=2 && |
| 778 | test_geometric_repack_needed true splitFactor=3 && |
| 779 | test_subcommand git repack -d -l -q --geometric=3 --write-midx <trace2.txt |
| 780 | ) |
| 781 | ' |
| 782 | |
| 783 | test_expect_success 'pre-auto-gc hook runs exactly once' ' |
| 784 | test_when_finished "rm -rf repo" && |
| 785 | git init repo && |
| 786 | ( |
| 787 | cd repo && |
| 788 | write_script .git/hooks/pre-auto-gc <<-\EOF && |
| 789 | echo hook >>hook.log |
| 790 | EOF |
| 791 | |
| 792 | # Satisfy the auto condition for multiple tasks, both in the |
| 793 | # foreground and in the background phase. |
| 794 | git config set maintenance.reflog-expire.auto -1 && |
| 795 | git config set maintenance.geometric-repack.auto -1 && |
| 796 | git config set maintenance.rerere-gc.auto -1 && |
| 797 | |
| 798 | GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \ |
| 799 | git maintenance run --auto 2>/dev/null && |
| 800 | |
| 801 | # The successful hook does not inhibit any of the tasks... |
| 802 | test_maintenance_tasks trace2.txt <<-\EOF && |
| 803 | reflog-expire foreground |
| 804 | geometric-repack |
| 805 | rerere-gc |
| 806 | EOF |
| 807 | # ... but it must only have been executed a single time. |
| 808 | test_line_count = 1 hook.log |
| 809 | ) |
| 810 | ' |
| 811 | |
| 812 | test_expect_success 'pre-auto-gc hook can inhibit geometric strategy' ' |
| 813 | test_when_finished "rm -rf repo" && |
| 814 | git init repo && |
| 815 | ( |
| 816 | cd repo && |
| 817 | write_script .git/hooks/pre-auto-gc <<-\EOF && |
| 818 | echo hook >>hook.log |
| 819 | exit 1 |
| 820 | EOF |
| 821 | |
| 822 | git config set maintenance.reflog-expire.auto -1 && |
| 823 | git config set maintenance.geometric-repack.auto -1 && |
| 824 | git config set maintenance.rerere-gc.auto -1 && |
| 825 | |
| 826 | # Maintenance would be required... |
| 827 | git maintenance is-needed --auto && |
| 828 | |
| 829 | GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \ |
| 830 | git maintenance run --auto 2>/dev/null && |
| 831 | |
| 832 | # ... but the failing hook inhibits all tasks. The hook itself |
| 833 | # is expected to be the only child process being spawned, and |
| 834 | # it must only run a single time. |
| 835 | test_grep "child_start.*pre-auto-gc" trace2.txt && |
| 836 | test_maintenance_tasks trace2.txt <<-\EOF && |
| 837 | EOF |
| 838 | test_line_count = 1 hook.log |
| 839 | ) |
| 840 | ' |
| 841 | |
| 842 | test_expect_success 'pre-auto-gc hook can inhibit gc strategy' ' |
| 843 | test_when_finished "rm -rf repo" && |
| 844 | git init repo && |
| 845 | ( |
| 846 | cd repo && |
| 847 | write_script .git/hooks/pre-auto-gc <<-\EOF && |
| 848 | echo hook >>hook.log |
| 849 | exit 1 |
| 850 | EOF |
| 851 | |
| 852 | git config set maintenance.strategy gc && |
| 853 | git config set maintenance.auto false && |
| 854 | git config set gc.auto 3 && |
| 855 | |
| 856 | test_oid_init && |
| 857 | |
| 858 | # We need to create two objects whose hashes start with 17 |
| 859 | # since this is what the gc task counts. |
| 860 | test_commit "$(test_oid blob17_1)" && |
| 861 | test_commit "$(test_oid blob17_2)" && |
| 862 | |
| 863 | # Maintenance would be required... |
| 864 | git maintenance is-needed --auto && |
| 865 | |
| 866 | GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \ |
| 867 | git maintenance run --auto 2>/dev/null && |
| 868 | |
| 869 | # ... but the failing hook inhibits all tasks. The hook itself |
| 870 | # is expected to be the only child process being spawned, and |
| 871 | # it must only run a single time. |
| 872 | test_grep "child_start.*pre-auto-gc" trace2.txt && |
| 873 | test_maintenance_tasks trace2.txt <<-\EOF && |
| 874 | EOF |
| 875 | test_subcommand_flex ! git trace2 && |
| 876 | test_line_count = 1 hook.log |
| 877 | ) |
| 878 | ' |
| 879 | |
| 880 | test_expect_success 'pre-auto-gc hook does not run when no maintenance is needed' ' |
| 881 | test_when_finished "rm -rf repo" && |
| 882 | git init repo && |
| 883 | ( |
| 884 | cd repo && |
| 885 | write_script .git/hooks/pre-auto-gc <<-\EOF && |
| 886 | echo hook >>hook.log |
| 887 | EOF |
| 888 | test_must_fail git maintenance is-needed --auto && |
| 889 | git maintenance run --auto 2>/dev/null && |
| 890 | test_path_is_missing hook.log |
| 891 | ) |
| 892 | ' |
| 893 | |
| 894 | test_expect_success 'pre-auto-gc hook does not run without --auto' ' |
| 895 | test_when_finished "rm -rf repo" && |
| 896 | git init repo && |
| 897 | test_hook -C repo pre-auto-gc <<-\EOF && |
| 898 | echo hook >>hook.log |
| 899 | EOF |
| 900 | ( |
| 901 | cd repo && |
| 902 | GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \ |
| 903 | git maintenance run 2>/dev/null && |
| 904 | test_grep "\[\"git\",\"repack\"," trace2.txt && |
| 905 | test_path_is_missing hook.log |
| 906 | ) |
| 907 | ' |
| 908 | |
| 909 | test_expect_success 'pack-refs task' ' |
| 910 | for n in $(test_seq 1 5) |
| 911 | do |
| 912 | git branch -f to-pack/$n HEAD || return 1 |
| 913 | done && |
| 914 | GIT_TRACE2_EVENT="$(pwd)/pack-refs.txt" \ |
| 915 | git maintenance run --task=pack-refs && |
| 916 | test_subcommand git pack-refs --all --prune <pack-refs.txt |
| 917 | ' |
| 918 | |
| 919 | test_expect_success 'reflog-expire task' ' |
| 920 | GIT_TRACE2_EVENT="$(pwd)/reflog-expire.txt" \ |
| 921 | git maintenance run --task=reflog-expire && |
| 922 | test_subcommand git reflog expire --all <reflog-expire.txt |
| 923 | ' |
| 924 | |
| 925 | test_expect_success 'reflog-expire task --auto only packs when exceeding limits' ' |
| 926 | git reflog expire --all --expire=now && |
| 927 | test_commit reflog-one && |
| 928 | test_commit reflog-two && |
| 929 | |
| 930 | ! git -c maintenance.reflog-expire.auto=3 \ |
| 931 | maintenance is-needed --auto --task=reflog-expire && |
| 932 | GIT_TRACE2_EVENT="$(pwd)/reflog-expire-auto.txt" \ |
| 933 | git -c maintenance.reflog-expire.auto=3 maintenance run --auto --task=reflog-expire && |
| 934 | test_subcommand ! git reflog expire --all <reflog-expire-auto.txt && |
| 935 | |
| 936 | git -c maintenance.reflog-expire.auto=2 \ |
| 937 | maintenance is-needed --auto --task=reflog-expire && |
| 938 | GIT_TRACE2_EVENT="$(pwd)/reflog-expire-auto.txt" \ |
| 939 | git -c maintenance.reflog-expire.auto=2 maintenance run --auto --task=reflog-expire && |
| 940 | test_subcommand git reflog expire --all <reflog-expire-auto.txt |
| 941 | ' |
| 942 | |
| 943 | test_expect_worktree_prune () { |
| 944 | negate= |
| 945 | if test "$1" = "!" |
| 946 | then |
| 947 | negate="!" |
| 948 | shift |
| 949 | fi |
| 950 | |
| 951 | rm -f "worktree-prune.txt" && |
| 952 | GIT_TRACE2_EVENT="$(pwd)/worktree-prune.txt" "$@" && |
| 953 | test_subcommand $negate git worktree prune --expire 3.months.ago <worktree-prune.txt |
| 954 | } |
| 955 | |
| 956 | test_expect_success 'worktree-prune task without --auto always prunes' ' |
| 957 | test_expect_worktree_prune git maintenance run --task=worktree-prune |
| 958 | ' |
| 959 | |
| 960 | test_expect_success 'worktree-prune task --auto only prunes with prunable worktree' ' |
| 961 | test_expect_worktree_prune ! git maintenance run --auto --task=worktree-prune && |
| 962 | mkdir .git/worktrees && |
| 963 | : >.git/worktrees/abc && |
| 964 | git maintenance is-needed --auto --task=worktree-prune && |
| 965 | test_expect_worktree_prune git maintenance run --auto --task=worktree-prune |
| 966 | ' |
| 967 | |
| 968 | test_expect_success 'worktree-prune task with --auto honors maintenance.worktree-prune.auto' ' |
| 969 | # A negative value should always prune. |
| 970 | test_expect_worktree_prune git -c maintenance.worktree-prune.auto=-1 maintenance run --auto --task=worktree-prune && |
| 971 | |
| 972 | mkdir .git/worktrees && |
| 973 | : >.git/worktrees/first && |
| 974 | : >.git/worktrees/second && |
| 975 | : >.git/worktrees/third && |
| 976 | |
| 977 | # Zero should never prune. |
| 978 | test_expect_worktree_prune ! git -c maintenance.worktree-prune.auto=0 maintenance run --auto --task=worktree-prune && |
| 979 | # A positive value should require at least this many prunable worktrees. |
| 980 | test_expect_worktree_prune ! git -c maintenance.worktree-prune.auto=4 maintenance run --auto --task=worktree-prune && |
| 981 | git -c maintenance.worktree-prune.auto=3 maintenance is-needed --auto --task=worktree-prune && |
| 982 | test_expect_worktree_prune git -c maintenance.worktree-prune.auto=3 maintenance run --auto --task=worktree-prune |
| 983 | ' |
| 984 | |
| 985 | test_expect_success 'worktree-prune task honors gc.worktreePruneExpire' ' |
| 986 | git worktree add worktree && |
| 987 | rm -rf worktree && |
| 988 | |
| 989 | rm -f worktree-prune.txt && |
| 990 | ! git -c gc.worktreePruneExpire=1.week.ago maintenance is-needed --auto --task=worktree-prune && |
| 991 | GIT_TRACE2_EVENT="$(pwd)/worktree-prune.txt" git -c gc.worktreePruneExpire=1.week.ago maintenance run --auto --task=worktree-prune && |
| 992 | test_subcommand ! git worktree prune --expire 1.week.ago <worktree-prune.txt && |
| 993 | test_path_is_dir .git/worktrees/worktree && |
| 994 | |
| 995 | rm -f worktree-prune.txt && |
| 996 | git -c gc.worktreePruneExpire=now maintenance is-needed --auto --task=worktree-prune && |
| 997 | GIT_TRACE2_EVENT="$(pwd)/worktree-prune.txt" git -c gc.worktreePruneExpire=now maintenance run --auto --task=worktree-prune && |
| 998 | test_subcommand git worktree prune --expire now <worktree-prune.txt && |
| 999 | test_path_is_missing .git/worktrees/worktree |
| 1000 | ' |
| 1001 | |
| 1002 | test_expect_rerere_gc () { |
| 1003 | negate= |
| 1004 | if test "$1" = "!" |
| 1005 | then |
| 1006 | negate="!" |
| 1007 | shift |
| 1008 | fi |
| 1009 | |
| 1010 | # An automatic run passes --skip-locked to "git rerere gc". |
| 1011 | skip_locked= |
| 1012 | case " $* " in |
| 1013 | *" --auto "*) |
| 1014 | skip_locked=--skip-locked |
| 1015 | ;; |
| 1016 | esac |
| 1017 | |
| 1018 | rm -f "rerere-gc.txt" && |
| 1019 | GIT_TRACE2_EVENT="$(pwd)/rerere-gc.txt" "$@" && |
| 1020 | test_subcommand $negate git rerere gc $skip_locked <rerere-gc.txt |
| 1021 | } |
| 1022 | |
| 1023 | test_expect_success 'rerere-gc task without --auto always collects garbage' ' |
| 1024 | test_expect_rerere_gc git maintenance run --task=rerere-gc |
| 1025 | ' |
| 1026 | |
| 1027 | test_expect_success 'rerere-gc task with --auto only prunes with stale entries' ' |
| 1028 | test_when_finished "rm -rf .git/rr-cache" && |
| 1029 | entry_1=.git/rr-cache/171$(echo $ZERO_OID | cut -c4-) && |
| 1030 | entry_2=.git/rr-cache/172$(echo $ZERO_OID | cut -c4-) && |
| 1031 | entry_3=.git/rr-cache/173$(echo $ZERO_OID | cut -c4-) && |
| 1032 | |
| 1033 | # Without the "rr-cache" directory there is nothing to prune. |
| 1034 | ! git maintenance is-needed --auto --task=rerere-gc && |
| 1035 | test_expect_rerere_gc ! git maintenance run --auto --task=rerere-gc && |
| 1036 | |
| 1037 | # Fresh unresolved entries are not stale. |
| 1038 | for e in $entry_1 $entry_2 $entry_3 |
| 1039 | do |
| 1040 | mkdir -p $e && |
| 1041 | echo preimage >$e/preimage || return 1 |
| 1042 | done && |
| 1043 | ! git maintenance is-needed --auto --task=rerere-gc && |
| 1044 | test_expect_rerere_gc ! git maintenance run --auto --task=rerere-gc && |
| 1045 | |
| 1046 | # Entries are sampled using the "17" prefix, so we scale up the |
| 1047 | # estimate by 256. A single entry is not sufficient to reach the |
| 1048 | # default limit of 512. |
| 1049 | test-tool chmtime =-$((16 * 86400)) $entry_1/preimage && |
| 1050 | ! git maintenance is-needed --auto --task=rerere-gc && |
| 1051 | |
| 1052 | # A second prunable entry will reach the limit though and will thus get |
| 1053 | # pruned. |
| 1054 | test-tool chmtime =-$((16 * 86400)) $entry_2/preimage && |
| 1055 | git maintenance is-needed --auto --task=rerere-gc && |
| 1056 | |
| 1057 | # The prunable entries are gone, the other one remains. |
| 1058 | test_expect_rerere_gc git maintenance run --auto --task=rerere-gc && |
| 1059 | test_path_is_missing $entry_1 && |
| 1060 | test_path_is_missing $entry_2 && |
| 1061 | test_path_is_dir $entry_3 |
| 1062 | ' |
| 1063 | |
| 1064 | test_expect_success 'rerere-gc task with --auto honors maintenance.rerere-gc.auto' ' |
| 1065 | test_when_finished "rm -rf .git/rr-cache" && |
| 1066 | entry=.git/rr-cache/171$(echo $ZERO_OID | cut -c4-) && |
| 1067 | |
| 1068 | # A negative value should always prune. |
| 1069 | git -c maintenance.rerere-gc.auto=-1 maintenance is-needed --auto --task=rerere-gc && |
| 1070 | test_expect_rerere_gc git -c maintenance.rerere-gc.auto=-1 maintenance run --auto --task=rerere-gc && |
| 1071 | |
| 1072 | # A positive value prunes only when the estimated number of stale |
| 1073 | # entries is at least as big. A single sampled entry counts for 256 |
| 1074 | # estimated entries. |
| 1075 | mkdir -p $entry && |
| 1076 | echo preimage >$entry/preimage && |
| 1077 | test-tool chmtime =-$((16 * 86400)) $entry/preimage && |
| 1078 | |
| 1079 | ! git -c maintenance.rerere-gc.auto=257 maintenance is-needed --auto --task=rerere-gc && |
| 1080 | test_expect_rerere_gc ! git -c maintenance.rerere-gc.auto=257 maintenance run --auto --task=rerere-gc && |
| 1081 | test_path_is_dir $entry && |
| 1082 | |
| 1083 | git -c maintenance.rerere-gc.auto=256 maintenance is-needed --auto --task=rerere-gc && |
| 1084 | test_expect_rerere_gc git -c maintenance.rerere-gc.auto=256 maintenance run --auto --task=rerere-gc && |
| 1085 | test_path_is_missing $entry && |
| 1086 | |
| 1087 | # Zero should never prune. |
| 1088 | mkdir -p $entry && |
| 1089 | echo preimage >$entry/preimage && |
| 1090 | test-tool chmtime =-$((16 * 86400)) $entry/preimage && |
| 1091 | ! git -c maintenance.rerere-gc.auto=0 maintenance is-needed --auto --task=rerere-gc && |
| 1092 | test_expect_rerere_gc ! git -c maintenance.rerere-gc.auto=0 maintenance run --auto --task=rerere-gc |
| 1093 | ' |
| 1094 | |
| 1095 | test_expect_success 'rerere-gc task with --auto succeeds while MERGE_RR is locked' ' |
| 1096 | test_when_finished "rm -rf .git/rr-cache .git/MERGE_RR.lock" && |
| 1097 | # Without "rr-cache", rerere is disabled and never takes the lock. |
| 1098 | mkdir .git/rr-cache && |
| 1099 | >.git/MERGE_RR.lock && |
| 1100 | test_expect_rerere_gc git -c maintenance.rerere-gc.auto=-1 maintenance run --auto --task=rerere-gc |
| 1101 | ' |
| 1102 | |
| 1103 | test_expect_success 'rerere-gc task without --auto fails while MERGE_RR is locked' ' |
| 1104 | test_when_finished "rm -rf .git/rr-cache .git/MERGE_RR.lock" && |
| 1105 | mkdir .git/rr-cache && |
| 1106 | >.git/MERGE_RR.lock && |
| 1107 | test_must_fail git -c rerere.lockTimeout=0 maintenance run --task=rerere-gc 2>err && |
| 1108 | test_grep "Unable to create.*MERGE_RR\.lock" err |
| 1109 | ' |
| 1110 | |
| 1111 | test_expect_success '--auto and --schedule incompatible' ' |
| 1112 | test_must_fail git maintenance run --auto --schedule=daily 2>err && |
| 1113 | test_grep "cannot be used together" err |
| 1114 | ' |
| 1115 | |
| 1116 | test_expect_success '--task and --schedule incompatible' ' |
| 1117 | test_must_fail git maintenance run --task=pack-refs --schedule=daily 2>err && |
| 1118 | test_grep "cannot be used together" err |
| 1119 | ' |
| 1120 | |
| 1121 | test_expect_success 'invalid --schedule value' ' |
| 1122 | test_must_fail git maintenance run --schedule=annually 2>err && |
| 1123 | test_grep "unrecognized --schedule" err |
| 1124 | ' |
| 1125 | |
| 1126 | test_expect_success '--schedule inheritance weekly -> daily -> hourly' ' |
| 1127 | git config maintenance.loose-objects.enabled true && |
| 1128 | git config maintenance.loose-objects.schedule hourly && |
| 1129 | git config maintenance.commit-graph.enabled true && |
| 1130 | git config maintenance.commit-graph.schedule daily && |
| 1131 | git config maintenance.incremental-repack.enabled true && |
| 1132 | git config maintenance.incremental-repack.schedule weekly && |
| 1133 | |
| 1134 | GIT_TRACE2_EVENT="$(pwd)/hourly.txt" \ |
| 1135 | git maintenance run --schedule=hourly 2>/dev/null && |
| 1136 | test_maintenance_tasks hourly.txt <<-\EOF && |
| 1137 | prefetch |
| 1138 | loose-objects |
| 1139 | EOF |
| 1140 | |
| 1141 | GIT_TRACE2_EVENT="$(pwd)/daily.txt" \ |
| 1142 | git maintenance run --schedule=daily 2>/dev/null && |
| 1143 | test_maintenance_tasks daily.txt <<-\EOF && |
| 1144 | prefetch |
| 1145 | loose-objects |
| 1146 | commit-graph |
| 1147 | EOF |
| 1148 | |
| 1149 | GIT_TRACE2_EVENT="$(pwd)/weekly.txt" \ |
| 1150 | git maintenance run --schedule=weekly 2>/dev/null && |
| 1151 | test_maintenance_tasks weekly.txt <<-\EOF |
| 1152 | pack-refs foreground |
| 1153 | prefetch |
| 1154 | loose-objects |
| 1155 | incremental-repack |
| 1156 | commit-graph |
| 1157 | EOF |
| 1158 | ' |
| 1159 | |
| 1160 | test_expect_success 'maintenance.strategy inheritance' ' |
| 1161 | for task in commit-graph loose-objects incremental-repack |
| 1162 | do |
| 1163 | git config --unset maintenance.$task.schedule || return 1 |
| 1164 | done && |
| 1165 | |
| 1166 | test_when_finished git config --unset maintenance.strategy && |
| 1167 | git config maintenance.strategy incremental && |
| 1168 | |
| 1169 | GIT_TRACE2_EVENT="$(pwd)/incremental-hourly.txt" \ |
| 1170 | git maintenance run --schedule=hourly --quiet && |
| 1171 | GIT_TRACE2_EVENT="$(pwd)/incremental-daily.txt" \ |
| 1172 | git maintenance run --schedule=daily --quiet && |
| 1173 | GIT_TRACE2_EVENT="$(pwd)/incremental-weekly.txt" \ |
| 1174 | git maintenance run --schedule=weekly --quiet && |
| 1175 | |
| 1176 | test_maintenance_tasks incremental-hourly.txt <<-\EOF && |
| 1177 | prefetch |
| 1178 | commit-graph |
| 1179 | EOF |
| 1180 | |
| 1181 | test_maintenance_tasks incremental-daily.txt <<-\EOF && |
| 1182 | prefetch |
| 1183 | loose-objects |
| 1184 | incremental-repack |
| 1185 | commit-graph |
| 1186 | EOF |
| 1187 | |
| 1188 | test_maintenance_tasks incremental-weekly.txt <<-\EOF && |
| 1189 | pack-refs foreground |
| 1190 | prefetch |
| 1191 | loose-objects |
| 1192 | incremental-repack |
| 1193 | commit-graph |
| 1194 | EOF |
| 1195 | |
| 1196 | # Modify defaults |
| 1197 | git config maintenance.commit-graph.schedule daily && |
| 1198 | git config maintenance.loose-objects.schedule hourly && |
| 1199 | git config maintenance.incremental-repack.enabled false && |
| 1200 | |
| 1201 | GIT_TRACE2_EVENT="$(pwd)/modified-hourly.txt" \ |
| 1202 | git maintenance run --schedule=hourly --quiet && |
| 1203 | GIT_TRACE2_EVENT="$(pwd)/modified-daily.txt" \ |
| 1204 | git maintenance run --schedule=daily --quiet && |
| 1205 | |
| 1206 | test_maintenance_tasks modified-hourly.txt <<-\EOF && |
| 1207 | prefetch |
| 1208 | loose-objects |
| 1209 | EOF |
| 1210 | |
| 1211 | test_maintenance_tasks modified-daily.txt <<-\EOF |
| 1212 | prefetch |
| 1213 | loose-objects |
| 1214 | commit-graph |
| 1215 | EOF |
| 1216 | ' |
| 1217 | |
| 1218 | test_strategy () { |
| 1219 | STRATEGY="$1" |
| 1220 | shift |
| 1221 | |
| 1222 | rm -f trace2.txt && |
| 1223 | GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \ |
| 1224 | git -c maintenance.strategy=$STRATEGY maintenance run --quiet "$@" && |
| 1225 | test_maintenance_tasks trace2.txt |
| 1226 | } |
| 1227 | |
| 1228 | test_expect_success 'maintenance.strategy is respected' ' |
| 1229 | test_when_finished "rm -rf repo" && |
| 1230 | git init repo && |
| 1231 | ( |
| 1232 | cd repo && |
| 1233 | test_commit initial && |
| 1234 | |
| 1235 | test_must_fail git -c maintenance.strategy=unknown maintenance run 2>err && |
| 1236 | test_grep "unknown maintenance strategy: .unknown." err && |
| 1237 | |
| 1238 | test_strategy incremental <<-\EOF && |
| 1239 | gc foreground |
| 1240 | gc |
| 1241 | EOF |
| 1242 | |
| 1243 | test_strategy incremental --schedule=weekly <<-\EOF && |
| 1244 | pack-refs foreground |
| 1245 | prefetch |
| 1246 | loose-objects |
| 1247 | incremental-repack |
| 1248 | commit-graph |
| 1249 | EOF |
| 1250 | |
| 1251 | test_strategy gc <<-\EOF && |
| 1252 | gc foreground |
| 1253 | gc |
| 1254 | EOF |
| 1255 | |
| 1256 | test_strategy gc --schedule=weekly <<-\EOF && |
| 1257 | gc foreground |
| 1258 | gc |
| 1259 | EOF |
| 1260 | |
| 1261 | test_strategy geometric <<-\EOF && |
| 1262 | pack-refs foreground |
| 1263 | reflog-expire foreground |
| 1264 | geometric-repack |
| 1265 | commit-graph |
| 1266 | worktree-prune |
| 1267 | rerere-gc |
| 1268 | EOF |
| 1269 | |
| 1270 | test_strategy geometric --schedule=weekly <<-\EOF |
| 1271 | pack-refs foreground |
| 1272 | reflog-expire foreground |
| 1273 | geometric-repack |
| 1274 | commit-graph |
| 1275 | worktree-prune |
| 1276 | rerere-gc |
| 1277 | EOF |
| 1278 | ) |
| 1279 | ' |
| 1280 | |
| 1281 | test_expect_success 'register and unregister' ' |
| 1282 | test_when_finished git config --global --unset-all maintenance.repo && |
| 1283 | |
| 1284 | test_must_fail git maintenance unregister 2>err && |
| 1285 | test_grep "is not registered" err && |
| 1286 | git maintenance unregister --force && |
| 1287 | |
| 1288 | git config --global --add maintenance.repo /existing1 && |
| 1289 | git config --global --add maintenance.repo /existing2 && |
| 1290 | git config --global --get-all maintenance.repo >before && |
| 1291 | |
| 1292 | git maintenance register && |
| 1293 | test_cmp_config false maintenance.auto && |
| 1294 | git config --global --get-all maintenance.repo >between && |
| 1295 | cp before expect && |
| 1296 | pwd >>expect && |
| 1297 | test_cmp expect between && |
| 1298 | |
| 1299 | git maintenance unregister && |
| 1300 | git config --global --get-all maintenance.repo >actual && |
| 1301 | test_cmp before actual && |
| 1302 | |
| 1303 | git config --file ./other --add maintenance.repo /existing1 && |
| 1304 | git config --file ./other --add maintenance.repo /existing2 && |
| 1305 | git config --file ./other --get-all maintenance.repo >before && |
| 1306 | |
| 1307 | git maintenance register --config-file ./other && |
| 1308 | test_cmp_config false maintenance.auto && |
| 1309 | git config --file ./other --get-all maintenance.repo >between && |
| 1310 | cp before expect && |
| 1311 | pwd >>expect && |
| 1312 | test_cmp expect between && |
| 1313 | |
| 1314 | git maintenance unregister --config-file ./other && |
| 1315 | git config --file ./other --get-all maintenance.repo >actual && |
| 1316 | test_cmp before actual && |
| 1317 | |
| 1318 | test_must_fail git maintenance unregister 2>err && |
| 1319 | test_grep "is not registered" err && |
| 1320 | git maintenance unregister --force && |
| 1321 | |
| 1322 | test_must_fail git maintenance unregister --config-file ./other 2>err && |
| 1323 | test_grep "is not registered" err && |
| 1324 | git maintenance unregister --config-file ./other --force |
| 1325 | ' |
| 1326 | |
| 1327 | test_expect_success 'register with no value for maintenance.repo' ' |
| 1328 | cp .git/config .git/config.orig && |
| 1329 | test_when_finished mv .git/config.orig .git/config && |
| 1330 | |
| 1331 | cat >>.git/config <<-\EOF && |
| 1332 | [maintenance] |
| 1333 | repo |
| 1334 | EOF |
| 1335 | cat >expect <<-\EOF && |
| 1336 | error: missing value for '\''maintenance.repo'\'' |
| 1337 | EOF |
| 1338 | git maintenance register 2>actual && |
| 1339 | test_cmp expect actual && |
| 1340 | git config maintenance.repo |
| 1341 | ' |
| 1342 | |
| 1343 | test_expect_success 'unregister with no value for maintenance.repo' ' |
| 1344 | cp .git/config .git/config.orig && |
| 1345 | test_when_finished mv .git/config.orig .git/config && |
| 1346 | |
| 1347 | cat >>.git/config <<-\EOF && |
| 1348 | [maintenance] |
| 1349 | repo |
| 1350 | EOF |
| 1351 | cat >expect <<-\EOF && |
| 1352 | error: missing value for '\''maintenance.repo'\'' |
| 1353 | EOF |
| 1354 | test_expect_code 128 git maintenance unregister 2>actual.raw && |
| 1355 | grep ^error actual.raw >actual && |
| 1356 | test_cmp expect actual && |
| 1357 | git config maintenance.repo && |
| 1358 | |
| 1359 | git maintenance unregister --force 2>actual.raw && |
| 1360 | grep ^error actual.raw >actual && |
| 1361 | test_cmp expect actual && |
| 1362 | git config maintenance.repo |
| 1363 | ' |
| 1364 | |
| 1365 | test_expect_success !MINGW 'register and unregister with regex metacharacters' ' |
| 1366 | META="a+b*c" && |
| 1367 | git init "$META" && |
| 1368 | git -C "$META" maintenance register && |
| 1369 | git config --get-all --show-origin maintenance.repo && |
| 1370 | git config --get-all --global --fixed-value \ |
| 1371 | maintenance.repo "$(pwd)/$META" && |
| 1372 | git -C "$META" maintenance unregister && |
| 1373 | test_must_fail git config --get-all --global --fixed-value \ |
| 1374 | maintenance.repo "$(pwd)/$META" |
| 1375 | ' |
| 1376 | |
| 1377 | test_expect_success 'start without GIT_TEST_MAINT_SCHEDULER' ' |
| 1378 | test_when_finished "rm -rf systemctl.log script repo" && |
| 1379 | mkdir script && |
| 1380 | write_script script/systemctl <<-\EOF && |
| 1381 | echo "$*" >>../systemctl.log |
| 1382 | EOF |
| 1383 | git init repo && |
| 1384 | ( |
| 1385 | cd repo && |
| 1386 | sane_unset GIT_TEST_MAINT_SCHEDULER && |
| 1387 | PATH="$PWD/../script:$PATH" git maintenance start --scheduler=systemd |
| 1388 | ) && |
| 1389 | test_grep -- "--user list-timers" systemctl.log && |
| 1390 | test_grep -- "enable --now git-maintenance@" systemctl.log |
| 1391 | ' |
| 1392 | |
| 1393 | test_expect_success 'start --scheduler=<scheduler>' ' |
| 1394 | test_expect_code 129 git maintenance start --scheduler=foo 2>err && |
| 1395 | test_grep "unrecognized --scheduler argument" err && |
| 1396 | |
| 1397 | test_expect_code 129 git maintenance start --no-scheduler 2>err && |
| 1398 | test_grep "unknown option" err && |
| 1399 | |
| 1400 | test_expect_code 128 \ |
| 1401 | env GIT_TEST_MAINT_SCHEDULER="launchctl:true,schtasks:true" \ |
| 1402 | git maintenance start --scheduler=crontab 2>err && |
| 1403 | test_grep "fatal: crontab scheduler is not available" err |
| 1404 | ' |
| 1405 | |
| 1406 | test_expect_success 'start from empty cron table' ' |
| 1407 | GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start --scheduler=crontab && |
| 1408 | |
| 1409 | # start registers the repo |
| 1410 | git config --get --global --fixed-value maintenance.repo "$(pwd)" && |
| 1411 | |
| 1412 | test_grep "for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=daily" cron.txt && |
| 1413 | test_grep "for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=hourly" cron.txt && |
| 1414 | test_grep "for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=weekly" cron.txt |
| 1415 | ' |
| 1416 | |
| 1417 | test_expect_success 'stop from existing schedule' ' |
| 1418 | GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop && |
| 1419 | |
| 1420 | # stop does not unregister the repo |
| 1421 | git config --get --global --fixed-value maintenance.repo "$(pwd)" && |
| 1422 | |
| 1423 | # Operation is idempotent |
| 1424 | GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop && |
| 1425 | test_must_be_empty cron.txt |
| 1426 | ' |
| 1427 | |
| 1428 | test_expect_success 'start preserves existing schedule' ' |
| 1429 | echo "Important information!" >cron.txt && |
| 1430 | GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start --scheduler=crontab && |
| 1431 | test_grep "Important information!" cron.txt |
| 1432 | ' |
| 1433 | |
| 1434 | test_expect_success 'magic markers are correct' ' |
| 1435 | grep "GIT MAINTENANCE SCHEDULE" cron.txt >actual && |
| 1436 | cat >expect <<-\EOF && |
| 1437 | # BEGIN GIT MAINTENANCE SCHEDULE |
| 1438 | # END GIT MAINTENANCE SCHEDULE |
| 1439 | EOF |
| 1440 | test_cmp actual expect |
| 1441 | ' |
| 1442 | |
| 1443 | test_expect_success 'stop preserves surrounding schedule' ' |
| 1444 | echo "Crucial information!" >>cron.txt && |
| 1445 | GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop && |
| 1446 | test_grep "Important information!" cron.txt && |
| 1447 | test_grep "Crucial information!" cron.txt |
| 1448 | ' |
| 1449 | |
| 1450 | test_expect_success 'start and stop macOS maintenance' ' |
| 1451 | # ensure $HOME can be compared against hook arguments on all platforms |
| 1452 | pfx=$(cd "$HOME" && pwd) && |
| 1453 | |
| 1454 | write_script print-args <<-\EOF && |
| 1455 | echo $* | sed "s:gui/[0-9][0-9]*:gui/[UID]:" >>args |
| 1456 | EOF |
| 1457 | |
| 1458 | rm -f args && |
| 1459 | GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl && |
| 1460 | |
| 1461 | # start registers the repo |
| 1462 | git config --get --global --fixed-value maintenance.repo "$(pwd)" && |
| 1463 | |
| 1464 | ls "$HOME/Library/LaunchAgents" >actual && |
| 1465 | cat >expect <<-\EOF && |
| 1466 | org.git-scm.git.daily.plist |
| 1467 | org.git-scm.git.hourly.plist |
| 1468 | org.git-scm.git.weekly.plist |
| 1469 | EOF |
| 1470 | test_cmp expect actual && |
| 1471 | |
| 1472 | rm -f expect && |
| 1473 | for frequency in hourly daily weekly |
| 1474 | do |
| 1475 | PLIST="$pfx/Library/LaunchAgents/org.git-scm.git.$frequency.plist" && |
| 1476 | test_xmllint "$PLIST" && |
| 1477 | test_grep schedule=$frequency "$PLIST" && |
| 1478 | echo "bootout gui/[UID] $PLIST" >>expect && |
| 1479 | echo "bootstrap gui/[UID] $PLIST" >>expect || return 1 |
| 1480 | done && |
| 1481 | test_cmp expect args && |
| 1482 | |
| 1483 | rm -f args && |
| 1484 | GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance stop && |
| 1485 | |
| 1486 | # stop does not unregister the repo |
| 1487 | git config --get --global --fixed-value maintenance.repo "$(pwd)" && |
| 1488 | |
| 1489 | printf "bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \ |
| 1490 | hourly daily weekly >expect && |
| 1491 | test_cmp expect args && |
| 1492 | ls "$HOME/Library/LaunchAgents" >actual && |
| 1493 | test_line_count = 0 actual |
| 1494 | ' |
| 1495 | |
| 1496 | test_expect_success 'use launchctl list to prevent extra work' ' |
| 1497 | # ensure we are registered |
| 1498 | GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl && |
| 1499 | |
| 1500 | # do it again on a fresh args file |
| 1501 | rm -f args && |
| 1502 | GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl && |
| 1503 | |
| 1504 | ls "$HOME/Library/LaunchAgents" >actual && |
| 1505 | cat >expect <<-\EOF && |
| 1506 | list org.git-scm.git.hourly |
| 1507 | list org.git-scm.git.daily |
| 1508 | list org.git-scm.git.weekly |
| 1509 | EOF |
| 1510 | test_cmp expect args |
| 1511 | ' |
| 1512 | |
| 1513 | test_expect_success 'start and stop Windows maintenance' ' |
| 1514 | write_script print-args <<-\EOF && |
| 1515 | echo $* >>args |
| 1516 | while test $# -gt 0 |
| 1517 | do |
| 1518 | case "$1" in |
| 1519 | /xml) shift; xmlfile=$1; break ;; |
| 1520 | *) shift ;; |
| 1521 | esac |
| 1522 | done |
| 1523 | test -z "$xmlfile" || cp "$xmlfile" "$xmlfile.xml" |
| 1524 | EOF |
| 1525 | |
| 1526 | rm -f args && |
| 1527 | GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance start --scheduler=schtasks && |
| 1528 | |
| 1529 | # start registers the repo |
| 1530 | git config --get --global --fixed-value maintenance.repo "$(pwd)" && |
| 1531 | |
| 1532 | for frequency in hourly daily weekly |
| 1533 | do |
| 1534 | test_grep "/create /tn Git Maintenance ($frequency) /f /xml" args && |
| 1535 | file=$(ls .git/schedule_${frequency}*.xml) && |
| 1536 | test_xmllint "$file" || return 1 |
| 1537 | done && |
| 1538 | |
| 1539 | rm -f args && |
| 1540 | GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance stop && |
| 1541 | |
| 1542 | # stop does not unregister the repo |
| 1543 | git config --get --global --fixed-value maintenance.repo "$(pwd)" && |
| 1544 | |
| 1545 | printf "/delete /tn Git Maintenance (%s) /f\n" \ |
| 1546 | hourly daily weekly >expect && |
| 1547 | test_cmp expect args |
| 1548 | ' |
| 1549 | |
| 1550 | test_expect_success 'start and stop Linux/systemd maintenance' ' |
| 1551 | write_script print-args <<-\EOF && |
| 1552 | printf "%s\n" "$*" >>args |
| 1553 | EOF |
| 1554 | |
| 1555 | XDG_CONFIG_HOME="$PWD" && |
| 1556 | export XDG_CONFIG_HOME && |
| 1557 | rm -f args && |
| 1558 | GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args" git maintenance start --scheduler=systemd-timer && |
| 1559 | |
| 1560 | # start registers the repo |
| 1561 | git config --get --global --fixed-value maintenance.repo "$(pwd)" && |
| 1562 | |
| 1563 | for schedule in hourly daily weekly |
| 1564 | do |
| 1565 | test_path_is_file "systemd/user/git-maintenance@$schedule.timer" || return 1 |
| 1566 | done && |
| 1567 | test_path_is_file "systemd/user/git-maintenance@.service" && |
| 1568 | |
| 1569 | test_systemd_analyze_verify "systemd/user/git-maintenance@hourly.service" && |
| 1570 | test_systemd_analyze_verify "systemd/user/git-maintenance@daily.service" && |
| 1571 | test_systemd_analyze_verify "systemd/user/git-maintenance@weekly.service" && |
| 1572 | |
| 1573 | test_grep "core.askPass=true" "systemd/user/git-maintenance@.service" && |
| 1574 | test_grep "credential.interactive=false" "systemd/user/git-maintenance@.service" && |
| 1575 | |
| 1576 | printf -- "--user enable --now git-maintenance@%s.timer\n" hourly daily weekly >expect && |
| 1577 | test_cmp expect args && |
| 1578 | |
| 1579 | rm -f args && |
| 1580 | GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args" git maintenance stop && |
| 1581 | |
| 1582 | # stop does not unregister the repo |
| 1583 | git config --get --global --fixed-value maintenance.repo "$(pwd)" && |
| 1584 | |
| 1585 | for schedule in hourly daily weekly |
| 1586 | do |
| 1587 | test_path_is_missing "systemd/user/git-maintenance@$schedule.timer" || return 1 |
| 1588 | done && |
| 1589 | test_path_is_missing "systemd/user/git-maintenance@.service" && |
| 1590 | |
| 1591 | printf -- "--user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect && |
| 1592 | test_cmp expect args |
| 1593 | ' |
| 1594 | |
| 1595 | test_expect_success 'start and stop when several schedulers are available' ' |
| 1596 | write_script print-args <<-\EOF && |
| 1597 | printf "%s\n" "$*" | sed "s:gui/[0-9][0-9]*:gui/[UID]:; s:\(schtasks /create .* /xml\).*:\1:;" >>args |
| 1598 | EOF |
| 1599 | |
| 1600 | rm -f args && |
| 1601 | GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=systemd-timer && |
| 1602 | printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \ |
| 1603 | hourly daily weekly >expect && |
| 1604 | printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \ |
| 1605 | hourly daily weekly >>expect && |
| 1606 | printf -- "systemctl --user enable --now git-maintenance@%s.timer\n" hourly daily weekly >>expect && |
| 1607 | test_cmp expect args && |
| 1608 | |
| 1609 | rm -f args && |
| 1610 | GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=launchctl && |
| 1611 | printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect && |
| 1612 | printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \ |
| 1613 | hourly daily weekly >>expect && |
| 1614 | for frequency in hourly daily weekly |
| 1615 | do |
| 1616 | PLIST="$pfx/Library/LaunchAgents/org.git-scm.git.$frequency.plist" && |
| 1617 | echo "launchctl bootout gui/[UID] $PLIST" >>expect && |
| 1618 | echo "launchctl bootstrap gui/[UID] $PLIST" >>expect || return 1 |
| 1619 | done && |
| 1620 | test_cmp expect args && |
| 1621 | |
| 1622 | rm -f args && |
| 1623 | GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=schtasks && |
| 1624 | printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect && |
| 1625 | printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \ |
| 1626 | hourly daily weekly >>expect && |
| 1627 | printf "schtasks /create /tn Git Maintenance (%s) /f /xml\n" \ |
| 1628 | hourly daily weekly >>expect && |
| 1629 | test_cmp expect args && |
| 1630 | |
| 1631 | rm -f args && |
| 1632 | GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance stop && |
| 1633 | printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect && |
| 1634 | printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \ |
| 1635 | hourly daily weekly >>expect && |
| 1636 | printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \ |
| 1637 | hourly daily weekly >>expect && |
| 1638 | test_cmp expect args |
| 1639 | ' |
| 1640 | |
| 1641 | test_expect_success 'register preserves existing strategy' ' |
| 1642 | git config maintenance.strategy none && |
| 1643 | git maintenance register && |
| 1644 | test_config maintenance.strategy none && |
| 1645 | git config --unset maintenance.strategy && |
| 1646 | git maintenance register && |
| 1647 | test_config maintenance.strategy incremental |
| 1648 | ' |
| 1649 | |
| 1650 | test_expect_success 'fails when running outside of a repository' ' |
| 1651 | nongit test_must_fail git maintenance run && |
| 1652 | nongit test_must_fail git maintenance stop && |
| 1653 | nongit test_must_fail git maintenance start && |
| 1654 | nongit test_must_fail git maintenance register && |
| 1655 | nongit test_must_fail git maintenance unregister |
| 1656 | ' |
| 1657 | |
| 1658 | test_expect_success 'fails when configured to use an invalid strategy' ' |
| 1659 | test_must_fail git -c maintenance.strategy=invalid maintenance run --schedule=hourly 2>err && |
| 1660 | test_grep "unknown maintenance strategy: .invalid." err |
| 1661 | ' |
| 1662 | |
| 1663 | test_expect_success 'register and unregister bare repo' ' |
| 1664 | test_when_finished "git config --global --unset-all maintenance.repo || :" && |
| 1665 | test_might_fail git config --global --unset-all maintenance.repo && |
| 1666 | git init --bare barerepo && |
| 1667 | ( |
| 1668 | cd barerepo && |
| 1669 | git maintenance register && |
| 1670 | git config --get --global --fixed-value maintenance.repo "$(pwd)" && |
| 1671 | git maintenance unregister && |
| 1672 | test_must_fail git config --global --get-all maintenance.repo |
| 1673 | ) |
| 1674 | ' |
| 1675 | |
| 1676 | test_expect_success 'failed schedule prevents config change' ' |
| 1677 | git init --bare failcase && |
| 1678 | |
| 1679 | for scheduler in crontab launchctl schtasks systemctl |
| 1680 | do |
| 1681 | GIT_TEST_MAINT_SCHEDULER="$scheduler:false" && |
| 1682 | export GIT_TEST_MAINT_SCHEDULER && |
| 1683 | test_must_fail \ |
| 1684 | git -C failcase maintenance start && |
| 1685 | test_must_fail git -C failcase config maintenance.auto || return 1 |
| 1686 | done |
| 1687 | ' |
| 1688 | |
| 1689 | test_expect_success '--no-detach causes maintenance to not run in background' ' |
| 1690 | test_when_finished "rm -rf repo" && |
| 1691 | git init repo && |
| 1692 | ( |
| 1693 | cd repo && |
| 1694 | |
| 1695 | # Prepare the repository such that git-maintenance(1) ends up |
| 1696 | # outputting something. |
| 1697 | test_commit something && |
| 1698 | git config set maintenance.gc.enabled false && |
| 1699 | git config set maintenance.loose-objects.enabled true && |
| 1700 | git config set maintenance.loose-objects.auto 1 && |
| 1701 | git config set maintenance.incremental-repack.enabled true && |
| 1702 | |
| 1703 | GIT_TRACE2_EVENT="$(pwd)/trace.txt" \ |
| 1704 | git maintenance run --no-detach >out 2>&1 && |
| 1705 | ! test_region maintenance detach trace.txt |
| 1706 | ) |
| 1707 | ' |
| 1708 | |
| 1709 | test_expect_success PIPE '--detach holds maintenance lock until daemonized child exits' ' |
| 1710 | test_when_finished "rm -rf repo" && |
| 1711 | git init repo && |
| 1712 | ( |
| 1713 | cd repo && |
| 1714 | |
| 1715 | git config maintenance.auto false && |
| 1716 | git config core.lockfilepid true && |
| 1717 | |
| 1718 | git remote add origin /does/not/exist && |
| 1719 | git config set remote.origin.uploadpack "cat fifo-uploadpack" && |
| 1720 | |
| 1721 | mkfifo fifo-uploadpack fifo-maint && |
| 1722 | |
| 1723 | # Open the maintenance FIFO, as otherwise spawning |
| 1724 | # git-maintenance(1) would block. Note that we need to open it |
| 1725 | # as read-write, as otherwise we would block here already. |
| 1726 | exec 9<>fifo-maint && |
| 1727 | |
| 1728 | { git maintenance run --task=prefetch --detach 7>&9 & } && |
| 1729 | parent="$!" && |
| 1730 | |
| 1731 | # Reap the parent process so that the exec call below will not |
| 1732 | # get SIGCHLD. |
| 1733 | wait "$parent" && |
| 1734 | |
| 1735 | # Open the git-upload-pack(1) FIFO for writing, which will |
| 1736 | # block until the upload-pack script opens it for reading. Once |
| 1737 | # exec returns, we know that the daemonized child is alive and |
| 1738 | # pinned. |
| 1739 | exec 8>fifo-uploadpack && |
| 1740 | |
| 1741 | test_path_is_file .git/objects/maintenance.lock && |
| 1742 | test_path_is_file .git/objects/"maintenance~pid.lock" && |
| 1743 | |
| 1744 | # Verify that the maintenance.lock still exists, and |
| 1745 | # that it was created by the parent process, not the |
| 1746 | # child. |
| 1747 | echo "pid $parent" >expect && |
| 1748 | test_cmp expect .git/objects/"maintenance~pid.lock" && |
| 1749 | |
| 1750 | # Reopen the maintenance FIFO as read-only so that |
| 1751 | # git-maintenance(1) is the only writer. This will cause it to |
| 1752 | # close the FIFO once the process exits. |
| 1753 | exec 9<&- && |
| 1754 | exec 9<fifo-maint && |
| 1755 | |
| 1756 | # Close the FIFO used by git-upload-pack(1) to unblock it and |
| 1757 | # then wait until the maintenance FIFO is closed by |
| 1758 | # git-maintenance(1), indicating that it has exited. |
| 1759 | exec 8>&- && |
| 1760 | cat <&9 && |
| 1761 | |
| 1762 | test_path_is_missing .git/objects/maintenance.lock && |
| 1763 | test_path_is_missing .git/objects/"maintenance~pid.lock" |
| 1764 | ) |
| 1765 | ' |
| 1766 | |
| 1767 | test_expect_success '--detach causes maintenance to run in background' ' |
| 1768 | test_when_finished "rm -rf repo" && |
| 1769 | git init repo && |
| 1770 | ( |
| 1771 | cd repo && |
| 1772 | |
| 1773 | test_commit something && |
| 1774 | git config set maintenance.gc.enabled false && |
| 1775 | git config set maintenance.loose-objects.enabled true && |
| 1776 | git config set maintenance.loose-objects.auto 1 && |
| 1777 | git config set maintenance.incremental-repack.enabled true && |
| 1778 | |
| 1779 | # The extra file descriptor gets inherited to the child |
| 1780 | # process, and by reading stdout we thus essentially wait for |
| 1781 | # that descriptor to get closed, which indicates that the child |
| 1782 | # is done, too. |
| 1783 | does_not_matter=$(GIT_TRACE2_EVENT="$(pwd)/trace.txt" \ |
| 1784 | git maintenance run --detach 9>&1) && |
| 1785 | test_region maintenance detach trace.txt |
| 1786 | ) |
| 1787 | ' |
| 1788 | |
| 1789 | test_expect_success 'repacking loose objects is quiet' ' |
| 1790 | test_when_finished "rm -rf repo" && |
| 1791 | git init repo && |
| 1792 | ( |
| 1793 | cd repo && |
| 1794 | |
| 1795 | test_commit something && |
| 1796 | git config set maintenance.gc.enabled false && |
| 1797 | git config set maintenance.loose-objects.enabled true && |
| 1798 | git config set maintenance.loose-objects.auto 1 && |
| 1799 | |
| 1800 | git maintenance run --quiet >out 2>&1 && |
| 1801 | test_must_be_empty out |
| 1802 | ) |
| 1803 | ' |
| 1804 | |
| 1805 | test_expect_success 'maintenance aborts with existing lock file' ' |
| 1806 | test_when_finished "rm -rf repo script" && |
| 1807 | mkdir script && |
| 1808 | write_script script/systemctl <<-\EOF && |
| 1809 | true |
| 1810 | EOF |
| 1811 | |
| 1812 | git init repo && |
| 1813 | : >repo/.git/objects/schedule.lock && |
| 1814 | test_must_fail env PATH="$PWD/script:$PATH" git -C repo maintenance start --scheduler=systemd 2>err && |
| 1815 | test_grep "Another scheduled git-maintenance(1) process seems to be running" err |
| 1816 | ' |
| 1817 | |
| 1818 | test_done |