another tool for SVN – list_repositories.pl

 

 

One of the features of subversion + apache2 is the ability to list repositories – natively. 

SVNParentPath /apps/repos
SVNListParentPath on

Unfortunately, once you restrict the httpd.conf to individual repositories and start handling permissions separately you lose that.  Both of these permissions, set at the top level, at the parent path to the repositories, have to be commented out to have the individual permissions on the directories below take effect.

And listing just the repositories wasn’t enough for what I had in mind – I wanted a read-only table showing an individual developer or a team lead for a project who has what permissions for the subversion repositories. All the users, all the repositories.

Just your basic cgi.

 

repos

 

 and the script:


#! /bin/perl -w

# script to parse and display users and repositories and rights (R, RW)

use strict;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);

##----------------------------
## Variables
##----------------------------

#debug
my $debug = 0; #set to 0 to turn off, 1 (true) to turn on

# title for page
my $PageTitle = "List of SVN Repositories";

## repository directory
my $SVN = "/apps/repos";
## location for htpasswd files
my $HTDIR = "/apps/apache2/conf/htpasswd";
## Set untainted path
$ENV{PATH} = '/apps/apache2/bin:/bin:/usr/bin:/usr/local/bin';
$ENV{IFS} = "" if $ENV{IFS} ne "";

# css
my $css = "http://<your server name>/css/main.css";
my $headerimg = "http://<your server name>/css/roger_rabbit_120.jpg";

##------------------------------
## MAIN
##------------------------------

&standard_header;
my ($ref_repos, $ref_tabledata, $ref_users) = &CreateTableSpace;
&DisplayTable ($ref_repos, $ref_tabledata, $ref_users);
&standard_footer;
exit;

##------------------------------
## subs
##------------------------------

sub standard_header {
    print header();
    print start_html(-Title => "$PageTitle", -BGCOLOR=>"White",
        -style => {
            -src => "${css}"
        }
    );
    print ("<div id="header">n");
    print p("<img src="${headerimg}" title="Wells logo" alt="wells logo"/>n");
    print ("</div>n");
    print ("<div id="headertitle">n");
    print h3("Repositories<br/>nusers | read (R) | read & write (RW)n"); # start_multipart_form() if file upload
}

sub standard_footer {
    print end_html();
}

sub CreateTableSpace {
    my $ref_repos = &GetBlankRepos;
    my @repos = @$ref_repos; # dereference
    my %tabledata = (); # hash to hold table data
    my @users = (); # list of users
    my %seen = ();
    foreach my $rep (@repos) {
        open(FILE, "$HTDIR/${rep}_read") || croak "Failed to open $HTDIR/${rep}_read for reading...";
        my @filelines = <FILE>;
        close FILE;
        foreach my $line (@filelines) {
            # lines are user:passwd
            my ($user, $pass) = split (":", $line);
            unless ($seen{$user}) {
                $seen{$user} = 1; # save as seen
                push (@users, $user); # save the user to a list
            }
            $tabledata{$rep}{$user} = "read";
        }
        open (FILE, "$HTDIR/${rep}_write") || croak "Failed to open $HTDIR/${rep}_write for reading";
        my @file_lines = <FILE>;
        close FILE;
        foreach my $line (@file_lines) {
            # lines are user:passwd
            my ($user, $pass) = split (":", $line);
            unless ($seen{$user}) {
                $seen{$user} = 1; # save as seen
                push (@users, $user); # save the user to a list
            }
            $tabledata{$rep}{$user} = "readwrite";
        }
    }
    return (@repos, %tabledata, @users);
}

sub GetBlankRepos {
    my @repos = ();
    # list $SVN
    opendir (DIR, $SVN) || croak "Failed to open directory $SVN for reading...";
    while (defined(my $file = readdir(DIR))) {
        # skip ".", ".." and .<hidden> files...
        if ($file =~ /^./) {
            next;
        } else {
            push(@repos, $file);
        }
    }
    return (@repos);
}

sub DisplayTable {
    my $ref_repos = shift;  #@repos
    my $ref_tabledata = shift; # %tabledata
    my $ref_users = shift; # @users
    # dereference
    my @repos = @$ref_repos;
    my %tabledata = %$ref_tabledata;
    my @users = @$ref_users;
    # repos across the top, users down, R or RW for permissions
    # $tabledata{$rep}{$user} = "readwrite";
    # start table
    print ("<table>n<tbody>n");
    # table header
    my $cols = ($#repos + 1);
    print ("<tr><td>Users</td><td colspan="$cols">Repositories</td></tr>n");
    print ("<div id="repotitles"><tr>n<td>&nbsp;</td>");
    foreach my $rep (@repos) {
        print ("<td>$rep</td>");
    }
    print ("n</tr>n</div>n");
    foreach my $user (@users) {
        print ("<tr>n<td>$user</td>");
        foreach my $repo (@repos) {
            if ($tabledata{$repo}{$user}) {
                if ($tabledata{$repo}{$user} eq "read") {
                    print ("<td>R</td>");
                } elsif ($tabledata{$repo}{$user} eq "readwrite") {
                    print ("<td>RW</td>");
                }
            } else {
                print ("<td>&nbsp; - &nbsp;</td>");
            }
        }
        print ("n</tr>n");
    }
    # finish table
    print ("</tbody>n</table>n");
}