Skip to content

Instantly share code, notes, and snippets.

@mdavey
Last active December 26, 2015 09:39
Show Gist options
  • Save mdavey/7130837 to your computer and use it in GitHub Desktop.
Save mdavey/7130837 to your computer and use it in GitHub Desktop.
PHP code to count who was last to commit each line of code (uses svn blame)
<?php
function is_library($filename)
{
static $libraryNames = array(
'/Zend/',
'/html2ps',
'/simpletest/',
'/phpMailer/',
'/dompdf',
'/nusoap',
);
foreach($libraryNames as $libraryName)
{
if(strpos($filename, $libraryName) !== false)
{
return true;
}
}
return false;
}
function svn_blame_count($filename, &$counter)
{
$filename_arg = escapeshellarg($filename);
exec("svn blame $filename_arg", $output);
foreach($output as $line)
{
// Blame output is: (whitespace) revision (whitespace) name (whitespace) code
preg_match('#^\s+(\d+)\s+([a-zA-Z]+)(.*)$#', $line, $matches);
if(($matches === false) || (count($matches) === 0))
{
throw new Exception("Unable to parse svn blame in $filename. Line: \"$line\"");
}
list(,$rev,$name,$text) = $matches;
// Skip blank lines
if(trim($text) === '')
{
continue;
}
if(!isset($counter))
{
$counter[$name] = 0;
}
$counter[$name]++;
}
}
$counter = array();
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('/home/matthewd/b3/trunk/'));
$objects = new RegexIterator($iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);
foreach($objects as $filename => $object)
{
if(is_library($filename))
{
continue;
}
echo "Parsing: $filename\n";
svn_blame_count($filename, $counter);
}
arsort($counter);
foreach($counter as $name => $lines)
{
echo "$name => $lines\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment