summaryrefslogtreecommitdiff
path: root/bin/annex-review-unused
blob: 082f058b9cf32da2b1b725d6d21b8384dd96a48c (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/perl
# PODNAME: annex-review-unused
# ABSTRACT: interactively process `git annex unused` output
#
# Copyright (C) 2019-2020  Sean Whitton <spwhitton@spwhitton.name>
#
# 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 <http://www.gnu.org/licenses/>.

=head1 SYNOPSIS

B<annex-review-unused> [B<--just-print>] [B<--from=>I<REMOTE>] [B<--used-refspec=>I<USEDREFSPEC>]

=head1 DESCRIPTION

This program lets you interactively review unused files in a git annex
repository, choosing which you'd like to drop.  By running git-log(1),
it provides more information about unused files than does B<git annex
unused>.

=head1 OPTIONS

=over 4

=item B<--just-print>

Run non-interactively: gather and print information about unused
files, and then exit.

=item B<--from=>I<REMOTE>

Passed on to B<git annex unused>.

=item B<--used-refspec=>I<USEDREFSPEC>

Passed on to B<git annex unused>.

=back

=head1 EXIT STATUS

0 if there are no unused files left at time of program exit, 1 if there are.

=head1 SEE ALSO

git-annex-unused(1), git-annex-dropunused(1)

=cut

use 5.028;
use strict;
use warnings;

use Getopt::Long;
use Git::Annex;
use Try::Tiny;
use Term::ReadKey;
use Term::ANSIColor;

my $annex = Git::Annex->new;

my $just_print = 0;
my ($uuid, $from_arg, $used_refspec_arg, %unused_opts, %dropunused_args);
GetOptions
  'from=s'         => \$from_arg,
  'used-refspec=s' => \$used_refspec_arg,
  'just-print'     => \$just_print;
if ($from_arg) {
    $unused_opts{from} = $dropunused_args{from} = $from_arg;
    #<<<
    try {
        ($uuid) = $annex->git->config("remote." . $from_arg . ".annex-uuid");
    } catch {
        die "couldn't determine an annex UUID for $from_arg remote";
    };
    #>>>
}
$unused_opts{used_refspec} = $used_refspec_arg if $used_refspec_arg;

my @to_drop;
my @unused_files = @{ $annex->unused(%unused_opts, log => 1) };
exit unless @unused_files;
if ($just_print) {
    say_spaced_bullet("There are unused files you can drop with"
          . " `git annex dropunused':");
    say "    " . $_->{number} . "     " . $_->{key} for @unused_files;
    print "\n";
}
my $i = 0;
UNUSED: while ($i < @unused_files) {
    my $unused_file = $unused_files[$i];

    # check the unused file still exists i.e. has not been dropped
    # already (in the case of reviewing unused files at a remote, just
    # check that it's not been dropped according to the local
    # git-annex branch by using readpresentkey rather than
    # checkpresentkey)
    my $contentlocation = $annex->abs_contentlocation($unused_file->{key});
    if ($from_arg) {
        #<<<
        try {
            $annex->git->annex("readpresentkey", $unused_file->{key}, $uuid);
        } catch {
            splice @unused_files, $i, 1;
            next UNUSED;
        };
        #>>>
    } elsif (!$contentlocation) {
        splice @unused_files, $i, 1;
        next UNUSED;
    }

    system qw(clear -x) unless $just_print;
    say_bold("unused file #" . $unused_file->{number} . ":");

    if ($unused_file->{bad} or $unused_file->{tmp}) {
        say "  looks like stale tmp or bad file, with key "
          . $unused_file->{key};
    } else {
        my @log_lines = map { s/^/  /r } @{ $unused_file->{log_lines} };
        unless ($just_print) {
            # truncate log output if necessary to ensure user's
            # terminal does not scroll
            my (undef, $height) = GetTerminalSize();
            splice @log_lines, (($height - 5) - @log_lines)
              if @log_lines > ($height - 5);
        }
        print "\n";
        say for @log_lines;
        unless ($just_print) {
            my $response;
          READKEY: while (1) {
                # before prompting, clear out stdin, to avoid
                # registered a keypress more than once
                ReadMode 4;
                1 while defined ReadKey(-1);

                my @opts = ('y', 'n');
                push @opts, 'o' if $contentlocation;
                push @opts, ('d', 'b') if $i > 0;
                print "Drop this unused files?  (" . join('/', @opts) . ") ";

                # Term::ReadKey docs recommend ReadKey(-1) but that
                # means we need an infinite loop calling ReadKey(-1)
                # over and over, which ramps up system load
                my $response = ReadKey(0);
                ReadMode 0;

                # respond to C-c
                exit 0 if ord($response) == 3;

                say $response;
                $response = lc($response);
                if ($response eq "y") {
                    push @to_drop, $unused_file->{number};
                    last READKEY;
                } elsif ($response eq "n") {
                    last READKEY;
                } elsif ($response eq "o" and defined $contentlocation) {
                    system "xdg-open", $contentlocation;
                } elsif ($response eq "b" and $i > 0) {
                    $i--;
                    $i--;
                    pop @to_drop
                      if @to_drop
                      and $to_drop[$#to_drop] eq $unused_files[$i]->{number};
                    next UNUSED;
                } elsif ($response eq "d" and $i > 0) {
                    # user wants to drop the list we've accumulated up
                    # until now and get out of this script
                    last UNUSED;
                } else {
                    say "invalid response";
                }
            }
        }
    }
    print "\n";
    $i++;
}

if (@to_drop) {
    say_spaced_bullet("Will dropunused"
          . (exists $dropunused_args{force} ? " with --force:" : ":"));
    say "@to_drop\n";
    $annex->git->annex("dropunused", \%dropunused_args, @to_drop)
      if prompt_yn("Go ahead with this?");
}

# exit value represents whether or not there are any unused files left
# after this run.  note that in --just-print mode, @to_drop will be
# empty, so we'll always exit non-zero if there are any unused files
exit(@to_drop != @unused_files);

sub say_bold { print colored(['bold'], @_), "\n" }

sub say_bullet { say_bold(" • ", @_) }

sub say_spaced_bullet { say_bold("\n", " • ", @_, "\n") }