Tuesday, April 1, 2008

SVN-script for examining repo usage

If you administrate more than one repository you should sometimes monitor the activity of your repositories, maybe some of them are not in use anymore.
For this you can use this nice perl-script, which will print out the number of commits in the last 180 days. You can also provide the number of days as argument.

#! /usr/bin/perl -w
# config area: put the path to your repositories in here!
$RepoPath="/opt/svn/";
$SVNPath="file://$RepoPath";
# only 1 parameter is needed: the number of days we will look for the commits
$TimeLimit=@ARGV[0];
# if user dont provide a number of days, we will default to 180 days
if ($TimeLimit eq ""){
$TimeLimit=180;
}
# print out a help message, if someone gets lost..
if ($TimeLimit eq "-h" || $TimeLimit eq "--help" || $TimeLimit eq "-help"){
print "Output the number of commits which occured in the last X days\n";
print "repo_usage [days]\n";
print " days (optional, defaults to 180 days)\n will provide the number of days\n where commits will be count.\n";
exit;
}

#.......here starts the main program.......
# first we will calculate the current date and the requested date
# after this we will build a string with the appropriate svn command
# at last step we will loop through the given directory and apply
# our svn command for all repositories
$svncmd = "svn log -q -r{";
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year+=1900;
$mon+=1;
$svncmd .= "$year-$mon-$mday}:{";

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time-60 * 60 * 24 * $TimeLimit);
$year+=1900;
$mon+=1;
$svncmd .= "$year-$mon-$mday} $SVNPath";
# here we loop through all repositories
# as svn outputs the log messages with a separator line
# we need to divide by 2 and svn starts with an additional separatorline
# thats why we need to subtract 1 line also
# to get the number of commits.
opendir(DIR, $RepoPath);
foreach (sort(readdir (DIR))){
if ($_ ne "." && $_ ne ".." ){
$output = `$svncmd$_ | wc -l`;
$output = ($output-1)/2;
print $_." :".$output."\n";
}
}

No comments: