Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Colored Subversion Commit Email
#!/usr/bin/php
<?php
/*
A Subversion post-commit hook. See below for more information.
Copyright (c) 2007, Jacob Kiers
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of Jacob Kiers nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This script is based on the Ruby example at
http://elliotth.blogspot.com/2005/02/better-subversion-post-commit-hook.html
It is completely rewritten in PHP and somewhat improved.
Edit the configurable stuff below, and add it to your repository's
hooks/post-commit file, using the following syntax:
/path/to/this/script "$REPOS" "$REV" --email "your@address.com" [options]
Don't forget to "chmod a+x post-commit".
See the Configuration section below te configure it.
Improvements:
* Now using the standard PHP mail function, instead of the sendmail command
* Added an extra check for the svnlook command
* Added checks for Added and Deleted files in the color coding
* Added an option to exclude the actual diff, printing only a list of changed files
* Added an option to set a prefix to the subject of the email
* Added an option to not color code the diffs in the email
*/
## Configuration
# You WILL need to change these.
$config['svnlook'] = "/usr/bin/svnlook";
# ------------------------------------------------------------------------
# Do some sanity checks
sanitize($argv, $config);
# Start the actual work
# svnlook might create files...
chdir("/tmp");
# Get the overview information
$info = explode("\n", `{$config['svnlook']} info {$config['repo']} -r {$config['rev']}`, 4);
$author = $info[0];
$date = $info[1];
$comment= explode("\n", $info[3]);
$body = "";
# Output the overview.
$body .= "<p><b>$author</b> $date</p>\n\n";
$body .= "<p>";
foreach($comment as $line)
{
$body .= htmlentities($line) . "<br />\n";
}
$body .= "</p>\n";
$body .= "<hr noshade>\n\n";
$body .= get_diff_body();
# Create the header
$header = "";
$header .= "From: {$config['address']}\n";
$header .= "Reply-to: {$config['address']}\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: text/html; charset=UTF-8\n";
$header .= "Content-Transfer-Encoding: 8bit\n";
$subject = ((isset($config['mail-prefix'])) ? $config['mail-prefix']." " : "") . "{$config['repo']} revision {$config['rev']}";
mail($config['address'], "$subject", $body, $header);
# Helper functions
function sanitize(array &$arguments, &$config)
{
$command = array_shift($arguments);
if (!file_exists($config['svnlook']) || !is_readable($config['svnlook']) || !is_executable($config['svnlook']))
{
echo "{$command}: Something is wrong with required file {$config['svnlook']}!\n";
echo "Please check if it exists, is readable and executable, \n";
echo "or edit {$command}!\n";
exit(1);
}
# Which revision in which repository
$config['repo'] = array_shift($arguments);
$config['rev'] = array_shift($arguments);
# By default, use color
$config['color'] = true;
if (!is_dir($config['repo']) || !is_numeric($config['rev']))
{
echo "The first and/or second argument(s) are invalid! Please check your configuration!\n";
exit(1);
}
parseOptions($arguments, $config);
if (!isset($config['address']))
{
echo "Don't forget to set the email address, using the -e or --email option!";
exit(1);
}
}
function parseOptions(&$arguments, &$config)
{
if (count($arguments) < 1)
{
return;
}
while(count($arguments) > 0)
{
$option = array_shift($arguments);
switch($option)
{
case '-d':
case '--diff':
$config['diff'] = true;
break;
case '-mp':
case '--mail-prefix':
if (count($arguments) > 0) $config['mail-prefix'] = array_shift($arguments);
break;
case '-e':
case '--email':
if (count($arguments) > 0) $config['address'] = array_shift($arguments);
break;
case '-nc':
case '--nocolor':
$config['color'] = false;
break;
default:
break;
}
}
}
function get_diff_body()
{
global $config;
# Get and output the changes
$changes = explode("\n", `{$config['svnlook']} diff {$config['repo']} -r {$config['rev']}`);
$diff = "<pre>";
foreach ($changes as $line)
{
$color = "black";
$printline = true;
$line = rtrim($line);
# First, check for filenames
if (eregi("^Modified: ", $line)) { $color = "gray"; }
elseif (eregi("^Deleted: ", $line)) { $color = "red"; }
elseif (eregi("^Added: ", $line)) { $color = "blue"; }
if (true == $printline)
{
if (eregi("^=+", $line) | eregi("^@@", $line)) { $color = "gray"; }
elseif (eregi("^\-", $line)) { $color = "red"; }
elseif (eregi("^\+", $line)) { $color = "blue";}
if ($config['color'])
{
$diff .= "<font color=\"$color\">" . htmlentities($line) . "</font>\n";
} else {
$diff .= htmlentities($line) . "\n";
}
}
}
$diff .= "</pre>";
return $diff;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment