Skip to content

Instantly share code, notes, and snippets.

@kenaniah
Last active August 14, 2018 08:44
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenaniah/5471280 to your computer and use it in GitHub Desktop.
Save kenaniah/5471280 to your computer and use it in GitHub Desktop.
Git post-receive (remote push) and post-fetch (local pull) hooks for trac. Since post-fetch is not a real hook, it must be called manually after every fetch.
#!/bin/bash
# This isn't really a git hook, but it's manually called it after every fetch run.
# This script essentially wraps the actual post-receive hook.
# Build the standard input for the post-receive hook
cat refs/heads/* | paste TRAC_HEAD - > post-fetch.tmp
find refs/heads/* | paste post-fetch.tmp - | awk '{print $1, $2, $3}' > post-fetch.stdin
# Call the post-receive hook just like the commits were pushed
cat post-fetch.stdin | hooks/post-receive
# Cleanup
rm post-fetch.tmp
rm post-fetch.stdin
#!/bin/sh
#
# Reads and notifies trac of only new commits that have not yet been dealt with.
#
# The "post-receive" script is run after receive-pack has accepted a pack
# and the repository has been updated. It is passed arguments in through
# stdin in the form
# <oldrev> <newrev> <refname>
# For example:
# aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master
#
TRAC_PATH="/path/to/trac/env"
LOCKFILE=post-receive.lock
# Check for the existence of the lock file and then schedule for later
if [ -e ${LOCKFILE} ] && kill -0 `cat ${LOCKFILE}`; then
at "now +5 minutes" hooks/post-fetch
exit
fi
# Make sure the lockfile is removed when we exit and then claim it
trap "rm -f ${LOCKFILE}; exit" INT TERM EXIT
echo $$ > ${LOCKFILE}
# Read the standard input
while read oldrev newrev refname ; do
echo "Processing branch: $refname"
# Read the last revisions for each branch from TRAC_HEAD
exclusions=$(cat TRAC_HEAD | uniq | sed -e 's/^/^/' -e 's/ / ^/g' | xargs echo)
echo "Exclusion list: $exclusions"
git rev-list --reverse $newrev $exclusions | while read rev ; do
trac-admin $TRAC_PATH changeset added '(default)' $rev
echo "Processed: $rev"
done
# Add to the exclusions file the latest revision from this branch
echo $newrev >> TRAC_HEAD
done
# Update the TRAC_HEAD file
cat refs/heads/* > TRAC_HEAD
# Clear the lock file
rm -f ${LOCKFILE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment