| 1 | package App::annex_to_annex; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: use hardlinks to migrate files between git annex repos | ||||||||||||||||||||||||||||
| 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 | 3 | $App::annex_to_annex::VERSION = '0.008'; | |||||||||||||||||||||||||||
| 19 | 3 | ▶ | use 5.028; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 3 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 3 | ▶ | use subs qw(main exit); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 3 | ▶ | use Digest::MD5::File qw(file_md5); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 3 | ▶ | use File::Basename qw(dirname basename); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 3 | ▶ | use File::Copy; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 3 | ▶ | use File::Find; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 3 | ▶ | use File::Spec::Functions qw(catfile rel2abs abs2rel); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 3 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 3 | ▶ | use Git::Annex; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 3 | my $exit_main = 0; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 3 | ▶ | CORE::exit main unless caller; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub main { | ||||||||||||||||||||||||||
|
Subroutine
main: not called CC=18 SCAR=58.3
| |||||||||||||||||||||||||||||
| 39 | 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 ""
| |||||||||||||||||||||||||||||
| 40 | 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 ""
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # only support v7 because supporting v5 too would make things quite | ||||||||||||||||||||||||||||
| 43 | # complex. require git-annex >=7.20191009 because it will refuse to | ||||||||||||||||||||||||||||
| 44 | # work in v5 repos, and because it supports `git annex find --unlocked` | ||||||||||||||||||||||||||||
| 45 | 0 | chomp(my %annex_version_fields = map { split ': ' } `git annex version`); | |||||||||||||||||||||||||||
| 46 | die "I need git-annex >=7.20191009 and a v7 repository\n" | ||||||||||||||||||||||||||||
| 47 | 0 | ▶ | unless $annex_version_fields{'git-annex version'} >= 7.20191009; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | die "need at least two arguments\n" unless @ARGV > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | my $dest = rel2abs pop @ARGV; | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | die "dest is not a directory\n" unless -d $dest; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | my $dest_device_id = (stat($dest))[0]; | |||||||||||||||||||||||||||
| 53 | 0 | my $dannex = Git::Annex->new($dest); | |||||||||||||||||||||||||||
| 54 | 0 | my $do_commit = 0; | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ($ARGV[0] eq '--commit') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | $do_commit = 1; | |||||||||||||||||||||||||||
| 57 | 0 | shift @ARGV; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | my @git_status = $dannex->git->RUN("status", { porcelain => 1 }); | |||||||||||||||||||||||||||
| 60 | 0 | ▶ | die "git repo containing $dest is not clean; please commit\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | unless @git_status == 0; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | #<<< | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 65 | 0 | $dannex->git->symbolic_ref({ quiet => 1 }, "HEAD"); | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | } catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 67 | 0 | die "$dest has a detached HEAD; aborting"; | |||||||||||||||||||||||||||
| 68 | 0 | }; | |||||||||||||||||||||||||||
| 69 | #>>> | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 0 | my @sources = map rel2abs($_), @ARGV; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # process one entry in @sources at a time because we can start up | ||||||||||||||||||||||||||||
| 74 | # annex batch processes for each of these as all files under each | ||||||||||||||||||||||||||||
| 75 | # entry in @sources will lie in the same annex | ||||||||||||||||||||||||||||
| 76 | 0 | foreach my $source (@sources) { | |||||||||||||||||||||||||||
| 77 | 0 | my $dir = dirname $source; | |||||||||||||||||||||||||||
| 78 | 0 | my $annex = Git::Annex->new($dir); | |||||||||||||||||||||||||||
| 79 | #<<< | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 81 | 0 | $annex->annex->status; | |||||||||||||||||||||||||||
| 82 | 0 | ▶ | } catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 83 | 0 | die "$source does not appear to lie within an annex\n"; | |||||||||||||||||||||||||||
| 84 | 0 | }; | |||||||||||||||||||||||||||
| 85 | #>>> | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | die "$source does not exist\n" unless -e $source; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if ($do_commit) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | my @git_status = $annex->git->RUN("status", { porcelain => 1 }); | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | die "git repo containing $source is not clean; please commit\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | unless @git_status == 0; | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | #<<< | ||||||||||||||||||||||||||||
| 94 | 0 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 95 | 0 | $annex->git->symbolic_ref({ quiet => 1 }, "HEAD"); | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | } catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 97 | 0 | die "$dest has a detached HEAD; aborting"; | |||||||||||||||||||||||||||
| 98 | 0 | }; | |||||||||||||||||||||||||||
| 99 | #>>> | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | my @missing | ||||||||||||||||||||||||||||
| 103 | 0 | = $annex->annex->find("--not", "--in", "here", | |||||||||||||||||||||||||||
| 104 | abs2rel $source, $annex->toplevel); | ||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if (@missing) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | say "Following annexed files are not present in this repo:"; | |||||||||||||||||||||||||||
| 107 | 0 | say for @missing; | |||||||||||||||||||||||||||
| 108 | 0 | die "cannot continue; please `git-annex get` them\n"; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # start batch processes | ||||||||||||||||||||||||||||
| 112 | 0 | my $lk = $annex->batch("lookupkey"); | |||||||||||||||||||||||||||
| 113 | 0 | my $cl = $annex->batch("contentlocation"); | |||||||||||||||||||||||||||
| 114 | 0 | my $find = $annex->batch("find", "--unlocked"); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | find({ | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | wanted => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=12 SCAR=50.5
| |||||||||||||||||||||||||||||
| 118 | 0 | my $rel = abs2rel $File::Find::name, $dir; | |||||||||||||||||||||||||||
| 119 | 0 | my $target = catfile $dest, $rel; | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | die "$target already exists!\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-e $target and not -d $target
MC/DC0%0/2
-e $target and not -d $target
-e $target
not -d $target
Truth table0%0/3
A and not(B)
A= -e $targetB= not -d $target
| |||||||||||||||||||||||||||||
| 121 | if -e $target and !-d $target; | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | my $key = $lk->ask($File::Find::name); | |||||||||||||||||||||||||||
| 124 | 0 | ▶ | if ($key) { # this is an annexed file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | my $content = rel2abs $cl->ask($key), $annex->toplevel; | |||||||||||||||||||||||||||
| 126 | 0 | my $content_device_id = (stat $content)[0]; | |||||||||||||||||||||||||||
| 127 | 0 | ▶ | if ($dest_device_id == $content_device_id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | link $content, $target; | |||||||||||||||||||||||||||
| 129 | } else { | ||||||||||||||||||||||||||||
| 130 | 0 | _copy_and_md5($content, $target); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | # add, and then maybe unlock. we don't use `-c | ||||||||||||||||||||||||||||
| 133 | # annex.addunlocked=true` because we want to | ||||||||||||||||||||||||||||
| 134 | # hardlink from .git/annex/objects in the source | ||||||||||||||||||||||||||||
| 135 | # to .git/annex/objects in the dest, rather than | ||||||||||||||||||||||||||||
| 136 | # having the unlocked copy in dest be hardlinked | ||||||||||||||||||||||||||||
| 137 | # to the source, or anything like that | ||||||||||||||||||||||||||||
| 138 | 0 | system "git", "-C", $dest, "annex", "add", $rel; | |||||||||||||||||||||||||||
| 139 | 0 | ▶ | system "git", "-C", $dest, "annex", "unlock", $rel | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | if $find->ask(abs2rel $File::Find::name, | ||||||||||||||||||||||||||||
| 141 | $annex->toplevel); | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # if using the default backend, quick sanity check | ||||||||||||||||||||||||||||
| 144 | 0 | ▶ | if ($key =~ /^SHA256E-s[0-9]+--([0-9a-f]+)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 0 | my $key_sum = $1; | |||||||||||||||||||||||||||
| 146 | 0 | chomp(my $dest_key | |||||||||||||||||||||||||||
| 147 | = `git -C "$dest" annex lookupkey "$rel"`); | ||||||||||||||||||||||||||||
| 148 | 0 | ▶ | if ($dest_key =~ /^SHA256E-s[0-9]+--([0-9a-f]+)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 0 | my $dest_key_sum = $1; | |||||||||||||||||||||||||||
| 150 | 0 | ▶ | die | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | "git-annex calculated a different checksum for $target" | ||||||||||||||||||||||||||||
| 152 | unless $key_sum eq $dest_key_sum; | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | } else { # this is not an annexed file | ||||||||||||||||||||||||||||
| 156 | 0 | ▶ | if (-d $File::Find::name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 0 | ▶ | mkdir $target unless -d $target; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | } else { | ||||||||||||||||||||||||||||
| 159 | 0 | _copy_and_md5($File::Find::name, $target); | |||||||||||||||||||||||||||
| 160 | 0 | system "git", "-C", $dest, | |||||||||||||||||||||||||||
| 161 | "-c", "annex.gitaddtoannex=false", "add", $rel; | ||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | 0 | ▶ | $annex->git->rm($File::Find::name) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | unless -d $File::Find::name; | ||||||||||||||||||||||||||||
| 166 | }, | ||||||||||||||||||||||||||||
| 167 | 0 | no_chdir => 1, | |||||||||||||||||||||||||||
| 168 | }, | ||||||||||||||||||||||||||||
| 169 | $source | ||||||||||||||||||||||||||||
| 170 | ); | ||||||||||||||||||||||||||||
| 171 | 0 | ▶ | $annex->git->commit({ message => "migrated by annex-to-annex" }) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | if $do_commit; | ||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | 0 | ▶ | $dannex->git->commit({ message => "add" }) if $do_commit; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | EXIT_MAIN: | |||||||||||||||||||||||||||
| 177 | return $exit_main; | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | ▶ | sub _copy_and_md5 { | ||||||||||||||||||||||||||
|
Subroutine
_copy_and_md5: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 181 | 0 | copy($_[0], $_[1]); | |||||||||||||||||||||||||||
| 182 | 0 | ▶ | die "md5 checksum failure after copying $_[0] to $_[1]!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | unless file_md5($_[0]) eq file_md5($_[1]); | ||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 0 | ▶ | sub exit { $exit_main = shift // 0; goto EXIT_MAIN } | ||||||||||||||||||||||||||
|
Subroutine
exit: not called CC=2 SCAR=17.9
Condition0%0/2
shift() // 0
MC/DC0%0/1
shift() // 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 3 | 1; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | __END__ | ||||||||||||||||||||||||||||