post-commit

Automatic build log

Tier 4engineering

Nobody being able to answer "what changed last week" without reading 200 commit messages. Each committer gets their own queue file, so merges never conflict over it.

POST-COMMIT COMMIT BUILD LOG STUB
Add this to your project
Add "Automatic build log" from ntent to this repo.

Fetch https://ntent.app/r/f/post-commit as plain text. Write it verbatim to scripts/git-hooks/post-commit. Then make it executable.

It needs docs/build-log/_pending-*.md ignored by git. If this repo does not have it, stop and tell me.

Then check it: Commit twice. docs/build-log/_pending-<your name>.md has 2 stubs and `git status` is clean, because the queue is ignored. Commit as a second author (change git config user.name) and a second file appears beside it: nobody appends to the same lines, so git never has a queue to merge. A non-Latin name keeps its letters (Zoë Müller is _pending-zoë-müller.md). With no usable user.name the email’s local part is used, and failing that the file is _pending-unknown.md rather than nothing.

Reads https://ntent.app/r/f/post-commit

Read the code
post-commitscripts/git-hooks/post-commit · make it executable
154 lines
#!/bin/bash
#
# Build-log stub feed.
#
# After each meaningful commit, append one line to your own inbox,
# docs/build-log/_pending-<author>.md. That file is a raw feed, not the build log
# itself: later, you or an agent turns each stub into a dated entry that says
# WHY, and ticks it off.
#
# THE QUEUE IS GITIGNORED, AND THAT IS THE WHOLE PERSISTENCE MODEL. A tracked
# queue dirties the worktree on every single commit, which aborts the next
# rebase mid-pick and makes "is my tree clean" a question with no useful answer.
# What gets committed is the drained entry. The stub never does. Install the
# .gitignore line with this hook, or you have chosen the tracked model by
# accident rather than on purpose.
#
# ONE INBOX PER COMMITTER, NOT ONE SHARED FILE. Every committer appends to the
# same end of the same file on every commit, which makes a shared queue the most
# conflict-prone path in a repo that tracks it. In the repo this was taken from,
# it was in conflict on 59 of the 63 merges that conflicted over three months,
# and not once was it a real disagreement. A stub needs no sharing: whoever wrote
# it drains it, and the file empties again. Key the queue on the committer and
# git never has to merge it at all.
#
# THE NAME. The slug comes from `git config user.name`: normalised to NFC,
# lowercased, every run of characters that are not Unicode letters, marks or
# digits turned into one `-`, the ends trimmed. "Ada O'Neil" is
# _pending-ada-o-neil.md, "Zoë Müller" is _pending-zoë-müller.md and "李雷" is
# _pending-李雷.md. An ASCII-only slug would give the last one nothing, and
# everyone with a non-Latin name would then share one file, which brings the
# conflicts back. If the name gives an empty slug, the local part of
# `git config user.email` is slugged the same way, and if that is empty too the
# file is _pending-unknown.md, so a checkout that has not configured git still
# logs rather than going quiet. Drain your own; another author's stubs are
# theirs to write up, because the reasoning is the point and you do not have it.
#
# The slug is made with node, which any repo using this hook has (the pre-commit
# hook runs its checks with it) and which does Unicode classes the same on macOS
# and Linux. If node is missing it falls back to sed, which keeps ASCII only.
#
# Why bother, when git log exists. Git log answers "what changed". It cannot
# answer "why is it like this", which is the question that costs a team the most
# time and that no tool reconstructs. On a project with a multi-year life and
# staff turnover, the why is the highest-return thing you can write down, and a
# feed is what makes writing it realistic: nobody remembers to start a log, but
# everybody can drain a queue.
#
# NON-BLOCKING BY CONSTRUCTION. post-commit runs after the commit is made, so
# nothing here can fail it. That is why the stub logging lives here and the
# gates live in pre-commit.

set -u

repo_root="$(git rev-parse --show-toplevel 2>/dev/null)" || exit 0
cd "$repo_root" || exit 0

git_dir="$(git rev-parse --git-dir 2>/dev/null)" || exit 0

# Skip replayed commits. During a rebase or cherry-pick these were already
# logged when first authored, and writing to the worktree here dirties it, which
# aborts the next pick mid-rebase. This one is not hypothetical; it will bite you
# the first time you rebase.
if [ -d "$git_dir/rebase-merge" ] || [ -d "$git_dir/rebase-apply" ] || [ -f "$git_dir/CHERRY_PICK_HEAD" ]; then
  exit 0
fi

# Whose inbox this is. See ONE INBOX PER COMMITTER above.
slugify () {
  if command -v node >/dev/null 2>&1 ; then
    node -e "process.stdout.write(process.argv[1].normalize('NFC').toLowerCase().replace(/[^\\p{L}\\p{M}\\p{N}]+/gu, '-').replace(/^-+|-+\$/g, ''))" -- "$1"
  else
    printf '%s' "$1" | tr '[:upper:]' '[:lower:]' | sed -e 's/[^a-z0-9]\{1,\}/-/g' -e 's/^-//' -e 's/-$//'
  fi
}
author="$(slugify "$(git config user.name 2>/dev/null || true)")"
if [ -z "$author" ] ; then
  author="$(slugify "$(git config user.email 2>/dev/null | cut -d@ -f1 || true)")"
