我目前正在使用svn来跟踪几个目录中文本文件的更改。我使用下面的代码(整个程序的一部分)来提取当前的修订版号并使用xml在perl / cgi脚本中显示它。我的目标是为过去24小时(或天)内更改的svn修订显示不同的颜色编号。我认为有一种方法可以使用svn --recursive函数。我知道如何查看具体的日期,但这是不断更新的。
my $SVNInfoXML=`svn --recursive --xml info data/text/`;
my $SVNInfo=XMLin($SVNInfoXML);
my %FileInfo=();my $SVNLatestRev=0;
for my $entry (@{$SVNInfo->{entry}}) {
$FileInfo{File::Basename::basename($entry->{path},'.diff')}=$entry;
$SVNLatestRev=($entry->{revision}>$SVNLatestRev)?$entry->{revision}:$SVNLatestRev;}
稍后在程序中我打印一个HTML表格,显示最新的svn修订号;但是,我不仅需要查看数字,还要查看是否在最后一天进行了修订。
答案 0 :(得分:2)
这是一个Perl脚本。正确?
为什么不让Perl弄清楚提前24小时的时间,然后使用Time::Piece
来解析Subversion日期?
事实上,您为什么使用svn info
而不仅仅是svn log --xml
。这将为您提供所有更改的历史记录,您只需查看每个日期,看它是否与您的旧日期相符。
要从24小时前获得时间,您可以使用:
use Time::Piece
use Time::Seconds #Constants that come in handy
my $current_time = localtime;
my $yesterday_time = $current_time - ONE_DAY;
现在,$yesterday_time
是24小时之前。
如果您使用XML::Simple
,则可以将svn log --xml $file
输出的格式转换为方便的结构。这是我写的一个简单的测试程序:
#! /usr/bin/perl
use strict;
use warnings;
use autodie;
use feature qw(say);
use XML::Simple qw(xml_in xml_out);
use Time::Piece;
use Time::Seconds;
my $file_name = "?Some Name?";
my $now = localtime;
my $yesterday_time = $now - ONE_DAY;
open (my $xml_file, "-|", qq(svn log --xml "$file_name"));
my $xml = xml_in($xml_file);
# XML is a reference to a hash with a single key 'logentry'
# This points to a reference to an array and each entry is
# a reference to a hash that contains the four pieces to the
# log entry.
my $entries_ref = $xml->{logentry};
foreach my $entry (@{$entries_ref}) {
# Each entry is a reference to a hash
my $revision = $entry->{revision};
my $author = $entry->{author};
my $date = $entry->{date};
my $message = $entry->{msg};
# For giggles, we print out the four items.
# In truth, there could be more than four items
# in the hash if there was also a revprop too.
say "$revision: $author: $date: $message";
# The date is in 100,000 of a second. We need to get
# rid of everything on the other side of the seconds
# decimal before we manipulate it.
$date =~ s/\.\d+Z$//; # Get rid of 10,000 of seconds
# Now, we can convert the "svn log" date to a Time::Piece
my $rev_date = Time::Piece->strptime($date, "%Y-%m-%dT%H:%M:%S");
# Compare the rev date to the $yesterday_time date
if ($rev_date < $yesterday_time) {
say "Revision $revision is older than one day"
}
}