Merge branch 'ew/rev-parse-since-test'
[git] / t / t1416-ref-transaction-hooks.sh
1 #!/bin/sh
2
3 test_description='reference transaction hooks'
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 .git/hooks &&
12         test_commit PRE &&
13         PRE_OID=$(git rev-parse PRE) &&
14         test_commit POST &&
15         POST_OID=$(git rev-parse POST)
16 '
17
18 test_expect_success 'hook allows updating ref if successful' '
19         test_when_finished "rm .git/hooks/reference-transaction" &&
20         git reset --hard PRE &&
21         write_script .git/hooks/reference-transaction <<-\EOF &&
22                 echo "$*" >>actual
23         EOF
24         cat >expect <<-EOF &&
25                 prepared
26                 committed
27         EOF
28         git update-ref HEAD POST &&
29         test_cmp expect actual
30 '
31
32 test_expect_success 'hook aborts updating ref in prepared state' '
33         test_when_finished "rm .git/hooks/reference-transaction" &&
34         git reset --hard PRE &&
35         write_script .git/hooks/reference-transaction <<-\EOF &&
36                 if test "$1" = prepared
37                 then
38                         exit 1
39                 fi
40         EOF
41         test_must_fail git update-ref HEAD POST 2>err &&
42         test_i18ngrep "ref updates aborted by hook" err
43 '
44
45 test_expect_success 'hook gets all queued updates in prepared state' '
46         test_when_finished "rm .git/hooks/reference-transaction actual" &&
47         git reset --hard PRE &&
48         write_script .git/hooks/reference-transaction <<-\EOF &&
49                 if test "$1" = prepared
50                 then
51                         while read -r line
52                         do
53                                 printf "%s\n" "$line"
54                         done >actual
55                 fi
56         EOF
57         cat >expect <<-EOF &&
58                 $ZERO_OID $POST_OID HEAD
59                 $ZERO_OID $POST_OID refs/heads/main
60         EOF
61         git update-ref HEAD POST <<-EOF &&
62                 update HEAD $ZERO_OID $POST_OID
63                 update refs/heads/main $ZERO_OID $POST_OID
64         EOF
65         test_cmp expect actual
66 '
67
68 test_expect_success 'hook gets all queued updates in committed state' '
69         test_when_finished "rm .git/hooks/reference-transaction actual" &&
70         git reset --hard PRE &&
71         write_script .git/hooks/reference-transaction <<-\EOF &&
72                 if test "$1" = committed
73                 then
74                         while read -r line
75                         do
76                                 printf "%s\n" "$line"
77                         done >actual
78                 fi
79         EOF
80         cat >expect <<-EOF &&
81                 $ZERO_OID $POST_OID HEAD
82                 $ZERO_OID $POST_OID refs/heads/main
83         EOF
84         git update-ref HEAD POST &&
85         test_cmp expect actual
86 '
87
88 test_expect_success 'hook gets all queued updates in aborted state' '
89         test_when_finished "rm .git/hooks/reference-transaction actual" &&
90         git reset --hard PRE &&
91         write_script .git/hooks/reference-transaction <<-\EOF &&
92                 if test "$1" = aborted
93                 then
94                         while read -r line
95                         do
96                                 printf "%s\n" "$line"
97                         done >actual
98                 fi
99         EOF
100         cat >expect <<-EOF &&
101                 $ZERO_OID $POST_OID HEAD
102                 $ZERO_OID $POST_OID refs/heads/main
103         EOF
104         git update-ref --stdin <<-EOF &&
105                 start
106                 update HEAD POST $ZERO_OID
107                 update refs/heads/main POST $ZERO_OID
108                 abort
109         EOF
110         test_cmp expect actual
111 '
112
113 test_expect_success 'interleaving hook calls succeed' '
114         test_when_finished "rm -r target-repo.git" &&
115
116         git init --bare target-repo.git &&
117
118         write_script target-repo.git/hooks/reference-transaction <<-\EOF &&
119                 echo $0 "$@" >>actual
120         EOF
121
122         write_script target-repo.git/hooks/update <<-\EOF &&
123                 echo $0 "$@" >>actual
124         EOF
125
126         cat >expect <<-EOF &&
127                 hooks/update refs/tags/PRE $ZERO_OID $PRE_OID
128                 hooks/reference-transaction prepared
129                 hooks/reference-transaction committed
130                 hooks/update refs/tags/POST $ZERO_OID $POST_OID
131                 hooks/reference-transaction prepared
132                 hooks/reference-transaction committed
133         EOF
134
135         git push ./target-repo.git PRE POST &&
136         test_cmp expect target-repo.git/actual
137 '
138
139 test_done