-
-
Notifications
You must be signed in to change notification settings - Fork 2
Update commands semantics #217
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
11 commits
Select commit
Hold shift + click to select a range
fdc66eb
Add version package
thelovekesh dae1fba
Update version normalization
thelovekesh 32fc11e
Update checks for enabling color output
thelovekesh b162ae7
Update package details colorization
thelovekesh f9f90bc
Update preferences order in color enabled check
thelovekesh 816972a
Fix leading zeros in 4th numeric segment
thelovekesh 4de1a5a
Add test cases for version.Normalize
thelovekesh cc4dfe6
Fix converting dot to hyphen in version meta
thelovekesh c7f978b
Add more test cases for version normalization
thelovekesh 598fe32
Update version normalization to strip all v prefixes from version string
thelovekesh ebd6ee0
Add version normalization edge cases tests
thelovekesh 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,132 @@ | ||
| package version | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "regexp" | ||
| "strings" | ||
|
|
||
| "github.com/Masterminds/semver/v3" | ||
| ) | ||
|
|
||
| const maxVersionLength = 64 | ||
|
|
||
| var ( | ||
| numericIdentifier = regexp.MustCompile(`^\d+$`) | ||
| trailingAlphaSuffix = regexp.MustCompile(`^(\d+(?:\.\d+){0,2})([A-Za-z][A-Za-z0-9.]*)$`) | ||
| ) | ||
|
|
||
| // Normalize converts a version string into strict semver format (X.Y.Z[-prerelease][+build]). | ||
| // | ||
| // It handles common PHP/WordPress patterns: | ||
| // - leading 'v' or 'V' prefix (v1.2.3 -> 1.2.3) | ||
| // - leading zeros in core segments (01.0.0 -> 1.0.0) | ||
| // - leading zeros in numeric prerelease segments (1.0.0.01 -> 1.0.0-1) | ||
| // - short forms (1, 1.2 -> 1.0.0, 1.2.0) | ||
| // - alphabetic suffixes without separator (1.0.0beta -> 1.0.0-beta) | ||
| // - 4+ dotted segments collapsed into prerelease (1.0.0.0 -> 1.0.0-0) | ||
| // | ||
| // The output is guaranteed to satisfy semver.StrictNewVersion and to be no | ||
| // longer than 64 characters. Inputs that cannot be normalized return an error. | ||
| func Normalize(version string) (string, error) { | ||
|
thelovekesh marked this conversation as resolved.
|
||
| version = strings.TrimSpace(version) | ||
| if version == "" { | ||
| return "", errors.New("version cannot be empty") | ||
| } | ||
|
|
||
| // Fast path for already-normalized versions. | ||
| if v, err := semver.StrictNewVersion(version); err == nil { | ||
| if len(version) <= maxVersionLength { | ||
| return v.String(), nil | ||
| } | ||
| } | ||
|
|
||
| version = strings.TrimLeft(version, "vV") | ||
| if version == "" { | ||
| return "", errors.New("version contains only 'v' prefixes") | ||
| } | ||
|
|
||
| // Isolate the core version from prerelease/build metadata to prevent | ||
| // mangling valid semver extensions (e.g., dotted prereleases) below. | ||
| core := version | ||
| var meta string | ||
|
|
||
| idxPre := strings.IndexByte(version, '-') | ||
| idxBld := strings.IndexByte(version, '+') | ||
| cutoff := -1 | ||
|
|
||
| if idxPre != -1 { | ||
| cutoff = idxPre | ||
| } | ||
| if idxBld != -1 && (cutoff == -1 || idxBld < cutoff) { | ||
| cutoff = idxBld | ||
| } | ||
|
|
||
| if cutoff != -1 { | ||
| core = version[:cutoff] | ||
| meta = version[cutoff:] | ||
| } | ||
|
|
||
| // Insert hyphen before an alphabetic qualifier with no separator. | ||
| // 1.0.0beta1 -> 1.0.0-beta1, 2.1rc1 -> 2.1-rc1, 3.0a -> 3.0-a | ||
| core = trailingAlphaSuffix.ReplaceAllString(core, "$1-$2") | ||
|
|
||
| // If the regex introduced a hyphen, move that new suffix into meta. | ||
| if idx := strings.IndexByte(core, '-'); idx != -1 { | ||
| meta = core[idx:] + meta | ||
| core = core[:idx] | ||
| } | ||
|
|
||
| // Collapse 4+ dotted segments into a prerelease, stripping leading zeros | ||
| // from any purely numeric segment (semver forbids them in numeric IDs). | ||
| // 1.0.0.0 -> 1.0.0-0 | ||
| // 1.0.0.01 -> 1.0.0-1 | ||
| // 1.0.0.01.02 -> 1.0.0-1.2 | ||
| // 1.2.3.4.5 -> 1.2.3-4.5 | ||
| // 1.0.0.alpha.1 -> 1.0.0-alpha.1 | ||
| // 1.0.0.0beta -> 1.0.0-0beta (mixed; left alone) | ||
| if parts := strings.Split(core, "."); len(parts) > 3 { | ||
| pre := make([]string, len(parts)-3) | ||
| for i, p := range parts[3:] { | ||
| pre[i] = stripLeadingZeros(p) | ||
| } | ||
| core = fmt.Sprintf("%s.%s.%s", parts[0], parts[1], parts[2]) | ||
|
|
||
| // Prepend the collapsed segments to any existing metadata | ||
| if meta == "" || meta[0] == '+' { | ||
| meta = "-" + strings.Join(pre, ".") + meta | ||
| } else { | ||
| // Merge with existing prerelease by replacing the initial '-' with a '.' | ||
| meta = "-" + strings.Join(pre, ".") + "." + meta[1:] | ||
| } | ||
| } | ||
|
|
||
| // Coerce to semver, which will handle leading zeros and short forms. | ||
| v, err := semver.NewVersion(core + meta) | ||
| if err != nil { | ||
| return "", fmt.Errorf("cannot normalize %q to semver: %w", version, err) | ||
| } | ||
|
|
||
| result := v.String() | ||
|
|
||
| if len(result) > maxVersionLength { | ||
| return "", fmt.Errorf("normalized result %q has length %d, exceeds maximum of %d", | ||
| result, len(result), maxVersionLength) | ||
| } | ||
| if _, err := semver.StrictNewVersion(result); err != nil { | ||
| return "", fmt.Errorf("normalized result %q is not strict semver: %w", result, err) | ||
| } | ||
|
thelovekesh marked this conversation as resolved.
|
||
|
|
||
| return result, nil | ||
| } | ||
|
thelovekesh marked this conversation as resolved.
|
||
|
|
||
| func stripLeadingZeros(s string) string { | ||
| if !numericIdentifier.MatchString(s) { | ||
| return s | ||
| } | ||
| trimmed := strings.TrimLeft(s, "0") | ||
| if trimmed == "" { | ||
| return "0" | ||
| } | ||
| return trimmed | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.