svn post-commit hook in Ubuntu

Recommended Ubuntu book

So, I’ve been messing around the scripts in Ubuntu. It’s fun… 2 am here and I finally got the post-commit hook to work the way I wanted it to. Had to learn quite a bit about bash scripts and php calling the bash scripts. 🙂

As I started in previous post – adding svn hooks in Ubuntu, this is a part 2 of that post. I am using SVNManager, and loading these hooks in the same /repo-templates folder I created in previous post.

To start, remove previous post-commit file from the folder if you created one as I made a new one. Then, lets create the files and make them executable:


sudo touch post-commit
sudo touch post-commit-email
sudo touch post-commit-send
sudo chmod +x post-commit post-commit-email post-commit-send

Lets start editing the post-commit file first:


sudo gedit post-commit

Here is what I got:

#!/bin/sh
 
REPOS="$1"
REV="$2"

$REPOS/hooks/post-commit-email $REPOS $REV

Next, I created post-commit-email file in php, change the path to php if needed (to get the path, type ‘which php’ in terminal). Also, make sure you get the right database connection and password:

sudo gedit post-commit-email

#!/usr/bin/php
 $val) {
  if($flag) {
	$emails .= ',';
  }
  $emails .= $val;
  $flag = 1;
}

mysql_close($con);

exec($repos.'/hooks/post-commit-send '.$repos.' '.$rev.' '.$repos_name.' '.$emails.'');

?>

At last, post-commit-send file:


sudo gedit post-commit-send

#!/bin/sh

REPOS="$1"
REV="$2"
REPOSNAME="$3"
SENDTO="$4"
SENDFROM=root@localhost

LIMITDIFF=200
CHANGELOG=`svnlook log -r $REV $REPOS`
AUTHOR=`svnlook author -r $REV $REPOS`
CHANGED=`svnlook changed -r $REV $REPOS`
DIFF=`svnlook diff -r $REV $REPOS | head --lines=$LIMITDIFF`
DATE=`date`
TMPFILE=/tmp/svn$REV-$RANDOM.message
	 
SUBJECT="New SVN Commit By $AUTHOR On $REPOSNAME, Revision #$REV"
echo "-------------------- SVN Commit Notification --------------------
Repository: $REPOSNAME
Revision:   $REV
Author:     $AUTHOR
Date:       $DATE
	 
-----------------------------------------------------------------
Log Message:
-----------------------------------------------------------------
$CHANGELOG
	 
-----------------------------------------------------------------
Changes:
-----------------------------------------------------------------
$CHANGED
 
-----------------------------------------------------------------
Diff: (only first $LIMITDIFF lines shown)
-----------------------------------------------------------------
$DIFF
" > $TMPFILE

#echo $SENDTO >> $REPOS/hooks/error_log 
	 
# Send email
cat $TMPFILE | mail -r "$SENDFROM" -s "$SUBJECT" "$SENDTO"
 
# Cleanup
rm $TMPFILE

Change the email if needed and make sure that the backtick character is correct as it may change when you copy from this page. I got the last file from this post and modified it a bit.

Let me know if this worked for you. 🙂


One Response to “svn post-commit hook in Ubuntu