Merge branch 'rs/t4202-invert-grep-test-fix'
[git/debian.git] / t / t7113-post-index-change-hook.sh
bloba21781d68a1abf91bf2d34d029a1c90732991f2b
1 #!/bin/sh
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
9 . ./test-lib.sh
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"
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
24 exit 1
26 if test "$2" -eq 1; then
27 echo "Invalid combination of flags passed to hook; updated_skipworktree is set." >testfailure
28 exit 1
30 if test -f ".git/index.lock"; then
31 echo ".git/index.lock exists" >testfailure
32 exit 3
34 if ! test -f ".git/index"; then
35 echo ".git/index does not exist" >testfailure
36 exit 3
38 echo "success" >testsuccess
39 EOF
40 mkdir -p dir2 &&
41 touch dir2/file1.txt &&
42 touch dir2/file2.txt &&
43 : force index to be dirty &&
44 test-tool chmtime +60 dir1/file1.txt &&
45 git status &&
46 test_path_is_file testsuccess && rm -f testsuccess &&
47 test_path_is_missing testfailure &&
48 git add . &&
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 &&
57 git update-index &&
58 test_path_is_missing testsuccess &&
59 test_path_is_missing testfailure &&
60 git reset --soft &&
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
69 exit 1
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
73 exit 2
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
78 exit 3
80 if ! test -f ".git/index"; then
81 echo "updated_workdir set but .git/index does not exist" >testfailure
82 exit 3
84 else
85 echo "update_workdir should be set for checkout" >testfailure
86 exit 4
88 echo "success" >testsuccess
89 EOF
90 : force index to be dirty &&
91 test-tool chmtime +60 dir1/file1.txt &&
92 git checkout main &&
93 test_path_is_file testsuccess && rm -f testsuccess &&
94 test_path_is_missing testfailure &&
95 test-tool chmtime +60 dir1/file1.txt &&
96 git checkout HEAD &&
97 test_path_is_file testsuccess && rm -f testsuccess &&
98 test_path_is_missing testfailure &&
99 test-tool chmtime +60 dir1/file1.txt &&
100 git reset --hard &&
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
112 exit 1
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
116 exit 2
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
121 exit 3
123 if ! test -f ".git/index"; then
124 echo "updated_skipworktree set but .git/index does not exist" >testfailure
125 exit 3
127 else
128 echo "updated_skipworktree should be set for reset --mixed and update-index" >testfailure
129 exit 4
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
148 test_done