...
 
Commits (4)
  • wincent's avatar
    Import hooks from "content" branch · 7815e0d0
    wincent authored
    Removed them from that branch in ba7aaac9 (but note, the content branch
    may be rewritten at any time so there's a chance that hash won't be
    valid). They will need further tweaks, I think, to make them do the
    right thing in this context.
    7815e0d0
  • wincent's avatar
    Add -x flag to hooks/install · 60750e96
    wincent authored
    Just to provide visual confirmation that it actually did something.
    60750e96
  • wincent's avatar
    Update hooks to work from both branches · c167caeb
    wincent authored
    On "master" branch (the top level worktree) they do nothing, except for
    checking that they are up-to-date.
    
    On the "content" branch (assumed to be a worktree checkout nested under
    the main repo at "content/) we check to make sure we're not amending
    already-indexed content, and additionally tag content deploys with a
    local timestamp.
    c167caeb
  • wincent's avatar
    0d842249
#!/bin/sh
set -e
set -x
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"
#!/bin/bash
#
# Prevent careless amends of already-indexed content.
warning() {
echo "warning [pre-commit]: $@"
}
error() {
echo "error [pre-commit]: $@"
}
INSTALLED_HOOK="$(git rev-parse --git-common-dir)/hooks/pre-commit"
MAIN_WORKTREE_INFO=$(git worktree list --porcelain | head -1)
MAIN_WORKTREE_HOOK="${MAIN_WORKTREE_INFO/#worktree /}/hooks/pre-commit"
HOOK_FINGERPRINT=$(shasum -a 512 "$INSTALLED_HOOK" | awk '{ print $1 }')
REPO_FINGERPRINT=$(shasum -a 512 "$MAIN_WORKTREE_HOOK" | awk '{ print $1 }')
if [ "$HOOK_FINGERPRINT" != "$REPO_FINGERPRINT" ]; 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
if [ "$(git symbolic-ref HEAD)" != refs/heads/content ]; then
# Only run on "content" branch.
exit 0
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
INSTALLED_HOOK="$(git rev-parse --git-common-dir)/hooks/pre-push"
MAIN_WORKTREE_INFO=$(git worktree list --porcelain | head -1)
MAIN_WORKTREE_HOOK="${MAIN_WORKTREE_INFO/#worktree /}/hooks/pre-push"
HOOK_FINGERPRINT=$(shasum -a 512 "$INSTALLED_HOOK" | awk '{ print $1 }')
REPO_FINGERPRINT=$(shasum -a 512 "$MAIN_WORKTREE_HOOK" | awk '{ print $1 }')
if [ "$HOOK_FINGERPRINT" != "$REPO_FINGERPRINT" ]; 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" "$LOCAL_REF"
fi
done