From 7c6d991939009024e233d345d3e165017ff17e41 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Sat, 1 Feb 2020 12:07:44 -0700 Subject: add .git dir utils Signed-off-by: Sean Whitton --- lib/Git/Annex.pm | 17 +++++++++++++++++ t/11_utils.t | 29 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100755 t/11_utils.t diff --git a/lib/Git/Annex.pm b/lib/Git/Annex.pm index 1cd26f4..7d923f9 100644 --- a/lib/Git/Annex.pm +++ b/lib/Git/Annex.pm @@ -66,6 +66,7 @@ use Cwd; use File::chdir; use Git::Wrapper; use Git::Repository; +use File::Spec::Functions qw(catfile rel2abs); use Moo; use namespace::clean; @@ -103,6 +104,22 @@ has repo => ( # Git::Repository::new, so we chdir and let call without arguments default => sub { local $CWD = shift->toplevel; Git::Repository->new }); +has _unused_cache => ( + is => "lazy", + default => sub { shift->_git_path(catfile(qw(annex unused_info))) }); + +sub _clear_unused_cache { + my $self = shift; + delete $self->{_unused}; + unlink $self->_unused_cache; +} + +sub _git_path { + my ($self, $input) = @_; + my ($path) = $self->git->rev_parse({ git_path => 1 }, $input); + rel2abs($path, $self->toplevel); +} + around BUILDARGS => sub { my (undef, undef, @args) = @_; { toplevel => $args[0] // getcwd }; diff --git a/t/11_utils.t b/t/11_utils.t new file mode 100755 index 0000000..6602cb8 --- /dev/null +++ b/t/11_utils.t @@ -0,0 +1,29 @@ +#!/usr/bin/perl + +use 5.028; +use strict; +use warnings; +use lib 't/lib'; + +use Test::More; +use Git::Annex; +use File::Spec::Functions qw(catfile); +use t::Setup; +use File::Slurp; + +with_temp_annexes { + my $temp = shift; + my $annex = Git::Annex->new("source1"); + my $unused_info = catfile($temp, qw(source1 .git annex unused_info)); + ok $annex->_git_path("blah") eq catfile($temp, qw(source1 .git blah)), + "_git_path resolves a path"; + ok $annex->_unused_cache eq $unused_info, + "_unused_cache resolves to correct path"; + $annex->{_unused} = { foo => "bar" }; + write_file $unused_info, "baz\n"; + $annex->_clear_unused_cache; + ok !exists $annex->{_unused}, "_clear_unused_cache clears unused hashref"; + ok !-f $unused_info, "_clear_unused_cache deletes the cache"; +}; + +done_testing; -- cgit v1.2.3