package VCP::Revs ; =head1 NAME VCP::Revs - A collection of VCP::Rev objects. =head1 SYNOPSIS =head1 DESCRIPTION Right now, all revs are kept in memory, but we will enable storing them to disk and recovering them at some point so that we don't gobble huge tracts of RAM. =head1 METHODS =over =cut $VERSION = 1 ; use strict ; use Carp ; use VCP::Debug ":debug" ; use VCP::Rev ; use fields ( 'REVS', ## The revs, sorted or not 'BY_ID', ## A HASH of revisions, indexed by ID 'BY_NAME_BRANCH_ID', ## A HASH of revisions, indexed by name and branch_id ) ; =item new =cut sub new { my $class = CORE::shift ; $class = ref $class || $class ; my $self ; { no strict 'refs' ; $self = bless [ \%{"$class\::FIELDS"} ], $class ; } $self->{REVS} = [] ; $self->{BY_ID} = {} ; return $self ; } =item add $revs->add( $rev ) ; $revs->add( $rev1, $rev2, ... ) ; Adds a revision or revisions to the collection. The ( name, rev_id, branch_id ) tuple must be unique, if a second rev is Ced with the same values, an exception is thrown. =cut sub add { my VCP::Revs $self = CORE::shift ; Carp::confess "undef passed" if grep ! defined, @_; if ( debugging $self || debugging scalar caller ) { debug( "vcp: queuing ", $_->as_string ) for @_ ; } for my $r ( @_ ) { my $id = $r->id; croak "Can't add same revision twice: '" . $r->as_string if $self->{BY_ID}->{$id} ; push @{$self->{REVS}}, $r ; $self->{BY_ID}->{$id} = $r ; $self->{BY_NAME_BRANCH_ID}->{$r->_name_branch_id} = $r; } } =item set $revs->set( $rev ) ; $revs->set( $rev1, $rev2, ... ) ; Sets the list of revs. =cut sub set { my VCP::Revs $self = CORE::shift ; Carp::confess "undef passed" if grep !defined, @_; if ( debugging $self || debugging scalar caller ) { require UNIVERSAL; Carp::confess "unblessed ref passed" if grep !UNIVERSAL::can( $_, "as_string" ), @_; debug( "vcp: queuing ", $_->as_string ) for @_ ; } @{$self->{REVS}} = @_ ; } =item get @revs = $revs->get ; ## return a list of all revs $rev = $revs->get( $id ) ; ## return the rev with a given ID (or die()) =cut sub get { my VCP::Revs $self = CORE::shift ; return @{$self->{REVS}} unless @_; my ( $id ) = @_; Carp::confess "Could not find revision with id='$id'" unless exists $self->{BY_ID}->{$id}; return $self->{BY_ID}->{$id}; } =item get_last_added my $previous_r = $revs->get_last_added( $r ); Gets the last revision of the named file on this branch. This is used because most repositories output the most recent revision first, and we need to point each revision at its preceding revision for branching and for RevML to do diffs. die()s unless a previous revision is found. =cut sub get_last_added { my VCP::Revs $self = shift; my ( $r ) = @_; my $nb = $r->_name_branch_id; die "Could not find revision with name(branch_id)='$nb'\n" unless exists $self->{BY_NAME_BRANCH_ID}->{$nb}; $self->{BY_NAME_BRANCH_ID}->{$nb}; } =item sort # Using a custom sort function: $revs->sort( sub { ... } ) ; Note: Don't use $a and $b in your sort function. They're package globals and that's not your package. See L for more details. =cut sub sort { my VCP::Revs $self = CORE::shift ; my ( $sort_func ) = @_ ; @{$self->{REVS}} = sort $sort_func, @{$self->{REVS}} ; } =item shift while ( $r = $revs->shift ) { ... } Call L before calling this :-). =cut sub shift { my VCP::Revs $self = CORE::shift ; my $r = shift @{$self->{REVS}} ; delete $self->{BY_ID}->{$r->id} if $r; delete $self->{BY_NAME_BRANCH_ID}->{$r->_name_branch_id} if $r; return $r; } =item as_array_ref Returns an ARRAY ref of all revs. =cut sub as_array_ref { my VCP::Revs $self = CORE::shift ; return $self->{REVS} ; } =head1 SUBCLASSING This class uses the fields pragma, so you'll need to use base and possibly fields in any subclasses. =head1 COPYRIGHT Copyright 2000, Perforce Software, Inc. All Rights Reserved. This module and the VCP package are licensed according to the terms given in the file LICENSE accompanying this distribution, a copy of which is included in L. =head1 AUTHOR Barrie Slaymaker =cut 1