Skip to content

Commit 4499e14

Browse files
committed
refactor: add shebang check for scripts and hooks in ensure_executable_scripts
github#345 (comment)
1 parent c7800f4 commit 4499e14

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/specify_cli/__init__.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,18 @@ def download_and_extract_template(project_path: Path, ai_assistant: str, script_
676676
return project_path
677677

678678

679+
def _has_shebang(file_path: Path) -> bool:
680+
"""Check if file has shebang, handling UTF-8 BOM."""
681+
try:
682+
with file_path.open("rb") as f:
683+
head = f.read(5)
684+
# Skip UTF-8 BOM if present
685+
if head.startswith(b'\xef\xbb\xbf'):
686+
head = head[3:]
687+
return head.startswith(b"#!")
688+
except Exception:
689+
return False
690+
679691
def ensure_executable_scripts(project_path: Path, tracker: StepTracker | None = None) -> None:
680692
"""Ensure POSIX .sh scripts under .specify/scripts and hooks under .specify/hooks have execute bits (no-op on Windows)."""
681693
if os.name == "nt":
@@ -691,11 +703,7 @@ def ensure_executable_scripts(project_path: Path, tracker: StepTracker | None =
691703
try:
692704
if script.is_symlink() or not script.is_file():
693705
continue
694-
try:
695-
with script.open("rb") as f:
696-
if f.read(2) != b"#!":
697-
continue
698-
except Exception:
706+
if not _has_shebang(script):
699707
continue
700708
st = script.stat(); mode = st.st_mode
701709
if mode & 0o111:
@@ -719,11 +727,7 @@ def ensure_executable_scripts(project_path: Path, tracker: StepTracker | None =
719727
if (hook.is_symlink() or not hook.is_file() or
720728
hook.name == "README.md" or hook.name.endswith(".sample")):
721729
continue
722-
try:
723-
with hook.open("rb") as f:
724-
if f.read(2) != b"#!":
725-
continue
726-
except Exception:
730+
if not _has_shebang(hook):
727731
continue
728732
st = hook.stat()
729733
mode = st.st_mode

0 commit comments

Comments
 (0)