debian-mirror-gitlab/lib/gitlab/version_info.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

96 lines
2.1 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module Gitlab
class VersionInfo
include Comparable
attr_reader :major, :minor, :patch
2022-08-13 15:12:31 +05:30
VERSION_REGEX = /(\d+)\.(\d+)\.(\d+)/.freeze
def self.parse(str, parse_suffix: false)
2022-08-27 11:52:29 +05:30
if str.is_a?(self)
2022-08-13 15:12:31 +05:30
str
elsif str && m = str.match(VERSION_REGEX)
VersionInfo.new(m[1].to_i, m[2].to_i, m[3].to_i, parse_suffix ? m.post_match : nil)
2014-09-02 18:07:02 +05:30
else
VersionInfo.new
end
end
2022-08-13 15:12:31 +05:30
def initialize(major = 0, minor = 0, patch = 0, suffix = nil)
2014-09-02 18:07:02 +05:30
@major = major
@minor = minor
@patch = patch
2022-08-13 15:12:31 +05:30
@suffix_s = suffix.to_s
2014-09-02 18:07:02 +05:30
end
2022-08-13 15:12:31 +05:30
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
2014-09-02 18:07:02 +05:30
def <=>(other)
return unless other.is_a? VersionInfo
return unless valid? && other.valid?
if other.major < @major
1
elsif @major < other.major
-1
elsif other.minor < @minor
1
elsif @minor < other.minor
-1
elsif other.patch < @patch
1
elsif @patch < other.patch
-1
2022-08-13 15:12:31 +05:30
elsif @suffix_s.empty? && other.suffix.present?
1
elsif other.suffix.empty? && @suffix_s.present?
-1
2014-09-02 18:07:02 +05:30
else
2022-08-13 15:12:31 +05:30
suffix <=> other.suffix
2014-09-02 18:07:02 +05:30
end
end
2022-08-13 15:12:31 +05:30
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity
2014-09-02 18:07:02 +05:30
def to_s
if valid?
2022-08-13 15:12:31 +05:30
"%d.%d.%d%s" % [@major, @minor, @patch, @suffix_s]
2014-09-02 18:07:02 +05:30
else
2022-08-13 15:12:31 +05:30
'Unknown'
2014-09-02 18:07:02 +05:30
end
end
2022-08-27 11:52:29 +05:30
def to_json(*_args)
{ major: @major, minor: @minor, patch: @patch }.to_json
end
2022-08-13 15:12:31 +05:30
def suffix
@suffix ||= @suffix_s.strip.gsub('-', '.pre.').scan(/\d+|[a-z]+/i).map do |s|
/^\d+$/ =~ s ? s.to_i : s
end.freeze
end
2014-09-02 18:07:02 +05:30
def valid?
@major >= 0 && @minor >= 0 && @patch >= 0 && @major + @minor + @patch > 0
end
2022-08-13 15:12:31 +05:30
def hash
[self.class, to_s].hash
end
def eql?(other)
(self <=> other) == 0
end
def same_minor_version?(other)
@major == other.major && @minor == other.minor
end
def without_patch
self.class.new(@major, @minor, 0)
end
2014-09-02 18:07:02 +05:30
end
end