#!/usr/bin/perl
use warnings;
use File::Temp qw/ tempfile tempdir /;
# ====================================================================
# Add the PATH this commit effects to the log
# ====================================================================
if (@ARGV < 2) { exit; }
my $dir = "svn log -vr ".quotemeta($ARGV[1])." file://".quotemeta($ARGV[0]);
$dir = `$dir`;
if ($dir eq '') { exit; }
$dir =~ s/\r?\n/\n/sg;
$dir =~ s/^.*?Changed paths:\n//s;
$dir =~ m/\n(?:\n|$)(.*)/s;
$log = $1;
if ($log =~ m/PATH:/) { exit; }
$log =~ s/^\n*//;
$log =~ s/\n*-+$//;
$dir =~ s/\n(?:\n|$).*//s;
$dir =~ s/^.....//mg;
#(@branches) = $dir =~ m/^(.*\(from .*\))$/mg;
$dir =~ s/^(.*?)\s+\(from .*\)$/$1/mg;
while ($dir =~ m/\n\n/s)
{
	$dir =~ s/\n\n/\n/gs;
}

@dirs = split(/\n/,$dir);
if (!@dirs) { exit 0; }
@topdirs = ();
$cmp = $dirs[0];
while (@topdirs != @dirs) {
	if ($cmp eq '') { $cmp = '/'; last; }
	$dir = quotemeta($cmp);
	@topdirs = grep /^$dir(?:\/|$)/, @dirs;
} continue {
	if (@topdirs != @dirs){
		$cmp =~ s/\/[^\/]*$//;
	}
}

$newlog = "PATH: $cmp\n$log";
($fh, $fn) = tempfile();
$fd = "/proc/$$/fd/".fileno($fh);
print $fh $newlog;
if ($pid = fork) {
	waitpid($pid,0);
	unlink $fn;
}
else {
	exec ("svnadmin", "setlog", "--bypass-hooks", $ARGV[0], "-r", $ARGV[1], $fd);
}

exit 0;


