t5548: refactor to reuse setup_upstream() function
Refactor the function setup_upstream_and_workbench(), extracting create_upstream_template() and setup_upstream() from it. The former is used to create the upstream repository template, while the latter is used to rebuild the upstream repository and will be reused in subsequent commits. To ensure that setup_upstream() works properly in both local and HTTP protocols, the HTTP settings have been moved to the setup_upstream() and setup_upstream_and_workbench() functions. Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com> Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jiang Xin committed
Feb 3, 2025 at 07:29 UTC
12ad6b8feaf9dbbe8f040ce8620700cae793708b
1 file changed
+53
-30
t/t5548-push-porcelain.sh
+53
-30
@@ -54,29 +54,65 @@ format_and_save_expect () {
54
sed -e 's/^> //' -e 's/Z$//' >expect
55
}
56
57
+create_upstream_template () {
58
+ git init --bare upstream-template.git &&
59
+ git clone upstream-template.git tmp_work_dir &&
60
+ create_commits_in tmp_work_dir A B &&
61
+ (
62
+ cd tmp_work_dir &&
63
+ git push origin \
64
+ $B:refs/heads/main \
65
+ $A:refs/heads/foo \
66
+ $A:refs/heads/bar \
67
+ $A:refs/heads/baz
68
+ ) &&
69
+ rm -rf tmp_work_dir
70
+}
71
+
72
+setup_upstream () {
73
+ if test $# -ne 1
74
+ then
75
+ BUG "location of upstream repository is not provided"
76
+ fi &&
77
+ rm -rf "$1" &&
78
+ if ! test -d upstream-template.git
79
+ then
80
+ create_upstream_template
81
+ fi &&
82
+ git clone --mirror upstream-template.git "$1" &&
83
+ # The upstream repository provides services using the HTTP protocol.
84
+ if ! test "$1" = "upstream.git"
85
+ then
86
+ git -C "$1" config http.receivepack true
87
+ fi
88
+}
89
+
90
setup_upstream_and_workbench () {
91
+ if test $# -ne 1
92
+ then
93
+ BUG "location of upstream repository is not provided"
94
+ fi
95
+ upstream="$1"
96
+
97
# Upstream after setup : main(B) foo(A) bar(A) baz(A)
98
# Workbench after setup : main(A)
99
test_expect_success "setup upstream repository and workbench" '
61
- rm -rf upstream.git workbench &&
62
- git init --bare upstream.git &&
63
- git init workbench &&
64
- create_commits_in workbench A B &&
100
+ setup_upstream "$upstream" &&
101
+ rm -rf workbench &&
102
+ git clone "$upstream" workbench &&
103
(
104
cd workbench &&
105
+ git update-ref refs/heads/main $A &&
106
# Try to make a stable fixed width for abbreviated commit ID,
107
# this fixed-width oid will be replaced with "<OID>".
108
git config core.abbrev 7 &&
70
- git remote add origin ../upstream.git &&
71
- git update-ref refs/heads/main $A &&
72
- git push origin \
73
- $B:refs/heads/main \
74
- $A:refs/heads/foo \
75
- $A:refs/heads/bar \
76
- $A:refs/heads/baz
109
+ git config advice.pushUpdateRejected false
110
) &&
78
- git -C "workbench" config advice.pushUpdateRejected false &&
79
- upstream=upstream.git
111
+ # The upstream repository provides services using the HTTP protocol.
112
+ if ! test "$upstream" = "upstream.git"
113
+ then
114
+ git -C workbench remote set-url origin "$HTTPD_URL/smart/upstream.git"
115
+ fi
116
'
117
}
118
@@ -88,7 +124,7 @@ run_git_push_porcelain_output_test() {
124
;;
125
file)
126
PROTOCOL="builtin protocol"
91
- URL_PREFIX="\.\."
127
+ URL_PREFIX=".*"
128
;;
129
esac
130
@@ -247,10 +283,8 @@ run_git_push_porcelain_output_test() {
283
'
284
}
285
250
-# Initialize the upstream repository and local workbench.
251
-setup_upstream_and_workbench
286
+setup_upstream_and_workbench upstream.git
287
253
-# Run git-push porcelain test on builtin protocol
288
run_git_push_porcelain_output_test file
289
290
ROOT_PATH="$PWD"
@@ -258,21 +292,10 @@ ROOT_PATH="$PWD"
292
. "$TEST_DIRECTORY"/lib-httpd.sh
293
. "$TEST_DIRECTORY"/lib-terminal.sh
294
start_httpd
261
-
262
-# Re-initialize the upstream repository and local workbench.
263
-setup_upstream_and_workbench
264
-
265
-test_expect_success "setup for http" '
266
- git -C upstream.git config http.receivepack true &&
267
- upstream="$HTTPD_DOCUMENT_ROOT_PATH/upstream.git" &&
268
- mv upstream.git "$upstream" &&
269
-
270
- git -C workbench remote set-url origin $HTTPD_URL/smart/upstream.git
271
-'
272
-
295
setup_askpass_helper
296
275
-# Run git-push porcelain test on HTTP protocol
297
+setup_upstream_and_workbench "$HTTPD_DOCUMENT_ROOT_PATH/upstream.git"
298
+
299
run_git_push_porcelain_output_test http
300
301
test_done