App/git_annex_reviewunused.pm

filter: Statementstmt 25.4% 30 / 118 Branchbran 1.7% 1 / 56 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 47.3% 9 / 19 Podpod n/a totaltotal 17.3% 40 / 231 CC 38 SCAR 66.7
CC
38
Cov
16%
main
1260.0
_prompt_yn
12.0
exit
6.0
CRAP 788.8
↑ Summary Git/Annex.pm »
1 package App::git_annex_reviewunused;
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::git_annex_reviewunused::VERSION = '0.008';
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 0 ▶sub main {
Subroutine main: not called CC=35 SCAR=71.4
36 0 ▶ shift if $_[0] and ref $_[0] eq ""; # in case main called as a class method
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 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 0 ($uuid) = $annex->git->config("remote." . $from_arg . ".annex-uuid");
52 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
53 0 die "couldn't determine an annex UUID for $from_arg remote";
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 my @unused_files = grep {
61 # check the unused file still exists i.e. has not been dropped
62 # already (in the case of reviewing unused files at a remote,
63 # just check that it's not been dropped according to the local
64 # git-annex branch by using readpresentkey rather than
65 # checkpresentkey)
66  
67 0 my $ret = $_->{contentlocation}
68 0 = $annex->abs_contentlocation($_->{key});
69  
70 0 ▶ if ($from_arg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_arg) 0 0 0
71 #<<<
72 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 0 $annex->annex->readpresentkey($_->{key}, $uuid);
74 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 0 $ret = 0;
76 0 };
77 #>>>
78 }
79  
80 0 $ret;
81 } $annex->unused(%unused_opts, log => 1)->@*;
82 0 ▶ exit unless @unused_files;
Branch: 1 branch
BranchTrueFalseTotal
unless @unused_files 0 0 0
83 0 ▶ if ($just_print) {
Branch: 1 branch
BranchTrueFalseTotal
if ($just_print) 0 0 0
84 0 _say_spaced_bullet("There are unused files you can drop with"
85 . " `git annex dropunused':");
86 0 say " " . $_->{number} . " " . $_->{key} for @unused_files;
87 0 print "\n";
88 }
89 0 my $i = 0;
90 0 ▶ UNUSED: while ($i < @unused_files) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @unused_files) 0 0 0
91 0 my $unused_file = $unused_files[$i];
92 0 my $contentlocation = $unused_file->{contentlocation};
93  
94 0 ▶ system qw(clear -x) unless $just_print;
Branch: 1 branch
BranchTrueFalseTotal
unless $just_print 0 0 0
95 0 _say_bold("unused file #" . $unused_file->{number} . ":");
96  
97 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
98 say " looks like stale tmp or bad file, with key "
99 0 . $unused_file->{key};
100 } else {
101 0 my @log_lines = map { s/^/ /r } @{ $unused_file->{log_lines} };
102 0 ▶ unless ($just_print) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($just_print) 0 0 0
103 # truncate log output if necessary to ensure user's
104 # terminal does not scroll
105 0 my (undef, $height) = GetTerminalSize;
106 0 ▶ splice @log_lines, $height - (5 + @log_lines)
Branch: 1 branch
BranchTrueFalseTotal
if 5 + @log_lines > $height 0 0 0
107 if 5 + @log_lines > $height;
108 }
109 0 print "\n";
110 0 say for @log_lines;
111 0 ▶ unless ($just_print) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($just_print) 0 0 0
112 0 my $response;
113 0 READKEY: while (1) {
114 # before prompting, clear out stdin, to avoid
115 # registered a keypress more than once
116 0 ReadMode 4;
117 0 ▶ 1 while defined ReadKey(-1);
Branch: 1 branch
BranchTrueFalseTotal
defined ReadKey(-1) and do { '???' } 0 0 0
118  
119 0 my @opts = ('y', 'n');
120 0 ▶ push @opts, 'o' if $contentlocation;
Branch: 1 branch
BranchTrueFalseTotal
if $contentlocation 0 0 0
121 0 ▶ push @opts, ('d', 'b') if $i > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i > 0 0 0 0
122 0 print "Drop this unused files? ("
123 . join('/', @opts) . ") ";
124  
125 # Term::ReadKey docs recommend ReadKey(-1) but that
126 # means we need an infinite loop calling ReadKey(-1)
127 # over and over, which ramps up system load
128 0 my $response = ReadKey(0);
129 0 ReadMode 0;
130  
131 # respond to C-c
132 0 ▶ exit 0 if ord $response == 3;
Branch: 1 branch
BranchTrueFalseTotal
if ord $response == 3 0 0 0
133  
134 0 say $response;
135 0 $response = lc($response);
136 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
137 0 push @to_drop, $unused_file->{number};
138 0 last READKEY;
139 } elsif ($response eq "n") {
140 0 last READKEY;
141 ▶ } 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
142 0 system "xdg-open", $contentlocation;
143 ▶ } 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
144 0 $i--;
145 pop @to_drop
146 if @to_drop
147 and $to_drop[$#to_drop] eq
148 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
149 0 next UNUSED;
150 ▶ } 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
151 # user wants to drop the list we've accumulated up
152 # until now and get out of this script
153 0 last UNUSED;
154 } else {
155 0 say "invalid response";
156 }
157 }
158 }
159 }
160 0 print "\n";
161 0 $i++;
162 }
163  
164 0 ▶ if (@to_drop) {
Branch: 1 branch
BranchTrueFalseTotal
if (@to_drop) 0 0 0
165 0 _say_spaced_bullet("Will dropunused with --force:");
166 0 say "@to_drop\n";
167 0 ▶ $annex->annex->dropunused(\%dropunused_args, "--force", @to_drop)
Branch: 1 branch
BranchTrueFalseTotal
if _prompt_yn("Go ahead with this?") 0 0 0
168 if _prompt_yn("Go ahead with this?");
169 }
170  
171 # exit value represents whether or not there are any unused files left
172 # after this run. note that in --just-print mode, @to_drop will be
173 # empty, so we'll always exit non-zero if there are any unused files
174 0 exit(@to_drop != @unused_files);
175  
176 0 EXIT_MAIN:
177 return $exit_main;
178 }
179  
180 0 ▶sub _say_bold { print colored(['bold'], @_), "\n" }
Subroutine _say_bold: not called CC=1 SCAR=6.9
181  
182 0 ▶sub _say_bullet { _say_bold(" • ", @_) }
Subroutine _say_bullet: not called CC=1 SCAR=6.9
183  
184 0 ▶sub _say_spaced_bullet { _say_bold("\n", " • ", @_, "\n") }
Subroutine _say_spaced_bullet: not called CC=1 SCAR=6.9
185  
186 0 ▶sub _prompt_yn {
Subroutine _prompt_yn: not called CC=3 SCAR=24.8
187 0 my $prompt = shift;
188 0 local $| = 1;
189 0 my $response;
190 0 while (1) {
191 0 print colored(['bold'], "$prompt ");
192 0 chomp(my $response = <STDIN>);
193 0 ▶ return 1 if lc($response) eq "y";
Branch: 1 branch
BranchTrueFalseTotal
if lc $response eq "y" 0 0 0
194 0 ▶ return 0 if lc($response) eq "n";
Branch: 1 branch
BranchTrueFalseTotal
if lc $response eq "n" 0 0 0
195 0 say "invalid response";
196 }
197 }
198  
199 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
200  
201 1 1;
202  
203 __END__
↑ Summary Git/Annex.pm »