使用posh git将远程名称添加到PowerShell提示符
我正在使用posh git将git信息添加到我的PowerShell提示符中。我想让我的提示符包含远程存储库名称,如,但找不到这样做的方法 以下是我的个人资料和自定义提示:使用posh git将远程名称添加到PowerShell提示符,git,powershell,github,terminal,prompt,Git,Powershell,Github,Terminal,Prompt,我正在使用posh git将git信息添加到我的PowerShell提示符中。我想让我的提示符包含远程存储库名称,如,但找不到这样做的方法 以下是我的个人资料和自定义提示: function prompt { $origLastExitCode = $LASTEXITCODE $curPath = $ExecutionContext.SessionState.Path.CurrentLocation.Path if ($curPath.ToLower().StartsW
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 = "><"
这就是我想要的:
spike@Jacob-Laptop [spikespaz/batch-media-file-converter] (master == +0 ~1 -0 !)
~\Documents\Projects\GUI Utilities\batch-media-file-converter >
我用以下代码实现了它:
$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
只有当前目录是git目录时,才必须执行它。用测试路径“.git”
检查
命令git remote get url origin
返回远程url,如https://github.com/spikespaz/batch-media-file-converter
。这可以在/
字符处拆分,其中最后两个索引是(用户,repo)
我还通过使用regex(([^\/]*)\/([^\/]*)$
)提取用户和存储库名称来实现它,但我认为拆分速度更快
我看到的唯一问题是,从命令返回的URL末尾可能有.git
或其他内容。它也可以是SSH地址。我不使用这两种类型的git地址,所以如果有人发现这会中断,请告诉我
$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
这是完整的代码:请参见编辑。
函数提示符(){
}
导入模块posh git
我仍然想知道在这个例子中它是如何完成的,我相信无论它是如何完成的,它都会更干净,所以我不会接受这一点作为答案,而是将它作为一个解决方法留在这里编辑2:我现在接受这个答案,因为我本来希望有人回应,但没有人回应。这是迄今为止我找到的唯一解决办法
编辑:如果您喜欢此配置文件配置,我已经做了一个要点,无论何时更改它,我都会进行更新
$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
function Prompt() {
<...>
}
Import-Module posh-git