-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix version comparison logic in update checker #4466
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
base: master
Are you sure you want to change the base?
Fix version comparison logic in update checker #4466
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Summary of ChangesHello @Gautam-aman, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical bug in the update checker that caused it to misidentify older versions as newer, resulting in erroneous update prompts. The change introduces a sophisticated numeric version comparison mechanism, ensuring that update notifications are accurate and only triggered for genuinely newer versions. This enhancement improves the reliability of the update checker without altering any other system behaviors. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request fixes a version comparison bug by implementing a numeric comparison for version strings. The overall approach is good, and the addition of unit tests is appreciated.
I have two main points of feedback:
- The new version comparison logic does not handle pre-release suffixes (like
-SNAPSHOTor-alpha) correctly, which can lead to missed update notifications. I've left a comment with more details and suggestions. - There's a performance improvement opportunity in the
parsePartmethod by avoiding repeated regex compilation. I've provided a code suggestion for this.
Addressing these points will make the update checker more robust and performant.
| static int compareVersions(String v1, String v2) { | ||
| String[] p1 = v1.split("\\."); | ||
| String[] p2 = v2.split("\\."); | ||
|
|
||
| int len = Math.max(p1.length, p2.length); | ||
| for (int i = 0; i < len; i++) { | ||
| int a = i < p1.length ? parsePart(p1[i]) : 0; | ||
| int b = i < p2.length ? parsePart(p2[i]) : 0; | ||
| if (a != b) { | ||
| return Integer.compare(a, b); | ||
| } | ||
| } | ||
| return 0; | ||
| } |
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.
The current implementation correctly handles numeric version parts but does not correctly handle pre-release identifiers like -alpha or -SNAPSHOT. For example, 1.0.0-SNAPSHOT should be considered an older version than 1.0.0, but this implementation will treat them as equal. This could lead to missed update notifications for users on pre-release versions.
I recommend adding test cases to cover these scenarios, for example:
assertThat(UpdateChecker.compareVersions("1.0.0-SNAPSHOT", "1.0.0")).isLessThan(0);
assertThat(UpdateChecker.compareVersions("1.0.0-alpha", "1.0.0-beta")).isLessThan(0);To support this, the compareVersions method would need to be updated to handle these suffixes. One approach is to check for a hyphen in version parts. If the numeric parts are equal, the version part with a suffix can be considered smaller than one without. If both have suffixes, they can be compared lexicographically.
| private static int parsePart(String s) { | ||
| try { | ||
| // Extract leading numeric portion (e.g., "1-SNAPSHOT" → "1") | ||
| String digits = s.replaceAll("^(\\d+).*$", "$1"); | ||
| return Integer.parseInt(digits); | ||
| } catch (NumberFormatException e) { | ||
| return 0; | ||
| } | ||
| } |
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.
Using String.replaceAll() with a regex can be inefficient, as it typically compiles the regular expression on each call. Since this method is called within a loop in compareVersions, this could have a performance impact. A more performant approach would be to parse the leading digits manually. This also makes the logic for extracting the numeric part more explicit.
private static int parsePart(String s) {
// Extract leading numeric portion (e.g., "1-SNAPSHOT" → "1")
int i = 0;
while (i < s.length() && Character.isDigit(s.charAt(i))) {
i++;
}
if (i == 0) {
return 0;
}
try {
return Integer.parseInt(s.substring(0, i));
} catch (NumberFormatException e) {
// Should only happen on integer overflow for very long version parts.
return 0;
}
}|
Thanks for the helpful review! 🙌
Please let me know if you'd like any further improvements. |
Summary
This PR fixes the issue where the update check incorrectly reports an older version as a newer one. For example, it reported
3.4.6as newer even when the currently used version was3.5.0.Root Cause
Version strings were compared lexicographically instead of numerically (e.g.,
"3.10"<"3.9"when using string comparison).Fix
3.5) as equivalent to3.5.0.testCompareVersions()to prevent regressions.Tests
All tests pass:
./gradlew :jib-plugins-common:test
./gradlew :jib-maven-plugin:test
This resolves the incorrect user warning which caused confusion as 3.4.6 is older than 3.5.0.
Notes
No behavior changes other than correcting version comparison.