3 # Copyright (c) 2020 Jiang Xin
5 test_description
='Test git push porcelain output'
7 TEST_PASSES_SANITIZE_LEAK
=true
10 # Create commits in <repo> and assign each commit's oid to shell variables
11 # given in the arguments (A, B, and C). E.g.:
13 # create_commits_in <repo> A B C
15 # NOTE: Never calling this function from a subshell since variable
16 # assignments will disappear when subshell exits.
17 create_commits_in
() {
18 repo
="$1" && test -d "$repo" ||
19 error
"Repository $repo does not exist."
25 test_commit
-C "$repo" --no-tag "$name" &&
26 eval $name=$
(git
-C "$repo" rev-parse HEAD
)
32 suffix
=${oid#???????} &&
42 # Format the output of git-push, git-show-ref and other commands to make a
43 # user-friendly and stable text. We can easily prepare the expect text
44 # without having to worry about future changes of the commit ID and spaces
46 make_user_friendly_and_stable_output
() {
48 -e "s/$(get_abbrev_oid $A)[0-9a-f]*/<COMMIT-A>/g" \
49 -e "s/$(get_abbrev_oid $B)[0-9a-f]*/<COMMIT-B>/g" \
50 -e "s/$ZERO_OID/<ZERO-OID>/g" \
51 -e "s#To $URL_PREFIX/upstream.git#To <URL/of/upstream.git>#"
54 format_and_save_expect
() {
55 sed -e 's/^> //' -e 's/Z$//' >expect
58 setup_upstream_and_workbench
() {
59 # Upstream after setup : main(B) foo(A) bar(A) baz(A)
60 # Workbench after setup : main(A)
61 test_expect_success
"setup upstream repository and workbench" '
62 rm -rf upstream.git workbench &&
63 git init --bare upstream.git &&
65 create_commits_in workbench A B &&
68 # Try to make a stable fixed width for abbreviated commit ID,
69 # this fixed-width oid will be replaced with "<OID>".
70 git config core.abbrev 7 &&
71 git remote add origin ../upstream.git &&
72 git update-ref refs/heads/main $A &&
79 git -C "workbench" config advice.pushUpdateRejected false &&
84 run_git_push_porcelain_output_test
() {
87 PROTOCOL
="HTTP protocol"
88 URL_PREFIX
="http://.*"
91 PROTOCOL
="builtin protocol"
96 # Refs of upstream : main(B) foo(A) bar(A) baz(A)
97 # Refs of workbench: main(A) baz(A) next(A)
98 # git-push : main(A) NULL (B) baz(A) next(A)
99 test_expect_success
"porcelain output of successful git-push ($PROTOCOL)" '
102 git update-ref refs/heads/main $A &&
103 git update-ref refs/heads/baz $A &&
104 git update-ref refs/heads/next $A &&
105 git push --porcelain --force origin \
112 make_user_friendly_and_stable_output <out >actual &&
113 format_and_save_expect <<-EOF &&
114 > To <URL/of/upstream.git>
115 > = refs/heads/baz:refs/heads/baz [up to date]
116 > <COMMIT-B>:refs/heads/bar <COMMIT-A>..<COMMIT-B>
117 > - :refs/heads/foo [deleted]
118 > + refs/heads/main:refs/heads/main <COMMIT-B>...<COMMIT-A> (forced update)
119 > * refs/heads/next:refs/heads/next [new branch]
122 test_cmp expect actual &&
124 git -C "$upstream" show-ref >out &&
125 make_user_friendly_and_stable_output <out >actual &&
126 cat >expect <<-EOF &&
127 <COMMIT-B> refs/heads/bar
128 <COMMIT-A> refs/heads/baz
129 <COMMIT-A> refs/heads/main
130 <COMMIT-A> refs/heads/next
132 test_cmp expect actual
135 # Refs of upstream : main(A) bar(B) baz(A) next(A)
136 # Refs of workbench: main(B) bar(A) baz(A) next(A)
137 # git-push : main(B) bar(A) NULL next(A)
138 test_expect_success
"atomic push failed ($PROTOCOL)" '
141 git update-ref refs/heads/main $B &&
142 git update-ref refs/heads/bar $A &&
143 test_must_fail git push --atomic --porcelain origin \
149 make_user_friendly_and_stable_output <out >actual &&
150 format_and_save_expect <<-EOF &&
151 To <URL/of/upstream.git>
152 > = refs/heads/next:refs/heads/next [up to date]
153 > ! refs/heads/bar:refs/heads/bar [rejected] (non-fast-forward)
154 > ! (delete):refs/heads/baz [rejected] (atomic push failed)
155 > ! refs/heads/main:refs/heads/main [rejected] (atomic push failed)
158 test_cmp expect actual &&
160 git -C "$upstream" show-ref >out &&
161 make_user_friendly_and_stable_output <out >actual &&
162 cat >expect <<-EOF &&
163 <COMMIT-B> refs/heads/bar
164 <COMMIT-A> refs/heads/baz
165 <COMMIT-A> refs/heads/main
166 <COMMIT-A> refs/heads/next
168 test_cmp expect actual
171 test_expect_success
"prepare pre-receive hook ($PROTOCOL)" '
172 test_hook --setup -C "$upstream" pre-receive <<-EOF
177 # Refs of upstream : main(A) bar(B) baz(A) next(A)
178 # Refs of workbench: main(B) bar(A) baz(A) next(A)
179 # git-push : main(B) bar(A) NULL next(A)
180 test_expect_success
"pre-receive hook declined ($PROTOCOL)" '
183 git update-ref refs/heads/main $B &&
184 git update-ref refs/heads/bar $A &&
185 test_must_fail git push --porcelain --force origin \
191 make_user_friendly_and_stable_output <out >actual &&
192 format_and_save_expect <<-EOF &&
193 To <URL/of/upstream.git>
194 > = refs/heads/next:refs/heads/next [up to date]
195 > ! refs/heads/bar:refs/heads/bar [remote rejected] (pre-receive hook declined)
196 > ! :refs/heads/baz [remote rejected] (pre-receive hook declined)
197 > ! refs/heads/main:refs/heads/main [remote rejected] (pre-receive hook declined)
200 test_cmp expect actual &&
202 git -C "$upstream" show-ref >out &&
203 make_user_friendly_and_stable_output <out >actual &&
204 cat >expect <<-EOF &&
205 <COMMIT-B> refs/heads/bar
206 <COMMIT-A> refs/heads/baz
207 <COMMIT-A> refs/heads/main
208 <COMMIT-A> refs/heads/next
210 test_cmp expect actual
213 test_expect_success
"remove pre-receive hook ($PROTOCOL)" '
214 rm "$upstream/hooks/pre-receive"
217 # Refs of upstream : main(A) bar(B) baz(A) next(A)
218 # Refs of workbench: main(B) bar(A) baz(A) next(A)
219 # git-push : main(B) bar(A) NULL next(A)
220 test_expect_success
"non-fastforward push ($PROTOCOL)" '
223 test_must_fail git push --porcelain origin \
229 make_user_friendly_and_stable_output <out >actual &&
230 format_and_save_expect <<-EOF &&
231 To <URL/of/upstream.git>
232 > = refs/heads/next:refs/heads/next [up to date]
233 > - :refs/heads/baz [deleted]
234 > refs/heads/main:refs/heads/main <COMMIT-A>..<COMMIT-B>
235 > ! refs/heads/bar:refs/heads/bar [rejected] (non-fast-forward)
238 test_cmp expect actual &&
240 git -C "$upstream" show-ref >out &&
241 make_user_friendly_and_stable_output <out >actual &&
242 cat >expect <<-EOF &&
243 <COMMIT-B> refs/heads/bar
244 <COMMIT-B> refs/heads/main
245 <COMMIT-A> refs/heads/next
247 test_cmp expect actual
251 # Initialize the upstream repository and local workbench.
252 setup_upstream_and_workbench
254 # Run git-push porcelain test on builtin protocol
255 run_git_push_porcelain_output_test
file
258 .
"$TEST_DIRECTORY"/lib-gpg.sh
259 .
"$TEST_DIRECTORY"/lib-httpd.sh
260 .
"$TEST_DIRECTORY"/lib-terminal.sh
263 # Re-initialize the upstream repository and local workbench.
264 setup_upstream_and_workbench
266 test_expect_success
"setup for http" '
267 git -C upstream.git config http.receivepack true &&
268 upstream="$HTTPD_DOCUMENT_ROOT_PATH/upstream.git" &&
269 mv upstream.git "$upstream" &&
271 git -C workbench remote set-url origin $HTTPD_URL/smart/upstream.git
276 # Run git-push porcelain test on HTTP protocol
277 run_git_push_porcelain_output_test http