#!/usr/bin/env sh
set -eu

# Only run Rust checks if .rs files are staged
if git diff --cached --name-only --diff-filter=ACMR | grep -q '\.rs$'; then
  if command -v just >/dev/null 2>&1; then
    just check_fix
  else
    cargo fmt --all
    cargo clippy --quiet --all-targets --all-features -- -D warnings
  fi
fi

if [ "${IFCHANGE_DISABLED:-}" != "1" ] && [ "${IFCHANGE_DISABLED:-}" != "true" ]; then
  if command -v ifchange >/dev/null 2>&1; then
    git diff --cached --no-ext-diff --relative | ifchange
  else
    git diff --cached --no-ext-diff --relative | cargo run --quiet --release --
  fi
fi
