Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
35 changes: 32 additions & 3 deletions pre_commit_hooks/check_shebang_scripts_are_executable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
42 changes: 42 additions & 0 deletions tests/check_shebang_scripts_are_executable_test.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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([])
Loading