From db119ea776cf400dfe867ae017599d18ffb14222 Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Tue, 11 Aug 2020 13:55:55 +0200 Subject: [PATCH] Adjust the branch reference fixer's logic - change references to temporary branches (e.g., "release-*" and "publish") to point to the 'main' branch - add --branch=BRANCH_NAME option to the nox session so that one can pass in a target branch to make all references point to - run "fix-branch-references" as the first pre-commit hook as it fails the fastest - bug fix: allow dots in branch references (e.g., "release-0.1.0") --- .pre-commit-config.yaml | 12 ++++++------ noxfile.py | 30 ++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 987b01d..828e482 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,6 +4,12 @@ repos: # Run the local formatting, linting, and testing tool chains. - repo: local hooks: + - id: local-fix-branch-references + name: Check for wrong branch references + entry: poetry run nox -s fix-branch-references -- + language: system + stages: [commit] + types: [text] - id: local-format name: Format the source files entry: poetry run nox -s format -- @@ -16,12 +22,6 @@ repos: language: system stages: [commit] types: [python] - - id: local-fix-branch-references - name: Adjust the branch references - entry: poetry run nox -s fix-branch-references -- - language: system - stages: [commit] - types: [text] - id: local-test-suite name: Run the entire test suite entry: poetry run nox -s test-suite -- diff --git a/noxfile.py b/noxfile.py index 49e49e0..06cdd7f 100644 --- a/noxfile.py +++ b/noxfile.py @@ -328,7 +328,7 @@ def test_suite(session): @nox.session(name='fix-branch-references', python=PYTHON, venv_backend='none') -def fix_branch_references(_): # noqa:WPS210 +def fix_branch_references(session): # noqa:WPS210 """Replace branch references with the current branch. Intended to be run as a pre-commit hook. @@ -337,12 +337,20 @@ def fix_branch_references(_): # noqa:WPS210 on github.com or nbviewer.jupyter.org that contain branch labels. This task rewrites these links such that they contain the branch reference - of the current branch. + of the current branch. If the branch is only a temporary one that is to be + merged into the 'main' branch, all references are adjusted to 'main' as well. + + This task may be called with one positional argument that is interpreted + as the branch to which all references are changed into. + The format must be "--branch=BRANCH_NAME". """ # Adjust this to add/remove glob patterns # whose links are re-written. paths = ['*.md', '**/*.md', '**/*.ipynb'] + # Get the branch git is currently on. + # This is the branch to which all references are changed into + # if none of the two exceptions below apply. branch = ( subprocess.check_output( # noqa:S603 ('git', 'rev-parse', '--abbrev-ref', 'HEAD'), @@ -350,19 +358,33 @@ def fix_branch_references(_): # noqa:WPS210 .decode() .strip() ) + # If the current branch is only a temporary one that is to be merged + # into 'main', we adjust all branch references to 'main' as well. + if branch.startswith('release') or branch.startswith('research'): + branch = 'main' + # If a "--branch=BRANCH_NAME" argument is passed in + # as the only positional argument, we use BRANCH_NAME. + # Note: The --branch is required as session.posargs contains + # the staged files passed in by pre-commit in most cases. + if session.posargs and len(session.posargs) == 1: + match = re.match( + pattern=r'^--branch=([\w\.-]+)$', string=session.posargs[0].strip(), + ) + if match: + branch = match.groups()[0] rewrites = [ { 'name': 'github', 'pattern': re.compile( - fr'((((http)|(https))://github\.com/{GITHUB_REPOSITORY}/((blob)|(tree))/)([\w-]+)/)', # noqa:E501 + fr'((((http)|(https))://github\.com/{GITHUB_REPOSITORY}/((blob)|(tree))/)([\w\.-]+)/)', # noqa:E501 ), 'replacement': fr'\2{branch}/', }, { 'name': 'nbviewer', 'pattern': re.compile( - fr'((((http)|(https))://nbviewer\.jupyter\.org/github/{GITHUB_REPOSITORY}/((blob)|(tree))/)([\w-]+)/)', # noqa:E501 + fr'((((http)|(https))://nbviewer\.jupyter\.org/github/{GITHUB_REPOSITORY}/((blob)|(tree))/)([\w\.-]+)/)', # noqa:E501 ), 'replacement': fr'\2{branch}/', },