From 4729918bc9e5bf7c409b04e111f8e416f351f3aa Mon Sep 17 00:00:00 2001 From: jack1142 <6032823+jack1142@users.noreply.github.com> Date: Tue, 6 Apr 2021 20:07:08 +0200 Subject: [PATCH] Disallow any branch name in checks_vcs_permalinks --- pre_commit_hooks/check_vcs_permalinks.py | 5 ++++- tests/check_vcs_permalinks_test.py | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pre_commit_hooks/check_vcs_permalinks.py b/pre_commit_hooks/check_vcs_permalinks.py index a30277ce..5231d7af 100644 --- a/pre_commit_hooks/check_vcs_permalinks.py +++ b/pre_commit_hooks/check_vcs_permalinks.py @@ -8,7 +8,10 @@ def _get_pattern(domain: str) -> Pattern[bytes]: - regex = rf'https://{domain}/[^/ ]+/[^/ ]+/blob/master/[^# ]+#L\d+' + regex = ( + rf'https://{domain}/[^/ ]+/[^/ ]+/blob/' + r'(?![a-fA-F0-9]{4,64}/)([^/. ]+)/[^# ]+#L\d+' + ) return re.compile(regex.encode()) diff --git a/tests/check_vcs_permalinks_test.py b/tests/check_vcs_permalinks_test.py index 7d5f86c2..ad591515 100644 --- a/tests/check_vcs_permalinks_test.py +++ b/tests/check_vcs_permalinks_test.py @@ -11,6 +11,8 @@ def test_passing(tmpdir): f.write_binary( # permalinks are ok b'https://github.com/asottile/test/blob/649e6/foo%20bar#L1\n' + # tags are ok + b'https://github.com/asottile/test/blob/1.0.0/foo%20bar#L1\n' # links to files but not line numbers are ok b'https://github.com/asottile/test/blob/master/foo%20bar\n' # regression test for overly-greedy regex @@ -23,7 +25,8 @@ def test_failing(tmpdir, capsys): with tmpdir.as_cwd(): tmpdir.join('f.txt').write_binary( b'https://github.com/asottile/test/blob/master/foo#L1\n' - b'https://example.com/asottile/test/blob/master/foo#L1\n', + b'https://example.com/asottile/test/blob/master/foo#L1\n' + b'https://example.com/asottile/test/blob/main/foo#L1\n', ) assert main(('f.txt', '--additional-github-domain', 'example.com')) @@ -31,6 +34,7 @@ def test_failing(tmpdir, capsys): assert out == ( 'f.txt:1:https://github.com/asottile/test/blob/master/foo#L1\n' 'f.txt:2:https://example.com/asottile/test/blob/master/foo#L1\n' + 'f.txt:3:https://example.com/asottile/test/blob/main/foo#L1\n' '\n' 'Non-permanent github link detected.\n' 'On any page on github press [y] to load a permalink.\n'