-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: version prefix detection #5943
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
75207f3
fix: fix version prefix detection
risu729 ab9b88e
fix: fix import
risu729 08cbc96
[autofix.ci] apply automated fixes
autofix-ci[bot] 3bab46f
Merge branch 'main' into version-prefix-fix
risu729 f2fa3e1
test: match to current behaviour
risu729 e7bfb98
refactor: use split_version_prefix functions
risu729 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
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
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
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,98 @@ | ||
| use versions::{Mess, Versioning}; | ||
|
|
||
| /// splits a version number into an optional prefix and the remaining version string | ||
| pub fn split_version_prefix(version: &str) -> (String, String) { | ||
| version | ||
| .char_indices() | ||
| .find_map(|(i, c)| { | ||
| if c.is_ascii_digit() { | ||
| if i == 0 { | ||
| return Some(i); | ||
| } | ||
| // If the previous char is a delimiter or 'v', we found a split point. | ||
| let prev_char = version.chars().nth(i - 1).unwrap(); | ||
| if ['-', '_', '/', '.', 'v', 'V'].contains(&prev_char) { | ||
| return Some(i); | ||
| } | ||
| } | ||
| None | ||
| }) | ||
| .map_or_else( | ||
| || ("".into(), version.into()), | ||
| |i| { | ||
| let (prefix, version) = version.split_at(i); | ||
| (prefix.into(), version.into()) | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| /// split a version number into chunks | ||
| /// given v: "1.2-3a4" return ["1", ".2", "-3a4"] | ||
|
||
| pub fn chunkify_version(v: &str) -> Vec<String> { | ||
| fn chunkify(m: &Mess, sep0: &str, chunks: &mut Vec<String>) { | ||
| for (i, chunk) in m.chunks.iter().enumerate() { | ||
| let sep = if i == 0 { sep0 } else { "." }; | ||
| chunks.push(format!("{sep}{chunk}")); | ||
| } | ||
| if let Some((next_sep, next_mess)) = &m.next { | ||
| chunkify(next_mess, next_sep.to_string().as_ref(), chunks) | ||
| } | ||
| } | ||
|
|
||
| let mut chunks = vec![]; | ||
| // don't parse "latest", otherwise bump from latest to any version would have one chunk only | ||
| if v != "latest" { | ||
| if let Some(v) = Versioning::new(v) { | ||
| let m = match v { | ||
| Versioning::Ideal(sem_ver) => sem_ver.to_mess(), | ||
| Versioning::General(version) => version.to_mess(), | ||
| Versioning::Complex(mess) => mess, | ||
| }; | ||
| chunkify(&m, "", &mut chunks); | ||
| } | ||
| } | ||
| chunks | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::{chunkify_version, split_version_prefix}; | ||
|
|
||
| #[test] | ||
| fn test_split_version_prefix() { | ||
| assert_eq!(split_version_prefix("latest"), ("".into(), "latest".into())); | ||
| assert_eq!(split_version_prefix("v1.2.3"), ("v".into(), "1.2.3".into())); | ||
| assert_eq!( | ||
| split_version_prefix("mountpoint-s3-v1.2.3-5_beta.5"), | ||
| ("mountpoint-s3-v".into(), "1.2.3-5_beta.5".into()) | ||
| ); | ||
| assert_eq!( | ||
| split_version_prefix("cli/1.2.3"), | ||
| ("cli/".into(), "1.2.3".into()) | ||
| ); | ||
| assert_eq!( | ||
| split_version_prefix("temurin-17.0.7+7"), | ||
| ("temurin-".into(), "17.0.7+7".into()) | ||
| ); | ||
| assert_eq!(split_version_prefix("1.2"), ("".into(), "1.2".into())); | ||
| assert_eq!( | ||
| split_version_prefix("2:1.2.1"), | ||
| ("".into(), "2:1.2.1".into()) | ||
| ); | ||
| assert_eq!( | ||
| split_version_prefix("2025-05-17"), | ||
| ("".into(), "2025-05-17".into()) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_chunkify_version() { | ||
| assert_eq!(chunkify_version("1.2-3a4"), vec!["1", ".2", "-3a4"]); | ||
| assert_eq!(chunkify_version("latest"), Vec::<String>::new()); | ||
| assert_eq!(chunkify_version("1.0.0"), vec!["1", ".0", ".0"]); | ||
| assert_eq!( | ||
| chunkify_version("2.3.4-beta"), | ||
| vec!["2", ".3", ".4", "-beta"] | ||
| ); | ||
| } | ||
| } | ||
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.
Bug: Unicode Indexing Mismatch Causes Panic
The
split_version_prefixfunction incorrectly mixes byte indices fromchar_indices()with character indices forchars().nth(). Whenchar_indices()returns a byte indexifor a digit,version.chars().nth(i - 1)attempts to access the character at character positioni - 1. For strings containing multi-byte Unicode characters, this can causeunwrap()to panic becausei - 1(a byte index) may exceed the actual character count, leadingchars().nth()to returnNone. It can also result in incorrect prefix detection if a panic does not occur.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.
I don't think this is a concern, version numbers probably don't support unicode anyways
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.
Yes, and also I think it's incorrect.
char_indicesdoesn't return byte indices.