Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/t/t7900-maintenance.sh # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / t / t7900-maintenance.sh` 1818 lines ยท 60038 bytes Raw Blame History 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 " 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 $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..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=' ' 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 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 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 .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 /dev/null && 403 test_subcommand ! git prune-packed --quiet /dev/null && 411 test_subcommand git prune-packed --quiet /dev/null && 416 test_subcommand git prune-packed --quiet 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 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` /dev/null && 545 test_subcommand ! git multi-pack-index write --no-progress /dev/null && 555 test_subcommand ! git multi-pack-index write --no-progress /dev/null && 567 test_subcommand git multi-pack-index write --no-progress /dev/null && 599 test_subcommand git repack -d -l -q --geometric=2 \ 600 --write-midx 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 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 /dev/null && 687 test_subcommand git repack -d -l -q --cruft --cruft-expiration=2.weeks.ago \ 688 --write-midx 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 >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 .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 $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=' ' 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&- && 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