Merge branch 'wl/new-command-doc'
[git.git] / t / t7113-post-index-change-hook.sh
blob58e55a7c7791616fa22efec12c79350c308fcaf9
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 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
23 exit 1
25 if test "$2" -eq 1; then
26 echo "Invalid combination of flags passed to hook; updated_skipworktree is set." >testfailure
27 exit 1
29 if test -f ".git/index.lock"; then
30 echo ".git/index.lock exists" >testfailure
31 exit 3
33 if ! test -f ".git/index"; then
34 echo ".git/index does not exist" >testfailure
35 exit 3
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
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
68 exit 1
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
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
79 if ! test -f ".git/index"; then
80 echo "updated_workdir set but .git/index does not exist" >testfailure
81 exit 3
83 else
84 echo "update_workdir should be set for checkout" >testfailure
85 exit 4
87 echo "success" >testsuccess
88 EOF
89 : force index to be dirty &&
90 test-tool chmtime +60 dir1/file1.txt &&
91 git checkout main &&
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
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
111 exit 1
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
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
122 if ! test -f ".git/index"; then
123 echo "updated_skipworktree set but .git/index does not exist" >testfailure
124 exit 3
126 else
127 echo "updated_skipworktree should be set for reset --mixed and update-index" >testfailure
128 exit 4
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
147 test_done