Reputation: 2125
I'm using posh-git to add git information to my PowerShell prompt. I want to have my prompt include the remote repository name like the examples but can't find a way to do so.
Below is my profile with my custom prompt:
function prompt {
$origLastExitCode = $LASTEXITCODE
$curPath = $ExecutionContext.SessionState.Path.CurrentLocation.Path
if ($curPath.ToLower().StartsWith($Home.ToLower())) {
$curPath = "~" + $curPath.SubString($Home.Length)
}
Write-Host "$env:UserName" -NoNewline -ForegroundColor Cyan
Write-Host "@" -NoNewline -ForegroundColor Yellow
Write-Host "$(hostname)" -NoNewline
Write-VcsStatus
Write-Host "`n$curPath" -NoNewline
$LASTEXITCODE = $origLastExitCode
" $('>' * ($nestedPromptLevel + 1)) "
}
Import-Module posh-git
$global:GitPromptSettings.BeforeText = " ("
$global:GitPromptSettings.AfterText = ")"
$global:GitPromptSettings.EnableWindowTitle = "posh-git ~"
$global:GitPromptSettings.EnableStashStatus = $true
$global:GitPromptSettings.BeforeStashText = " {"
$global:GitPromptSettings.AfterStashText = "}"
$global:GitPromptSettings.BeforeStashForegroundColor = "Yellow"
$global:GitPromptSettings.AfterStashForegroundColor = "Yellow"
$global:GitPromptSettings.BranchUntrackedSymbol = "::"
$global:GitPromptSettings.BranchGoneStatusSymbol = "~~"
$global:GitPromptSettings.BranchIdenticalStatusToSymbol = "=="
$global:GitPromptSettings.BranchAheadStatusSymbol = "<<"
$global:GitPromptSettings.BranchBehindStatusSymbol = ">>"
$global:GitPromptSettings.BranchBehindAndAheadStatusSymbol = "><"
This is the result:
spike@Jacob-Laptop (master == +0 ~1 -0 !)
~\Documents\Projects\GUI Utilities\batch-media-file-converter >
This is what I want:
spike@Jacob-Laptop [spikespaz/batch-media-file-converter] (master == +0 ~1 -0 !)
~\Documents\Projects\GUI Utilities\batch-media-file-converter >
Upvotes: 3
Views: 312
Reputation: 2125
I got it working with the following code:
$remoteName = (git remote get-url origin).Split("/")
Write-Host $remoteName[-2] -NoNewline -ForegroundColor Cyan
Write-Host "/" -NoNewline -ForegroundColor Yellow
Write-Host $remoteName[-1] -NoNewline
Write-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), "([^\/]*)\/([^\/]*)$").Groups
Write-Host $remoteName[1] -NoNewline -ForegroundColor Cyan
Write-Host "/" -NoNewline -ForegroundColor Yellow
Write-Host $remoteName[2] -NoNewline
Write-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
Upvotes: 1