#!/usr/bin/perl -w
use strict;
use FileHandle;
use Getopt::Long qw(:config require_order);

my $tag;
my $nolog;

GetOptions(
    'tag=s'	=> \$tag,
    'revision!'	=> \$nolog,
) or exit 2;

die "expecting reference tag\n" unless defined $tag;


# Find the WC directory. This is the first arg that doesn't start with a
# '-'. Our coding standard forbids directory names starting with '-'.

my $wcpath;
for (@ARGV) {
    if (!/^-/) {
	$wcpath = $_;
	last;
    }
}
$wcpath ||= ".";


# determine repository URL from svn info

my @urlline = grep { /^URL:/ } split /\r?\n/, qx(svn info $wcpath);
die "no URL in svn info $wcpath\n" unless @urlline;
my $urlwc = $urlline[0] =~ /\s+(.*)/ && $1;
die "cannot determine URL\n" unless $urlwc;
unless ($urlwc =~ m#^([^/]+)//([^/]*)/([^/]+)#) {
    die "URL not recognized in $urlwc\n";
}
my ($proto, $host, $repo, $url) = ($1, $2, $3, $&);


# determine branch point $copyrev of tag

my $logcmd = "svn log --stop-on-copy -q $url/$tag";
my $logH = FileHandle->new("$logcmd |") or die "cannot execute $logcmd\n";
my $copyrev;
while (<$logH>) {
    /^r(\d+)/ and $copyrev = $1;
}
close $logH;
die "cannot find branch point of $url/$tag\n" unless defined $copyrev;


# generate output

if ($nolog) {
    print "$copyrev\n";
    exit 0;
}

system("svn log -r$copyrev:HEAD " . join(" ", @ARGV));

exit;

__END__
=head1 NAME

svnlogsince - Print subversion log of revisions since a certain branchpoint

=head1 SYNOPSIS

svnlogsince --tag=I<tagdir> [--revision] [WCpath]

=head1 DESCRIPTION

The program searches for the latest branchpoint I<rev> of I<tagdir> and
performs an C<svn log -r rev:HEAD WCpath>. The full URL of tagdir is
found by concatenating the repository URL of WCpath and the tagdir
specified. Default for WCpath is C<.> (the current directory).

=head1 OPTIONS

If the option C<--revision> is specified, only the revision number of
the branch point will be output.

=head1 EXAMPLE

Show the revision number when the TAGS/5.06.00 release was created.

    svnlogsince --rev --tag=TAGS/5.06.00

Produce a log in XML format of the revisions in the Producten directory
since the TAGS/5.06.00 release was created.

    svnlogsince --tag=TAGS/5.06.00 Producten --xml

=head1 AUTHOR

Arjen Bax

=head1 VERSION

$Id: svnlogsince 16 2005-04-04 12:08:29Z lnpbax1 $

=head1 COPYRIGHT

Public domain. Share and enjoy!

=head1 SEE ALSO

Subversion, http://subversion.tigris.org/

=cut


