From 8870a641d59b0d2db62180344f2174b5adc0bd20 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Fri, 29 Nov 2019 18:10:10 -0700 Subject: [PATCH 1/3] Fix #718 do not create HOME env var --- src/Utils.ps1 | 9 ++++++--- src/posh-git.psm1 | 3 --- test/DefaultPrompt.Tests.ps1 | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Utils.ps1 b/src/Utils.ps1 index 955f13f0d..54d863bd1 100644 --- a/src/Utils.ps1 +++ b/src/Utils.ps1 @@ -291,11 +291,14 @@ function Get-PromptPath { $stringComparison = Get-PathStringComparison + # $HOME is defined by PowerShell on all platforms. If for some reason it isn't defined, use a fallback. + $homePath = if ($HOME) {$HOME} else {[System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::UserProfile)} + # Abbreviate path by replacing beginning of path with ~ *iff* the path is under the user's home dir - if ($abbrevHomeDir -and $currentPath -and !$currentPath.Equals($Home, $stringComparison) -and - $currentPath.StartsWith($Home, $stringComparison)) { + if ($abbrevHomeDir -and $currentPath -and !$currentPath.Equals($homePath, $stringComparison) -and + $currentPath.StartsWith($homePath, $stringComparison)) { - $currentPath = "~" + $currentPath.SubString($Home.Length) + $currentPath = "~" + $currentPath.SubString($homePath.Length) } return $currentPath diff --git a/src/posh-git.psm1 b/src/posh-git.psm1 index 1f2b14d75..204dc9720 100644 --- a/src/posh-git.psm1 +++ b/src/posh-git.psm1 @@ -13,9 +13,6 @@ param([switch]$ForcePoshGitPrompt) . $PSScriptRoot\GitTabExpansion.ps1 . $PSScriptRoot\TortoiseGit.ps1 -if (!$Env:HOME) { $Env:HOME = "$Env:HOMEDRIVE$Env:HOMEPATH" } -if (!$Env:HOME) { $Env:HOME = "$Env:USERPROFILE" } - $IsAdmin = Test-Administrator # Get the default prompt definition. diff --git a/test/DefaultPrompt.Tests.ps1 b/test/DefaultPrompt.Tests.ps1 index 66a38abf0..0bbd91077 100644 --- a/test/DefaultPrompt.Tests.ps1 +++ b/test/DefaultPrompt.Tests.ps1 @@ -14,7 +14,7 @@ Describe 'Default Prompt Tests - NO ANSI' { Context 'Prompt with no Git summary' { It 'Returns the expected prompt string' { - Set-Location $env:HOME -ErrorAction Stop + Set-Location $HOME -ErrorAction Stop $res = [string](&$prompt *>&1) $res | Should BeExactly "$(Get-PromptConnectionInfo)$(GetHomePath)> " } @@ -152,7 +152,7 @@ Describe 'Default Prompt Tests - ANSI' { Context 'Prompt with no Git summary' { It 'Returns the expected prompt string' { - Set-Location $env:HOME -ErrorAction Stop + Set-Location $HOME -ErrorAction Stop $res = &$prompt $res | Should BeExactly "$(Get-PromptConnectionInfo)$(GetHomePath)> " } From 8fec4caf02ee9762830f8bd99b47119606ad7364 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Sun, 1 Dec 2019 12:52:15 -0700 Subject: [PATCH 2/3] Create env:home is certain cases but provide option to disable Modified after reviewing issue #153 --- src/Utils.ps1 | 9 +++------ src/posh-git.psm1 | 14 +++++++++++++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/Utils.ps1 b/src/Utils.ps1 index 54d863bd1..955f13f0d 100644 --- a/src/Utils.ps1 +++ b/src/Utils.ps1 @@ -291,14 +291,11 @@ function Get-PromptPath { $stringComparison = Get-PathStringComparison - # $HOME is defined by PowerShell on all platforms. If for some reason it isn't defined, use a fallback. - $homePath = if ($HOME) {$HOME} else {[System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::UserProfile)} - # Abbreviate path by replacing beginning of path with ~ *iff* the path is under the user's home dir - if ($abbrevHomeDir -and $currentPath -and !$currentPath.Equals($homePath, $stringComparison) -and - $currentPath.StartsWith($homePath, $stringComparison)) { + if ($abbrevHomeDir -and $currentPath -and !$currentPath.Equals($Home, $stringComparison) -and + $currentPath.StartsWith($Home, $stringComparison)) { - $currentPath = "~" + $currentPath.SubString($homePath.Length) + $currentPath = "~" + $currentPath.SubString($Home.Length) } return $currentPath diff --git a/src/posh-git.psm1 b/src/posh-git.psm1 index 204dc9720..561a2d033 100644 --- a/src/posh-git.psm1 +++ b/src/posh-git.psm1 @@ -1,4 +1,4 @@ -param([switch]$ForcePoshGitPrompt) +param([bool]$ForcePoshGitPrompt, [bool]$SkipHomeEnvVar) . $PSScriptRoot\CheckRequirements.ps1 > $null @@ -13,6 +13,18 @@ param([switch]$ForcePoshGitPrompt) . $PSScriptRoot\GitTabExpansion.ps1 . $PSScriptRoot\TortoiseGit.ps1 +# At the point we drop v5 support, we can rely on using the built-in $IsWindows +if ($PSVersionTable.PSVersion.Major -eq 5) { + $IsWindows = $true +} + +# Only create HOME env var on Windows when not defined and using older Git +if ($IsWindows -and !$Env:HOME -and !$SkipHomeEnvVar -and !(Get-Command git).Source.EndsWith('cmd\git.exe')) { + $Env:HOME = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::UserProfile) + Write-Warning "posh-git set `$Env:HOME for this version of Git. To disable creation of `$Env:HOME" + Write-Warning "and this warning, consider upgrading Git or use: Import-Module posh-git -Args 0,1" +} + $IsAdmin = Test-Administrator # Get the default prompt definition. From 869fff9256fa65684272c3cb6298b150fab577ab Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Sat, 7 Dec 2019 14:03:13 -0700 Subject: [PATCH 3/3] Go back to not defining $env:HOME --- src/posh-git.psm1 | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/posh-git.psm1 b/src/posh-git.psm1 index 561a2d033..a9e090ab7 100644 --- a/src/posh-git.psm1 +++ b/src/posh-git.psm1 @@ -1,4 +1,4 @@ -param([bool]$ForcePoshGitPrompt, [bool]$SkipHomeEnvVar) +param([bool]$ForcePoshGitPrompt) . $PSScriptRoot\CheckRequirements.ps1 > $null @@ -13,18 +13,6 @@ param([bool]$ForcePoshGitPrompt, [bool]$SkipHomeEnvVar) . $PSScriptRoot\GitTabExpansion.ps1 . $PSScriptRoot\TortoiseGit.ps1 -# At the point we drop v5 support, we can rely on using the built-in $IsWindows -if ($PSVersionTable.PSVersion.Major -eq 5) { - $IsWindows = $true -} - -# Only create HOME env var on Windows when not defined and using older Git -if ($IsWindows -and !$Env:HOME -and !$SkipHomeEnvVar -and !(Get-Command git).Source.EndsWith('cmd\git.exe')) { - $Env:HOME = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::UserProfile) - Write-Warning "posh-git set `$Env:HOME for this version of Git. To disable creation of `$Env:HOME" - Write-Warning "and this warning, consider upgrading Git or use: Import-Module posh-git -Args 0,1" -} - $IsAdmin = Test-Administrator # Get the default prompt definition.