forked from fwininger/software-version
-
Notifications
You must be signed in to change notification settings - Fork 4
Add SoftwareVersion::Rpm comparing versions with rpmvercmp semantics #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jinxka
wants to merge
1
commit into
Cyberwatch:master
Choose a base branch
from
jinxka:issue_737
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| require 'software_version/version' | ||
| require 'software_version/rpm' | ||
|
|
||
| module SoftwareVersion | ||
| end | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| require 'software_version/version' | ||
|
|
||
| # Root namespace of the gem. | ||
| module SoftwareVersion | ||
| # Version comparing like RPM (rpmvercmp, as used by rpm and dnf/yum). | ||
| # | ||
| # It differs from the generic Version on a few points: | ||
| # - every non-alphanumeric character other than '~' and '^' is a mute | ||
| # separator, so `module+el8+2468` aligns with `module+el8.1.0+3366` the way | ||
| # dnf compares them, while the generic tokenizer keeps '+' as an ordered | ||
| # token and misaligns them; | ||
| # - the string is first split into `[epoch:]version[-release]` on the last | ||
| # dash, and each part is compared fully before the next one; | ||
| # - words carry no semantic (no pre-version like alpha/rc, no dropped | ||
| # rev/update) and are compared as raw case-sensitive strings; | ||
| # - the number of segments matters: 1.0 > 1, unlike the generic 1.0 = 1. | ||
| class Rpm < Version | ||
| def <=>(other) | ||
| other = SoftwareVersion.Rpm(other) | ||
| cmp = epoch <=> other.epoch | ||
| cmp = compare_tokens(version_tokens, other.version_tokens) if cmp.zero? | ||
| cmp = compare_tokens(release_tokens, other.release_tokens) if cmp.zero? | ||
| cmp | ||
| end | ||
|
|
||
| def epoch | ||
| evr[0] | ||
| end | ||
|
|
||
| protected | ||
|
|
||
| # Expose the version and release tokens, mostly for #version_parts. The | ||
| # EOV of the version part keeps the first number sequence from leaking | ||
| # into the release. | ||
| def tokens | ||
| @tokens ||= version_tokens + release_tokens | ||
| end | ||
|
|
||
| def version_tokens | ||
| @version_tokens ||= parse(evr[1]) | ||
| end | ||
|
|
||
| def release_tokens | ||
| @release_tokens ||= parse(evr[2]) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # Split the raw string into [epoch, version, release] like rpm does: the | ||
| # epoch is the leading digits before a ':' and defaults to 0, the release | ||
| # is everything after the last '-' and defaults to "". | ||
| def evr | ||
| @evr ||= begin | ||
| rest = @v.to_s | ||
| epoch = 0 | ||
| if (match = rest.match(/\A(\d+):(.*)\z/m)) | ||
| epoch = match[1].to_i | ||
| rest = match[2] | ||
| end | ||
| dash = rest.rindex('-') | ||
| dash ? [epoch, rest[0...dash], rest[(dash + 1)..]] : [epoch, rest, ''] | ||
| end | ||
| end | ||
|
|
||
| # rpm keeps only numbers, words, '~' and '^'; every other character is a | ||
| # separator without semantic value. Words are left raw: rpmvercmp has no | ||
| # special words and compares them as case-sensitive ASCII strings. | ||
| # | ||
| # A trailing '^' keeps the generic "highest version possible" meaning | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comportement différent de rpmvercmp mais très utile chez nous. |
||
| # instead of rpmvercmp's ("1.0^" > "1.0" but < "1.0.1"), so that ranges | ||
| # like `>= 1.0, <= 1.0^` keep matching every 1.0 subversion. Real rpm | ||
| # versions never end with '^', so this exception costs no fidelity. | ||
| def parse(version_string) | ||
| semantic_tokens = [] | ||
| literal_tokens = lex(version_string) | ||
| literal_tokens << nil # Sentinel for each_cons. | ||
| literal_tokens.each_cons(2) do |current, ahead| | ||
| case current[0] | ||
| when Token::DOT, Token::UNDERSCORE, Token::COLON, | ||
| Token::DASH, Token::PLUS | ||
| # Mute separators. | ||
| when Token::CARET | ||
| semantic_tokens << (ahead[0] == Token::EOV ? [Token::MAX, current[1]] : current) | ||
| else | ||
| semantic_tokens << current | ||
| end | ||
| end | ||
| semantic_tokens | ||
| end | ||
| end | ||
|
|
||
| # Convert the argument to an Rpm version. An existing Rpm is reused as-is; | ||
| # any other Version or String is (re)wrapped so it compares with rpm/dnf | ||
| # semantics. | ||
| def Rpm(version) | ||
| return version if version.is_a?(Rpm) | ||
|
|
||
| Rpm.new(version.is_a?(Version) ? version.v : version.to_s) | ||
| end | ||
| module_function :Rpm | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C'est ce que fait RPM mais pas sur de si on veut vraiment garder ce comportement la.