summaryrefslogtreecommitdiff
path: root/bin/annex-to-annex-dropunused
blob: a90b45f3f4085f6df04a9d63531fe621ab0bced3 (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
#!/usr/bin/perl
# PODNAME: annex-to-annex-dropunused
# ABSTRACT: drop old hardlinks migrated by annex-to-annex

# Copyright (C) 2019-2020 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 <http://www.gnu.org/licenses/>.

=head1 SYNOPSIS

B<annex-to-annex-dropunused>

=head1 DESCRIPTION

This program drops files which have been migrated by
annex-to-annex(1).  You can run it in the source annex(es) to get rid
of the old unused files which are known to have been migrated.

Only files which have been hardlinked into the destination annex will
be removed, i.e., no data will be irrevocably removed by this command.

=head1 OPTIONS

None.

=head1 SEE ALSO

annex-to-annex(1)

=cut

use 5.028;
use strict;
use warnings;

use autodie;
use Git::Annex;

# This script used to have a --dest option which specified the
# destination annex previously used with annex-to-annex.  Then, if the
# unused file had a hardlink count of 1, but was present in the
# destination annex, this script would drop it.
#
# That was somewhat dangerous functionality because it involves this
# script running `git annex dropunused --force` for files with a
# hardlink count of 1.  And further, it is not actually needed,
# because running annex-to-annex-reinject after
# annex-to-annex-dropunused handles such files in a way that is safer.
#
# It is still good to run this script before annex-to-annex-reinject
# to make the latter faster.

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

my @to_drop;
my @unused_files = grep { !$_->{bad} && !$_->{tmp} } @{ $annex->unused };

foreach my $unused_file (@unused_files) {
    my $content = $annex->abs_contentlocation($unused_file->{key});
    my $link_count = (stat $content)[3];
    my @logs
      = $annex->git->log({ no_textconv => 1 }, "-S", $unused_file->{key});

    next unless $logs[0] and $logs[0]->message =~ /migrated by annex-to-annex/;
    next unless $link_count > 1;

    push @to_drop, $unused_file->{number};
}

$annex->git->annex("dropunused", { force => 1 }, @to_drop);