#!/bin/sh
# albums pre-commit hook.
#
# A commit is only allowed when:
#   1. the working tree is clean: every change is staged (no unstaged
#      changes) and there are no untracked files, and
#   2. `make fix static` passes: fix lint/format, run static checks
#
# If `make fix` changes files, they are staged automatically (with a note
# naming them) so the commit includes the fixes.

set -u
root=$(git rev-parse --show-toplevel) || exit 1
cd "$root" || exit 1
[ -f hooks/lib.sh ] || {
    echo "albums hook: cannot find hooks/lib.sh next to this hook" >&2
    echo "albums hook: (is core.hooksPath set to this repo's hooks/ directory?)" >&2
    exit 1
}
. ./hooks/lib.sh

hook_note "pre-commit: working tree must be clean (no unstaged changes, no untracked files)"
require_commit_tree_clean

require_make
hook_note "pre-commit: running 'make fix static' (auto-fix, then all static checks; may take a minute)"
# QUIET=1: one line per check as it starts, full tool output only on failure
# (see the Makefile), so the hook's output stays short but never hides errors.
if ! QUIET=1 make fix static; then
    hook_fail "make fix static failed" \
"Fix the errors shown above, then re-run the git commit command"
fi

# make fix may have modified tracked files; stage them so the commit
# includes the fixes.
stage_auto_fixes
hook_note "pre-commit: OK"
