Skip to content

Commit

Permalink
Merge pull request #530 from youngminz/master
Browse files Browse the repository at this point in the history
Add --additional-domain to check-vcs-permalinks
  • Loading branch information
asottile authored Nov 18, 2020
2 parents 2323ac0 + 1f8151a commit 14e9f0e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ Attempts to load all TOML files to verify syntax.

#### `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.
Expand Down
35 changes: 25 additions & 10 deletions pre_commit_hooks/check_vcs_permalinks.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
6 changes: 4 additions & 2 deletions tests/check_vcs_permalinks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ def test_passing(tmpdir):
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'
Expand Down

0 comments on commit 14e9f0e

Please sign in to comment.