From 9d64075f5902052a83a722306239ac8d033b84b3 Mon Sep 17 00:00:00 2001 From: Konstantin Ryabitsev Date: Wed, 27 Jul 2022 13:08:28 -0400 Subject: ez: some cleanups and error handling for revision tagging Handle some of the potential errors that could come up when tagging historical versions. Signed-off-by: Konstantin Ryabitsev --- b4/ez.py | 95 ++++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 51 insertions(+), 44 deletions(-) diff --git a/b4/ez.py b/b4/ez.py index 7851929..b3d050c 100644 --- a/b4/ez.py +++ b/b4/ez.py @@ -952,53 +952,60 @@ def cmd_send(cmdargs: argparse.Namespace) -> None: logger.info('Sent %s messages', counter) mybranch = b4.git_get_current_branch() - if get_cover_strategy() == 'commit': - # Detach the head at our parent commit and apply the cover-less series - cover_commit = find_cover_commit() - gitargs = ['checkout', f'{cover_commit}~1'] - ecode, out = b4.git_run_command(None, gitargs) - if ecode > 0: - # TODO: do something - raise RuntimeError('Could not switch to a detached head') - # cherry-pick from cover letter to the last commit - last_commit = patches[-1][0] - gitargs = ['cherry-pick', f'{cover_commit}..{last_commit}'] - ecode, out = b4.git_run_command(None, gitargs) - if ecode > 0: - # TODO: do something - raise RuntimeError('Could not cherry-pick the cover-less range') - # Find out the head commit - gitargs = ['rev-parse', 'HEAD'] - ecode, out = b4.git_run_command(None, gitargs) - if ecode > 0: - # TODO: do something - raise RuntimeError('Could not find the HEAD commit of the detached head') - tagcommit = out.strip() - # Switch back to our branch - gitargs = ['checkout', mybranch] + revision = tracking['series']['revision'] + + try: + if get_cover_strategy() == 'commit': + # Detach the head at our parent commit and apply the cover-less series + cover_commit = find_cover_commit() + gitargs = ['checkout', f'{cover_commit}~1'] + ecode, out = b4.git_run_command(None, gitargs) + if ecode > 0: + raise RuntimeError('Could not switch to a detached head') + # cherry-pick from cover letter to the last commit + last_commit = patches[-1][0] + gitargs = ['cherry-pick', f'{cover_commit}..{last_commit}'] + ecode, out = b4.git_run_command(None, gitargs) + if ecode > 0: + raise RuntimeError('Could not cherry-pick the cover-less range') + # Find out the head commit + gitargs = ['rev-parse', 'HEAD'] + ecode, out = b4.git_run_command(None, gitargs) + if ecode > 0: + raise RuntimeError('Could not find the HEAD commit of the detached head') + tagcommit = out.strip() + # Switch back to our branch + gitargs = ['checkout', mybranch] + ecode, out = b4.git_run_command(None, gitargs) + if ecode > 0: + raise RuntimeError('Could not switch back to %s', mybranch) + else: + # TODO: commit-tip will have HEAD~1 + tagcommit = 'HEAD' + + # TODO: make sent/ prefix configurable? + tagprefix = 'sent/' + if mybranch.startswith('b4/'): + tagname = f'{tagprefix}{mybranch[3:]}-v{revision}' + else: + tagname = f'{tagprefix}{mybranch}-v{revision}' + + logger.debug('checking if we already have %s', tagname) + gitargs = ['rev-parse', f'refs/tags/{tagname}'] ecode, out = b4.git_run_command(None, gitargs) if ecode > 0: - # TODO: do something - raise RuntimeError('Could not switch back to %s', mybranch) - else: - # TODO: commit-tip will have HEAD~1 - tagcommit = 'HEAD' - - # TODO: make sent/ prefix configurable? - revision = tracking['series']['revision'] - tagprefix = 'sent/' - if mybranch.startswith('b4/'): - tagname = f'{tagprefix}{mybranch[3:]}-v{revision}' - else: - tagname = f'{tagprefix}{mybranch}-v{revision}' + logger.info('Tagging %s', tagname) + gitargs = ['tag', '-a', '-F', '-', tagname, tagcommit] + ecode, out = b4.git_run_command(None, gitargs, stdin=cover.encode()) + if ecode > 0: + # Not a fatal error, just complain about it + logger.info('Could not tag %s as %s:', tagcommit, tagname) + logger.info(out) + else: + logger.info('NOTE: Tagname %s already exists', tagname) - # TODO: check if we already have this tag for some reason - logger.info('Tagging %s', tagname) - gitargs = ['tag', '-a', '-F', '-', tagname, tagcommit] - ecode, out = b4.git_run_command(None, gitargs, stdin=cover.encode()) - if ecode > 0: - # TODO: do something - raise RuntimeError('Could not tag %s as %s', tagcommit, tagname) + except RuntimeError as ex: + logger.critical('Error tagging the revision: %s', ex) if not cover_msgid: return -- cgit v1.2.3