Welcome to mirror list, hosted at ThFree Co, Russian Federation.

version_info.rb « gitlab « lib « gitlab-utils « gems - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 21478c462590814b8ded7a53151e5b9368e4b2ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# frozen_string_literal: true

module Gitlab
  class VersionInfo
    include Comparable

    attr_reader :major, :minor, :patch

    VERSION_REGEX = /(\d+)\.(\d+)\.(\d+)/
    MILESTONE_REGEX = /\A(\d+)\.(\d+)\z/
    # To mitigate ReDoS, limit the length of the version string we're
    # willing to check
    MAX_VERSION_LENGTH = 128

    InvalidMilestoneError = Class.new(StandardError)

    def self.parse_from_milestone(str)
      raise InvalidMilestoneError if str.length > MAX_VERSION_LENGTH

      m = MILESTONE_REGEX.match(str)
      raise InvalidMilestoneError if m.nil?

      VersionInfo.new(m[1].to_i, m[2].to_i)
    end

    def self.parse(str, parse_suffix: false)
      return str if str.is_a?(self)

      if str && str.length <= MAX_VERSION_LENGTH
        match = str.match(VERSION_REGEX)
        if match
          return VersionInfo.new(match[1].to_i, match[2].to_i, match[3].to_i, parse_suffix ? match.post_match : nil)
        end
      end

      VersionInfo.new
    end

    def initialize(major = 0, minor = 0, patch = 0, suffix = nil) # rubocop:disable Metrics/ParameterLists
      @major = major
      @minor = minor
      @patch = patch
      @suffix_s = suffix.to_s
    end

    # rubocop:disable Metrics/CyclomaticComplexity
    # rubocop:disable Metrics/PerceivedComplexity
    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
      elsif @suffix_s.empty? && other.suffix.present?
        1
      elsif other.suffix.empty? && @suffix_s.present?
        -1
      else
        suffix <=> other.suffix
      end
    end
    # rubocop:enable Metrics/CyclomaticComplexity
    # rubocop:enable Metrics/PerceivedComplexity

    def to_s
      if valid?
        "%d.%d.%d%s" % [@major, @minor, @patch, @suffix_s] # rubocop:disable Style/FormatString
      else
        'Unknown'
      end
    end

    def to_json(*_args)
      { major: @major, minor: @minor, patch: @patch }.to_json
    end

    def suffix
      @suffix ||= @suffix_s.strip.gsub('-', '.pre.').scan(/\d+|[a-z]+/i).map do |s|
        /^\d+$/.match?(s) ? s.to_i : s
      end.freeze
    end

    def valid?
      @major >= 0 && @minor >= 0 && @patch >= 0 && @major + @minor + @patch > 0
    end

    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
  end
end