#!/bin/sh
# Wiki pre-commit hook, installed by `wiki new --git-hook`. A backstop for edits made
# outside an agent's workflows (an editor, Obsidian, by hand). `wiki precommit` does the
# checking, so the checks update with the command:
#   - sources in raw/ are never edited, renamed or deleted (adding new ones is fine;
#     README files and those [pending] ignore lists, such as a manifest, may change);
#   - with [weekly] in the config, the first commit of a week also commits last week's note;
#   - `wiki check --all` must report no errors;
#   - the wiki's own [hooks] pre_commit commands in .wiki-cli.toml must pass.
# Enabled per clone with: git config core.hooksPath .githooks (from the top of the
# repository: <wiki folder>/.githooks when the wiki is a folder of a code repo)
# Bypass (not recommended): git commit --no-verify
# Run from the wiki's own folder: git runs hooks from the top of the repository, which
# is a folder above the wiki when the wiki is a folder of a code repo.
cd "$(dirname "$0")/.." || exit 1
if command -v wiki >/dev/null 2>&1; then
  exec wiki precommit
fi
echo "pre-commit: 'wiki' is not installed; checked only that sources in raw/ are unchanged." >&2
changed=$(git diff --cached --name-only --diff-filter=MDR -- raw/ | grep -v -E '(^|/)README\.(md|txt)$')
if [ -n "$changed" ]; then
  echo "pre-commit: sources in raw/ are never edited, renamed or deleted:" >&2
  echo "$changed" | sed 's/^/  /' >&2
  echo "pre-commit: restore them (git restore --staged --worktree <file>), then commit again." >&2
  exit 1
fi
exit 0
