File Coverage

blib/lib/Darcs/Inventory/Patch.pm
Criterion Covered Total %
statement 42 58 72.4
branch 6 16 37.5
condition 2 5 40.0
subroutine 11 16 68.7
pod 5 8 62.5
total 66 103 64.0


line stmt bran cond sub pod time code
1             # Copyright (c) 2007-2012 David Caldwell, All Rights Reserved. -*- perl -*-
2              
3 3     3   16 package Darcs::Inventory::Patch; use base qw(Class::Accessor::Fast); use warnings; use strict;
  3     3   5  
  3     3   2757  
  3         29214  
  3         9  
  3         93  
  3         18  
  3         8  
  3         95  
4              
5 3     3   3300 use Digest::SHA qw(sha1_hex);
  3         13649  
  3         305  
6 3     3   3797 use Time::Local qw(timegm);
  3         6141  
  3         194  
7 3     3   3522 use POSIX qw(strftime);
  3         23904  
  3         31  
8 3     3   11436 use IPC::Run qw(run);
  3         164798  
  3         2699  
9              
10             Darcs::Inventory::Patch->mk_accessors(qw(date raw_date author undo name long hash file raw));
11              
12 0     0 0 0 sub darcs_date_str($) { strftime "%a %b %e %H:%M:%S %Z %Y", localtime $_[0] }
13              
14             sub date_from_darcs($) {
15 27 50   27 0 133 $_[0] =~ /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/ or die "Couldn't parse date: $_[0]";
16 27         167 my ($y,$m,$d,$h,$min,$s) = ($1,$2,$3,$4,$5,$6);
17 27         128 timegm($s,$min,$h,$d,$m-1,$y);
18             }
19              
20             sub parse_patch($) {
21 27     27 0 221 my @line = split /\n/, $_[0];
22 27         47 my %patch;
23 27 100       185 ($patch{file} = $1, pop @line) if $line[-1] =~ /^hash: ([0-9]{10}-[0-9a-f]{64})$/;
24 27         291 @patch{qw(name meta long)} = (shift @line, shift @line, join "", map substr("$_\n",1), @line);
25 27         68 chomp $patch{long};
26             # Jim Radford **20061013045032]
27 27 50       148 $patch{meta} =~ /^(.*)\*([-*])(\d{14})/ or die __PACKAGE__.": Internal Error--Couldn't parse patch:\n$_[0]\n";
28 27         114 @patch{qw(author undo date raw_date raw)} = ($1, !!($2 eq '-'), date_from_darcs($3), $3, $_[0]);
29              
30             # This hash code comes from reading this: http://wiki.darcs.net/DarcsWiki/NamedPatch
31             # Which roughly documents the make_filename function in darcs' src/Darcs/Patch/Info.lhs file.
32 27 100       1429 my $hashee = join('', @patch{qw(name author raw_date long)}).($patch{undo} ? 't' : 'f');
33 27         51 $hashee =~ s/\n//g;
34             #$patch{computed} = $hashee;
35 27         606 $patch{hash} = $patch{raw_date}. '-' .substr(sha1_hex($patch{author}), 0, 5). '-' . sha1_hex($hashee).'.gz';
36 27   66     94 $patch{file} ||= $patch{hash};
37 27         267 %patch;
38             }
39              
40             sub new($$) {
41 27     27 1 39 my ($class, $patch_string) = @_;
42 27         1054 my %patch = parse_patch($patch_string);
43 27         384 return bless { %patch }, $class;
44             }
45              
46             sub darcs_date($) {
47 0     0 1   darcs_date_str $_[0]->date
48             }
49              
50             sub as_string($) {
51 0     0 1   my ($p) = @_;
52 0 0         my $s = sprintf("%s %s\n %s %s\n%s\n", $p->darcs_date, $p->author, $p->undo ? 'UNDO:' : '*', $p->name, $p->long);
53 0           $s =~ s/^Ignore-this:\s+[0-9a-f]+\n//m;
54 0           $s;
55             }
56              
57             sub diff($) {
58 0     0 1   my ($self) = @_;
59 0           my $error;
60 0 0 0       run([qw(darcs diff -u --match), "hash ".$self->hash], '>', \$self->{diff}, '2>', \$error) or die "$error\n"
61             unless defined $self->{diff};
62 0           return $self->{diff};
63             }
64              
65             sub diffstat($) {
66 0     0 1   my ($self) = @_;
67 0 0         unless ($self->{diffstat}) {
68 0           my $diff = $self->diff;
69 0           my $error;
70 0 0         run([qw(diffstat)], '<', \$diff, '>', \$self->{diffstat}, '2>', \$error) or die "$error\n";
71             }
72 0           $self->{diffstat};
73             }
74              
75 3     3   3987 use overload '""' => \&as_string;
  3         2357  
  3         26  
76              
77             1;
78             __END__