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

时间:2018-02-10 22:10:55

标签: git powershell github terminal prompt

我正在使用posh-git将git信息添加到我的PowerShell提示符中。我希望我的提示包括远程存储库名称,如the examples,但无法找到方法。

以下是我的自定义提示符:

spike@Jacob-Laptop [spikespaz/batch-media-file-converter] (master == +0 ~1 -0 !)
~\Documents\Projects\GUI Utilities\batch-media-file-converter >

结果如下:

df <- data.frame(A1U_sweet = c(1, NA, 2),
                 A2F_dip = c(2, NA, 4),
                 A3U_bbq = c(1, NA, 7),
                 C1U_sweet = c(NA, 4, NA),
                 C2F_dip = c(NA, 1, NA),
                 C3U_bbq = c(NA, 2, NA))

这就是我想要的:

lapply()

1 个答案:

答案 0 :(得分:1)

我使用以下代码:

$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目录时才能执行它。检查Test-Path ".git"

命令git remote get-url origin返回远程URL,例如https://github.com/spikespaz/batch-media-file-converter。这可以在/个字符处拆分,其中最后两个索引为(user, repo)

我还通过使用正则表达式(([^\/]*)\/([^\/]*)$)提取用户和存储库名称来实现它,但我认为拆分更快。

我看到的唯一问题是,从命令返回的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

这是完整的代码: 请参阅编辑。

function Prompt() {
    <...>
}

Import-Module posh-git

我仍然想知道它是如何在示例中完成的,我确信它会更干净但是已经完成了,所以我不会接受这个作为答案而是留在这里作为答案解决方法。 编辑2:我现在接受这个作为答案,因为我曾期望别人做出回应,但没人做。这是迄今为止我发现的唯一解决方案。

编辑:如果你喜欢这个配置文件配置,我已经做了一个要点,每当我更改它时我都会更新。 https://git.io/vAqYP

相关问题