# Husky runs this with `sh -e` (see .husky/_/h), so any failure below aborts the commit.

staged=$(git diff --cached --name-only --diff-filter=ACMR)

# Nothing to do for an empty commit, or `git commit --amend --no-edit`.
[ -n "$staged" ] || exit 0

# Format the tree, then stage back only the paths this commit already touched.
#
# oxfmt walks the tree itself rather than taking a file list, and it exits non-zero on
# .astro, .svelte and .svg -- all of which this repo has -- so passing it the staged
# paths would need an allowlist that rots the first time it learns a new extension.
# Letting it choose, then re-staging selectively, keeps that knowledge in the formatter.
#
# Caveat: a partially staged file (git add -p) has its unstaged remainder swept in by
# the re-add. That is the same trade lint-staged makes, and far narrower than the
# `git add -A` this replaces, which committed the whole working tree every time.
pnpm fmt:fix

git diff --cached --name-only --diff-filter=ACMR -z | xargs -0 git add --
