mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-04 19:26:52 +00:00
Merge pull request #530 from youngminz/master
Add --additional-domain to check-vcs-permalinks
This commit is contained in:
commit
14e9f0e512
3 changed files with 33 additions and 12 deletions
|
|
@ -66,6 +66,10 @@ Attempts to load all TOML files to verify syntax.
|
||||||
|
|
||||||
#### `check-vcs-permalinks`
|
#### `check-vcs-permalinks`
|
||||||
Ensures that links to vcs websites are 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`
|
#### `check-xml`
|
||||||
Attempts to load all xml files to verify syntax.
|
Attempts to load all xml files to verify syntax.
|
||||||
|
|
|
||||||
|
|
@ -1,35 +1,50 @@
|
||||||
import argparse
|
import argparse
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
from typing import List
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
from typing import Pattern
|
||||||
from typing import Sequence
|
from typing import Sequence
|
||||||
|
|
||||||
|
|
||||||
GITHUB_NON_PERMALINK = re.compile(
|
def _get_pattern(domain: str) -> Pattern[bytes]:
|
||||||
br'https://github.com/[^/ ]+/[^/ ]+/blob/master/[^# ]+#L\d+',
|
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
|
retv = 0
|
||||||
with open(filename, 'rb') as f:
|
with open(filename, 'rb') as f:
|
||||||
for i, line in enumerate(f, 1):
|
for i, line in enumerate(f, 1):
|
||||||
if GITHUB_NON_PERMALINK.search(line):
|
for pattern in patterns:
|
||||||
sys.stdout.write(f'{filename}:{i}:')
|
if pattern.search(line):
|
||||||
sys.stdout.flush()
|
sys.stdout.write(f'{filename}:{i}:')
|
||||||
sys.stdout.buffer.write(line)
|
sys.stdout.flush()
|
||||||
retv = 1
|
sys.stdout.buffer.write(line)
|
||||||
|
retv = 1
|
||||||
return retv
|
return retv
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='*')
|
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)
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
|
patterns = [
|
||||||
|
_get_pattern(domain)
|
||||||
|
for domain in args.additional_github_domains
|
||||||
|
]
|
||||||
|
|
||||||
retv = 0
|
retv = 0
|
||||||
|
|
||||||
for filename in args.filenames:
|
for filename in args.filenames:
|
||||||
retv |= _check_filename(filename)
|
retv |= _check_filename(filename, patterns)
|
||||||
|
|
||||||
if retv:
|
if retv:
|
||||||
print()
|
print()
|
||||||
|
|
|
||||||
|
|
@ -22,13 +22,15 @@ def test_passing(tmpdir):
|
||||||
def test_failing(tmpdir, capsys):
|
def test_failing(tmpdir, capsys):
|
||||||
with tmpdir.as_cwd():
|
with tmpdir.as_cwd():
|
||||||
tmpdir.join('f.txt').write_binary(
|
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()
|
out, _ = capsys.readouterr()
|
||||||
assert out == (
|
assert out == (
|
||||||
'f.txt:1:https://github.com/asottile/test/blob/master/foo#L1\n'
|
'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'
|
'\n'
|
||||||
'Non-permanent github link detected.\n'
|
'Non-permanent github link detected.\n'
|
||||||
'On any page on github press [y] to load a permalink.\n'
|
'On any page on github press [y] to load a permalink.\n'
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue