3 test_description
='post index change hook'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 TEST_PASSES_SANITIZE_LEAK
=true
11 test_expect_success
'setup' '
13 touch dir1/file1.txt &&
14 echo testing >dir1/file2.txt &&
16 git commit -m "initial"
19 test_expect_success
'test status, add, commit, others trigger hook without flags set' '
20 test_hook post-index-change <<-\EOF &&
21 if test "$1" -eq 1; then
22 echo "Invalid combination of flags passed to hook; updated_workdir is set." >testfailure
25 if test "$2" -eq 1; then
26 echo "Invalid combination of flags passed to hook; updated_skipworktree is set." >testfailure
29 if test -f ".git/index.lock"; then
30 echo ".git/index.lock exists" >testfailure
33 if ! test -f ".git/index"; then
34 echo ".git/index does not exist" >testfailure
37 echo "success" >testsuccess
40 touch dir2/file1.txt &&
41 touch dir2/file2.txt &&
42 : force index to be dirty &&
43 test-tool chmtime +60 dir1/file1.txt &&
45 test_path_is_file testsuccess && rm -f testsuccess &&
46 test_path_is_missing testfailure &&
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 &&
57 test_path_is_missing testsuccess &&
58 test_path_is_missing testfailure &&
60 test_path_is_missing testsuccess &&
61 test_path_is_missing testfailure
64 test_expect_success
'test checkout and reset trigger the hook' '
65 test_hook post-index-change <<-\EOF &&
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
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
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
79 if ! test -f ".git/index"; then
80 echo "updated_workdir set but .git/index does not exist" >testfailure
84 echo "update_workdir should be set for checkout" >testfailure
87 echo "success" >testsuccess
89 : force index to be dirty &&
90 test-tool chmtime +60 dir1/file1.txt &&
92 test_path_is_file testsuccess && rm -f testsuccess &&
93 test_path_is_missing testfailure &&
94 test-tool chmtime +60 dir1/file1.txt &&
96 test_path_is_file testsuccess && rm -f testsuccess &&
97 test_path_is_missing testfailure &&
98 test-tool chmtime +60 dir1/file1.txt &&
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
107 test_expect_success
'test reset --mixed and update-index triggers the hook' '
108 test_hook post-index-change <<-\EOF &&
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
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
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
122 if ! test -f ".git/index"; then
123 echo "updated_skipworktree set but .git/index does not exist" >testfailure
127 echo "updated_skipworktree should be set for reset --mixed and update-index" >testfailure
130 echo "success" >testsuccess
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