I created my own script to load posh-git on demand:
https://github.com/luigiberrettini/Portable-GitShell/blob/master/New-GitShell.ps1
It worked fine until I cloned the latest posh-git version and I got the following warning:
Replacing old posh-git prompt. Did you copy profile.example.ps1 into $PROFILE?
I would like to avoid modifying PowerShell profiles, because I want to enable posh-git on demand via a Windows shortcut:
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "& 'C:\SmartGit\New-GitShell.ps1'"
Which configuration should I perform to avoid having that warning (I do not want to simply suppress the warning) and to be compliant with future posh-git versions (that will remove profile.example.ps1)?
At the moment I am using a modified New-GitShell.ps1 in which I set the prompt function to null:
Push-Location (Split-Path -Path $MyInvocation.MyCommand.Definition -Parent)
$scriptPath = Get-Location
$env:portable_git = (Get-ChildItem -Path $scriptPath\git* | Where-Object { $_.PSIsContainer }).FullName
$env:PLINK_PROTOCOL = "ssh"
$env:TERM = "msys"
$env:HOME = Resolve-Path $env:USERPROFILE
$env:TMP = $env:TEMP = [system.io.path]::gettemppath()
$msBuildPath = "$env:SystemRoot\Microsoft.NET\Framework\v4.0.30319"
$env:Path = "$env:Path;$env:HOME\bin;$env:portable_git\bin;$msbuildPath"
Set-Alias -Name git $env:portable_git\bin\git.exe
$poshGitFolder = "$scriptPath\poshGit"
if (!(Test-Path $poshGitFolder))
{
New-Item -Path $poshGitFolder -ItemType Directory | Out-Null
Start-Process -FilePath git -ArgumentList 'clone', 'https://github.com/dahlbyk/posh-git.git', "$poshGitFolder" -Wait -NoNewWindow
}
Set-Item Function:\prompt -Value $null
Import-Module "$poshGitFolder\src\posh-git.psm1"
Start-SshAgent -Quiet
Register-EngineEvent -SourceIdentifier ([System.Management.Automation.PsEngineEvent]::Exiting) -Action { Stop-SshAgent } | Out-Null
Pop-Location
Interesting... You should only see "Replacing old posh-git prompt. Did you copy profile.example.ps1 into $PROFILE?" if there's an existing prompt function that is essentially identical to the pre-v0.7.0 function (see #425). Before you set prompt to $null, try dumping Get-Content Function:prompt and check if it looks like this:
$realLASTEXITCODE = $LASTEXITCODE
Write-Host($pwd.ProviderPath) -nonewline
Write-VcsStatus
$global:LASTEXITCODE = $realLASTEXITCODE
return "> "
If it does, then you're getting a posh-git prompt definition from _somewhere_ and would want to get rid of that. If it doesn't, then #425 has a bug that needs to be fixed.
Also, swap these two lines:
Set-Item Function:\prompt -Value $null
Import-Module "$poshGitFolder\src\posh-git.psm1"
to
Import-Module "$poshGitFolder\src\posh-git.psm1"
Set-Item Function:\prompt -Value $null
Importing the posh-git module will not mess with any profile script. There is a separate command for that - Add-PoshGitToProfile. Don't call that command. :-) And by setting the prompt after importing Posh-Git you will override any change posh-git made to the prompt.
The existing prompt is the default PS one @dahlbyk:
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
# .Link
# http://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml
If I swap the lines as suggested by @rkeithhill the warning is written otherwise not
And the winner is:
If it doesn't, then #425 has a bug that needs to be fixed.
I'm comparing trimmed $collapsedLegacyPrompt to $collapsedLegacyPrompt. They will always be equal. Sigh.
@luigiberrettini try it now?
Thanks it works 馃憤
Most helpful comment
Thanks it works 馃憤