Skip to content

Commit 1f8151a

Browse files
youngminzasottile
authored andcommitted
Add --additional-github-domain to check-vcs-permalinks
1 parent 2323ac0 commit 1f8151a

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ Attempts to load all TOML files to verify syntax.
6666

6767
#### `check-vcs-permalinks`
6868
Ensures that links to vcs websites are permalinks.
69+
- `--additional-github-domain DOMAIN` - Add check for specified domain.
70+
Can be repeated multiple times. for example, if your company uses
71+
GitHub Enterprise you may use something like
72+
`--additional-github-domain github.example.com`
6973

7074
#### `check-xml`
7175
Attempts to load all xml files to verify syntax.

pre_commit_hooks/check_vcs_permalinks.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,50 @@
11
import argparse
22
import re
33
import sys
4+
from typing import List
45
from typing import Optional
6+
from typing import Pattern
57
from typing import Sequence
68

79

8-
GITHUB_NON_PERMALINK = re.compile(
9-
br'https://github.com/[^/ ]+/[^/ ]+/blob/master/[^# ]+#L\d+',
10-
)
10+
def _get_pattern(domain: str) -> Pattern[bytes]:
11+
regex = rf'https://{domain}/[^/ ]+/[^/ ]+/blob/master/[^# ]+#L\d+'
12+
return re.compile(regex.encode())
1113

1214

13-
def _check_filename(filename: str) -> int:
15+
def _check_filename(filename: str, patterns: List[Pattern[bytes]]) -> int:
1416
retv = 0
1517
with open(filename, 'rb') as f:
1618
for i, line in enumerate(f, 1):
17-
if GITHUB_NON_PERMALINK.search(line):
18-
sys.stdout.write(f'{filename}:{i}:')
19-
sys.stdout.flush()
20-
sys.stdout.buffer.write(line)
21-
retv = 1
19+
for pattern in patterns:
20+
if pattern.search(line):
21+
sys.stdout.write(f'{filename}:{i}:')
22+
sys.stdout.flush()
23+
sys.stdout.buffer.write(line)
24+
retv = 1
2225
return retv
2326

2427

2528
def main(argv: Optional[Sequence[str]] = None) -> int:
2629
parser = argparse.ArgumentParser()
2730
parser.add_argument('filenames', nargs='*')
31+
parser.add_argument(
32+
'--additional-github-domain',
33+
dest='additional_github_domains',
34+
action='append',
35+
default=['github.com'],
36+
)
2837
args = parser.parse_args(argv)
2938

39+
patterns = [
40+
_get_pattern(domain)
41+
for domain in args.additional_github_domains
42+
]
43+
3044
retv = 0
45+
3146
for filename in args.filenames:
32-
retv |= _check_filename(filename)
47+
retv |= _check_filename(filename, patterns)
3348

3449
if retv:
3550
print()

tests/check_vcs_permalinks_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ def test_passing(tmpdir):
2222
def test_failing(tmpdir, capsys):
2323
with tmpdir.as_cwd():
2424
tmpdir.join('f.txt').write_binary(
25-
b'https://github.com/asottile/test/blob/master/foo#L1\n',
25+
b'https://github.com/asottile/test/blob/master/foo#L1\n'
26+
b'https://example.com/asottile/test/blob/master/foo#L1\n',
2627
)
2728

28-
assert main(('f.txt',))
29+
assert main(('f.txt', '--additional-github-domain', 'example.com'))
2930
out, _ = capsys.readouterr()
3031
assert out == (
3132
'f.txt:1:https://github.com/asottile/test/blob/master/foo#L1\n'
33+
'f.txt:2:https://example.com/asottile/test/blob/master/foo#L1\n'
3234
'\n'
3335
'Non-permanent github link detected.\n'
3436
'On any page on github press [y] to load a permalink.\n'

0 commit comments

Comments
 (0)