考虑一个 Git 日志:
commit 4d6b30238fbfc972ea4505cadf43abd316506d9e
Author: Dotan Cohen <[email protected]>
Date: Mon Jan 11 22:41:21 2016 +0200
Final foobar version
commit 4d6b30238fbfc972ea4505cadf43abd316506d9e
Author: Dotan Cohen <[email protected]>
Date: Mon Jan 11 19:11:51 2016 +0200
Working foobars
commit 4d6b30238fbfc972ea4505cadf43abd316506d9e
Author: Dotan Cohen <[email protected]>
Date: Mon Jan 11 10:31:37 2016 +0200
Broken foobars
commit 4d6b30238fbfc972ea4505cadf43abd316506d9e
Author: Dotan Cohen <[email protected]>
Date: Mon Jan 10 21:47:22 2016 +0200
Added foobars
commit 4d6b30238fbfc972ea4505cadf43abd316506d9e
Author: Dotan Cohen <[email protected]>
Date: Mon Jan 10 11:54:12 2016 +0200
Preparation for foobars
我如何才能从每天的每个提交消息中获取第一次和最后一次的时间,然后做一些数学计算并估算出总共花费的时间?像这样的东西:
Date: Mon Jan 11 22:41:21 2016 +0200
Date: Mon Jan 11 10:31:37 2016 +0200
TOTAL A: 12:09:44
Date: Mon Jan 10 21:47:22 2016 +0200
Date: Mon Jan 10 11:54:12 2016 +0200
TOTAL B: 09:53:10
TOTAL: 22:02:54
出于此问题的目的,我们可以假设所有提交都是由同一个人完成的。请注意,每天可以有任意数量的提交,并且可以跨越不同月份或年份边界的任意天数。
答案1
下面的 Perl 代码应该会让你非常非常接近我认为你想要的。如果您不熟悉 Perl,则需要DateTime::Format::Strptime
从 CPAN 安装该模块cpan install DateTime::Format::Strptime
...。
然后,将 git 日志输出到文件中git log > git.log
。
之后,将以下代码粘贴到文件中,并将日志文件放在同一目录中,然后运行。
这不是我最漂亮或最高效的代码,但我只有几分钟的时间来组合一些东西。
#!/usr/bin/perl
use warnings;
use strict;
use DateTime::Format::Strptime;
my $log = 'git.log';
open my $fh, '<', $log or die $!;
my %dates;
my @order;
while (<$fh>){
if (/Date:\s+(.*?)(\d{2}:.*)/){
push @order, $1 if ! $dates{$1};
push @{ $dates{$1} }, "$1$2";
}
}
my $letter = 'A';
my $total_time = DateTime::Duration->new;
for my $day (@order){
my $start = $dates{$day}->[0];
my $end = $dates{$day}->[-1];
my $parser = DateTime::Format::Strptime->new(
pattern => '%a %b %d %H:%M:%S %Y %z',
on_error => 'croak',
);
my $dt1 = $parser->parse_datetime($start);
my $dt2 = $parser->parse_datetime($end);
my $total = $dt1 - $dt2;
$total_time = $total_time + $total;
print "$start\n$end\n";
print "Total $letter:\t";
print join ':', ($total->hours, $total->minutes, $total->seconds);
print "\n\n";
$letter++;
}
print "Total time overall: ";
print join ':', ($total_time->hours, $total_time->minutes, $total_time->seconds);
print "\n";