App/annex_to_annex_reinject.pm

filter: Statementstmt 40.9% 36 / 88 Branchbran 3.8% 1 / 26 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 61.1% 11 / 18 Podpod 100% 1 / 1 totaltotal 34.2% 49 / 143 CC 17 SCAR 37.2
CC
17
Cov
31%
main
110.0
__ANON__
42.0
_is_empty_dir
12.0
CRAP 41.4
« App/annex_to_annex.pm ↑ Summary App/annex_to_annex_dropunused.pm »
1 package App::annex_to_annex_reinject;
2 # ABSTRACT: annex-to-annex-reinject
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_to_annex_reinject::VERSION = '0.001';
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 autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Git::Annex;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use File::Basename qw(basename dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use File::chmod;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 $File::chmod::UMASK = 0;
28 1 ▶use File::Path qw(rmtree);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use File::Spec::Functions qw(rel2abs);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 ▶exit main() unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 1 1
34  
35  
36 sub main {
37 0 ▶ shift if $_[0] and ref $_[0] eq ""; # in case main called as a class method
Subroutine main: not called CC=10 SCAR=47.0
Pod documented
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
38 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
39  
40 0 ▶ die "usage: annex-to-annex-reinject SOURCEANNEX DESTANNEX\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV == 2 0 0 0
41 unless @ARGV == 2;
42  
43 0 my $source = Git::Annex->new($ARGV[0]);
44 0 my $dest = Git::Annex->new($ARGV[1]);
45 #<<<
46 try {
47 0 ▶ $source->git->rev_parse({ git_dir => 1 });
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 } catch {
49 0 ▶ die "$ARGV[0] doesn't look like a git repository ..\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 };
51 try {
52 0 ▶ $dest->git->rev_parse({ git_dir => 1 });
Subroutine __ANON__: not called CC=1 SCAR=6.9
53 } catch {
54 0 ▶ die "$ARGV[1] doesn't look like a git repository ..\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 0 };
56 #>>>
57  
58 # `git annex reinject` doesn't work in a bare repo atm
59 0 my $use_worktree
60 = ($dest->git->rev_parse({ is_bare_repository => 1 }))[0] eq 'true';
61 0 my ($temp, $worktree);
62 0 ▶ if ($use_worktree) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_worktree) 0 0 0
63 0 $temp = tempdir(CLEANUP => 1, DIR => dirname $ARGV[1]);
64 0 say "bare repo; our git worktree is in $temp";
65 0 $dest->git->worktree("add", { force => 1, detach => 1 },
66 rel2abs($temp), "synced/master");
67 }
68  
69 0 my ($source_uuid) = $source->git->config('annex.uuid');
70 0 ▶ die "couldn't get source annex uuid"
Branch: 1 branch
BranchTrueFalseTotal
unless $source_uuid =~ /\A[a-z0-9-]+\z/u 0 0 0
71 unless $source_uuid =~ /\A[a-z0-9-]+\z/;
72 0 my $spk = $source->batch("setpresentkey");
73  
74 0 my ($source_objects_dir)
75 = $source->git->rev_parse({ git_path => 1 }, "annex/objects");
76 0 $source_objects_dir = rel2abs $source_objects_dir, $ARGV[0];
77 0 ▶ my $reinject_from = $use_worktree ? $temp : $ARGV[1];
Branch: 1 branch
BranchTrueFalseTotal
$use_worktree ? : 0 0 0
78 0 say "reinjecting from $source_objects_dir into $reinject_from";
79 find({
80 wanted => sub {
81 0 ▶ -f or return;
Subroutine __ANON__: not called CC=6 SCAR=37.4
Branch: 1 branch
BranchTrueFalseTotal
-f $_ or return 0 0 0
82 0 say "\nconsidering $_";
83 0 my $dir = dirname $_;
84 0 chmod "u+w", $dir, $_;
85 0 system "git", "-C", $reinject_from, "annex", "reinject",
86 "--known", $_;
87 0 ▶ if (-e $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $_) { } 0 0 0
88 0 chmod "u-w", $dir, $_;
89 } else {
90 0 my $key = basename $_;
91 0 say "telling setpresentkey process '$key $source_uuid 0'";
92 0 say for $spk->say("$key $source_uuid 0");
93 # alt. to setpresentkey:
94 # say "fscking key $key in $ARGV[0]";
95 # system 'git', '-C', $ARGV[0], 'annex', 'fsck',
96 # '--numcopies=1', '--key', $key;
97 0 say "cleaning up empty dirs";
98 0 foreach
99 my $d ($dir, dirname($dir), dirname(dirname($dir))) {
100 0 ▶ last unless _is_empty_dir($d);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_empty_dir($d) 0 0 0
101 0 rmdir $d;
102 }
103 }
104 },
105 0 no_chdir => 1
106 },
107 $source_objects_dir
108 );
109 0 ▶ if ($use_worktree) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_worktree) 0 0 0
110 # we can't use `git worktree remove` because the way git-annex
111 # worktree support works breaks that command: git-annex replaces
112 # the .git worktree file with a symlink
113 0 rmtree $temp;
114 0 $dest->git->worktree("prune");
115 }
116  
117 # cause setpresentkey changes to be recorded in git-annex branch
118 0 undef $spk;
119 0 sleep 1;
120 0 $source->annex->merge;
121  
122 0 return 0;
123 }
124  
125  
126 sub _is_empty_dir {
127 0 ▶ -d $_[0] or return 0;
Subroutine _is_empty_dir: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
-d $_[0] or return 0 0 0 0
128 0 opendir(my $dirh, $_[0]);
129 0 ▶ my @files = grep { $_ ne '.' && $_ ne '..' } readdir $dirh;
Branch: 1 branch
BranchTrueFalseTotal
$_ ne "." and $_ ne ".." 0 0 0
130 0 return @files == 0;
131 }
132  
133 1 1;
134  
135 __END__
« App/annex_to_annex.pm ↑ Summary App/annex_to_annex_dropunused.pm »