Class: Git::Version

Inherits:
Data
  • Object
show all
Includes:
Comparable
Defined in:
lib/git/version.rb

Overview

Represents a git version with major, minor, and patch components

Git versions follow a strict major.minor.patch format. This class provides parsing from git command output (which may include platform suffixes) and comparison operations for version gating.

Examples:

Creating a version directly

version = Git::Version.new(2, 42, 1)
version.to_s  #=> "2.42.1"

Parsing from git version output

Git::Version.parse('git version 2.42.1')  #=> Git::Version.new(2, 42, 1)
Git::Version.parse('2.39.2 (Apple Git-143)')  #=> Git::Version.new(2, 39, 2)

Parsing versions with platform suffixes

Git::Version.parse('2.42.0.windows.1')  #=> Git::Version.new(2, 42, 0)

Comparing versions

Git::Version.new(2, 42, 1) > Git::Version.new(2, 28, 0)  #=> true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#majorObject (readonly)

Returns the value of attribute major

Returns:

  • (Object)

    the current value of major



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
# File 'lib/git/version.rb', line 43

Version = Data.define(:major, :minor, :patch) do
  include Comparable

  # Parse a version string into a Version object
  #
  # Handles git's version output format, stripping platform suffixes
  # (like `.windows.1` or `.vfs.0`) and padding two-segment versions
  # to three segments.
  #
  # @example Parse various version string formats
  #   Git::Version.parse('2.42.1')  #=> Git::Version.new(2, 42, 1)
  #   Git::Version.parse('git version 2.42.1')  #=> Git::Version.new(2, 42, 1)
  #   Git::Version.parse('2.42.0.windows.1')  #=> Git::Version.new(2, 42, 0)
  #
  # @param string [String] version string to parse
  #
  # @return [Git::Version] the parsed version
  #
  # @raise [Git::UnexpectedResultError] if the string cannot be parsed as a version
  #
  def self.parse(string)
    version_match = string&.match(/(\d+)\.(\d+)(?:\.(\d+))?/)
    raise Git::UnexpectedResultError, "Invalid version: #{string.inspect}" unless version_match

    major = version_match[1].to_i
    minor = version_match[2].to_i
    patch = (version_match[3] || '0').to_i

    new(major, minor, patch)
  end

  # Compare this version to another
  #
  # @param other [Git::Version] the version to compare to
  #
  # @return [Integer] -1, 0, or 1
  #
  def <=>(other)
    [major, minor, patch] <=> [other.major, other.minor, other.patch]
  end

  # Return the version as a dotted string
  #
  # @return [String] the version in "major.minor.patch" format
  #
  def to_s
    "#{major}.#{minor}.#{patch}"
  end

  # Return a readable representation
  #
  # @return [String] inspect string
  #
  def inspect
    "#<Git::Version #{self}>"
  end

  # Return the version as an array of integers
  #
  # Useful when legacy code expects the array shape returned by the
  # deprecated {Git::Lib#current_command_version} method.
  #
  # @return [Array<Integer>] [major, minor, patch]
  #
  # @example
  #   Git.git_version.to_a  #=> [2, 42, 0]
  #
  def to_a
    deconstruct
  end
end

#minorObject (readonly)

Returns the value of attribute minor

Returns:

  • (Object)

    the current value of minor



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
# File 'lib/git/version.rb', line 43

Version = Data.define(:major, :minor, :patch) do
  include Comparable

  # Parse a version string into a Version object
  #
  # Handles git's version output format, stripping platform suffixes
  # (like `.windows.1` or `.vfs.0`) and padding two-segment versions
  # to three segments.
  #
  # @example Parse various version string formats
  #   Git::Version.parse('2.42.1')  #=> Git::Version.new(2, 42, 1)
  #   Git::Version.parse('git version 2.42.1')  #=> Git::Version.new(2, 42, 1)
  #   Git::Version.parse('2.42.0.windows.1')  #=> Git::Version.new(2, 42, 0)
  #
  # @param string [String] version string to parse
  #
  # @return [Git::Version] the parsed version
  #
  # @raise [Git::UnexpectedResultError] if the string cannot be parsed as a version
  #
  def self.parse(string)
    version_match = string&.match(/(\d+)\.(\d+)(?:\.(\d+))?/)
    raise Git::UnexpectedResultError, "Invalid version: #{string.inspect}" unless version_match

    major = version_match[1].to_i
    minor = version_match[2].to_i
    patch = (version_match[3] || '0').to_i

    new(major, minor, patch)
  end

  # Compare this version to another
  #
  # @param other [Git::Version] the version to compare to
  #
  # @return [Integer] -1, 0, or 1
  #
  def <=>(other)
    [major, minor, patch] <=> [other.major, other.minor, other.patch]
  end

  # Return the version as a dotted string
  #
  # @return [String] the version in "major.minor.patch" format
  #
  def to_s
    "#{major}.#{minor}.#{patch}"
  end

  # Return a readable representation
  #
  # @return [String] inspect string
  #
  def inspect
    "#<Git::Version #{self}>"
  end

  # Return the version as an array of integers
  #
  # Useful when legacy code expects the array shape returned by the
  # deprecated {Git::Lib#current_command_version} method.
  #
  # @return [Array<Integer>] [major, minor, patch]
  #
  # @example
  #   Git.git_version.to_a  #=> [2, 42, 0]
  #
  def to_a
    deconstruct
  end
