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

#majorInteger (readonly)

The major version number

Returns:

  • (Integer)


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

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.
  #
  # @example Get version as integer components
  #   Git.git_version.to_a  #=> [2, 42, 0]
  #
  # @return [Array<Integer>] [major, minor, patch]
  def to_a
    deconstruct
  end
end

#minorInteger (readonly)

The minor version number

Returns:

  • (Integer)


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

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.
  #
  # @example Get version as integer components
  #   Git.git_version.to_a  #=> [2, 42, 0]
  #
  # @return [Array<Integer>] [major, minor, patch]
  def to_a
    deconstruct
  end
end

#patchInteger (readonly)

The patch version number

Returns:

  • (Integer)


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

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.
  #
  # @example Get version as integer components
  #   Git.git_version.to_a  #=> [2, 42, 0]
  #
  # @return [Array<Integer>] [major, minor, patch]
  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:



66
67
68
69
70
71
72
73
74
75
# File 'lib/git/version.rb', line 66

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



83
84
85
# File 'lib/git/version.rb', line 83

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

#inspectString

Return a readable representation

Returns:

  • (String)

    inspect string



99
100
101
# File 'lib/git/version.rb', line 99

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 Git::Lib#current_command_version method.

Examples:

Get version as integer components

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

Returns:

  • (Array<Integer>)

    [major, minor, patch]



112
113
114
# File 'lib/git/version.rb', line 112

def to_a
  deconstruct
end

#to_sString

Return the version as a dotted string

Returns:

  • (String)

    the version in "major.minor.patch" format



91
92
93
# File 'lib/git/version.rb', line 91

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