#!/bin/sh
# Auto-format staged Julia files with JuliaFormatter (blue style) before commit.
# JuliaFormatter lives in a persistent shared environment (~/.julia/environments/
# dpm-formatter) so it is installed once, not re-resolved on every commit.
staged_jl=$(git diff --cached --name-only --diff-filter=ACM -- 'src/*.jl' 'test/*.jl')
if [ -n "$staged_jl" ]; then
    julia -e 'using Pkg; Pkg.activate("dpm-formatter"; shared=true, io=devnull); haskey(Pkg.project().dependencies, "JuliaFormatter") || Pkg.add("JuliaFormatter"); using JuliaFormatter; format("src"); format("test")' || exit 1
    git add $staged_jl
fi

# Guard against accidentally staging gitignored artifacts (Manifest.toml, docs/build, *.pdf, changelog.md)
bad=$(git diff --cached --name-only --diff-filter=ACM | grep -E '(^|/)Manifest\.toml$|^docs/build/|^docs/src/changelog\.md$|\.pdf$')
if [ -n "$bad" ]; then
    echo "pre-commit: refusing to commit gitignored artifact(s):"
    echo "$bad"
    exit 1
fi

exit 0
