Git/Annex.pm

filter: Statementstmt 50.7% 64 / 126 Branchbran 17.6% 6 / 34 Conditioncond 9.3% 3 / 32 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 61.2% 19 / 31 Podpod 100% 7 / 7 totaltotal 40.0% 99 / 247 CC 33 SCAR 53.2
CC
33
Cov
38%
unused
552.0
new
6.2
_unused_cache
6.0
CRAP 203.4
« App/git_annex_reviewunused.pm ↑ Summary App/annex_to_annex.pm »
1 package Git::Annex;
2 # ABSTRACT: Perl interface to git-annex repositories
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 12 $Git::Annex::VERSION = '0.008';
19  
20 12 ▶use 5.028;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 12 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 12 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 12 ▶use Git::Wrapper;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 12 ▶use Git::Repository;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 12 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 12 ▶use File::Spec::Functions qw(catfile rel2abs);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 12 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 12 ▶use Data::Compare;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 12 ▶use List::Util qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 12 ▶use Time::HiRes qw(stat time);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 12 ▶use Git::Annex::BatchCommand;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 12 ▶use IPC::System::Simple qw(capturex);
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37  
38 14 ▶sub new {
Subroutine new: called CC=6 SCAR=18.2
Pod documented
39 14 my ($class, $toplevel) = @_;
40  
41 14 ▶ $toplevel = $toplevel ? rel2abs($toplevel) : getcwd;
Branch: 1 branch
BranchTrueFalseTotal
$toplevel ? : 10 4 14
42  
43 # if we're in a working tree, rise up to the root of the working
44 # tree -- for flexibility, don't require that we're actually in a
45 # git repo at all
46 14 my $pid = fork;
47 14 ▶ die "fork() failed: $!" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 14 14
48 14 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 10 4 14
49 10 wait;
50 10 ▶ chomp($toplevel = capturex "git",
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 10 10
51 "-C", $toplevel, "rev-parse", "--show-toplevel")
52 if $?;
53 } else {
54 4 close STDERR;
55 4 my $output;
56 4 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
57 4 $output = capturex "git", "-C", $toplevel, "rev-parse",
58 "--is-inside-work-tree";
59 4 };
60 4 ▶ exit($output and $output =~ /true/);
Condition33%1/3
$output && $output =~ /true/u
!ll&&!rl&&r
400
MC/DC0%0/2
$output && $output =~ /true/u
$output $output =~ /true/u
Truth table33%1/3
A && B
A= $outputB= $output =~ /true/u
ABresult
0X0
100
111
61 }
62  
63 10 bless { toplevel => $toplevel } => $class;
64 }
65  
66  
67 12 ▶sub toplevel { shift->{toplevel} }
Subroutine toplevel: called CC=1 SCAR=0.0
Pod documented
68  
69  
70 2 ▶sub git {
Subroutine git: called CC=2 SCAR=7.1
Pod documented
71 2 my $self = shift;
72 2 ▶ $self->{git} //= Git::Wrapper->new($self->toplevel);
Condition66%2/3
$self->{"git"} //= "Git::Wrapper"->new($self->toplevel)
l!l&&r!l&&!r
110
MC/DC0%0/2
$self->{"git"} //= "Git::Wrapper"->new($self->toplevel)
$self->{"git"} "Git::Wrapper"->new($self->toplevel)
Truth table66%2/3
A //= B
A= $self->{"git"}B= "Git::Wrapper"->new($self->toplevel)
ABresult
1X1
011
000
73 }
74  
75 # =attr repo
76  
77 # Returns an instance of L<Git::Repository> initialised in the repository.
78  
79 # =cut
80  
81 # has repo => (
82 # is => 'lazy',
83 # # we don't know (here) whether our repo is bare or not, so we
84 # # don't know whether to use the git_dir or work_tree arguments to
85 # # Git::Repository::new, so we chdir and let call without arguments
86 # default => sub { local $CWD = shift->toplevel; Git::Repository->new });
87  
88  
89 0 ▶sub unused {
Subroutine unused: not called CC=23 SCAR=63.1
Pod documented
90 0 my ($self, %opts) = @_;
91 0 ▶ $opts{log} //= 0;
Condition0%0/2
$opts{"log"} //= 0
l!l
00
MC/DC0%0/1
$opts{"log"} //= 0
$opts{"log"}
Truth table0%0/2
A
A= $opts{"log"}
Aresult
11
00
92 0 my $used_refspec_config;
93 0 ▶ try { ($used_refspec_config) = $self->git->config("annex.used-refspec") };
Subroutine __ANON__: not called CC=1 SCAR=6.9
94 $opts{used_refspec}
95 0 ▶ //= ($used_refspec_config // "+refs/heads/*:-refs/heads/synced/*");
Condition0%0/4
$opts{"used_refspec"} //= $used_refspec_config // "+refs/heads/*:-refs/heads/synced/*"
l!l
00
$used_refspec_config // "+refs/heads/*:-refs/heads/synced/*"
l!l
00
MC/DC0%0/1
$opts{"used_refspec"} //= $used_refspec_config // "+refs/heads/*:-refs/heads/synced/*"
$opts{"used_refspec"}
Truth table0%0/2
A
A= $opts{"used_refspec"}
Aresult
11
00
96  
97 0 my %unused_args;
98 0 for (qw(from used_refspec)) {
99 0 ▶ $unused_args{$_} = $opts{$_} if defined $opts{$_};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{$_} 0 0 0
100 }
101  
102 0 ▶ $self->{_unused} //= retrieve $self->_unused_cache
Branch: 1 branch
BranchTrueFalseTotal
if -e $self->_unused_cache 0 0 0
Condition0%0/3
$self->{"_unused"} //= retrieve($self->_unused_cache)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_unused"} //= retrieve($self->_unused_cache)
$self->{"_unused"} retrieve($self->_unused_cache)
Truth table0%0/3
A //= B
A= $self->{"_unused"}B= retrieve($self->_unused_cache)
ABresult
1X1
011
000
103 if -e $self->_unused_cache;
104 # see if cache needs to be invalidated, whether or not we just
105 # retrieved it
106 0 ▶ if (defined $self->{_unused}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_unused"}) 0 0 0
107 0 my $git_annex_unused = $self->_git_path(qw(annex unused));
108 0 my $last_unused = (stat $git_annex_unused)[9];
109 my %branch_timestamps
110 0 = map { split }
111 $self->git->for_each_ref(
112 { format => '%(refname:short) %(committerdate:unix)' },
113 "refs/heads/");
114  
115 # we don't need to invalidate the cache if the git-annex
116 # branch has changed, because the worst that can happen is we
117 # try to drop a file which has already been dropped
118 0 delete $branch_timestamps{'git-annex'};
119  
120 $self->_clear_unused_cache
121 unless $last_unused <= $self->{_unused}{timestamp}
122 and Compare(\%unused_args, $self->{_unused}{unused_args})
123 0 ▶ and all { $_ < $last_unused } values %branch_timestamps;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless $last_unused <= $self->{"_unused"}{"timestamp"} and Compare(\%unused_args, $self->{"_unused"}{"unused_args"}) and &all(sub { $_ < $last_unused; } , values %branch_timestamps) 0 0 0
Condition0%0/6
$last_unused <= $self->{"_unused"}{"timestamp"} and Compare(\%unused_args, $self->{"_unused"}{"unused_args"}) and &all(sub { $_ < $last_unused; } , values %branch_timestamps)
!ll&&!rl&&r
000
$last_unused <= $self->{"_unused"}{"timestamp"} and Compare(\%unused_args, $self->{"_unused"}{"unused_args"})
!ll&&!rl&&r
000
MC/DC0%0/3
$last_unused <= $self->{"_unused"}{"timestamp"} and Compare(\%unused_args, $self->{"_unused"}{"unused_args"}) and &all(sub { $_ < $last_unused; } , values %branch_timestamps)
$last_unused <= $self->{"_unused"}{"timestamp"} Compare(\%unused_args, $self->{"_unused"}{"unused_args"}) &all(sub { $_ < $last_unused; } , values %branch_timestamps)
Truth table0%0/4
A and B and C
A= $last_unused <= $self->{"_unused"}{"timestamp"}B= Compare(\%unused_args, $self->{"_unused"}{"unused_args"})C= &all(sub { $_ < $last_unused; } , values %branch_timestamps)
ABCresult
0XX0
10X0
1100
1111
124 }
125  
126 # get the unused info if we couldn't load from the cache or had to
127 # invalidate it
128 0 ▶ unless (defined $self->{_unused}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_unused"}) 0 0 0
129 0 my ($bad, $tmp) = (0, 0);
130 0 $self->{_unused}{unused_args} = \%unused_args;
131 # make a copy of %unused_args because Git::Wrapper will remove
132 # them from the hash
133 0 for ($self->annex->unused({%unused_args})) {
134 0 ▶ if (
Branch: 3 branches
BranchTrueFalseTotal
if (/Some corrupted files have been preserved by fsck, just in case/u) { } 0 0 0
elsif (/Some partially transferred data exists in temporary files/u) { } 0 0 0
elsif (/^ ([0-9]+) +([^ ]+)$/u) { } 0 0 0
135 /Some corrupted files have been preserved by fsck, just in case/
136 ) {
137 0 ($bad, $tmp) = (1, 0);
138 } elsif (
139 /Some partially transferred data exists in temporary files/) {
140 0 ($bad, $tmp) = (0, 1);
141 } elsif (/^ ([0-9]+) +([^ ]+)$/) {
142 0 push @{ $self->{_unused}{unused} },
143 { number => $1, key => $2, bad => $bad, tmp => $tmp };
144 }
145 }
146 0 $self->_store_unused_cache;
147 }
148  
149 # run any needed calls to git-log(1)
150 0 ▶ if ($opts{log}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"log"}) 0 0 0
151 0 my $changed = 0;
152 0 foreach my $unused_file (@{ $self->{_unused}{unused} }) {
153 next
154 if defined $unused_file->{log_lines}
155 or $unused_file->{bad}
156 0 ▶ or $unused_file->{tmp};
Branch: 1 branch
BranchTrueFalseTotal
if defined $unused_file->{"log_lines"} or $unused_file->{"bad"} or $unused_file->{"tmp"} 0 0 0
Condition0%0/6
defined $unused_file->{"log_lines"} or $unused_file->{"bad"} or $unused_file->{"tmp"}
l!l&&r!l&&!r
000
defined $unused_file->{"log_lines"} or $unused_file->{"bad"}
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $unused_file->{"log_lines"} or $unused_file->{"bad"} or $unused_file->{"tmp"}
defined $unused_file->{"log_lines"} $unused_file->{"bad"} $unused_file->{"tmp"}
Truth table0%0/4
A or B or C
A= defined $unused_file->{"log_lines"}B= $unused_file->{"bad"}C= $unused_file->{"tmp"}
ABCresult
1XX1
01X1
0011
0000
157 0 $changed = 1;
158 # We need the RUN here to avoid special postprocessing but
159 # also to get the -c option passed -- unclear how to pass
160 # short options to git itself, not the 'log' subcommand,
161 # with Git::Wrapper except by using RUN (passing long
162 # options to git itself is easy, per Git::Wrapper docs)
163 0 @{ $unused_file->{log_lines} } = $self->git->RUN(
164 "-c",
165 "diff.renameLimit=3000",
166 "log",
167 {
168 stat => 1,
169 no_textconv => 1
170 },
171 "--color=always",
172 "-S",
173 0 $unused_file->{key});
174 }
175 0 ▶ $self->_store_unused_cache if $changed;
Branch: 1 branch
BranchTrueFalseTotal
if $changed 0 0 0
176 }
177  
178 0 return $self->{_unused}{unused};
179 }
180  
181 0 ▶sub _unused_cache {
Subroutine _unused_cache: not called CC=2 SCAR=17.9
182 0 my $self = shift;
183 0 ▶ $self->{_unused_cache} //= $self->_git_path(qw(annex unused_info));
Condition0%0/3
$self->{"_unused_cache"} //= $self->_git_path("annex", "unused_info")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_unused_cache"} //= $self->_git_path("annex", "unused_info")
$self->{"_unused_cache"} $self->_git_path("annex", "unused_info")
Truth table0%0/3
A //= B
A= $self->{"_unused_cache"}B= $self->_git_path("annex", "unused_info")
ABresult
1X1
011
000
184 }
185  
186 0 ▶sub _store_unused_cache {
Subroutine _store_unused_cache: not called CC=1 SCAR=6.9
187 0 my $self = shift;
188 0 $self->{_unused}{timestamp} = time;
189 0 store $self->{_unused}, $self->_unused_cache;
190 }
191  
192 0 ▶sub _clear_unused_cache {
Subroutine _clear_unused_cache: not called CC=1 SCAR=6.9
193 0 my $self = shift;
194 0 delete $self->{_unused};
195 0 unlink $self->_unused_cache;
196 }
197  
198  
199 0 ▶sub abs_contentlocation {
Subroutine abs_contentlocation: not called CC=2 SCAR=17.9
Pod documented
200 0 my ($self, $key) = @_;
201 0 my $contentlocation;
202 0 ▶ try { ($contentlocation) = $self->annex->contentlocation($key) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
203 0 ▶ $contentlocation ? rel2abs($contentlocation, $self->toplevel) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$contentlocation ? : 0 0 0
204 }
205  
206  
207 0 ▶sub batch { Git::Annex::BatchCommand->new(@_) }
Subroutine batch: not called CC=1 SCAR=6.9
Pod documented
208  
209 0 ▶sub _git_path {
Subroutine _git_path: not called CC=1 SCAR=6.9
210 0 my ($self, @input) = @_;
211 0 my ($path) = $self->git->rev_parse({ git_path => 1 }, catfile @input);
212 0 rel2abs $path, $self->toplevel;
213 }
214  
215 12 package Git::Annex::Wrapper {
216 12 $Git::Annex::Wrapper::VERSION = '0.008';
217 0 ▶AUTOLOAD {
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
218 0 my $self = shift;
219 0 (my $subcommand = our $AUTOLOAD) =~ s/.+:://;
220 0 ▶ return if $subcommand eq "DESTROY";
Branch: 1 branch
BranchTrueFalseTotal
if $subcommand eq "DESTROY" 0 0 0
221 0 $subcommand =~ tr/_/-/;
222 0 $$self->git->RUN("annex", $subcommand, @_);
223 }
224 }
225  
226  
227 # credits to Git::Wrapper's author for the idea of accessing
228 # subcommands in this way; I've just extended that idea to
229 # subsubcommands of git
230 0 ▶sub annex {
Subroutine annex: not called CC=2 SCAR=17.9
Pod documented
231 0 my $self = shift;
232 0 ▶ $self->{annex} //= bless \$self => "Git::Annex::Wrapper";
Condition0%0/2
$self->{"annex"} //= bless(\$self, "Git::Annex::Wrapper")
l!l
00
MC/DC0%0/1
$self->{"annex"} //= bless(\$self, "Git::Annex::Wrapper")
$self->{"annex"}
Truth table0%0/2
A
A= $self->{"annex"}
Aresult
11
00
233 }
234  
235 12 1;
236  
237 __END__
« App/git_annex_reviewunused.pm ↑ Summary App/annex_to_annex.pm »