From c4078eb34f948fb50e46b86e1d89a18c05bc7e2f Mon Sep 17 00:00:00 2001 From: Sam Vilain Date: Wed, 16 Jul 2008 17:40:10 +1200 Subject: [PATCH] defer filesystem access to CIL->vcs if vcs_revision is set --- bin/cil | 2 +- lib/CIL.pm | 54 +++++++++++++++++++++++++++++++++++++++++++++++-- lib/CIL/Base.pm | 7 ++++--- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/bin/cil b/bin/cil index 90a7681..f00aee5 100755 --- a/bin/cil +++ b/bin/cil @@ -814,7 +814,7 @@ sub check_paths { my ($cil) = @_; # make sure an issue directory is available - unless ( -d $cil->IssueDir ) { + unless ( $cil->dir_exists($cil->IssueDir) ) { fatal("couldn't find '" . $cil->IssueDir . "' directory"); } } diff --git a/lib/CIL.pm b/lib/CIL.pm index 72676f1..942e27b 100644 --- a/lib/CIL.pm +++ b/lib/CIL.pm @@ -23,7 +23,7 @@ package CIL; use strict; use warnings; -use Carp; +use Carp qw(croak confess); use File::Glob qw(:glob); use CIL::VCS::Factory; @@ -36,6 +36,7 @@ __PACKAGE__->mk_accessors(qw( VCS UserName UserEmail vcs hook + vcs_revision )); my $defaults = { @@ -85,7 +86,13 @@ sub list_entities { $base = '' unless defined $base; my $globpath = $self->IssueDir . "/${prefix}_${base}*.cil"; - my @filenames = bsd_glob($globpath); + my @filenames; + if ( $self->vcs_revision ) { + @filenames = $self->vcs->glob_rev($self->vcs_revision, $globpath); + } + else { + @filenames = bsd_glob($globpath); + } my @entities; foreach my $filename ( sort @filenames ) { @@ -296,6 +303,49 @@ sub run_hook { } } +sub file_exists { + my ($self, $filename) = @_; + if ( $self->vcs_revision ) { + $self->vcs->file_exists($self->vcs_revision, $filename); + } + else { + -f $filename; + } +} + +sub dir_exists { + my ($self, $filename) = @_; + if ( $self->vcs_revision ) { + $self->vcs->dir_exists($self->vcs_revision, $filename); + } + else { + -f $filename; + } +} + +sub parse_cil_file { + my ($self, $filename, $last_field) = @_; + + if ( $self->vcs_revision ) { + my $fh = $self->vcs->get_fh($self->vcs_revision, $filename); + CIL::Utils->parse_from_fh($fh, $last_field); + } + else { + CIL::Utils->parse_cil_file($filename, $last_field); + } +} + +sub save { + my ($self, $filename, $data, @fields) = @_; + + if ( $self->vcs_revision ) { + confess "tried to ->save on alternate revision"; + } + else { + CIL::Utils->write_cil_file( $filename, $data, @fields ); + } +} + ## ---------------------------------------------------------------------------- 1; ## ---------------------------------------------------------------------------- diff --git a/lib/CIL/Base.pm b/lib/CIL/Base.pm index f9b932f..d911aae 100644 --- a/lib/CIL/Base.pm +++ b/lib/CIL/Base.pm @@ -40,9 +40,9 @@ sub new_from_name { my $filename = $class->create_filename($cil, $name); croak "filename '$filename' does no exist" - unless -f $filename; + unless $cil->file_exists($filename); - my $data = CIL::Utils->parse_cil_file($filename, $class->last_field); + my $data = $cil->parse_cil_file($filename, $class->last_field); my $issue = $class->new_from_data( $name, $data ); return $issue; } @@ -118,7 +118,8 @@ sub save { my $filename = $self->create_filename($cil, $self->name); my $fields = $self->fields(); - CIL::Utils->write_cil_file( $filename, $self->{data}, @$fields ); + + $cil->save( $filename, $self->{data}, @$fields ); } sub as_output { -- 2.39.5