App/annex_review_unused.pm

filter: Statementstmt 27.0% 30 / 111 Branchbran 1.7% 1 / 56 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 50.0% 9 / 18 Podpod n/a totaltotal 17.9% 40 / 223 CC 38 SCAR 68.5
CC
38
Cov
16%
main
1406.0
exit
6.0
__ANON__
2.0
CRAP 946.5
↑ Summary Git/Annex.pm »
1 package App::annex_review_unused;
2 # ABSTRACT: interactively process 'git annex unused' output
3 #
4 # Copyright (C) 2019-2020 Sean Whitton <spwhitton@spwhitton.name>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or (at
9 # your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 1 $App::annex_review_unused::VERSION = '0.004';
19 1 ▶use 5.028;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use subs qw(main exit);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Git::Annex;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 1 my $exit_main = 0;
31  
32 1 ▶CORE::exit main unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 1 1
33  
34  
35 sub main {
36 0 ▶ shift if $_[0] and ref $_[0] eq ""; # in case main called as a class method
Subroutine main: not called CC=37 SCAR=72.5
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and ref $_[0] eq "" 0 0 0
Condition0%0/3
$_[0] and ref $_[0] eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and ref $_[0] eq ""
$_[0] ref $_[0] eq ""
Truth table0%0/3
A and B
A= $_[0]B= ref $_[0] eq ""
ABresult
0X0
100
111
37 0 ▶ local @ARGV = @{ $_[0] } if $_[0] and ref $_[0] ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and ref $_[0] ne "" 0 0 0
Condition0%0/3
$_[0] and ref $_[0] ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and ref $_[0] ne ""
$_[0] ref $_[0] ne ""
Truth table0%0/3
A and B
A= $_[0]B= ref $_[0] ne ""
ABresult
0X0
100
111
38  
39 0 my $annex = Git::Annex->new;
40  
41 0 my $just_print = 0;
42 0 my ($uuid, $from_arg, $used_refspec_arg, %unused_opts, %dropunused_args);
43 0 GetOptions
44 'from=s' => \$from_arg,
45 'used-refspec=s' => \$used_refspec_arg,
46 'just-print' => \$just_print;
47 0 ▶ if ($from_arg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_arg) 0 0 0
48 0 $unused_opts{from} = $dropunused_args{from} = $from_arg;
49 #<<<
50 try {
51 0 ▶ ($uuid) = $annex->git->config("remote." . $from_arg . ".annex-uuid");
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 } catch {
53 0 ▶ die "couldn't determine an annex UUID for $from_arg remote";
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 0 };
55 #>>>
56 }
57 0 ▶ $unused_opts{used_refspec} = $used_refspec_arg if $used_refspec_arg;
Branch: 1 branch
BranchTrueFalseTotal
if $used_refspec_arg 0 0 0
58  
59 0 my @to_drop;
60 0 my @unused_files = @{ $annex->unused(%unused_opts, log => 1) };
61 0 ▶ exit unless @unused_files;
Branch: 1 branch
BranchTrueFalseTotal
unless @unused_files 0 0 0
62 0 ▶ if ($just_print) {
Branch: 1 branch
BranchTrueFalseTotal
if ($just_print) 0 0 0
63 0 _say_spaced_bullet("There are unused files you can drop with"
64 . " `git annex dropunused':");
65 0 say " " . $_->{number} . " " . $_->{key} for @unused_files;
66 0 print "\n";
67 }
68 0 my $i = 0;
69 0 ▶ UNUSED: while ($i < @unused_files) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @unused_files) 0 0 0
70 0 my $unused_file = $unused_files[$i];
71  
72 # check the unused file still exists i.e. has not been dropped
73 # already (in the case of reviewing unused files at a remote, just
74 # check that it's not been dropped according to the local
75 # git-annex branch by using readpresentkey rather than
76 # checkpresentkey)
77 0 my $contentlocation = $annex->abs_contentlocation($unused_file->{key});
78 0 ▶ if ($from_arg) {
Branch: 2 branches
BranchTrueFalseTotal
if ($from_arg) { } 0 0 0
elsif (not $contentlocation) { } 0 0 0
79 #<<<
80 try {
81 0 ▶ $annex->annex->readpresentkey($unused_file->{key}, $uuid);
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 } catch {
83 0 ▶ splice @unused_files, $i, 1;
Subroutine __ANON__: not called CC=1 SCAR=6.9
84 0 next UNUSED;
85 0 };
86 #>>>
87 } elsif (!$contentlocation) {
88 0 splice @unused_files, $i, 1;
89 0 next UNUSED;
90 }
91  
92 0 ▶ system qw(clear -x) unless $just_print;
Branch: 1 branch
BranchTrueFalseTotal
unless $just_print 0 0 0
93 0 _say_bold("unused file #" . $unused_file->{number} . ":");
94  
95 0 ▶ if ($unused_file->{bad} or $unused_file->{tmp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unused_file->{'bad'} or $unused_file->{'tmp'}) { } 0 0 0
Condition0%0/3
$unused_file->{"bad"} or $unused_file->{"tmp"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$unused_file->{"bad"} or $unused_file->{"tmp"}
$unused_file->{"bad"} $unused_file->{"tmp"}
Truth table0%0/3
A or B
A= $unused_file->{"bad"}B= $unused_file->{"tmp"}
ABresult
1X1
011
000
96 say " looks like stale tmp or bad file, with key "
97 0 . $unused_file->{key};
98 } else {
99 0 my @log_lines = map { s/^/ /r } @{ $unused_file->{log_lines} };
100 0 ▶ unless ($just_print) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($just_print) 0 0 0
101 # truncate log output if necessary to ensure user's
102 # terminal does not scroll
103 0 my (undef, $height) = GetTerminalSize;
104 0 ▶ splice @log_lines, $height - (5 + @log_lines)
Branch: 1 branch
BranchTrueFalseTotal
if 5 + @log_lines > $height 0 0 0
105 if 5 + @log_lines > $height;
106 }
107 0 print "\n";
108 0 say for @log_lines;
109 0 ▶ unless ($just_print) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($just_print) 0 0 0
110 0 my $response;
111 0 READKEY: while (1) {
112 # before prompting, clear out stdin, to avoid
113 # registered a keypress more than once
114 0 ReadMode 4;
115 0 ▶ 1 while defined ReadKey -1;
Branch: 1 branch
BranchTrueFalseTotal
defined ReadKey(-1) and do { '???' } 0 0 0
116  
117 0 my @opts = ('y', 'n');
118 0 ▶ push @opts, 'o' if $contentlocation;
Branch: 1 branch
BranchTrueFalseTotal
if $contentlocation 0 0 0
119 0 ▶ push @opts, ('d', 'b') if $i > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i > 0 0 0 0
120 0 print "Drop this unused files? ("
121 . join('/', @opts) . ") ";
122  
123 # Term::ReadKey docs recommend ReadKey(-1) but that
124 # means we need an infinite loop calling ReadKey(-1)
125 # over and over, which ramps up system load
126 0 my $response = ReadKey(0);
127 0 ReadMode 0;
128  
129 # respond to C-c
130 0 ▶ exit 0 if ord $response == 3;
Branch: 1 branch
BranchTrueFalseTotal
if ord $response == 3 0 0 0
131  
132 0 say $response;
133 0 $response = lc($response);
134 0 ▶ if ($response eq "y") {
Branch: 5 branches
BranchTrueFalseTotal
if ($response eq 'y') { } 0 0 0
elsif ($response eq 'n') { } 0 0 0
elsif ($response eq 'o' and defined $contentlocation) { } 0 0 0
elsif ($response eq 'b' and $i > 0) { } 0 0 0
elsif ($response eq 'd' and $i > 0) { } 0 0 0
135 0 push @to_drop, $unused_file->{number};
136 0 last READKEY;
137 } elsif ($response eq "n") {
138 0 last READKEY;
139 ▶ } elsif ($response eq "o" and defined $contentlocation) {
Condition0%0/3
$response eq "o" and defined $contentlocation
!ll&&!rl&&r
000
MC/DC0%0/2
$response eq "o" and defined $contentlocation
$response eq "o" defined $contentlocation
Truth table0%0/3
A and B
A= $response eq "o"B= defined $contentlocation
ABresult
0X0
100
111
140 0 system "xdg-open", $contentlocation;
141 ▶ } elsif ($response eq "b" and $i > 0) {
Condition0%0/3
$response eq "b" and $i > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$response eq "b" and $i > 0
$response eq "b" $i > 0
Truth table0%0/3
A and B
A= $response eq "b"B= $i > 0
ABresult
0X0
100
111
142 0 $i--;
143 pop @to_drop
144 if @to_drop
145 and $to_drop[$#to_drop] eq
146 0 ▶ $unused_files[$i]->{number};
Branch: 1 branch
BranchTrueFalseTotal
if @to_drop and $to_drop[$#to_drop] eq $unused_files[$i]{"number"} 0 0 0
Condition0%0/3
@to_drop and $to_drop[$#to_drop] eq $unused_files[$i]{"number"}
!ll&&!rl&&r
000
MC/DC0%0/2
@to_drop and $to_drop[$#to_drop] eq $unused_files[$i]{"number"}
@to_drop $to_drop[$#to_drop] eq $unused_files[$i]{"number"}
Truth table0%0/3
A and B
A= @to_dropB= $to_drop[$#to_drop] eq $unused_files[$i]{"number"}
ABresult
0X0
100
111
147 0 next UNUSED;
148 ▶ } elsif ($response eq "d" and $i > 0) {
Condition0%0/3
$response eq "d" and $i > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$response eq "d" and $i > 0
$response eq "d" $i > 0
Truth table0%0/3
A and B
A= $response eq "d"B= $i > 0
ABresult
0X0
100
111
149 # user wants to drop the list we've accumulated up
150 # until now and get out of this script
151 0 last UNUSED;
152 } else {
153 0 say "invalid response";
154 }
155 }
156 }
157 }
158 0 print "\n";
159 0 $i++;
160 }
161  
162 0 ▶ if (@to_drop) {
Branch: 1 branch
BranchTrueFalseTotal
if (@to_drop) 0 0 0
163 _say_spaced_bullet("Will dropunused"
164 0 ▶ . (exists $dropunused_args{force} ? " with --force:" : ":"));
Branch: 1 branch
BranchTrueFalseTotal
exists $dropunused_args{'force'} ? : 0 0 0
165 0 say "@to_drop\n";
166 0 ▶ $annex->annex->dropunused(\%dropunused_args, @to_drop)
Branch: 1 branch
BranchTrueFalseTotal
if prompt_yn("Go ahead with this?") 0 0 0
167 if prompt_yn("Go ahead with this?");
168 }
169  
170 # exit value represents whether or not there are any unused files left
171 # after this run. note that in --just-print mode, @to_drop will be
172 # empty, so we'll always exit non-zero if there are any unused files
173 0 exit(@to_drop != @unused_files);
174  
175 0 EXIT_MAIN:
176 return $exit_main;
177 }
178  
179 0 ▶sub _say_bold { print colored(['bold'], @_), "\n" }
Subroutine _say_bold: not called CC=1 SCAR=6.9
180  
181 0 ▶sub _say_bullet { _say_bold(" • ", @_) }
Subroutine _say_bullet: not called CC=1 SCAR=6.9
182  
183 0 ▶sub _say_spaced_bullet { _say_bold("\n", " • ", @_, "\n") }
Subroutine _say_spaced_bullet: not called CC=1 SCAR=6.9
184  
185 0 ▶sub exit { $exit_main = shift // 0; goto EXIT_MAIN }
Subroutine exit: not called CC=2 SCAR=17.9
Condition0%0/2
shift() // 0
l!l
00
MC/DC0%0/1
shift() // 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
186  
187 1 1;
188  
189 __END__
↑ Summary Git/Annex.pm »