fi
[ -n "$author" ] || author="unknown"

queue="docs/build-log/_pending-$author.md"

# "rapid" mode, for the case where the queue IS tracked, deliberately, and a
# burst of fast commits would otherwise dirty the worktree every time:
#
#   git config myproject.rapid true      (off: git config --unset myproject.rapid)
#
# Local to this checkout, so it never affects anyone else's clone. Stubs are
# still written, to a spool inside .git/ that git ignores by construction.
# Rapid means "do not touch my worktree", not "lose the feed". That includes
# creating the inbox: it is only made below when rapid is off.
rapid="$(git config --bool myproject.rapid 2>/dev/null || true)"
if [ "$rapid" = "true" ]; then
  queue="$git_dir/rapid-skipped-stubs"
fi

# Skip merge commits: they carry no authored narrative.
parents="$(git rev-list --parents -n 1 HEAD 2>/dev/null | wc -w | tr -d ' ')"
[ "${parents:-0}" -gt 2 ] && exit 0

# --root, so the very first commit in a repository is logged too. Without it,
# diff-tree has no parent to compare against and silently prints nothing, so the
# feed starts empty and looks like it is not wired up.
# core.quotepath=off: a non-ASCII name is otherwise printed quoted and escaped,
# and the build-log filter below would not recognise it.
files="$(git -c core.quotepath=off diff-tree --root --no-commit-id --name-only -r HEAD 2>/dev/null | sed '/^$/d')"
[ -z "$files" ] && exit 0

# Skip commits that only touch the build log, or the feed talks about itself.
real="$(printf '%s\n' "$files" | grep -vE '^docs/build-log/' || true)"
[ -z "$real" ] && exit 0

# Create the queue rather than returning. The earlier version exited quietly
# when the file was missing, and since nothing in the install plan created it,
# a fresh installation recorded nothing at all and looked exactly like a working
# one. A feed that starts empty and stays empty is indistinguishable from a
# feed nobody is draining. With one inbox per person the plan cannot create
# them anyway, so the first commit each person makes that gets logged does.
# It happens after the early exits above, so a merge or a commit that only
# touches the build log leaves no empty inbox and prints nothing.
if [ "$rapid" != "true" ] && [ ! -f "$queue" ] ; then
  mkdir -p "$(dirname "$queue")" || exit 0
  printf '# Pending build-log stubs (%s)\n\n' "$author" > "$queue" || exit 0
  echo "  build-log: created $queue. It is meant to be gitignored; if git is"
  echo "             tracking it, add docs/build-log/_pending-*.md to .gitignore now."
fi

hash="$(git rev-parse --short HEAD)"
subject="$(git log -1 --pretty=%s)"
cdate="$(git log -1 --date=format:%Y-%m-%d --pretty=%ad)"
nfiles="$(printf '%s\n' "$real" | wc -l | tr -d ' ')"

# Tag the area, so draining the queue later is a matter of sorting.
areas=""
add() { case ",$areas," in *",$1,"*) ;; *) areas="${areas:+$areas, }$1";; esac; }
printf '%s\n' "$real" | grep -qE '^src/app/api/'                  && add "api"
printf '%s\n' "$real" | grep -qE '^src/(components|app)/'          && add "ui"
printf '%s\n' "$real" | grep -qE '^(prisma/|src/lib/db)'           && add "data"
printf '%s\n' "$real" | grep -qE '^(scripts/|\.github/)'           && add "tooling"
printf '%s\n' "$real" | grep -qE '^(contract\.json|src/types/)'    && add "contract"
[ -z "$areas" ] && areas="other"

printf -- '- [ ] `%s` %s [%s] %s (%s file(s))\n' \
  "$hash" "$cdate" "$areas" "$subject" "$nfiles" >> "$queue"

# Say so every time it is bypassed. A silent skip is how a feed dies without
# anyone noticing that it died.
if [ "$rapid" = "true" ]; then
  echo "  build-log: rapid mode on. Stub spooled to ${queue}, worktree untouched."
  echo "             Drain it and 'git config --unset myproject.rapid' when done."
fi

exit 0

How to tell it works: Commit twice. docs/build-log/_pending-<your name>.md has 2 stubs and `git status` is clean, because the queue is ignored. Commit as a second author (change git config user.name) and a second file appears beside it: nobody appends to the same lines, so git never has a queue to merge. A non-Latin name keeps its letters (Zoë Müller is _pending-zoë-müller.md). With no usable user.name the email’s local part is used, and failing that the file is _pending-unknown.md rather than nothing.

Included in