mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-28 16:09:14 +01:00
feat: add auto-fix to pre-commit hook with --skip-fmt option
This commit is contained in:
parent
c1b1282d64
commit
2b83adb0f6
@ -5,11 +5,40 @@ set -euo pipefail
|
||||
repo_root=$(git rev-parse --show-toplevel)
|
||||
cd "$repo_root"
|
||||
|
||||
printf 'Running shell_check before committing...\n'
|
||||
printf 'Running auto-fixers and shell_check before committing...\n'
|
||||
|
||||
if ! scripts/meta/shell_check.sh --skip-install; then
|
||||
printf '\nCommit aborted: shell_check reported issues.\n' >&2
|
||||
printf 'Fix the lint problems and retry the commit.\n' >&2
|
||||
# Get list of staged shell files
|
||||
mapfile -t staged_files < <(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(sh|bash|zsh)$' || true)
|
||||
|
||||
if [[ ${#staged_files[@]} -gt 0 ]]; then
|
||||
printf 'Auto-fixing %d shell file(s)...\n' "${#staged_files[@]}"
|
||||
|
||||
# Auto-fix with shfmt if available
|
||||
if command -v shfmt > /dev/null 2>&1; then
|
||||
printf ' → Running shfmt...\n'
|
||||
for file in "${staged_files[@]}"; do
|
||||
if [[ -f $file ]]; then
|
||||
shfmt -w "$file" 2> /dev/null || true
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
# Re-stage the auto-fixed files
|
||||
for file in "${staged_files[@]}"; do
|
||||
if [[ -f $file ]]; then
|
||||
git add "$file"
|
||||
fi
|
||||
done
|
||||
|
||||
printf ' ✓ Auto-fixes applied and staged\n'
|
||||
fi
|
||||
|
||||
printf 'Running shell_check validation (shellcheck only, skip formatting check)...\n'
|
||||
|
||||
# Run shell_check but only check for actual errors (shellcheck, syntax), not formatting
|
||||
if ! scripts/meta/shell_check.sh --skip-install --skip-fmt 2>&1; then
|
||||
printf '\nCommit aborted: shell_check reported issues that cannot be auto-fixed.\n' >&2
|
||||
printf 'Fix the remaining problems and retry the commit.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
@ -23,6 +23,7 @@ ROOT_DIR="$DEFAULT_ROOT"
|
||||
SKIP_INSTALL="false"
|
||||
INSTALL_ONLY="false"
|
||||
LIST_ONLY="false"
|
||||
SKIP_FMT="false"
|
||||
VERBOSE="false"
|
||||
|
||||
log_info() {
|
||||
@ -38,7 +39,7 @@ log_error() {
|
||||
}
|
||||
|
||||
usage() {
|
||||
cat << EOF
|
||||
cat <<EOF
|
||||
Usage: $(basename "$0") [options]
|
||||
|
||||
Options:
|
||||
@ -46,6 +47,7 @@ Options:
|
||||
--skip-install Skip installing linters
|
||||
--install-only Only install linters, do not scan
|
||||
--list-only Only list discovered shell files, do not run linters
|
||||
--skip-fmt Skip shfmt formatting checks (useful after auto-fixing)
|
||||
--verbose Print additional details while running
|
||||
-h, --help Show this help
|
||||
|
||||
@ -74,6 +76,10 @@ while [[ $# -gt 0 ]]; do
|
||||
LIST_ONLY="true"
|
||||
shift
|
||||
;;
|
||||
--skip-fmt)
|
||||
SKIP_FMT="true"
|
||||
shift
|
||||
;;
|
||||
--verbose)
|
||||
VERBOSE="true"
|
||||
shift
|
||||
@ -95,7 +101,7 @@ if [[ ! -d $ROOT_DIR ]]; then
|
||||
exit 2
|
||||
fi
|
||||
|
||||
is_cmd() { command -v "$1" > /dev/null 2>&1; }
|
||||
is_cmd() { command -v "$1" >/dev/null 2>&1; }
|
||||
|
||||
is_arch() { is_cmd pacman; }
|
||||
have_aur_helper() { is_cmd yay || is_cmd paru; }
|
||||
@ -138,7 +144,7 @@ install_linters() {
|
||||
# checkbashisms may be in repos or AUR; try pacman first, then AUR helper
|
||||
if ! is_cmd checkbashisms; then
|
||||
if is_arch; then
|
||||
if ! sudo pacman -S --needed --noconfirm checkbashisms 2> /dev/null; then
|
||||
if ! sudo pacman -S --needed --noconfirm checkbashisms 2>/dev/null; then
|
||||
if have_aur_helper; then
|
||||
log_info "Installing checkbashisms from AUR (requires yay/paru)..."
|
||||
if is_cmd yay; then yay -S --noconfirm checkbashisms || true; fi
|
||||
@ -184,7 +190,7 @@ discover_shell_files() {
|
||||
local -a all
|
||||
all=()
|
||||
|
||||
if git -C "$base" rev-parse --is-inside-work-tree > /dev/null 2>&1; then
|
||||
if git -C "$base" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
||||
while IFS= read -r -d '' f; do all+=("$f"); done < <(git -C "$base" ls-files -z)
|
||||
while IFS= read -r -d '' f; do all+=("$f"); done < <(git -C "$base" ls-files --others --exclude-standard -z)
|
||||
else
|
||||
@ -217,7 +223,7 @@ discover_shell_files() {
|
||||
|
||||
# Check shebang
|
||||
local first
|
||||
first=$(head -n 1 -- "$abs" 2> /dev/null || true)
|
||||
first=$(head -n 1 -- "$abs" 2>/dev/null || true)
|
||||
if [[ $first =~ ^#! && $first =~ (ba|z|d|k)?sh ]]; then
|
||||
shells+=("$rel")
|
||||
continue
|
||||
@ -232,38 +238,38 @@ discover_shell_files() {
|
||||
done
|
||||
|
||||
# write lists
|
||||
: > "$REL_FILES_Z"
|
||||
: > "$ABS_FILES_Z"
|
||||
: >"$REL_FILES_Z"
|
||||
: >"$ABS_FILES_Z"
|
||||
for rel in "${shells[@]}"; do
|
||||
printf '%s\0' "$rel" >> "$REL_FILES_Z"
|
||||
printf '%s\0' "$base/$rel" >> "$ABS_FILES_Z"
|
||||
printf '%s\0' "$rel" >>"$REL_FILES_Z"
|
||||
printf '%s\0' "$base/$rel" >>"$ABS_FILES_Z"
|
||||
done
|
||||
}
|
||||
|
||||
print_file_list() {
|
||||
local count
|
||||
count=$(tr -cd '\0' < "$REL_FILES_Z" | wc -c)
|
||||
count=$(tr -cd '\0' <"$REL_FILES_Z" | wc -c)
|
||||
log_info "Discovered $count shell file(s) under $ROOT_DIR"
|
||||
if [[ $VERBOSE == "true" ]]; then
|
||||
tr '\0' '\n' < "$REL_FILES_Z" | sed 's/^/ - /'
|
||||
tr '\0' '\n' <"$REL_FILES_Z" | sed 's/^/ - /'
|
||||
fi
|
||||
}
|
||||
|
||||
run_linters() {
|
||||
local issues=0
|
||||
local count
|
||||
count=$(tr -cd '\0' < "$ABS_FILES_Z" | wc -c)
|
||||
count=$(tr -cd '\0' <"$ABS_FILES_Z" | wc -c)
|
||||
if [[ $count -eq 0 ]]; then
|
||||
log_warn "No shell files found to lint."
|
||||
return 0
|
||||
fi
|
||||
|
||||
mapfile -d '' -t FILES < "$ABS_FILES_Z"
|
||||
mapfile -d '' -t FILES <"$ABS_FILES_Z"
|
||||
|
||||
log_info "Running shellcheck..."
|
||||
local sc_out="$TMPDIR/shellcheck.txt"
|
||||
if is_cmd shellcheck; then
|
||||
if ! shellcheck -x -S style "${FILES[@]}" > "$sc_out" 2>&1; then
|
||||
if ! shellcheck -x -S style "${FILES[@]}" >"$sc_out" 2>&1; then
|
||||
issues=$((issues + 1))
|
||||
fi
|
||||
else
|
||||
@ -272,8 +278,10 @@ run_linters() {
|
||||
|
||||
log_info "Running shfmt (diff mode)..."
|
||||
local shfmt_out="$TMPDIR/shfmt.diff"
|
||||
if is_cmd shfmt; then
|
||||
if ! shfmt -d -i 2 -ci -sr -s "${FILES[@]}" > "$shfmt_out" 2>&1; then
|
||||
if [[ $SKIP_FMT == "true" ]]; then
|
||||
log_info "Skipping shfmt (--skip-fmt flag)"
|
||||
elif is_cmd shfmt; then
|
||||
if ! shfmt -d -i 2 -ci -sr -s "${FILES[@]}" >"$shfmt_out" 2>&1; then
|
||||
# shfmt returns non-zero when diff exists
|
||||
issues=$((issues + 1))
|
||||
fi
|
||||
@ -291,7 +299,7 @@ run_linters() {
|
||||
CBI_FILES=()
|
||||
for f in "${FILES[@]}"; do
|
||||
local first
|
||||
first=$(head -n 1 -- "$f" 2> /dev/null || true)
|
||||
first=$(head -n 1 -- "$f" 2>/dev/null || true)
|
||||
if [[ $first =~ bash || $first =~ zsh ]]; then
|
||||
continue
|
||||
fi
|
||||
@ -299,9 +307,9 @@ run_linters() {
|
||||
done
|
||||
if [[ ${#CBI_FILES[@]} -gt 0 ]]; then
|
||||
# checkbashisms exits 0 if OK, 1 if issues, other codes for tool warnings
|
||||
checkbashisms "${CBI_FILES[@]}" > "$cbi_out" 2>&1
|
||||
checkbashisms "${CBI_FILES[@]}" >"$cbi_out" 2>&1
|
||||
else
|
||||
: > "$cbi_out"
|
||||
: >"$cbi_out"
|
||||
fi
|
||||
cbi_status=$?
|
||||
if [[ $cbi_status -eq 1 ]]; then
|
||||
@ -325,7 +333,7 @@ run_linters() {
|
||||
SH_FILES=()
|
||||
for f in "${FILES[@]}"; do
|
||||
local first
|
||||
first=$(head -n 1 -- "$f" 2> /dev/null || true)
|
||||
first=$(head -n 1 -- "$f" 2>/dev/null || true)
|
||||
if [[ $first =~ bash ]]; then
|
||||
BASH_FILES+=("$f")
|
||||
elif [[ $first =~ zsh ]]; then
|
||||
@ -336,23 +344,23 @@ run_linters() {
|
||||
done
|
||||
|
||||
if [[ ${#BASH_FILES[@]} -gt 0 ]] && is_cmd bash; then
|
||||
if ! bash -n "${BASH_FILES[@]}" 2> "$bash_out"; then
|
||||
if ! bash -n "${BASH_FILES[@]}" 2>"$bash_out"; then
|
||||
issues=$((issues + 1))
|
||||
fi
|
||||
fi
|
||||
if [[ ${#ZSH_FILES[@]} -gt 0 ]] && is_cmd zsh; then
|
||||
if ! zsh -n "${ZSH_FILES[@]}" 2> "$zsh_out"; then
|
||||
if ! zsh -n "${ZSH_FILES[@]}" 2>"$zsh_out"; then
|
||||
issues=$((issues + 1))
|
||||
fi
|
||||
fi
|
||||
# prefer dash if present for /bin/sh style
|
||||
if [[ ${#SH_FILES[@]} -gt 0 ]]; then
|
||||
if is_cmd dash; then
|
||||
if ! dash -n "${SH_FILES[@]}" 2> "$sh_out"; then
|
||||
if ! dash -n "${SH_FILES[@]}" 2>"$sh_out"; then
|
||||
issues=$((issues + 1))
|
||||
fi
|
||||
elif is_cmd sh; then
|
||||
if ! sh -n "${SH_FILES[@]}" 2> "$sh_out"; then
|
||||
if ! sh -n "${SH_FILES[@]}" 2>"$sh_out"; then
|
||||
issues=$((issues + 1))
|
||||
fi
|
||||
fi
|
||||
@ -368,7 +376,9 @@ run_linters() {
|
||||
printf '\n\033[1;32m-- shellcheck: PASS (no issues) --\033[0m\n'
|
||||
fi
|
||||
|
||||
if [[ -s $shfmt_out ]]; then
|
||||
if [[ $SKIP_FMT == "true" ]]; then
|
||||
printf '\n\033[1;32m-- shfmt: SKIPPED (--skip-fmt) --\033[0m\n'
|
||||
elif [[ -s $shfmt_out ]]; then
|
||||
printf '\n\033[1m-- shfmt (diffs found) --\033[0m\n'
|
||||
cat "$shfmt_out"
|
||||
else
|
||||
|
||||
Loading…
Reference in New Issue
Block a user