From 8908f89ad1fa8061932a926c373cdd606a109575 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Mon, 2 Jan 2017 17:46:06 -0700 Subject: [PATCH 1/7] One attempt to exporting a prompt function. Fix #217. This would only export the prompt function if the prompt function is still set to the default. --- GitPrompt.ps1 | 24 ++++++++++++++++++++++++ posh-git.psd1 | 35 +++++++++++++++++++---------------- posh-git.psm1 | 43 +++++++++++++++++++++++++++++-------------- 3 files changed, 72 insertions(+), 30 deletions(-) diff --git a/GitPrompt.ps1 b/GitPrompt.ps1 index c3aaf913c..1047f0b30 100644 --- a/GitPrompt.ps1 +++ b/GitPrompt.ps1 @@ -306,6 +306,30 @@ $PoshGitVcsPrompt = { Write-GitStatus $GitStatus } +# A simple prompt that displays Git status summary info when inside of a Git repo. +function prompt { + $origLastExitCode = $LASTEXITCODE + + # A UNC path has no drive so it's better to use the ProviderPath e.g. "\\server\share". + # However for any path with a drive defined, it's better to use the Path property. + # In this case, ProviderPath is "\LocalMachine\My"" whereas Path is "Cert:\LocalMachine\My". + # The latter is more desirable. + $pathInfo = $ExecutionContext.SessionState.Path.CurrentLocation + $curPath = if ($pathInfo.Drive) { $pathInfo.Path } else { $pathInfo.ProviderPath } + if ($curPath -and $curPath.ToLower().StartsWith($Home.ToLower())) + { + $curPath = "~" + $curPath.SubString($Home.Length) + } + + Write-Host $curPath -NoNewline + + # Write the Git status summary information to the host. + Write-VcsStatus + + $global:LASTEXITCODE = $origLastExitCode + "> " +} + # Install handler for removal/unload of the module $Global:VcsPromptStatuses += $PoshGitVcsPrompt $ExecutionContext.SessionState.Module.OnRemove = { diff --git a/posh-git.psd1 b/posh-git.psd1 index e888c5cda..3bc251ed4 100644 --- a/posh-git.psd1 +++ b/posh-git.psd1 @@ -22,22 +22,25 @@ Description = 'A PowerShell environment for Git' PowerShellVersion = '2.0' # Functions to export from this module -FunctionsToExport = @('Invoke-NullCoalescing', - 'Write-GitStatus', - 'Write-Prompt', - 'Write-VcsStatus', - 'Get-GitStatus', - 'Enable-GitColors', - 'Get-GitDirectory', - 'TabExpansion', - 'Get-AliasPattern', - 'Get-SshAgent', - 'Start-SshAgent', - 'Stop-SshAgent', - 'Add-SshKey', - 'Get-SshPath', - 'Update-AllBranches', - 'tgit') +FunctionsToExport = @( + 'Invoke-NullCoalescing', + 'Write-GitStatus', + 'Write-Prompt', + 'Write-VcsStatus', + 'Get-GitStatus', + 'Enable-GitColors', + 'Get-GitDirectory', + 'TabExpansion', + 'Get-AliasPattern', + 'Get-SshAgent', + 'Start-SshAgent', + 'Stop-SshAgent', + 'Add-SshKey', + 'Get-SshPath', + 'Update-AllBranches', + 'prompt', + 'tgit' +) # Cmdlets to export from this module CmdletsToExport = @() diff --git a/posh-git.psm1 b/posh-git.psm1 index 96efea065..60da24ccf 100644 --- a/posh-git.psm1 +++ b/posh-git.psm1 @@ -1,4 +1,4 @@ -param([switch]$NoVersionWarn = $false) +param([switch]$NoVersionWarn) if (Get-Module posh-git) { return } @@ -11,15 +11,16 @@ if ($psv.Major -lt 3 -and !$NoVersionWarn) { "To suppress this warning, change your profile to include 'Import-Module posh-git -Args `$true'.") } -Push-Location $psScriptRoot -.\CheckVersion.ps1 > $null +& $PSScriptRoot\CheckVersion.ps1 > $null -. .\Utils.ps1 -. .\GitUtils.ps1 -. .\GitPrompt.ps1 -. .\GitTabExpansion.ps1 -. .\TortoiseGit.ps1 -Pop-Location +# Get the current prompt function *before* we define a prompt function by dot sourcing GitPrompt.ps1. +$currentPromptDef = if ($funcInfo = Get-Command prompt -ErrorAction SilentlyContinue) { $funcInfo.Definition } + +. $PSScriptRoot\Utils.ps1 +. $PSScriptRoot\GitUtils.ps1 +. $PSScriptRoot\GitPrompt.ps1 +. $PSScriptRoot\GitTabExpansion.ps1 +. $PSScriptRoot\TortoiseGit.ps1 if (!$Env:HOME) { $Env:HOME = "$Env:HOMEDRIVE$Env:HOMEPATH" } if (!$Env:HOME) { $Env:HOME = "$Env:USERPROFILE" } @@ -27,10 +28,9 @@ if (!$Env:HOME) { $Env:HOME = "$Env:USERPROFILE" } Get-TempEnv 'SSH_AGENT_PID' Get-TempEnv 'SSH_AUTH_SOCK' -Export-ModuleMember ` - -Alias @( - '??') ` - -Function @( +$exportModuleParams = @{ + Alias = @('??') # TODO: Remove in 1.0.0 + Function = @( 'Invoke-NullCoalescing', 'Write-GitStatus', 'Write-Prompt', @@ -46,6 +46,21 @@ Export-ModuleMember ` 'Add-SshKey', 'Get-SshPath', 'Update-AllBranches', - 'tgit') + 'tgit' + ) +} + +# Get the default prompt definition. +if ($psv.Major -eq 2) { + $defaultPromptDef = "`$(if (test-path variable:/PSDebugContext) { '[DBG]: ' } else { '' }) + 'PS ' + `$(Get-Location) + `$(if (`$nestedpromptlevel -ge 1) { '>>' }) + '> '" +} +else { + $defaultPromptDef = [Runspace]::DefaultRunspace.InitialSessionState.Commands['prompt'].Definition +} +# If there is no prompt function or the prompt function is the default, export the posh-git prompt function. +if (!$currentPromptDef -or ($currentPromptDef -eq $defaultPromptDef)) { + $exportModuleParams.Function += 'prompt' +} +Export-ModuleMember @exportModuleParams From dd95dfe3ccdf4d54e8246dce7a51f82a23e0a3c5 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Mon, 2 Jan 2017 21:15:55 -0700 Subject: [PATCH 2/7] Switch to set-item to change prompt. This was a tip from @lzybkr and it seems to solve the issue with removal of the module wiping out the prompt function. Yay! --- GitPrompt.ps1 | 28 -------------------- posh-git.psd1 | 1 - posh-git.psm1 | 72 ++++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 57 insertions(+), 44 deletions(-) diff --git a/GitPrompt.ps1 b/GitPrompt.ps1 index 1047f0b30..74fb0f4d1 100644 --- a/GitPrompt.ps1 +++ b/GitPrompt.ps1 @@ -306,32 +306,4 @@ $PoshGitVcsPrompt = { Write-GitStatus $GitStatus } -# A simple prompt that displays Git status summary info when inside of a Git repo. -function prompt { - $origLastExitCode = $LASTEXITCODE - - # A UNC path has no drive so it's better to use the ProviderPath e.g. "\\server\share". - # However for any path with a drive defined, it's better to use the Path property. - # In this case, ProviderPath is "\LocalMachine\My"" whereas Path is "Cert:\LocalMachine\My". - # The latter is more desirable. - $pathInfo = $ExecutionContext.SessionState.Path.CurrentLocation - $curPath = if ($pathInfo.Drive) { $pathInfo.Path } else { $pathInfo.ProviderPath } - if ($curPath -and $curPath.ToLower().StartsWith($Home.ToLower())) - { - $curPath = "~" + $curPath.SubString($Home.Length) - } - - Write-Host $curPath -NoNewline - - # Write the Git status summary information to the host. - Write-VcsStatus - - $global:LASTEXITCODE = $origLastExitCode - "> " -} - -# Install handler for removal/unload of the module $Global:VcsPromptStatuses += $PoshGitVcsPrompt -$ExecutionContext.SessionState.Module.OnRemove = { - $Global:VcsPromptStatuses = $Global:VcsPromptStatuses | Where-Object { $_ -ne $PoshGitVcsPrompt } -} diff --git a/posh-git.psd1 b/posh-git.psd1 index 3bc251ed4..d94163aa5 100644 --- a/posh-git.psd1 +++ b/posh-git.psd1 @@ -38,7 +38,6 @@ FunctionsToExport = @( 'Add-SshKey', 'Get-SshPath', 'Update-AllBranches', - 'prompt', 'tgit' ) diff --git a/posh-git.psm1 b/posh-git.psm1 index 60da24ccf..f3411c880 100644 --- a/posh-git.psm1 +++ b/posh-git.psm1 @@ -28,7 +28,62 @@ if (!$Env:HOME) { $Env:HOME = "$Env:USERPROFILE" } Get-TempEnv 'SSH_AGENT_PID' Get-TempEnv 'SSH_AUTH_SOCK' -$exportModuleParams = @{ +# Get the default prompt definition. +if ($psv.Major -eq 2) { + $defaultPromptDef = "`$(if (test-path variable:/PSDebugContext) { '[DBG]: ' } else { '' }) + 'PS ' + `$(Get-Location) + `$(if (`$nestedpromptlevel -ge 1) { '>>' }) + '> '" +} +else { + $defaultPromptDef = [Runspace]::DefaultRunspace.InitialSessionState.Commands['prompt'].Definition +} + +# If there is no prompt function or the prompt function is the default, export the posh-git prompt function. +$promptReplaced = $false +if (!$currentPromptDef -or ($currentPromptDef -eq $defaultPromptDef)) { + Set-Item Function:\prompt -Value { + $origLastExitCode = $LASTEXITCODE + + # A UNC path has no drive so it's better to use the ProviderPath e.g. "\\server\share". + # However for any path with a drive defined, it's better to use the Path property. + # In this case, ProviderPath is "\LocalMachine\My"" whereas Path is "Cert:\LocalMachine\My". + # The latter is more desirable. + $pathInfo = $ExecutionContext.SessionState.Path.CurrentLocation + $curPath = if ($pathInfo.Drive) { $pathInfo.Path } else { $pathInfo.ProviderPath } + if ($curPath -and $curPath.ToLower().StartsWith($Home.ToLower())) + { + $curPath = "~" + $curPath.SubString($Home.Length) + } + + # Write the current path. + Write-Host $curPath -NoNewline + + # Write the Git status summary information. + Write-VcsStatus + + # When in debug mode, let user know + if ((Test-Path Variable:\PSDebugContext) -or [runspace]::DefaultRunspace.Debugger.InBreakpoint) { + $promptSuffix = "`n[DBG]: PS>> " + } + else { + $promptSuffix = "`nPS> " + } + + $global:LASTEXITCODE = $origLastExitCode + $promptSuffix + } + + $promptReplaced = $true +} + +# Install handler for removal/unload of the module +$ExecutionContext.SessionState.Module.OnRemove = { + $Global:VcsPromptStatuses = $Global:VcsPromptStatuses | Where-Object { $_ -ne $PoshGitVcsPrompt } + + if ($promptReplaced) { + Set-Item Function:\prompt -Value ([scriptblock]::Create($defaultPromptDef)) + } +} + +$exportModuleMemberParams = @{ Alias = @('??') # TODO: Remove in 1.0.0 Function = @( 'Invoke-NullCoalescing', @@ -50,17 +105,4 @@ $exportModuleParams = @{ ) } -# Get the default prompt definition. -if ($psv.Major -eq 2) { - $defaultPromptDef = "`$(if (test-path variable:/PSDebugContext) { '[DBG]: ' } else { '' }) + 'PS ' + `$(Get-Location) + `$(if (`$nestedpromptlevel -ge 1) { '>>' }) + '> '" -} -else { - $defaultPromptDef = [Runspace]::DefaultRunspace.InitialSessionState.Commands['prompt'].Definition -} - -# If there is no prompt function or the prompt function is the default, export the posh-git prompt function. -if (!$currentPromptDef -or ($currentPromptDef -eq $defaultPromptDef)) { - $exportModuleParams.Function += 'prompt' -} - -Export-ModuleMember @exportModuleParams +Export-ModuleMember @exportModuleMemberParams From f7525bcde838478c750210f42beb9f2dedf14ae1 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Mon, 2 Jan 2017 21:21:53 -0700 Subject: [PATCH 3/7] Cleanup after removing prompt func from GitPrompt. --- posh-git.psm1 | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/posh-git.psm1 b/posh-git.psm1 index f3411c880..bd1145436 100644 --- a/posh-git.psm1 +++ b/posh-git.psm1 @@ -13,9 +13,6 @@ if ($psv.Major -lt 3 -and !$NoVersionWarn) { & $PSScriptRoot\CheckVersion.ps1 > $null -# Get the current prompt function *before* we define a prompt function by dot sourcing GitPrompt.ps1. -$currentPromptDef = if ($funcInfo = Get-Command prompt -ErrorAction SilentlyContinue) { $funcInfo.Definition } - . $PSScriptRoot\Utils.ps1 . $PSScriptRoot\GitUtils.ps1 . $PSScriptRoot\GitPrompt.ps1 @@ -38,6 +35,7 @@ else { # If there is no prompt function or the prompt function is the default, export the posh-git prompt function. $promptReplaced = $false +$currentPromptDef = if ($funcInfo = Get-Command prompt -ErrorAction SilentlyContinue) { $funcInfo.Definition } if (!$currentPromptDef -or ($currentPromptDef -eq $defaultPromptDef)) { Set-Item Function:\prompt -Value { $origLastExitCode = $LASTEXITCODE From 46933f87e55a7309861280355a3ce19679cb4ff1 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Tue, 3 Jan 2017 20:28:01 -0700 Subject: [PATCH 4/7] Set PSReadlineOption for 2 line prompt. Also handle the case where the user creates a different prompt function *after* posh-git sets the prompt function. In that case, we won't restore the original prompt function that we stashed. --- posh-git.psm1 | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/posh-git.psm1 b/posh-git.psm1 index bd1145436..45f213582 100644 --- a/posh-git.psm1 +++ b/posh-git.psm1 @@ -35,9 +35,12 @@ else { # If there is no prompt function or the prompt function is the default, export the posh-git prompt function. $promptReplaced = $false +$origPsrlExtraPromptLineCount = 0 +$poshGitPromptScriptBlock = $null + $currentPromptDef = if ($funcInfo = Get-Command prompt -ErrorAction SilentlyContinue) { $funcInfo.Definition } if (!$currentPromptDef -or ($currentPromptDef -eq $defaultPromptDef)) { - Set-Item Function:\prompt -Value { + $poshGitPromptScriptBlock = { $origLastExitCode = $LASTEXITCODE # A UNC path has no drive so it's better to use the ProviderPath e.g. "\\server\share". @@ -69,6 +72,20 @@ if (!$currentPromptDef -or ($currentPromptDef -eq $defaultPromptDef)) { $promptSuffix } + # Install the posh-git prompt as the default prompt + Set-Item Function:\prompt -Value $poshGitPromptScriptBlock + + # If default posh-git prompt is two lines then we should ensure PSReadline is set to handle that + if (Get-Command Set-PSReadlineOption -ErrorAction SilentlyContinue) { + try { + $origPsrlExtraPromptLineCount = (Get-PSReadlineOption).ExtraPromptLineCount + Set-PSReadlineOption -ExtraPromptLineCount 1 -ErrorAction SilentlyContinue + } + catch { + Write-Debug "Failed to get or set PSReadline ExtraPromptLineCount. Error: $_" + } + } + $promptReplaced = $true } @@ -77,7 +94,21 @@ $ExecutionContext.SessionState.Module.OnRemove = { $Global:VcsPromptStatuses = $Global:VcsPromptStatuses | Where-Object { $_ -ne $PoshGitVcsPrompt } if ($promptReplaced) { - Set-Item Function:\prompt -Value ([scriptblock]::Create($defaultPromptDef)) + # Check if the posh-git prompt function itself has been replaced. If so, do not restore the prompt function + $promptDef = if ($funcInfo = Get-Command prompt -ErrorAction SilentlyContinue) { $funcInfo.Definition } + if ($promptDef -eq $poshGitPromptScriptBlock) { + Set-Item Function:\prompt -Value ([scriptblock]::Create($defaultPromptDef)) + + # If present, put PSReadline ExtraPromptLineCount back to original value + if (Get-Command Set-PSReadlineOption -ErrorAction SilentlyContinue) { + try { + Set-PSReadlineOption -ExtraPromptLineCount $origPsrlExtraPromptLineCount -ErrorAction SilentlyContinue + } + catch { + Write-Debug "Failed to get or set PSReadline ExtraPromptLineCount. Error: $_" + } + } + } } } From 0cedf562366150189fba71caffe60d27a4a5d5d9 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Wed, 4 Jan 2017 00:19:42 -0700 Subject: [PATCH 5/7] Add PromptSuffix/PromptDebugSuffix settings. Change path string comparison to use [StringComparison] appropriate for the platform. Add warning in OnRemove that let's user know that their custom prompt function may cause posh-git to be re-imported. Change to use [scriptblock]::Create() otherwise $debugMode condition lied on PS v2 (although it worked fine on v5). Anyway, this change fixes this on v2. Also removed script to mess with PSReadline''s ExtraPromptLineCount. --- GitPrompt.ps1 | 3 +++ posh-git.psm1 | 70 +++++++++++++++++++++------------------------------ 2 files changed, 32 insertions(+), 41 deletions(-) diff --git a/GitPrompt.ps1 b/GitPrompt.ps1 index 74fb0f4d1..215f4524b 100644 --- a/GitPrompt.ps1 +++ b/GitPrompt.ps1 @@ -96,6 +96,9 @@ $global:GitPromptSettings = New-Object PSObject -Property @{ EnableWindowTitle = 'posh~git ~ ' + PromptSuffix = '> ' + PromptDebugSuffix = ' [DBG]>> ' + Debug = $false BranchNameLimit = 0 diff --git a/posh-git.psm1 b/posh-git.psm1 index 45f213582..f4a5bfbe6 100644 --- a/posh-git.psm1 +++ b/posh-git.psm1 @@ -33,83 +33,71 @@ else { $defaultPromptDef = [Runspace]::DefaultRunspace.InitialSessionState.Commands['prompt'].Definition } -# If there is no prompt function or the prompt function is the default, export the posh-git prompt function. +# If there is no prompt function or the prompt function is the default, replace the current prompt function definition $promptReplaced = $false -$origPsrlExtraPromptLineCount = 0 $poshGitPromptScriptBlock = $null $currentPromptDef = if ($funcInfo = Get-Command prompt -ErrorAction SilentlyContinue) { $funcInfo.Definition } if (!$currentPromptDef -or ($currentPromptDef -eq $defaultPromptDef)) { - $poshGitPromptScriptBlock = { - $origLastExitCode = $LASTEXITCODE + # Have to use [scriptblock]::Create() to get debugger detection to work in PS v2 + $poshGitPromptScriptBlock = [scriptblock]::Create(@' + $origLastExitCode = $global:LASTEXITCODE # A UNC path has no drive so it's better to use the ProviderPath e.g. "\\server\share". # However for any path with a drive defined, it's better to use the Path property. # In this case, ProviderPath is "\LocalMachine\My"" whereas Path is "Cert:\LocalMachine\My". # The latter is more desirable. $pathInfo = $ExecutionContext.SessionState.Path.CurrentLocation - $curPath = if ($pathInfo.Drive) { $pathInfo.Path } else { $pathInfo.ProviderPath } - if ($curPath -and $curPath.ToLower().StartsWith($Home.ToLower())) + $currentPath = if ($pathInfo.Drive) { $pathInfo.Path } else { $pathInfo.ProviderPath } + + # File system paths are case-sensitive on Linux and case-insensitive on Windows and macOS + if (($PSVersionTable.PSVersion.Major -ge 6) -and $IsLinux) { + $stringComparison = [System.StringComparison]::Ordinal + } + else { + $stringComparison = [System.StringComparison]::OrdinalIgnoreCase + } + + # Abbreviate path by replacing beginning of path with ~ *iff* the path is in the user's home dir + if ($currentPath -and $currentPath.StartsWith($Home, $stringComparison)) { - $curPath = "~" + $curPath.SubString($Home.Length) + $currentPath = "~" + $currentPath.SubString($Home.Length) } - # Write the current path. - Write-Host $curPath -NoNewline + # Write the abbreviated current path + Write-Host $currentPath -NoNewline - # Write the Git status summary information. + # Write the Git status summary information Write-VcsStatus - # When in debug mode, let user know - if ((Test-Path Variable:\PSDebugContext) -or [runspace]::DefaultRunspace.Debugger.InBreakpoint) { - $promptSuffix = "`n[DBG]: PS>> " - } - else { - $promptSuffix = "`nPS> " - } + # If stopped in the debugger, the prompt needs to indicate that in some fashion + $debugMode = (Test-Path Variable:/PSDebugContext) -or [runspace]::DefaultRunspace.Debugger.InBreakpoint + $promptSuffix = if ($debugMode) { $GitPromptSettings.PromptDebugSuffix } else { $GitPromptSettings.PromptSuffix } $global:LASTEXITCODE = $origLastExitCode $promptSuffix - } +'@) - # Install the posh-git prompt as the default prompt + # Set the posh-git prompt as the default prompt Set-Item Function:\prompt -Value $poshGitPromptScriptBlock - # If default posh-git prompt is two lines then we should ensure PSReadline is set to handle that - if (Get-Command Set-PSReadlineOption -ErrorAction SilentlyContinue) { - try { - $origPsrlExtraPromptLineCount = (Get-PSReadlineOption).ExtraPromptLineCount - Set-PSReadlineOption -ExtraPromptLineCount 1 -ErrorAction SilentlyContinue - } - catch { - Write-Debug "Failed to get or set PSReadline ExtraPromptLineCount. Error: $_" - } - } - $promptReplaced = $true } # Install handler for removal/unload of the module $ExecutionContext.SessionState.Module.OnRemove = { - $Global:VcsPromptStatuses = $Global:VcsPromptStatuses | Where-Object { $_ -ne $PoshGitVcsPrompt } + $global:VcsPromptStatuses = $global:VcsPromptStatuses | Where-Object { $_ -ne $PoshGitVcsPrompt } if ($promptReplaced) { # Check if the posh-git prompt function itself has been replaced. If so, do not restore the prompt function $promptDef = if ($funcInfo = Get-Command prompt -ErrorAction SilentlyContinue) { $funcInfo.Definition } if ($promptDef -eq $poshGitPromptScriptBlock) { Set-Item Function:\prompt -Value ([scriptblock]::Create($defaultPromptDef)) - - # If present, put PSReadline ExtraPromptLineCount back to original value - if (Get-Command Set-PSReadlineOption -ErrorAction SilentlyContinue) { - try { - Set-PSReadlineOption -ExtraPromptLineCount $origPsrlExtraPromptLineCount -ErrorAction SilentlyContinue - } - catch { - Write-Debug "Failed to get or set PSReadline ExtraPromptLineCount. Error: $_" - } - } + return } } + + Write-Warning 'If your prompt function uses any posh-git commands, it may cause the module to be re-imported.' } $exportModuleMemberParams = @{ From cfcd365b7fad07c957264e7428192c9c7ac9a24e Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Wed, 4 Jan 2017 10:36:26 -0700 Subject: [PATCH 6/7] Remove unnecessary $promptReplaced variable. Update to warning message in OnRemove. --- posh-git.psm1 | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/posh-git.psm1 b/posh-git.psm1 index f4a5bfbe6..6ea4b035a 100644 --- a/posh-git.psm1 +++ b/posh-git.psm1 @@ -34,7 +34,6 @@ else { } # If there is no prompt function or the prompt function is the default, replace the current prompt function definition -$promptReplaced = $false $poshGitPromptScriptBlock = $null $currentPromptDef = if ($funcInfo = Get-Command prompt -ErrorAction SilentlyContinue) { $funcInfo.Definition } @@ -80,24 +79,20 @@ if (!$currentPromptDef -or ($currentPromptDef -eq $defaultPromptDef)) { # Set the posh-git prompt as the default prompt Set-Item Function:\prompt -Value $poshGitPromptScriptBlock - - $promptReplaced = $true } # Install handler for removal/unload of the module $ExecutionContext.SessionState.Module.OnRemove = { $global:VcsPromptStatuses = $global:VcsPromptStatuses | Where-Object { $_ -ne $PoshGitVcsPrompt } - if ($promptReplaced) { - # Check if the posh-git prompt function itself has been replaced. If so, do not restore the prompt function - $promptDef = if ($funcInfo = Get-Command prompt -ErrorAction SilentlyContinue) { $funcInfo.Definition } - if ($promptDef -eq $poshGitPromptScriptBlock) { - Set-Item Function:\prompt -Value ([scriptblock]::Create($defaultPromptDef)) - return - } + # Check if the posh-git prompt function itself has been replaced. If so, do not restore the prompt function + $promptDef = if ($funcInfo = Get-Command prompt -ErrorAction SilentlyContinue) { $funcInfo.Definition } + if ($promptDef -eq $poshGitPromptScriptBlock) { + Set-Item Function:\prompt -Value ([scriptblock]::Create($defaultPromptDef)) + return } - Write-Warning 'If your prompt function uses any posh-git commands, it may cause the module to be re-imported.' + Write-Warning 'If your prompt function uses any posh-git commands, it will cause posh-git to be re-imported every time your prompt function is invoked.' } $exportModuleMemberParams = @{ From 9b54150e5bb85a0632aeb1c67363dde60d9eff86 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Wed, 4 Jan 2017 10:53:47 -0700 Subject: [PATCH 7/7] Remove prompt from example. Add check for $prompSuffix null/empty. --- posh-git.psm1 | 5 +++++ profile.example.ps1 | 19 ------------------- 2 files changed, 5 insertions(+), 19 deletions(-) diff --git a/posh-git.psm1 b/posh-git.psm1 index 6ea4b035a..bc310f9e9 100644 --- a/posh-git.psm1 +++ b/posh-git.psm1 @@ -73,6 +73,11 @@ if (!$currentPromptDef -or ($currentPromptDef -eq $defaultPromptDef)) { $debugMode = (Test-Path Variable:/PSDebugContext) -or [runspace]::DefaultRunspace.Debugger.InBreakpoint $promptSuffix = if ($debugMode) { $GitPromptSettings.PromptDebugSuffix } else { $GitPromptSettings.PromptSuffix } + # If user specifies $null or empty string, set to ' ' to avoid "PS>" unexpectedly being displayed + if (!$promptSuffix) { + $promptSuffix = ' ' + } + $global:LASTEXITCODE = $origLastExitCode $promptSuffix '@) diff --git a/profile.example.ps1 b/profile.example.ps1 index d14f3769b..8247668ed 100644 --- a/profile.example.ps1 +++ b/profile.example.ps1 @@ -10,25 +10,6 @@ else { throw "Failed to import posh-git." } -# Set up a simple prompt that displays Git status summary info when inside of a Git repo. -function global:prompt { - $origLastExitCode = $LASTEXITCODE - - # A UNC path has no drive so it's better to use the ProviderPath e.g. "\\server\share". - # However for any path with a drive defined, it's better to use the Path property. - # In this case, ProviderPath is "\LocalMachine\My"" whereas Path is "Cert:\LocalMachine\My". - # The latter is more desirable. - $pathInfo = $ExecutionContext.SessionState.Path.CurrentLocation - $curPath = if ($pathInfo.Drive) { $pathInfo.Path } else { $pathInfo.ProviderPath } - Write-Host $curPath -NoNewline - - # Write the Git status summary information to the host. - Write-VcsStatus - - $global:LASTEXITCODE = $origLastExitCode - "> " -} - # Settings for the prompt are in GitPrompt.ps1, so add any desired settings changes here. # Example: # $Global:GitPromptSettings.BranchBehindAndAheadDisplay = "Compact"