Skip to content

Commit 0933175

Browse files
committed
refactor: add shebang check for scripts and hooks in ensure_executable_scripts
#345 (comment)
1 parent 795f0e2 commit 0933175

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

src/specify_cli/__init__.py

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

702702

703+
def _has_shebang(file_path: Path) -> bool:
704+
"""Check if file has shebang, handling UTF-8 BOM."""
705+
try:
706+
with file_path.open("rb") as f:
707+
head = f.read(5)
708+
# Skip UTF-8 BOM if present
709+
if head.startswith(b'\xef\xbb\xbf'):
710+
head = head[3:]
711+
return head.startswith(b"#!")
712+
except Exception:
713+
return False
714+
703715
def ensure_executable_scripts(project_path: Path, tracker: StepTracker | None = None) -> None:
704716
"""Ensure POSIX .sh scripts under .specify/scripts and hooks under .specify/hooks have execute bits (no-op on Windows)."""
705717
if os.name == "nt":
@@ -715,11 +727,7 @@ def ensure_executable_scripts(project_path: Path, tracker: StepTracker | None =
715727
try:
716728
if script.is_symlink() or not script.is_file():
717729
continue
718-
try:
719-
with script.open("rb") as f:
720-
if f.read(2) != b"#!":
721-
continue
722-
except Exception:
730+
if not _has_shebang(script):
723731
continue
724732
st = script.stat(); mode = st.st_mode
725733
if mode & 0o111:
@@ -743,11 +751,7 @@ def ensure_executable_scripts(project_path: Path, tracker: StepTracker | None =
743751
if (hook.is_symlink() or not hook.is_file() or
744752
hook.name == "README.md" or hook.name.endswith(".sample")):
745753
continue
746-
try:
747-
with hook.open("rb") as f:
748-
if f.read(2) != b"#!":
749-
continue
750-
except Exception:
754+
if not _has_shebang(hook):
751755
continue
752756
st = hook.stat()
753757
mode = st.st_mode

0 commit comments

Comments
 (0)