Commit ba7aaac9 authored by wincent's avatar wincent

Move hooks onto main branch

Since switching to the `git-worktree` based workflow, my hooks haven't
been running. Worktrees in subdirectories don't have hooks of their own,
instead using the hooks in the main repo. So, let's move the hooks there
(and probably tweak them as well to work in the new context).
parent 01cf43f0
......@@ -2,11 +2,12 @@
The content and history of this branch may be re-written at any time.
## Setup
For editing, use `git worktree add content content` to create a nested checkout of the "content" branch inside the main repo. Run `hooks/install` in the main repo to set up hooks which prevent accidental amending of published/indexed content and tag deployments with timestamps.
## Scripts
* `scripts/set-up-hooks`: sets up Git hooks that:
* Prevent accidental amending of published/indexed history.
* Tag deployments with timestamps.
* `scripts/new-article`: create a new wiki article.
* `scripts/new-irl-entry`: create a new (snippet) entry in the "[Involuntary Reboot Log](https://wincent.com/tags/involuntary.reboot.log)".
* `scripts/new-snippet`: create a new snippet.
......
#!/bin/bash
#
# Prevent careless amends of already-indexed content.
warning() {
echo "warning [pre-commit]: $@"
}
error() {
echo "error [pre-commit]: $@"
}
HOOK="$(git rev-parse --git-dir)/hooks/pre-commit"
HOOK_FINGERPRINT=$(shasum -a 512 "$HOOK" | awk '{ print $1 }')
REPO_FINGERPINT=$(shasum -a 512 hooks/pre-commit | awk '{ print $1 }')
if [ "$HOOK_FINGERPRINT" != "$REPO_FINGERPINT" ]; then
error "installed hook does not match repo copy"
error "freshen with scripts/set-up-hooks or use --no-verify to force"
exit 1
fi
# Read null-delimited output using this trick:
# http://stackoverflow.com/a/8677566/2103996
MISSING_TAGS=0
while IFS= read -r -d '' CHANGE; do
TYPE=$(basename $(dirname "$CHANGE"))
if ! egrep --quiet "^tags: .*\b$TYPE\b.*$" "$CHANGE"; then
MISSING_TAGS=1
error "file $CHANGE is missing \"tags: $TYPE\" metadata"
fi
done < <(git diff --cached --diff-filter=AM --name-only -z HEAD -- content ':!content/images')
if [ $MISSING_TAGS -eq 1 ]; then
error "use --no-verify to force, or re-run 'scripts/tag-by-type'"
exit 1
fi
if [ -z "$PPID" ]; then
warning "unable to get \$PPID"
exit 0
fi
GIT_COMMAND=$(ps -ocommand= -p $PPID)
if [ -z "$GIT_COMMAND" ]; then
warning "unable to get \$PPID"
exit 0
fi
if [ "${GIT_COMMAND#*--amend}" == "$GIT_COMMAND" ]; then
# Not an amend commit; nothing to do.
exit 0
fi
LAST_INDEX=last-indexed-hash
PREFIX=masochist
CACHE_BREAKER=$(grep REDIS_CACHE_VERSION $PWD/../masochist/src/server/constants.js | perl -pe "s/.+'(\d+)'.*/\\1/")
if [ -z "$CACHE_BREAKER" ]; then
warning "unable to get cache breaker"
exit 0
fi
ALLOW_AMEND=1
HEAD=$(git rev-parse HEAD)
LAST_INDEXED_HASH=$(redis-cli get "$PREFIX:$CACHE_BREAKER:$LAST_INDEX")
if [ -z "$LAST_INDEXED_HASH" ]; then
warning "unable to get last indexed hash"
else
if [ "$LAST_INDEXED_HASH" = "$HEAD" ]; then
error "attempting amend of commit that has already been indexed locally"
ALLOW_AMEND=0
elif git merge-base --is-ancestor $LAST_INDEXED_HASH $HEAD; then
true # All good!
else
error "attempting amend of commit that has already been indexed locally"
ALLOW_AMEND=0
fi
fi
LATEST_TAG=$(git tag -l --sort=-version:refname | head -n 1)
if [ -z "$LATEST_TAG" ]; then
warning "unable to get latest tag"
else
LATEST_TAG_HASH=$(git rev-parse "$LATEST_TAG")
if [ "$LATEST_TAG_HASH" = "$HEAD" ]; then
error "attempting amend of commit that has already been indexed remotely"
ALLOW_AMEND=0
elif git merge-base --is-ancestor $LATEST_TAG_HASH $HEAD; then
true # All good!
else
error "attempting amend of commit that has already been indexed remotely"
ALLOW_AMEND=0
fi
fi
if [ $ALLOW_AMEND -ne 1 ]; then
error "use --no-verify to force"
exit 1
fi
#!/bin/sh
# Tag content deployments with a timestamp.
error() {
echo "error [pre-push]: $@"
}
if [ "$1" != "content" ]; then
exit 0
fi
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
CONTENT_REF=refs/heads/content
HOOK="$(git rev-parse --git-dir)/hooks/pre-push"
HOOK_FINGERPRINT=$(shasum -a 512 "$HOOK" | awk '{ print $1 }')
REPO_FINGERPINT=$(shasum -a 512 hooks/pre-push | awk '{ print $1 }')
if [ "$HOOK_FINGERPRINT" != "$REPO_FINGERPINT" ]; then
error "installed hook does not match repo copy"
error "freshen with scripts/set-up-hooks or use --no-verify to force"
exit 1
fi
while read LOCAL_REF LOCAL_SHA REMOTE_REF REMOTE_SHA
do
if [ "$LOCAL_REF" = "$CONTENT_REF" -a "$REMOTE_REF" = "$CONTENT_REF" ]; then
git tag "$TIMESTAMP"
fi
done
#!/bin/sh
set -e
GIT_DIR=$(git rev-parse --git-dir)
cd $(git rev-parse --show-toplevel)
cp hooks/pre-commit "$GIT_DIR/hooks/"
chmod +x "$GIT_DIR/hooks/pre-commit"
cp hooks/pre-push "$GIT_DIR/hooks/"
chmod +x "$GIT_DIR/hooks/pre-push"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment