summaryrefslogtreecommitdiff
path: root/perl5/Local/MrRepo/Repo/Git/Annex.pm
blob: 4becb768c56802991f5895519a69832e4f76ffbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package Local::MrRepo::Repo::Git::Annex;

# Copyright (C) 2019-2020, 2023 Sean Whitton
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

use 5.018;
use strict;
use warnings;
use lib "$ENV{HOME}/src/dotfiles/perl5";
use parent 'Local::MrRepo::Repo::Git';

use App::git_annex_reviewunused;
use Data::Compare;
use Exporter 'import';
use File::Spec::Functions qw(rel2abs catfile);
use Git::Wrapper;
use JSON;
use Local::ScriptStatus;
use Try::Tiny;
use Term::ReadKey;
use Local::Interactive qw(prompt prompt_yn);
use Storable;
use List::Util qw(all);
use File::chdir;

our @EXPORT_OK = ();

sub review {
    my $self = shift;

    my $issues = $self->SUPER::review(@_);

    # if there were git issues, fail now, as the annex issues can
    # produce copious output and it is nice to know there were no git
    # issues when reviewing the annex issues
    return $issues if $issues;

    # another command we could consider running is `git annex fsck --fast`

    # 1. Check for files stored only locally where that's not okay
    my @annex_find_output
      = $self->git->annex(
        qw(find --json --in here --and --not --copies=2 --and --lackingcopies=1)
      );
    unless (@annex_find_output == 0) {
        say_spaced_bullet("Some annex content is present only locally:");
        say "  $_" for map { ${decode_json($_)}{file} } @annex_find_output;
        print "\n";
        say_bold
          ("  use `git annex sync --content' to fix this (`mr push' should be");
        say_bold
          ("  configured to run that command with the right remotes)");
        $issues = 1;
    }
    # 2. Pull in unused files on external drives before we review.
    # This reduces the number of times we might have to review these
    # particular unused files.
    #
    # This relies on annex.used-refspec being set to something sensible for
    # the repository, and usually also that 'mr update' has done 'ga merge'.
    if ($self->config_bool("spwhitton.mrrepo.move-extdrive-unused", "false"))
    {
	foreach my $remote ($self->git->remote) {
	    my ($url) = $self->git->remote("get-url", $remote);
	    $url =~ m#\A/media/# and -d $url or next;
	    $self->git->annex("unused", { from => $remote });
	    system qw(git -C), $self->toplevel,
	      qw(annex move --unused --numcopies=1), "--from=$remote";
	    # TODO Empty .git/annex/tmp/ on the external drive?
	}
    }
    # 3. Check for unused files which we should be able to clean up
    if ($self->config_bool("spwhitton.mrrepo.review-unused", "true")) {
        #<<<
        try {
            local $CWD = $self->toplevel;
            $issues = App::git_annex_reviewunused->main(["--just-print"])
              || $issues;
        } catch {
            $issues = 1;
        };
        #>>>
    }

    return $issues;
}

1;