Diff.pm

filter: Statementstmt 81.0% 90 / 111 Branchbran 60.3% 35 / 58 Conditioncond 46.6% 7 / 15 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 9 / 9 Podpod 100% 4 / 4 totaltotal 71.0% 147 / 207 CC 43 SCAR 28.6
CC
43
Cov
72%
directory_diff
26.1
ls_dir
22.1
get_only
9.1
CRAP 17.4
↑ Summary Diff/Copy.pm »
1 package Directory::Diff;
2 2 require Exporter;
3 2 @ISA = qw(Exporter);
4 2 @EXPORT_OK = qw/ls_dir get_only get_diff directory_diff/;
5 2 %EXPORT_TAGS = (
6 all => \@EXPORT_OK,
7 );
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 our $VERSION = '0.09';
11 2 ▶use Carp qw/carp croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Cwd 'getcwd';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use File::Compare 'compare';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub ls_dir
16 12 ▶{
Subroutine ls_dir: called CC=16 SCAR=31.0
Pod documented
17 12 my ($dir, $verbose) = @_;
18 12 ▶ if (! defined $dir || ! -d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir and -d $dir) 0 12 12
Condition33%1/3
defined $dir and -d $dir
!ll&&!rl&&r
0012
MC/DC0%0/2
defined $dir and -d $dir
defined $dir -d $dir
Truth table33%1/3
A and B
A= defined $dirB= -d $dir
ABresult
0X0
100
111
19 0 croak "No such directory '$dir'";
20 }
21 12 my %ls;
22 12 ▶ if (! wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
unless (wantarray) 0 12 12
23 0 die "bad call with ignored value";
24 }
25 12 my $original_dir = getcwd ();
26 12 ▶ chdir ($dir) or die "chdir ($dir) failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $dir or die "chdir ($dir) failed: $!" 12 0 12
27 12 ▶ opendir (my $dh, ".") or die "opendir for $dir failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, "." or die "opendir for $dir failed: $!" 12 0 12
28 12 my @files = readdir ($dh);
29 12 for my $file (@files) {
30 40 ▶ if ($file eq '.' || $file eq '..') {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq "." or $file eq "..") 24 16 40
Condition100%3/3
$file eq "." or $file eq ".."
l!l&&r!l&&!r
121216
MC/DC100%2/2
$file eq "." or $file eq ".."
$file eq "." $file eq ".."
Truth table100%3/3
A or B
A= $file eq "."B= $file eq ".."
ABresult
1X1
011
000
31 24 next;
32 }
33 16 ▶ if (-f $file) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $file) { } 12 4 16
elsif (-d $file) { } 4 0 4
34 12 $ls{"$file"} = 1;
35 }
36 elsif (-d $file) {
37 4 my %subdir = ls_dir ($file, $verbose);
38 4 for my $subdir_file (keys %subdir) {
39 4 $ls{"$file/$subdir_file"} = 1;
40 }
41 4 $ls{"$file/"} = 1;
42 }
43 else {
44 0 warn "Skipping unknown type of file $file.\n";
45 }
46 }
47 12 ▶ closedir ($dh) or die "closedir for $dir failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
closedir $dh or die "closedir for $dir failed: $!" 12 0 12
48 12 ▶ chdir ($original_dir) or die "chdir for $original_dir failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $original_dir or die "chdir for $original_dir failed: $!" 12 0 12
49 12 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 12 12
50 0 for my $k (keys %ls) {
51 0 print "$k $ls{$k}\n";
52 }
53 }
54 12 return %ls;
55 }
56  
57 sub get_only
58 5 ▶{
Subroutine get_only: called CC=7 SCAR=22.0
Pod documented
59 5 my ($ls_dir1_ref, $ls_dir2_ref, $verbose) = @_;
60  
61 5 ▶ if (ref ($ls_dir1_ref) ne "HASH" ||
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ls_dir1_ref ne "HASH" or ref $ls_dir2_ref ne "HASH") 0 5 5
Condition33%1/3
ref $ls_dir1_ref ne "HASH" or ref $ls_dir2_ref ne "HASH"
l!l&&r!l&&!r
005
MC/DC0%0/2
ref $ls_dir1_ref ne "HASH" or ref $ls_dir2_ref ne "HASH"
ref $ls_dir1_ref ne "HASH" ref $ls_dir2_ref ne "HASH"
Truth table33%1/3
A or B
A= ref $ls_dir1_ref ne "HASH"B= ref $ls_dir2_ref ne "HASH"
ABresult
1X1
011
000
62 ref ($ls_dir2_ref) ne "HASH") {
63 0 croak "get_only requires hash references as arguments";
64 }
65 5 my %only;
66  
67 # d1e = directory one entry
68
69 5 for my $d1e (keys %$ls_dir1_ref) {
70 13 ▶ if (! $ls_dir2_ref->{$d1e}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ls_dir2_ref->{$d1e}) 5 8 13
71 5 $only{$d1e} = 1;
72 5 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 5 5
73 0 print "$d1e is only in first directory.\n";
74 }
75 }
76 }
77 5 ▶ if (! wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
unless (wantarray) 0 5 5
78 0 croak "bad call";
79 }
80 5 return %only;
81 }
82  
83 sub get_diff
84 4 ▶{
Subroutine get_diff: called CC=8 SCAR=22.0
Pod documented
85 4 my ($dir1, $ls_dir1_ref, $dir2, $ls_dir2_ref) = @_;
86 4 ▶ if (ref ($ls_dir1_ref) ne "HASH" ||
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ls_dir1_ref ne "HASH" or ref $ls_dir2_ref ne "HASH") 0 4 4
Condition33%1/3
ref $ls_dir1_ref ne "HASH" or ref $ls_dir2_ref ne "HASH"
l!l&&r!l&&!r
004
MC/DC0%0/2
ref $ls_dir1_ref ne "HASH" or ref $ls_dir2_ref ne "HASH"
ref $ls_dir1_ref ne "HASH" ref $ls_dir2_ref ne "HASH"
Truth table33%1/3
A or B
A= ref $ls_dir1_ref ne "HASH"B= ref $ls_dir2_ref ne "HASH"
ABresult
1X1
011
000
87 ref ($ls_dir2_ref) ne "HASH") {
88 0 croak "get_diff requires hash references as arguments 2 and 4";
89 }
90 4 my %different;
91 4 for my $file (keys %$ls_dir1_ref) {
92 8 my $d1file = "$dir1/$file";
93 8 ▶ if ($ls_dir2_ref->{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ls_dir2_ref->{$file}) 8 0 8
94 8 ▶ if (! -f $d1file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $d1file) 2 6 8
95 # croak "Bad file / directory combination $d1file";
96 2 next;
97 }
98 6 my $d2file = "$dir2/$file";
99 6 ▶ if (compare ($d1file, $d2file) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (compare($d1file, $d2file) != 0) 4 2 6
100 4 $different{$file} = 1;
101 }
102 }
103 }
104 4 ▶ if (! wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
unless (wantarray) 0 4 4
105 0 croak "Bad call";
106 }
107 4 return %different;
108 }
109  
110 sub directory_diff
111 2 ▶{
Subroutine directory_diff: called CC=15 SCAR=32.6
Pod documented
112 2 my ($dir1, $dir2, $callback_ref, $verbose) = @_;
113 2 ▶ if (! $dir1 || ! $dir2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir1 and $dir2) 0 2 2
Condition33%1/3
$dir1 and $dir2
!ll&&!rl&&r
002
MC/DC0%0/2
$dir1 and $dir2
$dir1 $dir2
Truth table33%1/3
A and B
A= $dir1B= $dir2
ABresult
0X0
100
111
114 0 croak "directory_diff requires two directory names";
115 }
116 2 ▶ if (! -d $dir1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir1) 0 2 2
117 0 croak "directory_diff: first directory '$dir1' does not exist";
118 }
119 2 ▶ if (! -d $dir2) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir2) 0 2 2
120 0 croak "directory_diff: second directory '$dir2' does not exist";
121 }
122 2 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 2 2
123 0 print "Directory diff of $dir1 and $dir2 in progress ...\n";
124 }
125 2 ▶ if (! $callback_ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($callback_ref) 0 2 2
126 0 croak "directory_diff: no callbacks supplied";
127 }
128 2 ▶ if (ref $callback_ref ne "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $callback_ref ne "HASH") 0 2 2
129 0 croak "directory_diff: callback not hash reference";
130 }
131 2 my %ls_dir1 = ls_dir ($dir1, $verbose);
132 2 my %ls_dir2 = ls_dir ($dir2, $verbose);
133 # Data to pass to called back functions.
134 2 my $data = $callback_ref->{data};
135 # Call back a function on each file which is only in directory 1.
136 2 my $d1cb = $callback_ref->{dir1_only};
137 2 ▶ if ($d1cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d1cb) 1 1 2
138 # Files which are only in directory 1.
139 1 my %dir1_only = get_only (\%ls_dir1, \%ls_dir2, $verbose);
140 1 for my $file (keys %dir1_only) {
141 0 &{$d1cb} ($data, $dir1, $file, $verbose);
142 }
143 }
144 # Call back a function on each file which is only in directory 2.
145 2 my $d2cb = $callback_ref->{dir2_only};
146 2 ▶ if ($d2cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d2cb) 2 0 2
147 # Files which are only in directory 2.
148 2 my %dir2_only = get_only (\%ls_dir2, \%ls_dir1, $verbose);
149 2 for my $file (keys %dir2_only) {
150 0 &{$d2cb} ($data, $dir2, $file, $verbose);
151 }
152 }
153 # Call back a function on each file which is in both directories
154 # but different.
155 2 my $diff_cb = $callback_ref->{diff};
156 2 ▶ if ($diff_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff_cb) 2 0 2
157 # Files which are in both directories but are different.
158 2 my %diff_files = get_diff ($dir1, \%ls_dir1, $dir2, \%ls_dir2, $verbose);
159 2 for my $file (keys %diff_files) {
160 2 &{$diff_cb} ($data, $dir1, $dir2, $file, $verbose);
161 }
162 }
163 2 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 0 2 2
164 0 carp "directory_diff does not return a meaningful value";
165 }
166 2 return;
167 }
168  
169 2 1;
170  
↑ Summary Diff/Copy.pm »