Thread: [Lxr-commits] CVS: lxr/lib/LXR/Files Plain.pm,1.24,1.24.2.1 BK.pm,1.1.2.2,1.1.2.3
Brought to you by:
ajlittoz
From: Malcolm B. <mb...@us...> - 2005-02-13 19:35:14
|
Update of /cvsroot/lxr/lxr/lib/LXR/Files In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13184/lib/LXR/Files Modified Files: Tag: bk-dev-branch Plain.pm BK.pm Log Message: Updated implementation of BitKeeper interface. Now uses bk rset to get all the details of the tree. Can correctly access various versions of the files across moves, deletes etc. Index: Plain.pm =================================================================== RCS file: /cvsroot/lxr/lxr/lib/LXR/Files/Plain.pm,v retrieving revision 1.24 retrieving revision 1.24.2.1 diff -u -d -r1.24 -r1.24.2.1 --- Plain.pm 21 Jul 2004 20:44:31 -0000 1.24 +++ Plain.pm 13 Feb 2005 19:35:05 -0000 1.24.2.1 @@ -100,6 +100,10 @@ my ($self, $pathname, $release) = @_; my ($dir, $node, @dirs, @files); + if($pathname !~ m!/$!) { + $pathname = $pathname . '/'; + } + $dir = $self->toreal($pathname, $release); opendir(DIR, $dir) || return (); FILE: while (defined($node = readdir(DIR))) { @@ -164,14 +168,4 @@ return %index; } -sub allreleases { - my ($self, $filename) = @_; - - opendir(SRCDIR, $self->{'rootpath'}); - my @dirs = readdir(SRCDIR); - closedir(SRCDIR); - - return grep { /^[^\.]/ && -r $self->toreal($filename, $_) } @dirs; -} - 1; Index: BK.pm =================================================================== RCS file: /cvsroot/lxr/lxr/lib/LXR/Files/Attic/BK.pm,v retrieving revision 1.1.2.2 retrieving revision 1.1.2.3 diff -u -d -r1.1.2.2 -r1.1.2.3 --- BK.pm 12 Dec 2004 02:41:39 -0000 1.1.2.2 +++ BK.pm 13 Feb 2005 19:35:05 -0000 1.1.2.3 @@ -22,15 +22,18 @@ use strict; use File::Spec; +use Cwd; use Time::Local; use LXR::Common; -use vars qw(%tree_cache); +use vars qw(%tree_cache @ISA); + +@ISA = ("LXR::Files"); sub new { - my ( $self, $rootpath ) = @_; + my ($self, $rootpath) = @_; - $self = bless( {}, $self ); + $self = bless({}, $self); $self->{'rootpath'} = $rootpath; $self->{'rootpath'} =~ s!/*$!!; @@ -38,50 +41,143 @@ } sub insert_entry { - my ( $newtree, $path, $entry ) = @_; - $$newtree{$path} = {} if !defined( $$newtree{$path} ); - $newtree->{$path}{$entry} = 1; + my ($newtree, $path, $entry, $curfile, $rev) = @_; + $$newtree{$path} = {} if !defined($$newtree{$path}); + $newtree->{$path}{$entry} = { 'curpath' => $curfile, 'revision' => $rev }; } sub getdir { - my ( $self, $pathname, $release ) = @_; + my ($self, $pathname, $release) = @_; - if ( !defined $tree_cache{$release} ) { + $self->fill_cache($release); + $pathname = File::Spec->rootdir() if $pathname eq ''; + return keys %{ $tree_cache{$release}->{$pathname} }; +} - # Not in cache, so need to build - my @all_entries = $self->get_tree( $release, $pathname ); - my %newtree = (); - my ( $entry, $path, $file, $vol, @dirs ); +sub fill_cache { + my ($self, $release) = @_; + + return if (defined $tree_cache{$release}); + + # Not in cache, so need to build + my @all_entries = $self->get_tree($release); - $newtree{ File::Spec->rootdir() } = {}; + my %newtree = (); + my ($entry, $path, $file, $vol, @dirs); + my ($curfile, $histfile, $rev); + $newtree{''} = {}; - foreach $entry (@all_entries) { - ( $vol, $path, $file ) = File::Spec->splitpath( File::Spec->rootdir() . $entry ); - insert_entry( \%newtree, $path, $file ); - while($path ne File::Spec->rootdir()) { - # Insert any directories in path into hash - ($vol, $path, $file) = File::Spec->splitpath( - File::Spec->catdir(File::Spec->splitdir($path))); - insert_entry(\%newtree, $path, $file); - } + foreach $entry (@all_entries) { + ($curfile, $histfile, $rev) = split /\|/, $entry; + ($vol, $path, $file) = File::Spec->splitpath($histfile); + insert_entry(\%newtree, $path, $file, $curfile, $rev); + while ($path ne File::Spec->rootdir() && $path ne '') { + # Insert any directories in path into hash + ($vol, $path, $file) = + File::Spec->splitpath( + File::Spec->catdir(File::Spec->splitdir($path))); + insert_entry(\%newtree, $path, $file); } - $tree_cache{$release} = \%newtree; } - return keys %{$tree_cache{$release}->{$pathname}}; + + # Make / point to '' + $newtree{ File::Spec->rootdir() } = $newtree{''}; + delete $newtree{''}; + + $tree_cache{$release} = \%newtree; } -sub get_tree() { - my ( $self, $release ) = @_; +sub get_tree { + my ($self, $release) = @_; my $real = $self->{'rootpath'}; + my $dir = getcwd(); + chdir($self->{'rootpath'}); - open( X, "bk -r prs -r$release $real|" ) or die "Can't exec bk prs"; - - my @files = <X>; - @files = grep /^P /, @files; - map( s/^P //, @files ); - chomp @files; +# This command provide 3 part output - the current filename, the historical filename & the revision + open(X, "bk rset -h -l$release 2>/dev/null |") or die "Can't exec bk rset"; + chdir($dir); + my $line_to_junk = <X>; # Remove the Changelist|Changelist line at start + my @files = <X>; close X; + chomp @files; + + # remove any BitKeeper metadata except for deleted files + @files = grep (!(m!^BitKeeper! && !m!^BitKeeper/deleted/!), @files); return @files; } + +sub getfilehandle { + my ($self, $pathname, $release) = @_; + $pathname = canonise($pathname); + my $fileh = undef; + my $dir = getcwd(); + chdir($self->{'rootpath'}); + if ($self->file_exists($pathname, $release)) { + my $info = $self->getfileinfo($pathname, $release); + my $ver = $info->{'revision'}; + my $where = $info->{'curpath'}; + open($fileh, "bk get -p -r$ver $where 2>/dev/null |") + or die "Error executing bk get"; + } + chdir($dir); + return $fileh; +} + +sub canonise { + my $path = shift; + $path =~ s!^/!!; + return $path; +} + +# Check that the specified pathname, version combination exists in repository +sub file_exists { + my ($self, $pathname, $release) = @_; + + # Look the file up in the treecache + return defined($self->getfileinfo($pathname, $release)); +} + +sub getfileinfo { + my ($self, $pathname, $release) = @_; + $self->fill_cache($release); # Normally expect this to be present anyway + $pathname = canonise($pathname); + + my ($vol, $path, $file) = File::Spec->splitpath($pathname); + $path = File::Spec->rootdir() if $path eq ''; + + return $tree_cache{$release}{$path}{$file}; +} + + +sub getfile { + my ($self, $pathname, $release) = @_; + $pathname = canonise($pathname); + my $fileh = $self->getfilehandle($pathname, $release); + + return undef unless $fileh; + my $buffer = join('', $fileh->getlines); + close $fileh; + return $buffer; +} + +sub tmpfile { + my ($self, $filename, $release) = @_; + my ($tmp, $buf); + + $buf = $self->getfile($filename, $release); + return undef unless defined($buf); + + $tmp = + $config->tmpdir + . '/bktmp.' + . time . '.' + . $$ . '.' + . &LXR::Common::tmpcounter; + open(TMP, "> $tmp") || return undef; + print(TMP $buf); + close(TMP); + + return $tmp; +} |