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