Ben Peart | 1956ecd | 2019-02-15 12:59:21 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='post index change hook' |
| 4 | |
Johannes Schindelin | 01dc813 | 2020-11-18 23:44:39 +0000 | [diff] [blame] | 5 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
Johannes Schindelin | 334afbc | 2020-11-18 23:44:19 +0000 | [diff] [blame] | 6 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 7 | |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 8 | TEST_PASSES_SANITIZE_LEAK=true |
Ben Peart | 1956ecd | 2019-02-15 12:59:21 -0500 | [diff] [blame] | 9 | . ./test-lib.sh |
| 10 | |
| 11 | test_expect_success 'setup' ' |
| 12 | mkdir -p dir1 && |
| 13 | touch dir1/file1.txt && |
| 14 | echo testing >dir1/file2.txt && |
| 15 | git add . && |
| 16 | git commit -m "initial" |
| 17 | ' |
| 18 | |
| 19 | test_expect_success 'test status, add, commit, others trigger hook without flags set' ' |
Ævar Arnfjörð Bjarmason | bef805b | 2022-03-17 11:13:14 +0100 | [diff] [blame] | 20 | test_hook post-index-change <<-\EOF && |
Ben Peart | 1956ecd | 2019-02-15 12:59:21 -0500 | [diff] [blame] | 21 | if test "$1" -eq 1; then |
| 22 | echo "Invalid combination of flags passed to hook; updated_workdir is set." >testfailure |
| 23 | exit 1 |
| 24 | fi |
| 25 | if test "$2" -eq 1; then |
| 26 | echo "Invalid combination of flags passed to hook; updated_skipworktree is set." >testfailure |
| 27 | exit 1 |
| 28 | fi |
| 29 | if test -f ".git/index.lock"; then |
| 30 | echo ".git/index.lock exists" >testfailure |
| 31 | exit 3 |
| 32 | fi |
| 33 | if ! test -f ".git/index"; then |
| 34 | echo ".git/index does not exist" >testfailure |
| 35 | exit 3 |
| 36 | fi |
| 37 | echo "success" >testsuccess |
| 38 | EOF |
| 39 | mkdir -p dir2 && |
| 40 | touch dir2/file1.txt && |
| 41 | touch dir2/file2.txt && |
| 42 | : force index to be dirty && |
| 43 | test-tool chmtime +60 dir1/file1.txt && |
| 44 | git status && |
| 45 | test_path_is_file testsuccess && rm -f testsuccess && |
| 46 | test_path_is_missing testfailure && |
| 47 | git add . && |
| 48 | test_path_is_file testsuccess && rm -f testsuccess && |
| 49 | test_path_is_missing testfailure && |
| 50 | git commit -m "second" && |
| 51 | test_path_is_file testsuccess && rm -f testsuccess && |
| 52 | test_path_is_missing testfailure && |
| 53 | git checkout -- dir1/file1.txt && |
| 54 | test_path_is_file testsuccess && rm -f testsuccess && |
| 55 | test_path_is_missing testfailure && |
| 56 | git update-index && |
| 57 | test_path_is_missing testsuccess && |
| 58 | test_path_is_missing testfailure && |
| 59 | git reset --soft && |
| 60 | test_path_is_missing testsuccess && |
| 61 | test_path_is_missing testfailure |
| 62 | ' |
| 63 | |
| 64 | test_expect_success 'test checkout and reset trigger the hook' ' |
Ævar Arnfjörð Bjarmason | bef805b | 2022-03-17 11:13:14 +0100 | [diff] [blame] | 65 | test_hook post-index-change <<-\EOF && |
Ben Peart | 1956ecd | 2019-02-15 12:59:21 -0500 | [diff] [blame] | 66 | if test "$1" -eq 1 && test "$2" -eq 1; then |
| 67 | echo "Invalid combination of flags passed to hook; updated_workdir and updated_skipworktree are both set." >testfailure |
| 68 | exit 1 |
| 69 | fi |
| 70 | if test "$1" -eq 0 && test "$2" -eq 0; then |
| 71 | echo "Invalid combination of flags passed to hook; neither updated_workdir or updated_skipworktree are set." >testfailure |
| 72 | exit 2 |
| 73 | fi |
| 74 | if test "$1" -eq 1; then |
| 75 | if test -f ".git/index.lock"; then |
| 76 | echo "updated_workdir set but .git/index.lock exists" >testfailure |
| 77 | exit 3 |
| 78 | fi |
| 79 | if ! test -f ".git/index"; then |
| 80 | echo "updated_workdir set but .git/index does not exist" >testfailure |
| 81 | exit 3 |
| 82 | fi |
| 83 | else |
| 84 | echo "update_workdir should be set for checkout" >testfailure |
| 85 | exit 4 |
| 86 | fi |
| 87 | echo "success" >testsuccess |
| 88 | EOF |
| 89 | : force index to be dirty && |
| 90 | test-tool chmtime +60 dir1/file1.txt && |
Johannes Schindelin | 01dc813 | 2020-11-18 23:44:39 +0000 | [diff] [blame] | 91 | git checkout main && |
Ben Peart | 1956ecd | 2019-02-15 12:59:21 -0500 | [diff] [blame] | 92 | test_path_is_file testsuccess && rm -f testsuccess && |
| 93 | test_path_is_missing testfailure && |
| 94 | test-tool chmtime +60 dir1/file1.txt && |
| 95 | git checkout HEAD && |
| 96 | test_path_is_file testsuccess && rm -f testsuccess && |
| 97 | test_path_is_missing testfailure && |
| 98 | test-tool chmtime +60 dir1/file1.txt && |
| 99 | git reset --hard && |
| 100 | test_path_is_file testsuccess && rm -f testsuccess && |
| 101 | test_path_is_missing testfailure && |
| 102 | git checkout -B test && |
| 103 | test_path_is_file testsuccess && rm -f testsuccess && |
| 104 | test_path_is_missing testfailure |
| 105 | ' |
| 106 | |
| 107 | test_expect_success 'test reset --mixed and update-index triggers the hook' ' |
Ævar Arnfjörð Bjarmason | bef805b | 2022-03-17 11:13:14 +0100 | [diff] [blame] | 108 | test_hook post-index-change <<-\EOF && |
Ben Peart | 1956ecd | 2019-02-15 12:59:21 -0500 | [diff] [blame] | 109 | if test "$1" -eq 1 && test "$2" -eq 1; then |
| 110 | echo "Invalid combination of flags passed to hook; updated_workdir and updated_skipworktree are both set." >testfailure |
| 111 | exit 1 |
| 112 | fi |
| 113 | if test "$1" -eq 0 && test "$2" -eq 0; then |
| 114 | echo "Invalid combination of flags passed to hook; neither updated_workdir or updated_skipworktree are set." >testfailure |
| 115 | exit 2 |
| 116 | fi |
| 117 | if test "$2" -eq 1; then |
| 118 | if test -f ".git/index.lock"; then |
| 119 | echo "updated_skipworktree set but .git/index.lock exists" >testfailure |
| 120 | exit 3 |
| 121 | fi |
| 122 | if ! test -f ".git/index"; then |
| 123 | echo "updated_skipworktree set but .git/index does not exist" >testfailure |
| 124 | exit 3 |
| 125 | fi |
| 126 | else |
| 127 | echo "updated_skipworktree should be set for reset --mixed and update-index" >testfailure |
| 128 | exit 4 |
| 129 | fi |
| 130 | echo "success" >testsuccess |
| 131 | EOF |
| 132 | : force index to be dirty && |
| 133 | test-tool chmtime +60 dir1/file1.txt && |
| 134 | git reset --mixed --quiet HEAD~1 && |
| 135 | test_path_is_file testsuccess && rm -f testsuccess && |
| 136 | test_path_is_missing testfailure && |
| 137 | git hash-object -w --stdin <dir1/file2.txt >expect && |
| 138 | git update-index --cacheinfo 100644 "$(cat expect)" dir1/file1.txt && |
| 139 | test_path_is_file testsuccess && rm -f testsuccess && |
| 140 | test_path_is_missing testfailure && |
| 141 | git update-index --skip-worktree dir1/file2.txt && |
| 142 | git update-index --remove dir1/file2.txt && |
| 143 | test_path_is_file testsuccess && rm -f testsuccess && |
| 144 | test_path_is_missing testfailure |
| 145 | ' |
| 146 | |
| 147 | test_done |