From 24c39ec653185c53642006266e6c29917c1b0cca Mon Sep 17 00:00:00 2001 From: MentalOfCrow <145018989+MentalOfCrow@users.noreply.github.com> Date: Mon, 30 Nov 2026 19:54:46 +0100 Subject: [PATCH] Check shebang executable modes outside Git repositories --- README.md | 2 + .../check_shebang_scripts_are_executable.py | 35 ++++++++++++++-- ...eck_shebang_scripts_are_executable_test.py | 42 +++++++++++++++++++ 3 files changed, 76 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8432455f..10faa486 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,8 @@ Check for files that contain merge conflict strings. #### `check-shebang-scripts-are-executable` Checks that scripts with shebangs are executable. +Inside a Git repository, it checks the mode recorded in the index. +On POSIX systems it can also check explicitly named files outside a repository. #### `check-symlinks` Checks for symlinks which do not point to anything. diff --git a/pre_commit_hooks/check_shebang_scripts_are_executable.py b/pre_commit_hooks/check_shebang_scripts_are_executable.py index 937425b0..03901c61 100644 --- a/pre_commit_hooks/check_shebang_scripts_are_executable.py +++ b/pre_commit_hooks/check_shebang_scripts_are_executable.py @@ -2,22 +2,51 @@ from __future__ import annotations import argparse +import os import shlex +import stat import sys from collections.abc 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 CalledProcessError +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. + if sys.platform != 'win32': # pragma: win32 no cover + try: + cmd_output( + 'git', 'rev-parse', '--git-dir', + env=dict(os.environ, LC_ALL='C'), + ) + except CalledProcessError as e: + _, _, _, _, stderr = e.args + if not stderr.startswith(b'fatal: not a git repository ('): + raise + return _check_fs_filemode(paths) + + # The Git index records the intended mode even on Windows, where + # the filesystem doesn't track the POSIX executable bit. return _check_git_filemode(paths) +def _check_fs_filemode(paths: Sequence[str]) -> int: # pragma: win32 no cover + retv = 0 + for path in paths: + mode = os.stat(path).st_mode + if ( + stat.S_ISREG(mode) and + not mode & (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) and + has_shebang(path) + ): + _message(path) + retv = 1 + return retv + + def _check_git_filemode(paths: Sequence[str]) -> int: seen: set[str] = set() for ls_file in git_ls_files(paths): diff --git a/tests/check_shebang_scripts_are_executable_test.py b/tests/check_shebang_scripts_are_executable_test.py index e4bd07ca..f303d97e 100644 --- a/tests/check_shebang_scripts_are_executable_test.py +++ b/tests/check_shebang_scripts_are_executable_test.py @@ -1,12 +1,14 @@ from __future__ import annotations import os +import sys import pytest from pre_commit_hooks.check_shebang_scripts_are_executable import \ _check_git_filemode from pre_commit_hooks.check_shebang_scripts_are_executable import main +from pre_commit_hooks.util import CalledProcessError from pre_commit_hooks.util import cmd_output @@ -87,3 +89,43 @@ def test_git_executable_shebang(temp_git_dir, content, mode, expected): filenames = [path for path in [str(path)] if os.access(path, os.X_OK)] assert main(filenames) == expected + + +@pytest.mark.skipif(sys.platform == 'win32', reason='requires POSIX modes') +@pytest.mark.parametrize( + ('content', 'mode', 'expected'), + ( + pytest.param('#!python', 0o755, 0, id='executable shebang'), + pytest.param('#!python', 0o644, 1, id='non-executable shebang'), + pytest.param('plain text', 0o644, 0, id='no shebang'), + ), +) +def test_shebang_outside_git_repository( + tmp_path, monkeypatch, content, mode, expected, +): + monkeypatch.chdir(tmp_path) + path = tmp_path / 'script' + path.write_text(content) + path.chmod(mode) + + assert main([path.name]) == expected + + +@pytest.mark.skipif(sys.platform == 'win32', reason='requires POSIX modes') +def test_shebang_in_git_uses_index_mode(temp_git_dir): + with temp_git_dir.as_cwd(): + path = temp_git_dir.join('script') + path.write('#!python') + os.chmod(str(path), 0o755) + cmd_output('git', 'add', '--chmod=-x', 'script') + + assert main(['script']) == 1 + + +@pytest.mark.skipif(sys.platform == 'win32', reason='uses POSIX fallback') +def test_shebang_propagates_other_git_errors(tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + monkeypatch.setenv('GIT_DIR', str(tmp_path / 'missing')) + + with pytest.raises(CalledProcessError): + main([])