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