version comparison fixed
This commit is contained in:
parent
1d63578841
commit
332e8f4b01
1 changed files with 50 additions and 47 deletions
|
|
@ -149,26 +149,16 @@ module DebImporter
|
||||||
end # Enumerator
|
end # Enumerator
|
||||||
end # def
|
end # def
|
||||||
|
|
||||||
# Determine which version string denotes the newer package
|
|
||||||
# v1: version of the first package
|
|
||||||
# v2: version of the second package
|
|
||||||
# op: the comparison operator (default: 'gt' / greater-than)
|
|
||||||
#
|
|
||||||
# The algorithm is described in the Debian Policy at
|
|
||||||
# https://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version
|
|
||||||
# The reference implementation is "dpkg --compare-versions …"
|
|
||||||
def dpkg_compare_version(v1, v2, op: 'gt')
|
|
||||||
system("dpkg --compare-versions #{v1} #{op} #{v2}")
|
|
||||||
end
|
|
||||||
|
|
||||||
class Version
|
class Version
|
||||||
|
attr_reader :epoch, :version, :revision
|
||||||
|
|
||||||
def initialize(version_string)
|
def initialize(version_string)
|
||||||
@version_string = version_string
|
@version_string = version_string
|
||||||
# Split into "[epoch:]version[-revision]"
|
# Split into "[epoch:]version[-revision]"
|
||||||
unless version_string =~ /^(?<epoch>(\d+)\:)?(?<version>.+)(?<revision>\-[\+\.~]+)?/
|
unless /^(?<epoch>(\d+)\:)?(?<version>.+)(?<revision>\-[\+\.~]+)?/ =~ version_string
|
||||||
raise ArgumentError, "Cannot parse version string: #{version_string}"
|
raise ArgumentError, "Cannot parse version string: #{version_string}"
|
||||||
end
|
end
|
||||||
@epoch = epoch if epoch
|
@epoch = epoch ? epoch : 0
|
||||||
@version = version
|
@version = version
|
||||||
@revision = revision if revision
|
@revision = revision if revision
|
||||||
end # /def
|
end # /def
|
||||||
|
|
@ -177,43 +167,56 @@ module DebImporter
|
||||||
@version
|
@version
|
||||||
end
|
end
|
||||||
|
|
||||||
def >(a,b)
|
def >(other)
|
||||||
return true if a.epoch > b.epoch
|
dpkg_compare_version(@version_string, other)
|
||||||
return true if version_compare(a.version, b.version)
|
# return true if a.epoch > b.epoch
|
||||||
return true if version_compare(a.revision, b.revision)
|
# return true if version_compare(a.version, b.version)
|
||||||
|
# return true if version_compare(a.revision, b.revision)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def version_compare(x,y)
|
# Determine which version string denotes the newer package
|
||||||
# Compare a version string (like the upstream_version or
|
# v1: version of the first package
|
||||||
# debian_revision string) against another version string.
|
# v2: version of the second package
|
||||||
# The algorithm works like this:
|
# op: the comparison operator (default: 'gt' / greater-than)
|
||||||
#
|
#
|
||||||
# The strings are compared from left to right.
|
# The algorithm is described in the Debian Policy at
|
||||||
# First the initial part of each string consisting entirely of non-digit
|
# https://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version
|
||||||
# characters is determined. These two parts (one of which may be empty)
|
# The reference implementation is "dpkg --compare-versions …"
|
||||||
# are compared lexically. If a difference is found it is returned.
|
def dpkg_compare_version(v1, v2, op: 'gt')
|
||||||
# The lexical comparison is a comparison of ASCII values modified so
|
system("dpkg --compare-versions #{v1} #{op} #{v2}")
|
||||||
# that all the letters sort earlier than all the non-letters and so that
|
|
||||||
# a tilde sorts before anything, even the end of a part.
|
|
||||||
# For example, the following parts are in sorted order from
|
|
||||||
# earliest to latest: ~~, ~~a, ~, the empty part, a.[37]
|
|
||||||
#
|
|
||||||
# Then the initial part of the remainder of each string which
|
|
||||||
# consists entirely of digit characters is determined. The
|
|
||||||
# numerical values of these two parts are compared, and any
|
|
||||||
# difference found is returned as the result of the comparison.
|
|
||||||
# For these purposes an empty string (which can only occur at
|
|
||||||
# the end of one or both version strings being compared) counts as zero.
|
|
||||||
#
|
|
||||||
# These two steps (comparing and removing initial non-digit strings
|
|
||||||
# and initial digit strings) are repeated until a difference is
|
|
||||||
# found or both strings are exhausted.
|
|
||||||
|
|
||||||
(x.chars).zip(y.chars) do |xchar,ychar|
|
|
||||||
puts xchar, ychar
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# def ___version_compare(x,y)
|
||||||
|
# # Compare a version string (like the upstream_version or
|
||||||
|
# # debian_revision string) against another version string.
|
||||||
|
# # The algorithm works like this:
|
||||||
|
# #
|
||||||
|
# # The strings are compared from left to right.
|
||||||
|
# # First the initial part of each string consisting entirely of non-digit
|
||||||
|
# # characters is determined. These two parts (one of which may be empty)
|
||||||
|
# # are compared lexically. If a difference is found it is returned.
|
||||||
|
# # The lexical comparison is a comparison of ASCII values modified so
|
||||||
|
# # that all the letters sort earlier than all the non-letters and so that
|
||||||
|
# # a tilde sorts before anything, even the end of a part.
|
||||||
|
# # For example, the following parts are in sorted order from
|
||||||
|
# # earliest to latest: ~~, ~~a, ~, the empty part, a.[37]
|
||||||
|
# #
|
||||||
|
# # Then the initial part of the remainder of each string which
|
||||||
|
# # consists entirely of digit characters is determined. The
|
||||||
|
# # numerical values of these two parts are compared, and any
|
||||||
|
# # difference found is returned as the result of the comparison.
|
||||||
|
# # For these purposes an empty string (which can only occur at
|
||||||
|
# # the end of one or both version strings being compared) counts as zero.
|
||||||
|
# #
|
||||||
|
# # These two steps (comparing and removing initial non-digit strings
|
||||||
|
# # and initial digit strings) are repeated until a difference is
|
||||||
|
# # found or both strings are exhausted.
|
||||||
|
|
||||||
|
# (x.chars).zip(y.chars) do |xchar,ychar|
|
||||||
|
# puts xchar, ychar
|
||||||
|
# end
|
||||||
|
# end
|
||||||
end # /class
|
end # /class
|
||||||
end # module
|
end # module
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue