Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prompt configuration without editing PowerShell profiles #439

Closed
luigiberrettini opened this issue Feb 23, 2017 · 6 comments
Closed

Prompt configuration without editing PowerShell profiles #439

luigiberrettini opened this issue Feb 23, 2017 · 6 comments

Comments

@luigiberrettini
Copy link

luigiberrettini commented Feb 23, 2017

System Details

  • posh-git version/path: 0.0 C:\SmartGit\poshGit\src
  • PowerShell version: 4.0
  • git version 2.10.0.windows.1
  • OS: Microsoft Windows NT 6.3.9600.0

Issue Description

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
@dahlbyk
Copy link
Owner

dahlbyk commented Feb 23, 2017

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.

@rkeithhill
Copy link
Collaborator

rkeithhill commented Feb 23, 2017

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.

@luigiberrettini
Copy link
Author

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

@dahlbyk
Copy link
Owner

dahlbyk commented Feb 27, 2017

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.

@dahlbyk
Copy link
Owner

dahlbyk commented Feb 27, 2017

@luigiberrettini try it now?

@luigiberrettini
Copy link
Author

Thanks it works 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants