CustomTools:SVN Analysis

From Maisqual Private Wiki

Jump to: navigation, search

This is a quick script that gets informations from a SVN repository and stores them as a csv file.


Contents


[edit] Usage and Description

$ perl svn_analyse.pl http://svn.apache.org/repos/asf/ant/antlibs/antunit/trunk/

svn_analyse.pl counts the number of commits for each file in the HEAD of the SVN URL provided in command-line.

It first runs a svn ls -R on the URL, and for each file gets the number of commits (svn log).

Two files are created:

  • svn_analyse.log, with all logged information about connection, revisions and count for each file,
  • svn_analyse.txt, the output file that holds all informations about the number of commits as a csv format with:
    • the name of the file been analysed,
    • the number of commits received for the file.


[edit] Requirements

This script has been written to run on GNU/Linux system, but should be ok on any system with Perl and Subversion.

The svn command must be in the PATH.


[edit] Example Run

Here is an example run of the script with the SVN URL of Ant -- http://svn.apache.org/repos/asf/ant/antlibs/antunit/trunk/.

boris@samarcande custom $ perl svn_analyse.pl http://svn.apache.org/repos/asf/ant/antlibs/antunit/trunk/
Getting list of files.
Treating file [http://svn.apache.org/repos/asf/ant/antlibs/antunit/trunk//NOTICE].
  Found revision [r1063181].
  Found revision [r756744].
  Found revision [r552213].
  Found revision [r425607].
  Found revision [r424944].
  > [5] revisions found.
Treating file [http://svn.apache.org/repos/asf/ant/antlibs/antunit/trunk//README].
  Found revision [r689353].
  Found revision [r447724].
  > [2] revisions found.
Treating file [http://svn.apache.org/repos/asf/ant/antlibs/antunit/trunk//build.xml].
  Found revision [r743906].
  Found revision [r463018].
  Found revision [r424944].
  Found revision [r348097].
  Found revision [r161885].
  Found revision [r161469].
  > [6] revisions found.

The svn_analyse.txt file output by the script looks like this:

NOTICE:5
README:2
build.xml:6
changes.xml:19
contributors.xml:8
doap_AntUnit.rdf:5
docs/:40
docs/antunit.html:17
docs/assert.html:5
docs/assertions.html:19
docs/expectfailure.html:5
docs/failurelistener.html:1
docs/index.html:11
docs/logcontent.html:2
docs/plainlistener.html:4
docs/xmllistener.html:6
maven-metadata.xml:1
project-template.pom:3
src/:120
src/etc/:43
src/etc/junit-frames.xsl:32
src/etc/junit-noframes.xsl:21
src/etc/testcases/:41


[edit] Script

#! /usr/bin/perl

use strict;
use File::Basename;

# Set the default log file name
my ($basename, $current_path, $suffix) = File::Basename::fileparse($0, qr/\.pl$/);
my $file_log = $basename . ".log";
my $file_out = $basename . ".txt";

my $debug = 1;

# Other global variables
my $start_time = localtime();

my $svn_url = shift or die "Usage: $0 svn_url";

# Catch kills to make sure sessions are freed.
$SIG{INT} = \&END;
$SIG{QUIT} = \&END;

print "Getting list of files.\n";
my @file_list =  `svn ls -R "$svn_url"`;

chomp @file_list;

END {
    close OUT;
    close LOG;
}

sub log {
    my $message = shift;

    print "$message" if ($debug);
    print LOG "$message";
}

open (OUT, ">>$file_out") or die "Cannot open file for output.";
open (LOG, ">>$file_log") or die "Cannot open file for log.";

foreach my $file (@file_list) {

    if ($file =~ m!\.svn!) { next; }

    &log("Treating file [$svn_url/$file].\n");
    my @svn_out = `svn log "$svn_url/$file"`;
    chomp @svn_out;

    my $commits = 0;
    foreach my $line (@svn_out) {
	if ($line =~ m!(r\d+) \| (.+) \| !) {
	    my $rev = $1;
	    &log("  Found revision [$rev].\n");
	    $commits++;
	}
    }
    print "  > [$commits] revisions found.\n";
    print OUT "$file:$commits\n";
}
Personal tools