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")
This commit is contained in:
Alexander Hess 2020-08-11 13:55:55 +02:00
parent 79f0ddf0fe
commit db119ea776
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
2 changed files with 32 additions and 10 deletions

View file

@ -4,6 +4,12 @@ repos:
# Run the local formatting, linting, and testing tool chains. # Run the local formatting, linting, and testing tool chains.
- repo: local - repo: local
hooks: 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 - id: local-format
name: Format the source files name: Format the source files
entry: poetry run nox -s format -- entry: poetry run nox -s format --
@ -16,12 +22,6 @@ repos:
language: system language: system
stages: [commit] stages: [commit]
types: [python] 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 - id: local-test-suite
name: Run the entire test suite name: Run the entire test suite
entry: poetry run nox -s test-suite -- entry: poetry run nox -s test-suite --

View file

@ -328,7 +328,7 @@ def test_suite(session):
@nox.session(name='fix-branch-references', python=PYTHON, venv_backend='none') @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. """Replace branch references with the current branch.
Intended to be run as a pre-commit hook. 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. on github.com or nbviewer.jupyter.org that contain branch labels.
This task rewrites these links such that they contain the branch reference 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 # Adjust this to add/remove glob patterns
# whose links are re-written. # whose links are re-written.
paths = ['*.md', '**/*.md', '**/*.ipynb'] 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 = ( branch = (
subprocess.check_output( # noqa:S603 subprocess.check_output( # noqa:S603
('git', 'rev-parse', '--abbrev-ref', 'HEAD'), ('git', 'rev-parse', '--abbrev-ref', 'HEAD'),
@ -350,19 +358,33 @@ def fix_branch_references(_): # noqa:WPS210
.decode() .decode()
.strip() .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 = [ rewrites = [
{ {
'name': 'github', 'name': 'github',
'pattern': re.compile( '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}/', 'replacement': fr'\2{branch}/',
}, },
{ {
'name': 'nbviewer', 'name': 'nbviewer',
'pattern': re.compile( '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}/', 'replacement': fr'\2{branch}/',
}, },