From 0c52344a51c93d650023ba0ed4c7b1fbf4b8b0b5 Mon Sep 17 00:00:00 2001 From: Sam Vilain Date: Wed, 16 Jul 2008 17:36:43 +1200 Subject: [PATCH] Add some filesystem access functions to CIL::VCS::* --- lib/CIL/VCS/Factory.pm | 2 +- lib/CIL/VCS/Git.pm | 46 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/CIL/VCS/Factory.pm b/lib/CIL/VCS/Factory.pm index ec5da53..52ac43e 100644 --- a/lib/CIL/VCS/Factory.pm +++ b/lib/CIL/VCS/Factory.pm @@ -30,7 +30,7 @@ use base qw( Class::Factory ); __PACKAGE__->register_factory_type( Null => 'CIL::VCS::Null' ); __PACKAGE__->register_factory_type( Git => 'CIL::VCS::Git' ); -foreach my $method_name ( qw(post_add) ) { +foreach my $method_name ( qw(post_add glob_rev file_exists dir_exists get_fh) ) { my $method = sub { my ($self) = @_; my $class = ref $self || $self; diff --git a/lib/CIL/VCS/Git.pm b/lib/CIL/VCS/Git.pm index e4a8286..3a6f97c 100644 --- a/lib/CIL/VCS/Git.pm +++ b/lib/CIL/VCS/Git.pm @@ -39,6 +39,52 @@ sub post_add { return [ "git add @files" ]; } +use Git; + +sub git { + my $self = shift; + $self->{git} ||= Git->repository; +} + +sub glob_rev { + my ($self, $rev, $path) = @_; + + # only support globbing the last element + my ($dir, $pattern) = $path =~ m{^([^\*]*/)([^/]*)$} + or croak "unsupported pattern '$path'"; + $pattern =~ s{([\\\.])}{\\$1}g; + $pattern =~ s{\*}{.*}g; + my @match; + $DB::single = 1; + for ( $self->git->command("ls-tree", $rev, $dir) ) { + chomp; + my ($blobid, $path) = m{([0-9a-f]{40})\s+(.*)} or die; + if ( $path =~ m{^\Q$dir\E$pattern$} ) { + push @match, $path; + } + } + @match; +} + +sub file_exists { + my ($self, $rev, $path) = @_; + + my $output = eval { $self->git->command("cat-file", "-t", "$rev:$path") }; + return ( $output && $output =~ /blob/ ); +} + +sub dir_exists { + my ($self, $rev, $path) = @_; + + my $output = eval { $self->git->command("cat-file", "-t", "$rev:$path") }; + return ( $output && $output =~ /tree/ ); +} + +sub get_fh { + my ($self, $rev, $path) = @_; + $self->git->command_output_pipe("cat-file", "blob", "$rev:$path"); +} + ## ---------------------------------------------------------------------------- 1; ## ---------------------------------------------------------------------------- -- 2.39.5