Skip to content

Commit f6adae5

Browse files
CopilotDRSDavidSoft
andcommitted
Fix version comparison to handle Git version strings correctly
Co-authored-by: DRSDavidSoft <[email protected]>
1 parent c039d97 commit f6adae5

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

vendor/psmodules/Cmder.ps1

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,22 @@ function Compare-Version {
5353
if ($null -eq $UserVersion) { return -1 }
5454
if ($null -eq $VendorVersion) { return 1 }
5555

56-
try {
57-
$userVer = [version]$UserVersion
58-
$vendorVer = [version]$VendorVersion
59-
return $userVer.CompareTo($vendorVer)
60-
} catch {
61-
# Fallback to string comparison if version parsing fails
62-
return [string]::Compare($UserVersion, $VendorVersion)
56+
# Extract all numeric parts from version strings (e.g., "2.49.0.windows.1" -> 2, 49, 0, 1)
57+
# This handles Git version strings like "2.49.0.windows.1" correctly
58+
$userParts = [regex]::Matches($UserVersion, '\d+') | ForEach-Object { [int]$_.Value }
59+
$vendorParts = [regex]::Matches($VendorVersion, '\d+') | ForEach-Object { [int]$_.Value }
60+
61+
# Compare each numeric part sequentially
62+
$maxLength = [Math]::Max($userParts.Count, $vendorParts.Count)
63+
for ($i = 0; $i -lt $maxLength; $i++) {
64+
$userPart = if ($i -lt $userParts.Count) { $userParts[$i] } else { 0 }
65+
$vendorPart = if ($i -lt $vendorParts.Count) { $vendorParts[$i] } else { 0 }
66+
67+
if ($userPart -gt $vendorPart) { return 1 }
68+
if ($userPart -lt $vendorPart) { return -1 }
6369
}
70+
71+
return 0
6472
}
6573

6674
function Compare-GitVersion {

0 commit comments

Comments
 (0)