Merge branch 'jk/HEAD-symref-in-xfer-namespaces' into maint
[git] / t / t7113-post-index-change-hook.sh
1 #!/bin/sh
2
3 test_description='post index change hook'
4
5 . ./test-lib.sh
6
7 test_expect_success 'setup' '
8         mkdir -p dir1 &&
9         touch dir1/file1.txt &&
10         echo testing >dir1/file2.txt &&
11         git add . &&
12         git commit -m "initial"
13 '
14
15 test_expect_success 'test status, add, commit, others trigger hook without flags set' '
16         mkdir -p .git/hooks &&
17         write_script .git/hooks/post-index-change <<-\EOF &&
18                 if test "$1" -eq 1; then
19                         echo "Invalid combination of flags passed to hook; updated_workdir is set." >testfailure
20                         exit 1
21                 fi
22                 if test "$2" -eq 1; then
23                         echo "Invalid combination of flags passed to hook; updated_skipworktree is set." >testfailure
24                         exit 1
25                 fi
26                 if test -f ".git/index.lock"; then
27                         echo ".git/index.lock exists" >testfailure
28                         exit 3
29                 fi
30                 if ! test -f ".git/index"; then
31                         echo ".git/index does not exist" >testfailure
32                         exit 3
33                 fi
34                 echo "success" >testsuccess
35         EOF
36         mkdir -p dir2 &&
37         touch dir2/file1.txt &&
38         touch dir2/file2.txt &&
39         : force index to be dirty &&
40         test-tool chmtime +60 dir1/file1.txt &&
41         git status &&
42         test_path_is_file testsuccess && rm -f testsuccess &&
43         test_path_is_missing testfailure &&
44         git add . &&
45         test_path_is_file testsuccess && rm -f testsuccess &&
46         test_path_is_missing testfailure &&
47         git commit -m "second" &&
48         test_path_is_file testsuccess && rm -f testsuccess &&
49         test_path_is_missing testfailure &&
50         git checkout -- dir1/file1.txt &&
51         test_path_is_file testsuccess && rm -f testsuccess &&
52         test_path_is_missing testfailure &&
53         git update-index &&
54         test_path_is_missing testsuccess &&
55         test_path_is_missing testfailure &&
56         git reset --soft &&
57         test_path_is_missing testsuccess &&
58         test_path_is_missing testfailure
59 '
60
61 test_expect_success 'test checkout and reset trigger the hook' '
62         write_script .git/hooks/post-index-change <<-\EOF &&
63                 if test "$1" -eq 1 && test "$2" -eq 1; then
64                         echo "Invalid combination of flags passed to hook; updated_workdir and updated_skipworktree are both set." >testfailure
65                         exit 1
66                 fi
67                 if test "$1" -eq 0 && test "$2" -eq 0; then
68                         echo "Invalid combination of flags passed to hook; neither updated_workdir or updated_skipworktree are set." >testfailure
69                         exit 2
70                 fi
71                 if test "$1" -eq 1; then
72                         if test -f ".git/index.lock"; then
73                                 echo "updated_workdir set but .git/index.lock exists" >testfailure
74                                 exit 3
75                         fi
76                         if ! test -f ".git/index"; then
77                                 echo "updated_workdir set but .git/index does not exist" >testfailure
78                                 exit 3
79                         fi
80                 else
81                         echo "update_workdir should be set for checkout" >testfailure
82                         exit 4
83                 fi
84                 echo "success" >testsuccess
85         EOF
86         : force index to be dirty &&
87         test-tool chmtime +60 dir1/file1.txt &&
88         git checkout master &&
89         test_path_is_file testsuccess && rm -f testsuccess &&
90         test_path_is_missing testfailure &&
91         test-tool chmtime +60 dir1/file1.txt &&
92         git checkout HEAD &&
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 reset --hard &&
97         test_path_is_file testsuccess && rm -f testsuccess &&
98         test_path_is_missing testfailure &&
99         git checkout -B test &&
100         test_path_is_file testsuccess && rm -f testsuccess &&
101         test_path_is_missing testfailure
102 '
103
104 test_expect_success 'test reset --mixed and update-index triggers the hook' '
105         write_script .git/hooks/post-index-change <<-\EOF &&
106                 if test "$1" -eq 1 && test "$2" -eq 1; then
107                         echo "Invalid combination of flags passed to hook; updated_workdir and updated_skipworktree are both set." >testfailure
108                         exit 1
109                 fi
110                 if test "$1" -eq 0 && test "$2" -eq 0; then
111                         echo "Invalid combination of flags passed to hook; neither updated_workdir or updated_skipworktree are set." >testfailure
112                         exit 2
113                 fi
114                 if test "$2" -eq 1; then
115                         if test -f ".git/index.lock"; then
116                                 echo "updated_skipworktree set but .git/index.lock exists" >testfailure
117                                 exit 3
118                         fi
119                         if ! test -f ".git/index"; then
120                                 echo "updated_skipworktree set but .git/index does not exist" >testfailure
121                                 exit 3
122                         fi
123                 else
124                         echo "updated_skipworktree should be set for reset --mixed and update-index" >testfailure
125                         exit 4
126                 fi
127                 echo "success" >testsuccess
128         EOF
129         : force index to be dirty &&
130         test-tool chmtime +60 dir1/file1.txt &&
131         git reset --mixed --quiet HEAD~1 &&
132         test_path_is_file testsuccess && rm -f testsuccess &&
133         test_path_is_missing testfailure &&
134         git hash-object -w --stdin <dir1/file2.txt >expect &&
135         git update-index --cacheinfo 100644 "$(cat expect)" dir1/file1.txt &&
136         test_path_is_file testsuccess && rm -f testsuccess &&
137         test_path_is_missing testfailure &&
138         git update-index --skip-worktree dir1/file2.txt &&
139         git update-index --remove dir1/file2.txt &&
140         test_path_is_file testsuccess && rm -f testsuccess &&
141         test_path_is_missing testfailure
142 '
143
144 test_done