mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-03-31 18:46:53 +00:00
Support checking executable bit without Git.
The check-shebang-scripts-are-executable hook already avoided false negatives in a Git repository by looking up the Git file mode rather than relying on the file mode in the file system. Git already automatically probes the file system for executable bit support. Use the file mode in the file system when we are not in a Git clone or it is trusted by Git according to its core.fileMode config variable.
This commit is contained in:
parent
9245e07a3e
commit
314fa5366d
2 changed files with 80 additions and 4 deletions
|
|
@ -2,6 +2,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import shlex
|
||||
import sys
|
||||
from typing import Sequence
|
||||
|
|
@ -9,13 +10,30 @@ from typing import Sequence
|
|||
from pre_commit_hooks.check_executables_have_shebangs import EXECUTABLE_VALUES
|
||||
from pre_commit_hooks.check_executables_have_shebangs import git_ls_files
|
||||
from pre_commit_hooks.check_executables_have_shebangs import has_shebang
|
||||
from pre_commit_hooks.util import cmd_output
|
||||
|
||||
|
||||
def check_shebangs(paths: list[str]) -> int:
|
||||
# Cannot optimize on non-executability here if we intend this check to
|
||||
# work on win32 -- and that's where problems caused by non-executability
|
||||
# (elsewhere) are most likely to arise from.
|
||||
return _check_git_filemode(paths)
|
||||
fs_tracks_executable_bit = cmd_output(
|
||||
'git', 'config', 'core.fileMode', retcode=None,
|
||||
).strip()
|
||||
return (
|
||||
_check_git_filemode(paths)
|
||||
if fs_tracks_executable_bit == 'false'
|
||||
else _check_fs_filemode(paths)
|
||||
)
|
||||
|
||||
|
||||
def _check_fs_filemode(
|
||||
paths: list[str],
|
||||
) -> int: # pragma: win32 no cover
|
||||
retv = 0
|
||||
for path in paths:
|
||||
if not os.access(path, os.X_OK) and has_shebang(path):
|
||||
_message(path)
|
||||
retv = 1
|
||||
|
||||
return retv
|
||||
|
||||
|
||||
def _check_git_filemode(paths: Sequence[str]) -> int:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue