Displaying a subversion commit log with PHP

By | September 14, 2011

I was recently tasked with a small project that had to pull commit logs from various subversion servers and repositories and display them on a web page.

The process was a lot simplier than I first anticipated.  Hopefully someone else will find this useful.   This php code snippet requires that the server have SHELL_EXEC() enabled and subversion must be installed.   We’ll use the SHELL_EXEC() function to actually use the subversion cli to grab the commit log from a remote server and store it in an xml file.

I’ll break down the code a bit. This first bit, is a simple helper function that will help us read the XML file itself. We call the function xml_atrribute().

function xml_attribute($object, $attribute)
{
    if(isset($object[$attribute]))
        return (string) $object[$attribute];
}

Next up is to actually run the subversion CLI client with all of the correct parameters to generate an xml file of the commit log. You will want to adjust the server, username, password, repository name and output path to meet your needs.

$test = shell_exec("svn log --xml --verbose svn://svn.mydomain.com/reponame --username USERNAME --password PASSWORD > /home/html/svnlog/reponame.xml ") ;

Alright, at this point we should have a nicely formatted XML file that we can read, loop through and display information from. Here is a simple bit that loops through the various XML elements and displays them. As commented we go one step further than just reading the commit comments, we actually display the files and what action was performed on them.

foreach ($xml->logentry as $logentry)
{
  $date = date('m/d/Y', strtotime((string) $logentry->date));
  foreach ($logentry->paths as $paths)
  {

	echo ' <BR><B>Revision: ' . xml_attribute($logentry, 'revision') . '</B>';
	echo ' <BR>Date: ' . $date;
	echo ' <BR>Message: ' . $logentry->msg;
	echo ' <BR>Commited By: ' . $logentry->author;
	echo ' <BR> ' ;

		/* 	Now lets loop through and get a
			list of files that have changed
			in this commit */
		echo '<UL>';
		foreach ($paths->path as $path)
		{
			echo "
<li>" . $path['action'] . "  " . $path . "</l1>";
		}
		echo '</UL>';		

	}

  }

Here is the entire script all put together and commented:

<?php

/* 	This is a helper function
	to aid in reading our XML
	file we are going to create */
function xml_attribute($object, $attribute)
{
    if(isset($object[$attribute]))
        return (string) $object[$attribute];
}

/* 	Run SVN to get an xml file
	of the commit log.
	SHELL_EXEC() must be enabled
	on the server.
*/
$test = shell_exec("svn log --xml --verbose svn://svn.mydomain.com/reponame --username USERNAME --password PASSWORD > /home/html/svnlog/reponame.xml ") ;

/* Pull the XML file in */
$xml = simplexml_load_file(dirname(__FILE__).'/reponame.xml');

/* now lets roll through the XML and display the results */
foreach ($xml->logentry as $logentry)
{
  $date = date('m/d/Y', strtotime((string) $logentry->date)); // format the date
  foreach ($logentry->paths as $paths)
  {

	echo ' <BR><B>Revision: ' . xml_attribute($logentry, 'revision') . '</B>';
	echo ' <BR>Date: ' . $date;
	echo ' <BR>Message: ' . $logentry->msg;
	echo ' <BR>Commited By: ' . $logentry->author;
	echo ' <BR> ' ;

		/* 	Now lets loop through and get a
			list of files that have changed
			in this commit */
		echo '<UL>';
		foreach ($paths->path as $path)
		{
			echo "
<li>" . $path['action'] . "  " . $path . "</l1>";
		}
		echo '</UL>';		

	}

  }

?>

It’s worth noting that when we read in the XML file we assume it lives in the same directory as the script reading it does. You can adjust this as needed. I Hope this is helpful to anyone out there needing to do something similar. The full source is here: svnsample.zip.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *