summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2019-12-19 20:14:35 -0700
committerSean Whitton <spwhitton@spwhitton.name>2019-12-19 20:15:53 -0700
commitfffd2ab58f158e1099ebdf796c442af2c77d6a3c (patch)
tree00b745c304f8058f23daf9ccabdf4caa226739ee /lib
parente2ce359006eadc09b12a3c7daba8decfdf8abdce (diff)
downloaddotfiles-fffd2ab58f158e1099ebdf796c442af2c77d6a3c.tar.gz
factor out some parts of the unused review & better tmp/bad handling
Diffstat (limited to 'lib')
-rw-r--r--lib/perl5/Local/MrRepo/Repo/Git/Annex.pm104
1 files changed, 58 insertions, 46 deletions
diff --git a/lib/perl5/Local/MrRepo/Repo/Git/Annex.pm b/lib/perl5/Local/MrRepo/Repo/Git/Annex.pm
index 87c51bc4..ea468251 100644
--- a/lib/perl5/Local/MrRepo/Repo/Git/Annex.pm
+++ b/lib/perl5/Local/MrRepo/Repo/Git/Annex.pm
@@ -79,64 +79,24 @@ sub review_unused {
$unused_args{from} = $dropunused_args{from} = $opts{from}
if defined $opts{from};
- my @to_drop = ();
- my @unused_lines = $self->git->annex("unused", \%unused_args);
- my @unused_files
- = map { /^ ([0-9]+) +([^ ]+)$/ ? { number => $1, key => $2 } : () }
- @unused_lines;
+ my @to_drop = ();
+ my @unused_files = @{ $self->get_unused(\%unused_args) };
+ $self->log_unused(\@unused_files);
return 0 if @unused_files == 0;
unless ($opts{interactive}) {
say_spaced_bullet("There are unused files you can drop with"
. " `git annex dropunused':");
- say for @unused_lines;
+ say " " . $_->{number} . " " . $_->{key} for @unused_files;
say "";
}
- foreach my $unused_file (@unused_files) {
- # We need the RUN here to avoid special postprocessing but
- # also to get the -c option passed -- unclear how to pass
- # short options to git itself, not the 'log' subcommand,
- # with Git::Wrapper except by using RUN (passing long
- # options to git itself is easy, per Git::Wrapper docs)
- @{ $unused_file->{log_lines} } = map { s/^/ /r } $self->git->RUN(
- "-c",
- "diff.renameLimit=3000",
- "log",
- {
- stat => 1,
- no_textconv => 1
- },
- "--color=always",
- "-S",
- $unused_file->{key});
- }
-
my $i = 0;
UNUSED: while ($i < @unused_files) {
my $unused_file = $unused_files[$i];
system('clear', '-x') if $opts{interactive};
say_bold("unused file #" . $unused_file->{number} . ":");
- # this way of determining whether a file is tmp or bad, rather
- # than really unused, is not great because it only works for
- # the local repo and makes unneeded `ga contentlocation`
- # calls. it would be better just to parse @unused_lines with
- # a proper state machine and thereby extract into separate
- # arrays the unused and the tmp/bad data
- my $is_tmp_or_bad = 0;
- my $content_location;
- try {
- ($content_location)
- = $self->git->annex("contentlocation", $unused_file->{key});
- $content_location = rel2abs($content_location, $self->toplevel);
- }
- catch {
- $is_tmp_or_bad = 1;
- };
-
- # $is_tmp_or_bad can only be trusted when we are operating on
- # the local repo, so guard for that here
- if ($is_tmp_or_bad && !defined $opts{from}) {
+ if ($unused_file->{bad} || $unused_file->{tmp}) {
say " looks like stale tmp or bad file, with key "
. $unused_file->{key};
} else {
@@ -180,7 +140,8 @@ sub review_unused {
} elsif ($response eq 'n') {
last;
} elsif ($response eq 'o') {
- system('xdg-open', $content_location);
+ system('xdg-open',
+ abs_contentlocation($unused_file->{key}));
} elsif ($response eq 'b' and $i > 0) {
pop @to_drop
if $to_drop[$#to_drop] eq
@@ -209,4 +170,55 @@ sub review_unused {
return @to_drop != @unused_files;
}
+sub get_unused {
+ my $self = shift;
+
+ my @unused_files;
+ my ($bad, $tmp) = (0, 0);
+ foreach ($self->git->annex("unused", @_)) {
+ if (/Some corrupted files have been preserved by fsck, just in case/) {
+ ($bad, $tmp) = (1, 0);
+ } elsif (/Some partially transferred data exists in temporary files/) {
+ ($bad, $tmp) = (0, 1);
+ } elsif (/^ ([0-9]+) +([^ ]+)$/) {
+ push @unused_files,
+ { number => $1, key => $2, bad => $bad, tmp => $tmp };
+ }
+ }
+ return \@unused_files;
+}
+
+sub log_unused {
+ my $self = shift;
+ my $unused_files = shift;
+
+ foreach my $unused_file (@$unused_files) {
+ # We need the RUN here to avoid special postprocessing but
+ # also to get the -c option passed -- unclear how to pass
+ # short options to git itself, not the 'log' subcommand,
+ # with Git::Wrapper except by using RUN (passing long
+ # options to git itself is easy, per Git::Wrapper docs)
+ @{ $unused_file->{log_lines} } = map { s/^/ /r } $self->git->RUN(
+ "-c",
+ "diff.renameLimit=3000",
+ "log",
+ {
+ stat => 1,
+ no_textconv => 1
+ },
+ "--color=always",
+ "-S",
+ $unused_file->{key});
+ }
+}
+
+sub abs_contentlocation {
+ my $self = shift;
+ my $key = shift;
+
+ my ($contentlocation)
+ = $self->git->annex("contentlocation", $key);
+ return rel2abs($contentlocation, $self->toplevel);
+}
+
1;