add open_nofollow() helper
[git] / t / t7113-post-index-change-hook.sh
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         mkdir -p .git/hooks &&
20         write_script .git/hooks/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
24                 fi
25                 if test "$2" -eq 1; then
26                         echo "Invalid combination of flags passed to hook; updated_skipworktree is set." >testfailure
27                         exit 1
28                 fi
29                 if test -f ".git/index.lock"; then
30                         echo ".git/index.lock exists" >testfailure
31                         exit 3
32                 fi
33                 if ! test -f ".git/index"; then
34                         echo ".git/index does not exist" >testfailure
35                         exit 3
36                 fi
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
62 '
63
64 test_expect_success 'test checkout and reset trigger the hook' '
65         write_script .git/hooks/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
69                 fi
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
73                 fi
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
78                         fi
79                         if ! test -f ".git/index"; then
80                                 echo "updated_workdir set but .git/index does not exist" >testfailure
81                                 exit 3
82                         fi
83                 else
84                         echo "update_workdir should be set for checkout" >testfailure
85                         exit 4
86                 fi
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
105 '
106
107 test_expect_success 'test reset --mixed and update-index triggers the hook' '
108         write_script .git/hooks/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
112                 fi
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
116                 fi
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
121                         fi
122                         if ! test -f ".git/index"; then
123                                 echo "updated_skipworktree set but .git/index does not exist" >testfailure
124                                 exit 3
125                         fi
126                 else
127                         echo "updated_skipworktree should be set for reset --mixed and update-index" >testfailure
128                         exit 4
129                 fi
130                 echo "success" >testsuccess
131         EOF
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
145 '
146
147 test_done