【问题标题】:How can I check for repo status using posh-git in a PowerShell function?如何在 PowerShell 函数中使用 posh-git 检查 repo 状态?
【发布时间】:2015-03-12 20:38:11
【问题描述】:

我们有一个 psake 编排,在调用“git clean -xdf”之前提示用户以下消息:

即将删除所有未跟踪的文件。按“Y”继续或任何 其他键取消。

如果存储库中存在将通过运行 clean -xdf 删除的未跟踪文件,我们希望显示此提示。

关于如何使用 posh-git 来回答“存储库中是否有任何未跟踪的更改?”的任何建议。来自 PowerShell?

这是现有的编排,供参考……

task CleanAll -description "Runs a git clean -xdf" {
    Write-Host "About to delete any uncommitted changes.  Press 'Y' to continue or any other key to cancel." -foregroundcolor "yellow"
    $continue = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp").Character
    IF ($continue -eq "Y" -or $continue -eq "y")
    {
        git clean -xdf
    }
    ELSE
    {
        Write-Error "CleanAll canceled."
    }
}

【问题讨论】:

  • git clean -xdf 不会恢复对跟踪文件的更改,它会删除 未跟踪 和忽略的文件。
  • @EtanReisner - 很公平。但问题实际上是关于有条件地显示或跳过提示,这并不能回答这个问题。我会将问题更新为更“正确”。无论如何,感谢您的回复。
  • 问题可能与误解有关。如果您认为git clean 就是这样做的,那么您可能会无缘无故地尝试这样做,因为事实并非如此。更重要的是,如何最好地回答这个问题取决于(某种程度上)什么变化/等等。你实际上需要测试。
  • @EtanReisner,感谢您的批评。对如何进行有任何建议?
  • 使用git status --porcelaingit status -z 的输出并在文件上查找?? 标记。这些是未跟踪的文件。不过,我不能为我的头顶上的 powershell 写这个。或者,只需检查 git clean -xdn 是否输出任何内容。

标签: powershell psake posh-git


【解决方案1】:

在@EtanReisner 的帮助下,我能够得到以下解决方案。它检查未跟踪的更改并提示是否有任何更改。否则它只会执行 git clean -xdf。

$gitStatus = (@(git status --porcelain) | Out-String)

IF ($gitStatus.Contains("??"))
{
    Write-Host "About to delete any untracked files.  Press 'Y' to continue or any other key to cancel." -foregroundcolor "yellow"
    $continue = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp").Character
    IF ($continue -eq "Y" -or $continue -eq "y")
    {
        git clean -xdf
    }
    ELSE
    {
        Write-Error "CleanAll canceled."
    }
}

git clean -xdf

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多