blib/lib/App/mvr.pm

filter: Statementstmt 87.1% 61 / 70 Branchbran 72.5% 29 / 40 Conditioncond 66.6% 8 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 91.6% 11 / 12 Podpod 100% 1 / 1 totaltotal 79.0% 113 / 143 CC 26 SCAR 29.7
CC
26
Cov
80%
mvr
29.8
__ANON__
12.6
__ANON__
2.0
CRAP 19.4
↑ Summary bin/mvr »
1 package App::mvr;
2 15 ▶use v5.14.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 # ABSTRACT: like mv, but clever
6 15 our $VERSION = '0.005'; # VERSION
7  
8 15 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 our @EXPORT = qw(mvr);
10  
11 15 ▶use Path::Tiny 0.034;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 15 our $VERBOSE = 0;
16  
17  
18 4 ▶my $duplicates = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
19 4 my $A = shift;
20 4 my $B = shift;
21 4 ▶ return if $A->stat->size != $B->stat->size; # avoid reading file off disk
Branch: 1 branch
BranchTrueFalseTotal
if $A->stat->size != $B->stat->size 0 4 4
22  
23 # Pull out the big guns
24 4 return $A->digest eq $B->digest;
25 15 };
26  
27 17 ▶sub mvr {
Subroutine mvr: called CC=22 SCAR=33.9
Pod documented
28 17 my %args = @_;
29 17 ▶ $args{dest} //= delete $args{destination};
Condition33%1/3
$args{"dest"} //= delete $args{"destination"}
l!l&&r!l&&!r
1700
MC/DC0%0/2
$args{"dest"} //= delete $args{"destination"}
$args{"dest"} delete $args{"destination"}
Truth table33%1/3
A //= B
A= $args{"dest"}B= delete $args{"destination"}
ABresult
1X1
011
000
30 17 ▶ $args{source} = [delete $args{source}] unless ref $args{source} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"source"} eq "ARRAY" 5 12 17
31  
32 17 my $dest = path( $args{dest} );
33 17 ▶ my $dest_is_dir = $dest->exists && $dest->is_dir;
Condition66%2/3
$dest->exists && $dest->is_dir
!ll&&!rl&&r
098
MC/DC50%1/2
$dest->exists && $dest->is_dir
$dest->exists $dest->is_dir
Truth table66%2/3
A && B
A= $dest->existsB= $dest->is_dir
ABresult
0X0
100
111
34 croak sprintf("target `%s' is not a directory\n", $dest)
35 17 ▶ if @{ $args{source} } > 1 and !$dest_is_dir;
Branch: 1 branch
BranchTrueFalseTotal
if @{$args{"source"};} > 1 and not $dest_is_dir 1 16 17
Condition100%3/3
@{$args{"source"};} > 1 and not $dest_is_dir
!ll&&!rl&&r
1511
MC/DC100%2/2
@{$args{"source"};} > 1 and not $dest_is_dir
@{$args{"source"};} > 1 not $dest_is_dir
Truth table100%3/3
A and not(B)
A= @{$args{"source"};} > 1B= not $dest_is_dir
ABresult
0X0
100
111
36  
37 16 ▶ STDOUT->autoflush(1) if $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $App::mvr::VERBOSE 12 4 16
38 16 foreach my $from ( map { path($_) } @{ $args{source} } ) {
39 18 ▶ print "\r${from}\e[K" if $VERBOSE == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $App::mvr::VERBOSE == 1 6 12 18
40  
41 18 ▶ unless ($from->exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($from->exists) 0 18 18
42 0 carp sprintf("Cannot stat `%s': No such file or directory\n", $from);
43 0 next;
44 }
45 18 ▶ my $to = path( $dest, ($dest_is_dir ? $from->basename : ()) );
Branch: 1 branch
BranchTrueFalseTotal
$dest_is_dir ? : 10 8 18
46 18 ▶ croak sprintf("`%s' and `%s' are the same file\n", $to, $from)
Branch: 1 branch
BranchTrueFalseTotal
if $from->absolute eq $to->absolute 1 17 18
47 if $from->absolute eq $to->absolute;
48  
49 17 ▶ if ($to->exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to->exists) 11 6 17
50 11 ▶ if ($args{deduplicate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"deduplicate"}) 4 7 11
51 4 ▶ print STDERR "File already exists; checking for duplication..."
Branch: 1 branch
BranchTrueFalseTotal
if $App::mvr::VERBOSE > 1 4 0 4
52 if $VERBOSE > 1;
53 4 ▶ if ($duplicates->($from, $to)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$duplicates($from, $to)) { } 2 2 4
54 2 ▶ printf STDERR
Branch: 1 branch
BranchTrueFalseTotal
if $App::mvr::VERBOSE > 1 2 0 2
55 " `%s' and `%s' are duplicates; removing the source file.\n",
56 $from->basename, $to->basename
57 if $VERBOSE > 1;
58 2 $from->remove;
59 2 $to->touch;
60 2 next;
61 }
62 else {
63 2 ▶ printf STDERR
Branch: 1 branch
BranchTrueFalseTotal
if $App::mvr::VERBOSE > 1 2 0 2
64 " `%s' and `%s' are not duplicates.\n",
65 $from->basename, $to->basename
66 if $VERBOSE > 1;
67 }
68 }
69  
70 9 my ($prefix, $suffix) = $to->basename =~ m{^(.*)\.(\w+)$};
71 9 ▶ $to = Path::Tiny->tempfile(
Branch: 2 branches
BranchTrueFalseTotal
$dest_is_dir ? : 4 5 9
$suffix ? : 2 7 9
Condition66%2/3
$prefix // $to->basename
l!l&&r!l&&!r
270
MC/DC0%0/2
$prefix // $to->basename
$prefix $to->basename
Truth table66%2/3
A // B
A= $prefixB= $to->basename
ABresult
1X1
011
000
72 UNLINK => 0,
73 TEMPLATE => ($prefix // $to->basename) . '-XXXXXX',
74 DIR => $dest_is_dir ? $dest : $dest->dirname,
75 ( $suffix ? (SUFFIX => ".$suffix") : () ),
76 );
77 9 ▶ printf STDERR "File already exists; renaming `%s' to `%s'\n",
Branch: 1 branch
BranchTrueFalseTotal
if $App::mvr::VERBOSE > 1 4 5 9
78 $from->basename, $to->basename
79 if $VERBOSE > 1;
80 }
81  
82 15 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
83 15 $from->move($to);
84 }
85 0 ▶ catch {
Subroutine __ANON__: not called CC=4 SCAR=25.3
86 15 ▶ use POSIX qw(:errno_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
87 0 ▶ if ($_->{err} == EXDEV) { # Invalid cross-device link
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{'err'} == 18) { } 0 0 0
88 0 ▶ printf STDERR "File can't be renamed across filesystems; copying `%s' to `%s' instead...",
Branch: 1 branch
BranchTrueFalseTotal
if $App::mvr::VERBOSE > 1 0 0 0
89 $from->basename, $to->basename
90 if $VERBOSE > 1;
91 0 $from->copy($to);
92 0 $to->touch( $from->stat->mtime );
93 0 ▶ print STDERR " done. Removing original file.\n" if $VERBOSE > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $App::mvr::VERBOSE > 1 0 0 0
94 0 $from->remove;
95 }
96 else {
97 0 die $_;
98 }
99 15 };
100 }
101 }
102  
103 15 1;
104  
105 __END__
↑ Summary bin/mvr »