end

#patchObject (readonly)

Returns the value of attribute patch

Returns:

  • (Object)

    the current value of patch



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
# File 'lib/git/version.rb', line 43

Version = Data.define(:major, :minor, :patch) do
  include Comparable

  # Parse a version string into a Version object
  #
  # Handles git's version output format, stripping platform suffixes
  # (like `.windows.1` or `.vfs.0`) and padding two-segment versions
  # to three segments.
  #
  # @example Parse various version string formats
  #   Git::Version.parse('2.42.1')  #=> Git::Version.new(2, 42, 1)
  #   Git::Version.parse('git version 2.42.1')  #=> Git::Version.new(2, 42, 1)
  #   Git::Version.parse('2.42.0.windows.1')  #=> Git::Version.new(2, 42, 0)
  #
  # @param string [String] version string to parse
  #
  # @return [Git::Version] the parsed version
  #
  # @raise [Git::UnexpectedResultError] if the string cannot be parsed as a version
  #
  def self.parse(string)
    version_match = string&.match(/(\d+)\.(\d+)(?:\.(\d+))?/)
    raise Git::UnexpectedResultError, "Invalid version: #{string.inspect}" unless version_match

    major = version_match[1].to_i
    minor = version_match[2].to_i
    patch = (version_match[3] || '0').to_i

    new(major, minor, patch)
  end

  # Compare this version to another
  #
  # @param other [Git::Version] the version to compare to
  #
  # @return [Integer] -1, 0, or 1
  #
  def <=>(other)
    [major, minor, patch] <=> [other.major, other.minor, other.patch]
  end

  # Return the version as a dotted string
  #
  # @return [String] the version in "major.minor.patch" format
  #
  def to_s
    "#{major}.#{minor}.#{patch}"
  end

  # Return a readable representation
  #
  # @return [String] inspect string
  #
  def inspect
    "#<Git::Version #{self}>"
  end

  # Return the version as an array of integers
  #
  # Useful when legacy code expects the array shape returned by the
  # deprecated {Git::Lib#current_command_version} method.
  #
  # @return [Array<Integer>] [major, minor, patch]
  #
  # @example
  #   Git.git_version.to_a  #=> [2, 42, 0]
  #
  def to_a
    deconstruct
  end
end

Class Method Details

.parse(string) ⇒ Git::Version

Parse a version string into a Version object

Handles git's version output format, stripping platform suffixes (like .windows.1 or .vfs.0) and padding two-segment versions to three segments.

Examples:

Parse various version string formats

Git::Version.parse('2.42.1')  #=> Git::Version.new(2, 42, 1)
Git::Version.parse('git version 2.42.1')  #=> Git::Version.new(2, 42, 1)
Git::Version.parse('2.42.0.windows.1')  #=> Git::Version.new(2, 42, 0)

Parameters:

  • string (String)

    version string to parse

Returns:

Raises:



63
64
65
66
67
68
69
70
71
72
# File 'lib/git/version.rb', line 63

def self.parse(string)
  version_match = string&.match(/(\d+)\.(\d+)(?:\.(\d+))?/)
  raise Git::UnexpectedResultError, "Invalid version: #{string.inspect}" unless version_match

  major = version_match[1].to_i
  minor = version_match[2].to_i
  patch = (version_match[3] || '0').to_i

  new(major, minor, patch)
end

Instance Method Details

#<=>(other) ⇒ Integer

Compare this version to another

Parameters:

Returns:

  • (Integer)

    -1, 0, or 1



80
81
82
# File 'lib/git/version.rb', line 80

def <=>(other)
  [major, minor, patch] <=> [other.major, other.minor, other.patch]
end

#inspectString

Return a readable representation

Returns:

  • (String)

    inspect string



96
97
98
# File 'lib/git/version.rb', line 96

def inspect
  "#<Git::Version #{self}>"
end

#to_aArray<Integer>

Return the version as an array of integers

Useful when legacy code expects the array shape returned by the deprecated Lib#current_command_version method.

Examples:

Git.git_version.to_a  #=> [2, 42, 0]

Returns:

  • (Array<Integer>)

    [major, minor, patch]



110
111
112
# File 'lib/git/version.rb', line 110

def to_a
  deconstruct
end

#to_sString

Return the version as a dotted string

Returns:

  • (String)

    the version in "major.minor.patch" format



88
89
90
# File 'lib/git/version.rb', line 88

def to_s
  "#{major}.#{minor}.#{patch}"
end