Add remote name to PowerShell prompt with posh-git
I got it working with the following code:
$remoteName = (git remote get-url origin).Split("/")Write-Host $remoteName[-2] -NoNewline -ForegroundColor CyanWrite-Host "/" -NoNewline -ForegroundColor YellowWrite-Host $remoteName[-1] -NoNewlineWrite-Host "]" -NoNewline -ForegroundColor Yellow
It has to only be executed if the current directory is a git directory. Check that with Test-Path ".git"
.
The command git remote get-url origin
returns the remote URL such as https://github.com/spikespaz/batch-media-file-converter
. This can be split at /
characters, where the last two indices are (user, repo)
.
I also got it working by extracting the user and repository name with regex (([^\/]*)\/([^\/]*)$
), but I presume splitting is faster.
The only problem I see with this is that the URL returned from the command may have .git
or something at the end. It could also be an SSH address. I don't use either of those types of git addresses, so if anyone finds that this breaks let me know.
$remoteName = [regex]::match((git remote get-url origin), "([^\/]*)\/([^\/]*)$").GroupsWrite-Host $remoteName[1] -NoNewline -ForegroundColor CyanWrite-Host "/" -NoNewline -ForegroundColor YellowWrite-Host $remoteName[2] -NoNewlineWrite-Host "]" -NoNewline -ForegroundColor Yellow
This is the full code: See edit.
function Prompt() { <...>}Import-Module posh-git
I would still like to know how it was done in the example, and I'm sure it would be cleaner however that was done, so I won't accept this as an answer but rather leave it here as a workaround. Edit 2: I am accepting this as an answer for now because I had expected someone else to respond, but nobody did. It is the only solution I've found this-far.
Edit: If you like this profile configuration, I've made a gist that I will update whenever I change it. https://git.io/vAqYP