From 26724018067fca77977e343263487bf2b6b25ba2 Mon Sep 17 00:00:00 2001 From: Frédéric Buclin Date: Mon, 2 Dec 2013 17:04:12 +0100 Subject: Bug 938300: vers_cmp() incorrectly compares module versions r=sgreen a=justdave --- Bugzilla/Version.pm | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 93 insertions(+), 5 deletions(-) (limited to 'Bugzilla/Version.pm') diff --git a/Bugzilla/Version.pm b/Bugzilla/Version.pm index 1dcd2b141..c6b178a8a 100644 --- a/Bugzilla/Version.pm +++ b/Bugzilla/Version.pm @@ -10,9 +10,10 @@ package Bugzilla::Version; use 5.10.1; use strict; -use parent qw(Bugzilla::Object); +use parent qw(Bugzilla::Object Exporter); + +@Bugzilla::Version::EXPORT = qw(vers_cmp); -use Bugzilla::Install::Util qw(vers_cmp); use Bugzilla::Util; use Bugzilla::Error; @@ -200,6 +201,53 @@ sub _check_product { return Bugzilla->user->check_can_admin_product($product->name); } +############################### +##### Functions #### +############################### + +# This is taken straight from Sort::Versions 1.5, which is not included +# with perl by default. +sub vers_cmp { + my ($a, $b) = @_; + + # Remove leading zeroes - Bug 344661 + $a =~ s/^0*(\d.+)/$1/; + $b =~ s/^0*(\d.+)/$1/; + + my @A = ($a =~ /([-.]|\d+|[^-.\d]+)/g); + my @B = ($b =~ /([-.]|\d+|[^-.\d]+)/g); + + my ($A, $B); + while (@A and @B) { + $A = shift @A; + $B = shift @B; + if ($A eq '-' and $B eq '-') { + next; + } elsif ( $A eq '-' ) { + return -1; + } elsif ( $B eq '-') { + return 1; + } elsif ($A eq '.' and $B eq '.') { + next; + } elsif ( $A eq '.' ) { + return -1; + } elsif ( $B eq '.' ) { + return 1; + } elsif ($A =~ /^\d+$/ and $B =~ /^\d+$/) { + if ($A =~ /^0/ || $B =~ /^0/) { + return $A cmp $B if $A cmp $B; + } else { + return $A <=> $B if $A <=> $B; + } + } else { + $A = uc $A; + $B = uc $B; + return $A cmp $B if $A cmp $B; + } + } + return @A <=> @B; +} + 1; __END__ @@ -243,11 +291,51 @@ below. =item C - Description: Returns the total of bugs that belong to the version. +=over + +=item B + +Returns the total of bugs that belong to the version. + +=item B + +none + +=item B + +Integer with the number of bugs. + +=back + +=back + +=head1 FUNCTIONS + +=over + +=item C + +=over + +=item B + +This is a comparison function, like you would use in C, except that +it compares two version numbers. So, for example, 2.10 would be greater +than 2.2. + +It's based on versioncmp from L, with some Bugzilla-specific +fixes. - Params: none. +=item B - Returns: Integer with the number of bugs. +C<$a> and C<$b> - The versions you want to compare. + +=item B + +C<-1> if C<$a> is less than C<$b>, C<0> if they are equal, or C<1> if C<$a> +is greater than C<$b>. + +=back =back -- cgit v1.2.3-24-g4f1b