Check git mode on Windows

This commit is contained in:
Max Rozentsveyg 2020-05-16 23:59:08 -04:00 committed by Anthony Sottile
parent 3d379a962d
commit 5195ba3449
2 changed files with 124 additions and 20 deletions

View file

@ -2,26 +2,60 @@
import argparse
import shlex
import sys
from typing import List
from typing import Optional
from typing import Sequence
from typing import Set
from pre_commit_hooks.util import cmd_output
EXECUTABLE_VALUES = frozenset(('1', '3', '5', '7'))
def check_has_shebang(path: str) -> int:
def check_executables(paths: List[str]) -> int:
if sys.platform == 'win32': # pragma: win32 cover
return _check_git_filemode(paths)
else: # pragma: win32 no cover
retv = 0
for path in paths:
if not _check_has_shebang(path):
_message(path)
retv = 1
return retv
def _check_git_filemode(paths: Sequence[str]) -> int:
outs = cmd_output('git', 'ls-files', '--stage', '--', *paths)
seen: Set[str] = set()
for out in outs.splitlines():
metadata, path = out.split('\t')
tagmode = metadata.split(' ', 1)[0]
is_executable = any(b in EXECUTABLE_VALUES for b in tagmode[-3:])
has_shebang = _check_has_shebang(path)
if is_executable and not has_shebang:
_message(path)
seen.add(path)
return int(bool(seen))
def _check_has_shebang(path: str) -> int:
with open(path, 'rb') as f:
first_bytes = f.read(2)
if first_bytes != b'#!':
quoted = shlex.quote(path)
print(
f'{path}: marked executable but has no (or invalid) shebang!\n'
f" If it isn't supposed to be executable, try: "
f'`chmod -x {quoted}`\n'
f' If it is supposed to be executable, double-check its shebang.',
file=sys.stderr,
)
return 1
else:
return 0
return first_bytes == b'#!'
def _message(path: str) -> None:
print(
f'{path}: marked executable but has no (or invalid) shebang!\n'
f" If it isn't supposed to be executable, try: "
f'`chmod -x {shlex.quote(path)}`\n'
f' If it is supposed to be executable, double-check its shebang.',
file=sys.stderr,
)
def main(argv: Optional[Sequence[str]] = None) -> int:
@ -29,12 +63,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
parser.add_argument('filenames', nargs='*')
args = parser.parse_args(argv)
retv = 0
for filename in args.filenames:
retv |= check_has_shebang(filename)
return retv
return check_executables(args.filenames)
if __name__ == '__main__':