LOC and Author Stats with Subversion

Very, very simple author and LOC statistic with Subversion blame function and PHP. So you can see how many lines are already in the repository and which user coded it.

[sourcecode language="php"]

// include only this folders
$dirs[] = '/var/www/portal/application';
$dirs[] = '/var/www/portal/html/scripts/k2';
$dirs[] = '/var/www/portal/html/scripts/service';
$dirs[] = '/var/www/portal/html/styles';

// function from php.net
function listFiles( $from = '.') {
if(! is_dir($from))
return false;

$files = array();
$dirs = array( $from);
while( NULL !== ($dir = array_pop( $dirs))) {
if( $dh = opendir($dir)) {
while( false !== ($file = readdir($dh))) {
if( $file == '.' || $file == '..')
continue;

$path = $dir . '/' . $file;
if( ! is_dir($path)) {
$ext = substr($file, strrpos($file, '.')+1);
// include file extensions
if (in_array($ext, array('html','php','phtml','inc','js','css'))) {
$files[] = $path;
}
} else {
// exclude subdirs
if (! in_array($file, array('fckeditor'))) {
$dirs[] = $path;
}
}
}
closedir($dh);
}
}
return $files;
}

$files = array();
foreach($dirs as $dir) {
$files = array_merge($files, listFiles($dir));
}
$stats = array();

foreach($files as $file) {
$tmpFile = '/tmp/blame.tmp';
exec("svn --non-interactive --username myuser --password mypass blame $file > $tmpFile”, $o, $r);
$output = file($tmpFile);

foreach($output as $line) {
$parts = split(‘[ ]‘, trim($line));
$author = $parts[1];
if (! isset($stats[$author])) {
$stats[$author] = 0;
}
$stats[$author]++;
}
}

var_dump($stats);
var_dump(count($files));
[/sourcecode]

Tags: , , , ,