Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow any branch name in checks_vcs_permalinks #582

Merged
merged 1 commit into from Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion pre_commit_hooks/check_vcs_permalinks.py
Expand Up @@ -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())


Expand Down
6 changes: 5 additions & 1 deletion tests/check_vcs_permalinks_test.py
Expand Up @@ -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
Expand All @@ -23,14 +25,16 @@ 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'))
out, _ = capsys.readouterr()
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'
Expand Down