Class: Git::Version
- Inherits:
-
Data
- Object
- Data
- Git::Version
- 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.
Instance Attribute Summary collapse
-
#major ⇒ Integer
readonly
The major version number.
-
#minor ⇒ Integer
readonly
The minor version number.
-
#patch ⇒ Integer
readonly
The patch version number.
Class Method Summary collapse
-
.parse(string) ⇒ Git::Version
Parse a version string into a Version object.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Compare this version to another.
-
#inspect ⇒ String
Return a readable representation.
-
#to_a ⇒ Array<Integer>
Return the version as an array of integers.
-
#to_s ⇒ String
Return the version as a dotted string.
Instance Attribute Details
#major ⇒ Integer (readonly)
The major version number
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 |
#minor ⇒ Integer (readonly)
The minor version number
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 |
#patch ⇒ Integer (readonly)
The patch version number
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.
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
83 84 85 |
# File 'lib/git/version.rb', line 83 def <=>(other) [major, minor, patch] <=> [other.major, other.minor, other.patch] end |
#inspect ⇒ String
Return a readable representation
99 100 101 |
# File 'lib/git/version.rb', line 99 def inspect "#<Git::Version #{self}>" end |
#to_a ⇒ Array<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.
112 113 114 |
# File 'lib/git/version.rb', line 112 def to_a deconstruct end |
#to_s ⇒ String
Return the version as a dotted string
91 92 93 |
# File 'lib/git/version.rb', line 91 def to_s "#{major}.#{minor}.#{patch}" end |