@@ -3937,28 +3937,37 @@ def _land(self, name: 'str', files: 'dict[str, str]', message: 'str',
39373937 def _land_after_fetch (self , name : 'str' , files : 'dict[str, str]' , message : 'str' ,
39383938 identity : 'Optional[tuple[tuple[str, str], tuple[str, str]]]' = None ,
39393939 ) -> 'tuple[str, dict[str, str]]' :
3940- """:meth:`_land`, but the push is left to a ``git`` shim that makes it
3941- once, the moment a ``git fetch`` run through it returns: the commit's
3942- sha, and the ``PATH`` that puts the shim first. It marks having fired
3943- by creating ``landed`` in the temporary directory.
3940+ """:meth:`_land`, but the push is left to :meth:`_after_first`, the
3941+ moment a ``git fetch`` returns: the commit's sha, and the ``PATH``.
39443942
39453943 """
39463944 clone = self ._stage (name , files , message , identity )
3945+ env = self ._after_first (
3946+ 'fetch' , f'exec "$git" -C { shlex .quote (str (clone ))} push -q origin HEAD:refs/heads/main' )
3947+ return self ._git (clone , 'rev-parse' , 'HEAD' ), env
3948+
3949+ def _after_first (self , command : 'str' , action : 'str' ) -> 'dict[str, str]' :
3950+ """The ``PATH`` that puts first a ``git`` shim running the real one,
3951+ ``$git``, and then, once, the ``sh`` line ``action``, the moment a
3952+ ``git <command>`` run through it succeeds. It marks having fired by
3953+ creating ``fired`` in the temporary directory.
3954+
3955+ """
39473956 real = shutil .which ('git' , path = self .env ['PATH' ])
39483957 assert real is not None # setUpClass skips without it
3949- git , marker = shlex .quote (real ), shlex . quote ( str (self .root / 'landed ' ))
3958+ marker = shlex .quote (str (self .root / 'fired ' ))
39503959 shim = self .root / 'shim'
39513960 shim .mkdir ()
39523961 (shim / 'git' ).write_text (
39533962 '#!/bin/sh\n '
3954- f'{ git } "$@" || exit\n '
3955- f'if [ "$1" = fetch ] && [ ! -e { marker } ]; then\n '
3963+ f'git={ shlex .quote (real )} \n '
3964+ '"$git" "$@" || exit\n '
3965+ f'if [ "$1" = { shlex .quote (command )} ] && [ ! -e { marker } ]; then\n '
39563966 f' : > { marker } \n '
3957- f' exec { git } -C { shlex . quote ( str ( clone )) } push -q origin HEAD:refs/heads/main \n '
3967+ f' { action } \n '
39583968 'fi\n ' , encoding = 'utf-8' )
39593969 (shim / 'git' ).chmod (0o755 )
3960- return (self ._git (clone , 'rev-parse' , 'HEAD' ),
3961- {'PATH' : f'{ shim } { os .pathsep } { self .env ["PATH" ]} ' })
3970+ return {'PATH' : f'{ shim } { os .pathsep } { self .env ["PATH" ]} ' }
39623971
39633972 def _bash (self , script : 'str' , cwd : 'pathlib.Path' , env : 'dict[str, str]' ,
39643973 ) -> 'tuple[subprocess.CompletedProcess[str], dict[str, str]]' :
@@ -4121,20 +4130,24 @@ def test_the_rule_is_run_from_the_checkout_not_from_the_tip_it_judges(self) -> N
41214130 def test_a_merge_landing_after_the_fetch_refuses_the_push (self ) -> None :
41224131 """The rebase is onto the tip the rule judged, ``$tip``, so a merge that
41234132 lands after the fetch is never under the bump: ``origin`` has moved past
4124- what is pushed, the push is not a fast-forward, and it is refused. The
4125- step fails having written nothing, so nothing is tagged or uploaded,
4126- and the merge's own ``Unit Tests`` run raises the next bump.
4133+ what is pushed, the push is not a fast-forward, and it is refused. That
4134+ is :meth:`test_a_bump_is_not_pushed_over_a_commit_with_no_verdict` one
4135+ step later, and it ends the same way (#1649): ``pushed=false``, a
4136+ notice, and a green job, and the merge's own ``Unit Tests`` run raises
4137+ the next bump.
41274138
41284139 """
41294140 clone = self ._clone ('first' )
41304141 merge , env = self ._land_after_fetch ('merge' , {'pcapkit/toolkit/scapy.py' : 'fix\n ' },
41314142 'fix(toolkit): a merge' , (self .HUMAN , self .GITHUB ))
41324143 self ._bump (clone , {0 : 'package0==2.0' })
41334144 result , outputs = self ._run (clone , ** env )
4134- self .assertTrue ((self .root / 'landed ' ).exists (), 'the merge never raced the fetch' )
4135- self .assertNotEqual (result .returncode , 0 , result .stdout + result .stderr )
4145+ self .assertTrue ((self .root / 'fired ' ).exists (), 'the merge never raced the fetch' )
4146+ self .assertEqual (result .returncode , 0 , result .stdout + result .stderr )
41364147 self .assertIn ('[rejected]' , result .stderr )
4137- self .assertEqual (outputs , {}, 'the step reported a push that did not land' )
4148+ self .assertEqual (outputs , {'pushed' : 'false' }, 'the step reported a push that did not land' )
4149+ self .assertIn (f'::notice title=Not pushed as main moved during the push::main moved '
4150+ f'from { self .base } to { merge } after the fetch' , result .stdout )
41384151 self .assertEqual (self ._origin ('rev-parse' , 'main' ), merge )
41394152 self .assertEqual (self ._git (clone , 'rev-parse' , 'HEAD^' ), self .base ,
41404153 'the bump was not rebased onto the tip the rule judged' )
@@ -4160,6 +4173,87 @@ def test_a_rebase_that_fetches_again_pushes_the_merge_under_the_bump(self) -> No
41604173 self .assertEqual (outputs , {'pushed' : 'true' , 'commit' : landed })
41614174 self .assertEqual (self ._origin ('rev-parse' , 'main^' ), merge )
41624175
4176+ def test_a_push_the_remote_declines_fails_the_step (self ) -> None :
4177+ """A refusal with ``main`` where it was fetched -- a hook here, a ruleset
4178+ on GitHub -- is no race, and must not pass as one: the step fails, as
4179+ every push failure did before #1649.
4180+
4181+ """
4182+ clone = self ._clone ('first' )
4183+ hook = self .origin / 'hooks' / 'pre-receive'
4184+ hook .write_text ('#!/bin/sh\n echo declined >&2\n exit 1\n ' , encoding = 'utf-8' )
4185+ hook .chmod (0o755 )
4186+ self ._bump (clone , {0 : 'package0==2.0' })
4187+ result , outputs = self ._run (clone )
4188+ self .assertNotEqual (result .returncode , 0 , result .stdout + result .stderr )
4189+ self .assertIn ('[remote rejected]' , result .stderr )
4190+ self .assertEqual (outputs , {})
4191+ self .assertNotIn ('::notice' , result .stdout )
4192+ self .assertEqual (self ._origin ('rev-parse' , 'main' ), self .base )
4193+
4194+ def test_a_remote_lost_after_the_fetch_fails_the_step (self ) -> None :
4195+ """A broken remote: the push fails, and so does the fetch that would ask
4196+ where ``main`` is now. With no answer the step fails rather than
4197+ guessing at a race.
4198+
4199+ """
4200+ clone = self ._clone ('first' )
4201+ gone = self .root / 'origin.gone'
4202+ env = self ._after_first ('fetch' , f'mv { shlex .quote (str (self .origin ))} '
4203+ f'{ shlex .quote (str (gone ))} ' )
4204+ self ._bump (clone , {0 : 'package0==2.0' })
4205+ result , outputs = self ._run (clone , ** env )
4206+ self .assertTrue ((self .root / 'fired' ).exists (), 'the remote was never lost' )
4207+ self .assertNotEqual (result .returncode , 0 , result .stdout + result .stderr )
4208+ self .assertEqual (outputs , {})
4209+ self .assertNotIn ('::notice' , result .stdout )
4210+ gone .rename (self .origin )
4211+ self .assertEqual (self ._origin ('rev-parse' , 'main' ), self .base )
4212+
4213+ def test_a_push_that_lands_but_reports_failure_fails_the_step (self ) -> None :
4214+ """``main`` moved, but to the bump itself: the push landed though git
4215+ said it did not, as when the connection drops after the remote updates
4216+ the ref. Calling that a race would report ``pushed=false`` and green
4217+ for a bump nothing then tags, so it fails, as git reported.
4218+
4219+ """
4220+ clone = self ._clone ('first' )
4221+ env = self ._after_first ('push' , "echo 'fatal: the remote end hung up unexpectedly' >&2"
4222+ '; exit 128' )
4223+ self ._bump (clone , {0 : 'package0==2.0' })
4224+ result , outputs = self ._run (clone , ** env )
4225+ self .assertTrue ((self .root / 'fired' ).exists (), 'the push never ran' )
4226+ self .assertEqual (result .returncode , 128 , result .stdout + result .stderr )
4227+ self .assertEqual (outputs , {})
4228+ self .assertNotIn ('::notice' , result .stdout )
4229+ self .assertEqual (self ._origin ('rev-parse' , 'main' ), self ._git (clone , 'rev-parse' , 'HEAD' ))
4230+ self .assertEqual (self ._origin ('rev-parse' , 'main^' ), self .base )
4231+
4232+ def test_a_push_that_lands_under_a_later_merge_but_reports_failure_fails_the_step (
4233+ self ) -> None :
4234+ """The test above with a merge landing over the bump before the step
4235+ looks: ``main`` is at a third commit, but one with the bump in its
4236+ history, so this is no race either.
4237+
4238+ """
4239+ clone = self ._clone ('first' )
4240+ merger = self ._stage ('merge' , {'pcapkit/toolkit/scapy.py' : 'fix\n ' },
4241+ 'fix(toolkit): a merge' , (self .HUMAN , self .GITHUB ))
4242+ git = f'"$git" -C { shlex .quote (str (merger ))} '
4243+ env = self ._after_first ('push' , f'{ git } pull -q --rebase origin main'
4244+ f' && { git } push -q origin HEAD:refs/heads/main'
4245+ "; echo 'fatal: the remote end hung up unexpectedly' >&2"
4246+ '; exit 128' )
4247+ self ._bump (clone , {0 : 'package0==2.0' })
4248+ result , outputs = self ._run (clone , ** env )
4249+ self .assertTrue ((self .root / 'fired' ).exists (), 'the push never ran' )
4250+ self .assertEqual (result .returncode , 128 , result .stdout + result .stderr )
4251+ self .assertEqual (outputs , {})
4252+ self .assertNotIn ('::notice' , result .stdout )
4253+ self .assertEqual (self ._origin ('rev-parse' , 'main' ), self ._git (merger , 'rev-parse' , 'HEAD' ))
4254+ self .assertEqual (self ._origin ('rev-parse' , 'main^' ), self ._git (clone , 'rev-parse' , 'HEAD' ),
4255+ 'the merge did not land over the bump' )
4256+
41634257 def test_a_rule_that_cannot_answer_fails_the_step (self ) -> None :
41644258 """No sha to compare is a broken run, not a stale one: the step fails,
41654259 writes nothing, and pushes nothing.
0 commit comments