| 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
| |||||||||||||||||||||||||||||||||
| 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"}
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"}
| |||||||||||||||||||||||||||||||||
| 30 | 17 | ▶ | $args{source} = [delete $args{source}] unless ref $args{source} eq 'ARRAY'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||
| 34 | croak sprintf("target `%s' is not a directory\n", $dest) | ||||||||||||||||||||||||||||||||
| 35 | 17 | ▶ | if @{ $args{source} } > 1 and !$dest_is_dir; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@{$args{"source"};} > 1 and not $dest_is_dir
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
| |||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | 16 | ▶ | STDOUT->autoflush(1) if $VERBOSE; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 38 | 16 | foreach my $from ( map { path($_) } @{ $args{source} } ) { | |||||||||||||||||||||||||||||||
| 39 | 18 | ▶ | print "\r${from}\e[K" if $VERBOSE == 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 18 | ▶ | unless ($from->exists) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 46 | 18 | ▶ | croak sprintf("`%s' and `%s' are the same file\n", $to, $from) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 47 | if $from->absolute eq $to->absolute; | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 17 | ▶ | if ($to->exists) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 50 | 11 | ▶ | if ($args{deduplicate}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 51 | 4 | ▶ | print STDERR "File already exists; checking for duplication..." | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 52 | if $VERBOSE > 1; | ||||||||||||||||||||||||||||||||
| 53 | 4 | ▶ | if ($duplicates->($from, $to)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 54 | 2 | ▶ | printf STDERR | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$prefix // $to->basename
MC/DC0%0/2
$prefix // $to->basename
$prefix
$to->basename
Truth table66%2/3
A // B
A= $prefixB= $to->basename
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | printf STDERR "File can't be renamed across filesystems; copying `%s' to `%s' instead...", | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 94 | 0 | $from->remove; | |||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||||||
| 97 | 0 | die $_; | |||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||
| 99 | 15 | }; | |||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | 15 | 1; | |||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | __END__ | ||||||||||||||||||||||||||||||||