refactor: centralize pre-commit file filtering

This commit is contained in:
Peter Steinberger
2026-02-16 03:35:56 +01:00
parent 91c49dd0ea
commit c1655982d4
5 changed files with 119 additions and 22 deletions

View File

@@ -2,7 +2,21 @@
set -euo pipefail
# Security: avoid option-injection from malicious file names (e.g. "--force").
ROOT_DIR="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
RUN_NODE_TOOL="$ROOT_DIR/scripts/pre-commit/run-node-tool.sh"
FILTER_FILES="$ROOT_DIR/scripts/pre-commit/filter-staged-files.mjs"
if [[ ! -x "$RUN_NODE_TOOL" ]]; then
echo "Missing helper: $RUN_NODE_TOOL" >&2
exit 1
fi
if [[ ! -f "$FILTER_FILES" ]]; then
echo "Missing helper: $FILTER_FILES" >&2
exit 1
fi
# Security: avoid option-injection from malicious file names (e.g. "--all", "--force").
# Robustness: NUL-delimited file list handles spaces/newlines safely.
mapfile -d '' -t files < <(git diff --cached --name-only --diff-filter=ACMR -z)
@@ -10,24 +24,15 @@ if [ "${#files[@]}" -eq 0 ]; then
exit 0
fi
lint_files=()
format_files=()
for file in "${files[@]}"; do
case "$file" in
*.ts | *.tsx | *.js | *.jsx | *.mjs | *.cjs) lint_files+=("$file") ;;
esac
case "$file" in
*.ts | *.tsx | *.js | *.jsx | *.mjs | *.cjs | *.json | *.md | *.mdx) format_files+=("$file") ;;
esac
done
mapfile -d '' -t lint_files < <(node "$FILTER_FILES" lint -- "${files[@]}")
mapfile -d '' -t format_files < <(node "$FILTER_FILES" format -- "${files[@]}")
if [ "${#lint_files[@]}" -gt 0 ]; then
pnpm lint --fix -- "${lint_files[@]}"
"$RUN_NODE_TOOL" oxlint --type-aware --fix -- "${lint_files[@]}"
fi
if [ "${#format_files[@]}" -gt 0 ]; then
pnpm format -- "${format_files[@]}"
"$RUN_NODE_TOOL" oxfmt --write -- "${format_files[@]}"
fi
git add -- "${files[@]}"