Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gitolite-Trac post-recieve hook
#!/bin/bash
# Gitolite-Trac post-receive hook
# Script written by Nilesh Govindrajan; http://nileshgr.com
# Updated by giotti to support multiple repositories.
# Assumptions:
# 1: all trac environments are saved under one common directory
# defined in tracenv. e.g. /path/to/all/my/trac/envs
# 2: all repositories of an environment follow the naming-pattern
# <environment><delimeter><repository name>[<delimeter><repository name continued>...],
# e.g. someproject-somerepository, or someproject-somerepository-with-extra-text
# 3: The Name of the repo itself (defined in trac) is the same as the git-repository
# without the environment name; e.g. somerepository, or
# somerepository-with-extra-text, instead of someproject-somerepository
# 4: Capitalization is the same throughout gitolite and trac (see todo in
# changelog.
#
# Changelog:
# 1.02
# - Added skipping the hook, when gitolite-admin is detected
# - Added multi-repository support for all environments in one directory-tree
# - TODO: implement capitalization mapping of trac <-> git,
# e.g. when repo is "Foo-Bar" and trac env is "foo" with repo "Bar"
# This script is released under the GNU GPL v3
# Licensed changed to BSD as suggested by Giotti
set -u
tracenv=/var/www/trac/projects # Full path to trac environment except for the last bit
tracadmin=/usr/local/bin/trac-admin # Path to trac-admin
tracdefault='"(default)"' # Trac default repository name (usually: "(default)")
logfile=/var/www/trac/log/post-receive.log # you can always log to /dev/null
delimeter="-" # you can select a different separation-delimeter here (e.g. "_")
date > ${logfile} # init logfile (if you want a continuous log, change ">" to ">>"
# Check that the repo in question is something other than gitolite-admin, otherwise exit.
if [ "${GL_REPO}" != "gitolite-admin" ]
then
reponame="${GL_REPO}"
else
echo "gitolite-admin modification detected. Exiting post-receive hook..." >> ${logfile}
exit 0
fi
# save current IFS
OIFS=$IFS
# split $GL_REPO by delimeter
IFS="$delimeter" && parts=($GL_REPO)
#create new repo-string containing all array parts except the first (e.g. ${parts[0]})
reponame="${parts[*]:1}"
if [ -z "$reponame" ]; then reponame=$tracdefault; fi # (or set to trac-default)
# append first part of GL_REPO parts to tracenv
tracenv="$tracenv/${parts[0]}"
# restore original IFS
IFS=$OIFS
# log our selection
echo "GL_REPO was set to: ${GL_REPO}. Using repo-name: $reponame on trac-environment: $tracenv" >> ${logfile}
while read -s line
do
[ -z "$line" ] && break
commithash=$(echo $line | cut -d' ' -f2)
# prepare changeset command
cmd1="$tracadmin $tracenv changeset added $reponame $commithash"
# prepare resync command to be called, when deleting commits & using "git push -f"
cmd2="$tracadmin $tracenv repository resync $reponame"
# run either changeset or repository resync
eval "$cmd1" || eval "$cmd2"
# log commands
echo "CMD1: $cmd1" >> ${logfile}
echo "CMD2: $cmd2" >> ${logfile}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.