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 ⇒ Object
readonly
Returns the value of attribute major.
-
#minor ⇒ Object
readonly
Returns the value of attribute minor.
-
#patch ⇒ Object
readonly
Returns the value of attribute patch.
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 ⇒ Object (readonly)
Returns the value of attribute 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 |
#minor ⇒ Object (readonly)
Returns the value of attribute 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 |
#patch ⇒ Object (readonly)
Returns the value of attribute 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.
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
80 81 82 |
# File 'lib/git/version.rb', line 80 def <=>(other) [major, minor, patch] <=> [other.major, other.minor, other.patch] end |
#inspect ⇒ String
Return a readable representation
96 97 98 |
# File 'lib/git/version.rb', line 96 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 Lib#current_command_version method.
110 111 112 |
# File 'lib/git/version.rb', line 110 def to_a deconstruct end |
#to_s ⇒ String
Return the version as a dotted string
88 89 90 |
# File 'lib/git/version.rb', line 88 def to_s "#{major}.#{minor}.#{patch}" end |