#!/bin/sh # # Writulos pre-push hook: refuse a push while the docs are behind the code. # # WHY THIS EXISTS, AND WHAT IT IS NOT # A GitHub Action runs AFTER a push has landed. It can fail a check and stop a # merge. That is `fail_on_drift`, and for most teams it is the right place for # the gate, because it runs on the server where nobody can skip it. # # Nothing hosted can stop the push itself. Only something on your own machine # can, and this is that. It is a COURTESY, not a control: anyone can pass # --no-verify, delete the file, or never install it. Use this to catch yourself # before the push; use fail_on_drift for the guarantee. # # WHY SHELL AND NOT NODE # A git hook is an extensionless file in someone else's repository, and Node # decides CommonJS-vs-ESM from the nearest package.json, so the same hook file # runs in one project and crashes in the next. A hook has exactly two things it # can rely on: a POSIX shell and git. This uses only those. # # NO NETWORK, NO API KEY, NO ACCOUNT # Drift is arithmetic over your own history: every doc Writulos writes carries # the commit it was generated from, and this counts the commits that have # touched the source file since. It reads only your repository and sends # nothing anywhere. It therefore cannot check your plan, and does not pretend # to. The plan-aware gate is the one in CI. # # INSTALL # curl -fsSL https://scribe.writulos.com/pre-push.sh -o .git/hooks/pre-push # chmod +x .git/hooks/pre-push # # CONFIGURE (all optional) # git config writulos.docsDir docs # where your generated docs live # git config writulos.extensions js,ts,py # which source files to check # git config writulos.warnOnly true # report, never block # # SKIP ONCE # git push --no-verify set -u NUL_SHA="0000000000000000000000000000000000000000" DOCS_DIR=$(git config --get writulos.docsDir || echo "docs") EXTENSIONS=$(git config --get writulos.extensions || echo "js,ts,jsx,tsx,py,java,kt,scala,go,rb,rs,cs,cpp,c") WARN_ONLY=$(git config --get writulos.warnOnly || echo "false") # Extensions as a grep alternation: "js,ts" -> "\.\(js\|ts\)$" EXT_PATTERN="\\.\\($(printf '%s' "$EXTENSIONS" | sed 's/[[:space:]]//g; s/,/\\|/g')\\)$" stale_count=0 unknown_count=0 checked=0 report="" # The commit a doc was generated at, read from the stamp Writulos embeds in it. doc_commit() { [ -f "$1" ] || return 1 sed -n 's/.*writulos:meta.*"generatedAtCommitSha"[[:space:]]*:[[:space:]]*"\([0-9a-fA-F]\{7,40\}\)".*/\1/p' "$1" \ | head -n 1 } # Every source file a push of would publish, given . # # A brand-new branch has no remote counterpart to diff against, so it falls back # to the single commit at the tip. Checking too FEW files is the right way to be # wrong here: a hook that blocks a push over a file the push does not contain is # a hook people uninstall. changed_files() { local_sha=$1 remote_sha=$2 if [ -n "$remote_sha" ] && [ "$remote_sha" != "$NUL_SHA" ]; then git diff --name-only --diff-filter=ACMR "$remote_sha" "$local_sha" 2>/dev/null else git diff --name-only --diff-filter=ACMR "$local_sha~1" "$local_sha" 2>/dev/null fi } check_range() { local_sha=$1 remote_sha=$2 [ "$local_sha" = "$NUL_SHA" ] && return 0 # a branch deletion pushes no files changed_files "$local_sha" "$remote_sha" | grep "$EXT_PATTERN" | while IFS= read -r file; do [ -n "$file" ] || continue doc="$DOCS_DIR/${file%.*}.md" sha=$(doc_commit "$doc") || continue [ -n "$sha" ] || continue # `rev-list --count .. -- `: commits that touched this file # since its doc was written. A git failure prints nothing, which is the # "could not measure" case below, never silently a zero. drift=$(git rev-list --count "$sha..$local_sha" -- "$file" 2>/dev/null) if [ -z "$drift" ]; then printf 'unknown\t%s\n' "$file" elif [ "$drift" -gt 0 ]; then printf 'stale\t%s\t%s\t%s\n' "$file" "$drift" "$doc" else printf 'ok\t%s\n' "$file" fi done } # Read git's ref list from stdin. Run by hand with no pipe, fall back to HEAD so # `sh .git/hooks/pre-push` is a useful thing to type. input=$(cat 2>/dev/null || true) [ -n "$input" ] || input="refs/heads/HEAD HEAD refs/heads/HEAD $NUL_SHA" results=$( printf '%s\n' "$input" | while read -r _local_ref local_sha _remote_ref remote_sha; do [ -n "${local_sha:-}" ] || continue check_range "$local_sha" "${remote_sha:-}" done ) checked=$(printf '%s\n' "$results" | grep -c .) stale_count=$(printf '%s\n' "$results" | grep -c '^stale' || true) unknown_count=$(printf '%s\n' "$results" | grep -c '^unknown' || true) [ "$checked" -eq 0 ] && exit 0 if [ "$stale_count" -eq 0 ] && [ "$unknown_count" -eq 0 ]; then echo "Writulos: docs are current for every file in this push." exit 0 fi printf '%s\n' "$results" | while IFS="$(printf '\t')" read -r kind file drift doc; do case "$kind" in stale) if [ "$drift" -eq 1 ]; then echo " $file: docs are 1 commit behind ($doc)" >&2 else echo " $file: docs are $drift commits behind ($doc)" >&2 fi ;; # An unmeasured file is never reported as up to date, and never blocks # either: "I could not check" is not "I found a problem". unknown) echo " $file: could not be checked (shallow clone?)" >&2 ;; esac done if [ "$stale_count" -eq 0 ]; then echo "" >&2 echo "Writulos: nothing is known to be stale; the files above just could not be measured." >&2 exit 0 fi echo "" >&2 if [ "$stale_count" -eq 1 ]; then echo "Writulos: 1 file in this push has documentation behind the code." >&2 else echo "Writulos: $stale_count files in this push have documentation behind the code." >&2 fi case "$(printf '%s' "$WARN_ONLY" | tr '[:upper:]' '[:lower:]')" in 1|true|yes) echo "Pushing anyway (writulos.warnOnly is set)." >&2 exit 0 ;; esac echo "Regenerate the docs and commit them, or push with --no-verify to skip this check." >&2 exit 1