Add --additional-github-domain to check-vcs-permalinks
diff --git a/README.md b/README.md index 24d716f..2a76268 100644 --- a/README.md +++ b/README.md
@@ -66,6 +66,10 @@ #### `check-vcs-permalinks` Ensures that links to vcs websites are permalinks. + - `--additional-github-domain DOMAIN` - Add check for specified domain. + Can be repeated multiple times. for example, if your company uses + GitHub Enterprise you may use something like + `--additional-github-domain github.example.com` #### `check-xml` Attempts to load all xml files to verify syntax.
diff --git a/pre_commit_hooks/check_vcs_permalinks.py b/pre_commit_hooks/check_vcs_permalinks.py index bf698e1..a30277c 100644 --- a/pre_commit_hooks/check_vcs_permalinks.py +++ b/pre_commit_hooks/check_vcs_permalinks.py
@@ -1,35 +1,50 @@ import argparse import re import sys +from typing import List from typing import Optional +from typing import Pattern from typing import Sequence -GITHUB_NON_PERMALINK = re.compile( - br'https://github.com/[^/ ]+/[^/ ]+/blob/master/[^# ]+#L\d+', -) +def _get_pattern(domain: str) -> Pattern[bytes]: + regex = rf'https://{domain}/[^/ ]+/[^/ ]+/blob/master/[^# ]+#L\d+' + return re.compile(regex.encode()) -def _check_filename(filename: str) -> int: +def _check_filename(filename: str, patterns: List[Pattern[bytes]]) -> int: retv = 0 with open(filename, 'rb') as f: for i, line in enumerate(f, 1): - if GITHUB_NON_PERMALINK.search(line): - sys.stdout.write(f'{filename}:{i}:') - sys.stdout.flush() - sys.stdout.buffer.write(line) - retv = 1 + for pattern in patterns: + if pattern.search(line): + sys.stdout.write(f'{filename}:{i}:') + sys.stdout.flush() + sys.stdout.buffer.write(line) + retv = 1 return retv def main(argv: Optional[Sequence[str]] = None) -> int: parser = argparse.ArgumentParser() parser.add_argument('filenames', nargs='*') + parser.add_argument( + '--additional-github-domain', + dest='additional_github_domains', + action='append', + default=['github.com'], + ) args = parser.parse_args(argv) + patterns = [ + _get_pattern(domain) + for domain in args.additional_github_domains + ] + retv = 0 + for filename in args.filenames: - retv |= _check_filename(filename) + retv |= _check_filename(filename, patterns) if retv: print()
diff --git a/tests/check_vcs_permalinks_test.py b/tests/check_vcs_permalinks_test.py index 19b1c35..7d5f86c 100644 --- a/tests/check_vcs_permalinks_test.py +++ b/tests/check_vcs_permalinks_test.py
@@ -22,13 +22,15 @@ 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://github.com/asottile/test/blob/master/foo#L1\n' + b'https://example.com/asottile/test/blob/master/foo#L1\n', ) - assert main(('f.txt',)) + 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' '\n' 'Non-permanent github link detected.\n' 'On any page on github press [y] to load a permalink.\n'