Skip to content

Commit

Permalink
Merge pull request #885 from dscho/support-gfw-rc-versions
Browse files Browse the repository at this point in the history
Avoid warning when using -rc versions of Git for Windows
  • Loading branch information
dahlbyk authored Mar 31, 2022
2 parents 7764b4f + 55ef5d0 commit f5255b6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/CheckRequirements.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ if (!(Get-Command git -TotalCount 1 -ErrorAction SilentlyContinue)) {
return
}

if ([string](git --version 2> $null) -match '(?<ver>\d+(?:\.\d+)+)(?<g4w>\.windows|\.vfs)?') {
$script:GitVersion = [System.Version]$Matches['ver']
function Test-GitVersion ($version = $([string](git --version 2> $null))) {
if ($version -notmatch '(?<ver>\d+(?:\.\d+)+)(?<g4w>(?<rc>[-.]rc\d+)?\.windows|\.vfs)?') {
Write-Warning "posh-git could not parse Git version ($version)"
$script:GitVersion = $version
return $false
}

# On Windows, check if Git is not "Git for Windows"
if ((($PSVersionTable.PSVersion.Major -le 5) -or $IsWindows) -and !$Matches['g4w']) {
Expand All @@ -19,8 +23,12 @@ if ([string](git --version 2> $null) -match '(?<ver>\d+(?:\.\d+)+)(?<g4w>\.windo
Write-Warning 'You appear to have an unsupported Git distribution; setting $GitPromptSettings.AnsiConsole = $false. posh-git recommends Git for Windows.'
}
}

$script:GitVersion = [System.Version]$Matches['ver']

return $GitVersion -ge $requiredVersion
}

if ($GitVersion -lt $requiredVersion) {
if (!(Test-GitVersion)) {
Write-Warning "posh-git requires Git $requiredVersion or better. You have $GitVersion."
}
45 changes: 45 additions & 0 deletions test/CheckRequirements.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Not using BeforeAll because discovery doesn't like InModuleScope without posh-git loaded
. $PSScriptRoot\Shared.ps1

Describe 'Test-GitVersion' {
InModuleScope 'posh-git' {
It 'Returns true for Git for Windows newer than 2.15 (<_>)' -ForEach @(
'git version 2.33.0.rc2.windows.1',
'git version 2.33.0-rc2.windows.1',
'git version 2.31.0.vfs.0.1',
'git version 2.15.0.windows.0',
'git version 2.100.0.windows.0',
'git version 3.0.0.windows.0'
) {
Mock Write-Warning {}

Test-GitVersion $_ | Should -Be $true

Should -Not -Invoke Write-Warning
}

It 'Returns false for Git for Windows older than 2.15 (<_>)' -ForEach @(
'git version 0.1.0.windows',
'git version 1.99.0.windows',
'git version 2.14.0.windows'
) {
Mock Write-Warning {}

Test-GitVersion $_ | Should -Be $false

Should -Not -Invoke Write-Warning
}

It 'Returns false for unparseable version (<_>)' -ForEach @(
'git version 1'
) {
Mock Write-Warning {} -Verifiable -ParameterFilter { $Message -like '*could not parse*' }

Test-GitVersion $_ | Should -Be $false

Should -InvokeVerifiable Write-Warning
}

# TODO: Test Cygwin warning and POSHGIT_CYGWIN_WARNING
}
}

0 comments on commit f5255b6

Please sign in to comment.