#!/usr/bin/env bash
# commit-guard: a Claude Code PreToolUse(Bash) hook.
# Blocks git commit, git push and version bumps until you have approved a ship.
#
# Bypass: when you HAVE approved, the assistant appends "# ship-ok" to the command.
# That marker is a deliberate, visible acknowledgement and never appears by accident.
#
# Reads the tool call JSON on stdin. Exit code 2 blocks the command and shows the
# message below to the assistant.

input="$(cat)"

cmd="$(printf '%s' "$input" | python3 -c 'import sys,json; print(json.load(sys.stdin).get("tool_input",{}).get("command",""))' 2>/dev/null)"

# Approved-ship bypass marker.
if printf '%s' "$cmd" | grep -q 'ship-ok'; then
  exit 0
fi

# The operations that must wait for approval.
if printf '%s' "$cmd" | grep -Eq '(^|[;&| ])git( +-C +[^ ]+)? +(commit|push)\b|npm +version\b|npm +publish\b|yarn +version\b'; then
  echo "BLOCKED by commit-guard: no git commit/push or version bump on your own initiative." >&2
  echo "Stop after the code edit and say it is ready to ship." >&2
  echo "If the user HAS said ship/push/release, re-run the command with '# ship-ok' appended." >&2
  exit 2
fi

exit 0
