简体   繁体   English

使用posh-git将远程名称添加到PowerShell提示符

[英]Add remote name to PowerShell prompt with posh-git

I'm using posh-git to add git information to my PowerShell prompt. 我正在使用posh-git将git信息添加到我的PowerShell提示符中。 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 >

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. 只有在当前目录是git目录时才能执行它。 Check that with Test-Path ".git" . 使用Test-Path ".git"检查它。

The command git remote get-url origin returns the remote URL such as https://github.com/spikespaz/batch-media-file-converter . 命令git remote get-url origin返回远程URL,例如https://github.com/spikespaz/batch-media-file-converter This can be split at / characters, where the last two indices are (user, repo) . 这可以在/字符处拆分,其中最后两个索引是(user, repo)

I also got it working by extracting the user and repository name with regex ( ([^\\/]*)\\/([^\\/]*)$ ), but I presume splitting is faster. 我也通过使用regex( ([^\\/]*)\\/([^\\/]*)$ )提取用户和存储库名称来实现它,但我认为拆分更快。

The only problem I see with this is that the URL returned from the command may have .git or something at the end. 我看到的唯一问题是从命令返回的URL最后可能有.git或其他东西。 It could also be an SSH address. 它也可以是SSH地址。 I don't use either of those types of git addresses, so if anyone finds that this breaks let me know. 我不使用这些类型的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

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. 编辑2:我现在接受这个作为答案,因为我曾期望别人回应,但没有人做过。 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 https://git.io/vAqYP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM