1

我目前正在使用 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 修订号;但是,我不仅需要查看数字,还需要查看是否在最后一天进行了修改。

4

1 回答 1

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"
    }
}
于 2012-07-20T02:14:21.657 回答