#!/bin/sh
# Two guards, both bypassable with --no-verify when you mean it.
#   1. master is not a working branch.
#   2. a code commit stays under 300 changed lines.
# Markdown is exempt from the size cap on purpose: docs land as one batch.

branch=$(git symbolic-ref --short HEAD 2>/dev/null)

case "$branch" in
	master|main)
		echo "pre-commit: refusing to commit on $branch." >&2
		echo "  task start <vikunja-id>   # branch off origin/master, write TASK.md" >&2
		exit 1
		;;
esac

# Added + deleted lines across staged files that are not markdown.
# numstat prints "-\t-\t<path>" for binaries; those count 0 and that is fine,
# a binary blob is not the kind of diff this cap exists to stop.
loc=$(git diff --cached --numstat -- . ':(exclude)*.md' |
	awk '$1 ~ /^[0-9]+$/ { a += $1 } $2 ~ /^[0-9]+$/ { d += $2 } END { print a + d + 0 }')

if [ "$loc" -gt 300 ]; then
	echo "pre-commit: $loc changed lines in non-markdown files, cap is 300." >&2
	echo "  Split it. Each commit should be one reviewable idea." >&2
	echo "  git reset <path> to unstage, or --no-verify if this genuinely cannot split." >&2
	exit 1
fi

exit 0
