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
11 rm -rf workbench upstream
&&
12 test_create_repo upstream
&&
13 test_create_repo workbench
&&
16 git config receive.denyCurrentBranch warn
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}
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
() {
35 -e "/^To / { s/ */ /g; p; }" \
36 -e "/^ ! / { s/ */ /g; p; }"
39 test_expect_success
'atomic push works for a single branch' '
44 git push --mirror up &&
46 git push --atomic up main
51 test_expect_success
'atomic push works for two branches' '
57 git push --mirror up &&
59 git checkout second &&
61 git push --atomic up main second
63 test_refs main main &&
64 test_refs second second
67 test_expect_success
'atomic push works in combination with --mirror' '
72 git checkout -b second &&
74 git push --atomic --mirror up
76 test_refs main main &&
77 test_refs second second
80 test_expect_success
'atomic push works in combination with --force' '
85 git branch second main &&
87 git checkout second &&
89 test_commit three_b &&
91 git push --mirror up &&
92 # The actual test is below
94 test_commit three_a &&
95 git checkout second &&
96 git reset --hard HEAD^ &&
97 git push --force --atomic up main second
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' '
111 git checkout -b second main &&
115 git push --mirror up &&
116 git reset --hard HEAD~2 &&
120 test_must_fail git push --atomic --all up
122 test_refs main HEAD@{7} &&
123 test_refs second HEAD@{4}
126 test_expect_success
'atomic push fails if one tag fails remotely' '
132 git checkout -b second main &&
136 # a third party modifies the server side:
139 git checkout second &&
140 git tag test_tag second
142 # see if we can now push both branches.
147 git checkout second &&
150 test_must_fail git push --tags --atomic up main second
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' '
161 git checkout -b second main &&
167 HOOKDIR="$(git rev-parse --git-dir)/hooks" &&
168 HOOK="$HOOKDIR/update" &&
169 mkdir -p "$HOOKDIR" &&
170 write_script "$HOOK" <<-\EOF
171 # only allow update to main from now on
172 test "$1" = "refs/heads/main"
179 git checkout second &&
181 test_must_fail git push --atomic up main second
183 test_refs main HEAD@{3} &&
184 test_refs second HEAD@{1}
187 test_expect_success
'atomic push is not advertised if configured' '
191 git config receive.advertiseatomic 0
196 git push --mirror up &&
198 test_must_fail git push --atomic up main
200 test_refs main HEAD@{1}
203 # References in upstream : main(1) one(1) foo(1)
204 # References in workbench: main(2) foo(1) two(2) bar(2)
205 # Atomic push : main(2) two(2) bar(2)
206 test_expect_success
'atomic push reports (reject by update hook)' '
212 git push up main one foo &&
216 mkdir -p upstream/.git/hooks &&
217 cat >upstream/.git/hooks/update <<-EOF &&
220 if test "\$1" = "refs/heads/bar"
222 echo >&2 "Pusing to branch bar is prohibited"
226 chmod a+x upstream/.git/hooks/update
233 test_must_fail git -C workbench \
234 push --atomic up main two bar >out 2>&1 &&
235 fmt_status_report <out >actual &&
236 cat >expect <<-EOF &&
238 ! [remote rejected] main -> main (atomic push failure)
239 ! [remote rejected] two -> two (atomic push failure)
240 ! [remote rejected] bar -> bar (hook declined)
242 test_cmp expect actual
245 # References in upstream : main(1) one(1) foo(1)
246 # References in workbench: main(2) foo(1) two(2) bar(2)
247 test_expect_success
'atomic push reports (mirror, but reject by update hook)' '
250 git remote remove up &&
251 git remote add up ../upstream
253 test_must_fail git -C workbench \
254 push --atomic --mirror up >out 2>&1 &&
255 fmt_status_report <out >actual &&
256 cat >expect <<-EOF &&
258 ! [remote rejected] main -> main (atomic push failure)
259 ! [remote rejected] one (atomic push failure)
260 ! [remote rejected] bar -> bar (hook declined)
261 ! [remote rejected] two -> two (atomic push failure)
263 test_cmp expect actual
266 # References in upstream : main(2) one(1) foo(1)
267 # References in workbench: main(1) foo(1) two(2) bar(2)
268 test_expect_success
'atomic push reports (reject by non-ff)' '
269 rm upstream/.git/hooks/update &&
273 git reset --hard HEAD^
275 test_must_fail git -C workbench \
276 push --atomic up main foo bar >out 2>&1 &&
277 fmt_status_report <out >actual &&
278 cat >expect <<-EOF &&
280 ! [rejected] main -> main (non-fast-forward)
281 ! [rejected] bar -> bar (atomic push failed)
283 test_cmp expect actual