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