Merge tag 'l10n-2.36.0-rnd2.1' of https://github.com/git-l10n/git-po
[alt-git.git] / t / t5543-atomic-push.sh
blob70431122a40360c265dc28627c4208dc360c000c
1 #!/bin/sh
3 test_description='pushing to a repository using the atomic push option'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 . ./test-lib.sh
10 mk_repo_pair () {
11 rm -rf workbench upstream &&
12 test_create_repo upstream &&
13 test_create_repo workbench &&
15 cd upstream &&
16 git config receive.denyCurrentBranch warn
17 ) &&
19 cd workbench &&
20 git remote add up ../upstream
24 # Compare the ref ($1) in upstream with a ref value from workbench ($2)
25 # i.e. test_refs second HEAD@{2}
26 test_refs () {
27 test $# = 2 &&
28 git -C upstream rev-parse --verify "$1" >expect &&
29 git -C workbench rev-parse --verify "$2" >actual &&
30 test_cmp expect actual
33 fmt_status_report () {
34 sed -n \
35 -e "/^To / { s/ */ /g; p; }" \
36 -e "/^ ! / { s/ */ /g; p; }"
39 test_expect_success 'atomic push works for a single branch' '
40 mk_repo_pair &&
42 cd workbench &&
43 test_commit one &&
44 git push --mirror up &&
45 test_commit two &&
46 git push --atomic up main
47 ) &&
48 test_refs main main
51 test_expect_success 'atomic push works for two branches' '
52 mk_repo_pair &&
54 cd workbench &&
55 test_commit one &&
56 git branch second &&
57 git push --mirror up &&
58 test_commit two &&
59 git checkout second &&
60 test_commit three &&
61 git push --atomic up main second
62 ) &&
63 test_refs main main &&
64 test_refs second second
67 test_expect_success 'atomic push works in combination with --mirror' '
68 mk_repo_pair &&
70 cd workbench &&
71 test_commit one &&
72 git checkout -b second &&
73 test_commit two &&
74 git push --atomic --mirror up
75 ) &&
76 test_refs main main &&
77 test_refs second second
80 test_expect_success 'atomic push works in combination with --force' '
81 mk_repo_pair &&
83 cd workbench &&
84 test_commit one &&
85 git branch second main &&
86 test_commit two_a &&
87 git checkout second &&
88 test_commit two_b &&
89 test_commit three_b &&
90 test_commit four &&
91 git push --mirror up &&
92 # The actual test is below
93 git checkout main &&
94 test_commit three_a &&
95 git checkout second &&
96 git reset --hard HEAD^ &&
97 git push --force --atomic up main second
98 ) &&
99 test_refs main main &&
100 test_refs second second
103 # set up two branches where main can be pushed but second can not
104 # (non-fast-forward). Since second can not be pushed the whole operation
105 # will fail and leave main untouched.
106 test_expect_success 'atomic push fails if one branch fails' '
107 mk_repo_pair &&
109 cd workbench &&
110 test_commit one &&
111 git checkout -b second main &&
112 test_commit two &&
113 test_commit three &&
114 test_commit four &&
115 git push --mirror up &&
116 git reset --hard HEAD~2 &&
117 test_commit five &&
118 git checkout main &&
119 test_commit six &&
120 test_must_fail git push --atomic --all up
121 ) &&
122 test_refs main HEAD@{7} &&
123 test_refs second HEAD@{4}
126 test_expect_success 'atomic push fails if one tag fails remotely' '
127 # prepare the repo
128 mk_repo_pair &&
130 cd workbench &&
131 test_commit one &&
132 git checkout -b second main &&
133 test_commit two &&
134 git push --mirror up
135 ) &&
136 # a third party modifies the server side:
138 cd upstream &&
139 git checkout second &&
140 git tag test_tag second
141 ) &&
142 # see if we can now push both branches.
144 cd workbench &&
145 git checkout main &&
146 test_commit three &&
147 git checkout second &&
148 test_commit four &&
149 git tag test_tag &&
150 test_must_fail git push --tags --atomic up main second
151 ) &&
152 test_refs main HEAD@{3} &&
153 test_refs second HEAD@{1}
156 test_expect_success 'atomic push obeys update hook preventing a branch to be pushed' '
157 mk_repo_pair &&
159 cd workbench &&
160 test_commit one &&
161 git checkout -b second main &&
162 test_commit two &&
163 git push --mirror up
164 ) &&
165 test_hook -C upstream update <<-\EOF &&
166 # only allow update to main from now on
167 test "$1" = "refs/heads/main"
170 cd workbench &&
171 git checkout main &&
172 test_commit three &&
173 git checkout second &&
174 test_commit four &&
175 test_must_fail git push --atomic up main second
176 ) &&
177 test_refs main HEAD@{3} &&
178 test_refs second HEAD@{1}
181 test_expect_success 'atomic push is not advertised if configured' '
182 mk_repo_pair &&
184 cd upstream &&
185 git config receive.advertiseatomic 0
186 ) &&
188 cd workbench &&
189 test_commit one &&
190 git push --mirror up &&
191 test_commit two &&
192 test_must_fail git push --atomic up main
193 ) &&
194 test_refs main HEAD@{1}
197 # References in upstream : main(1) one(1) foo(1)
198 # References in workbench: main(2) foo(1) two(2) bar(2)
199 # Atomic push : main(2) two(2) bar(2)
200 test_expect_success 'atomic push reports (reject by update hook)' '
201 mk_repo_pair &&
203 cd workbench &&
204 test_commit one &&
205 git branch foo &&
206 git push up main one foo &&
207 git tag -d one
208 ) &&
210 mkdir -p upstream/.git/hooks &&
211 cat >upstream/.git/hooks/update <<-EOF &&
212 #!/bin/sh
214 if test "\$1" = "refs/heads/bar"
215 then
216 echo >&2 "Pusing to branch bar is prohibited"
217 exit 1
220 chmod a+x upstream/.git/hooks/update
221 ) &&
223 cd workbench &&
224 test_commit two &&
225 git branch bar
226 ) &&
227 test_must_fail git -C workbench \
228 push --atomic up main two bar >out 2>&1 &&
229 fmt_status_report <out >actual &&
230 cat >expect <<-EOF &&
231 To ../upstream
232 ! [remote rejected] main -> main (atomic push failure)
233 ! [remote rejected] two -> two (atomic push failure)
234 ! [remote rejected] bar -> bar (hook declined)
236 test_cmp expect actual
239 # References in upstream : main(1) one(1) foo(1)
240 # References in workbench: main(2) foo(1) two(2) bar(2)
241 test_expect_success 'atomic push reports (mirror, but reject by update hook)' '
243 cd workbench &&
244 git remote remove up &&
245 git remote add up ../upstream
246 ) &&
247 test_must_fail git -C workbench \
248 push --atomic --mirror up >out 2>&1 &&
249 fmt_status_report <out >actual &&
250 cat >expect <<-EOF &&
251 To ../upstream
252 ! [remote rejected] main -> main (atomic push failure)
253 ! [remote rejected] one (atomic push failure)
254 ! [remote rejected] bar -> bar (hook declined)
255 ! [remote rejected] two -> two (atomic push failure)
257 test_cmp expect actual
260 # References in upstream : main(2) one(1) foo(1)
261 # References in workbench: main(1) foo(1) two(2) bar(2)
262 test_expect_success 'atomic push reports (reject by non-ff)' '
263 rm upstream/.git/hooks/update &&
265 cd workbench &&
266 git push up main &&
267 git reset --hard HEAD^
268 ) &&
269 test_must_fail git -C workbench \
270 push --atomic up main foo bar >out 2>&1 &&
271 fmt_status_report <out >actual &&
272 cat >expect <<-EOF &&
273 To ../upstream
274 ! [rejected] main -> main (non-fast-forward)
275 ! [rejected] bar -> bar (atomic push failed)
277 test_cmp expect actual
280 test_done