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 mkdir -p .git/hooks &&
21 write_script .git/hooks/post-index-change <<-\EOF &&
22 if test "$1" -eq 1; then
23 echo "Invalid combination of flags passed to hook; updated_workdir is set." >testfailure
26 if test "$2" -eq 1; then
27 echo "Invalid combination of flags passed to hook; updated_skipworktree is set." >testfailure
30 if test -f ".git/index.lock"; then
31 echo ".git/index.lock exists" >testfailure
34 if ! test -f ".git/index"; then
35 echo ".git/index does not exist" >testfailure
38 echo "success" >testsuccess
41 touch dir2/file1.txt &&
42 touch dir2/file2.txt &&
43 : force index to be dirty &&
44 test-tool chmtime +60 dir1/file1.txt &&
46 test_path_is_file testsuccess && rm -f testsuccess &&
47 test_path_is_missing testfailure &&
49 test_path_is_file testsuccess && rm -f testsuccess &&
50 test_path_is_missing testfailure &&
51 git commit -m "second" &&
52 test_path_is_file testsuccess && rm -f testsuccess &&
53 test_path_is_missing testfailure &&
54 git checkout -- dir1/file1.txt &&
55 test_path_is_file testsuccess && rm -f testsuccess &&
56 test_path_is_missing testfailure &&
58 test_path_is_missing testsuccess &&
59 test_path_is_missing testfailure &&
61 test_path_is_missing testsuccess &&
62 test_path_is_missing testfailure
65 test_expect_success
'test checkout and reset trigger the hook' '
66 write_script .git/hooks/post-index-change <<-\EOF &&
67 if test "$1" -eq 1 && test "$2" -eq 1; then
68 echo "Invalid combination of flags passed to hook; updated_workdir and updated_skipworktree are both set." >testfailure
71 if test "$1" -eq 0 && test "$2" -eq 0; then
72 echo "Invalid combination of flags passed to hook; neither updated_workdir or updated_skipworktree are set." >testfailure
75 if test "$1" -eq 1; then
76 if test -f ".git/index.lock"; then
77 echo "updated_workdir set but .git/index.lock exists" >testfailure
80 if ! test -f ".git/index"; then
81 echo "updated_workdir set but .git/index does not exist" >testfailure
85 echo "update_workdir should be set for checkout" >testfailure
88 echo "success" >testsuccess
90 : force index to be dirty &&
91 test-tool chmtime +60 dir1/file1.txt &&
93 test_path_is_file testsuccess && rm -f testsuccess &&
94 test_path_is_missing testfailure &&
95 test-tool chmtime +60 dir1/file1.txt &&
97 test_path_is_file testsuccess && rm -f testsuccess &&
98 test_path_is_missing testfailure &&
99 test-tool chmtime +60 dir1/file1.txt &&
101 test_path_is_file testsuccess && rm -f testsuccess &&
102 test_path_is_missing testfailure &&
103 git checkout -B test &&
104 test_path_is_file testsuccess && rm -f testsuccess &&
105 test_path_is_missing testfailure
108 test_expect_success
'test reset --mixed and update-index triggers the hook' '
109 write_script .git/hooks/post-index-change <<-\EOF &&
110 if test "$1" -eq 1 && test "$2" -eq 1; then
111 echo "Invalid combination of flags passed to hook; updated_workdir and updated_skipworktree are both set." >testfailure
114 if test "$1" -eq 0 && test "$2" -eq 0; then
115 echo "Invalid combination of flags passed to hook; neither updated_workdir or updated_skipworktree are set." >testfailure
118 if test "$2" -eq 1; then
119 if test -f ".git/index.lock"; then
120 echo "updated_skipworktree set but .git/index.lock exists" >testfailure
123 if ! test -f ".git/index"; then
124 echo "updated_skipworktree set but .git/index does not exist" >testfailure
128 echo "updated_skipworktree should be set for reset --mixed and update-index" >testfailure
131 echo "success" >testsuccess
133 : force index to be dirty &&
134 test-tool chmtime +60 dir1/file1.txt &&
135 git reset --mixed --quiet HEAD~1 &&
136 test_path_is_file testsuccess && rm -f testsuccess &&
137 test_path_is_missing testfailure &&
138 git hash-object -w --stdin <dir1/file2.txt >expect &&
139 git update-index --cacheinfo 100644 "$(cat expect)" dir1/file1.txt &&
140 test_path_is_file testsuccess && rm -f testsuccess &&
141 test_path_is_missing testfailure &&
142 git update-index --skip-worktree dir1/file2.txt &&
143 git update-index --remove dir1/file2.txt &&
144 test_path_is_file testsuccess && rm -f testsuccess &&
145 test_path_is_missing testfailure