簡體   English   中英

禁止從Powershell $ error輸出外部命令錯誤

[英]Suppress external command error output from Powershell $error

在我的個人資料中,我通過prompt功能自定義了提示,以嵌入git分支信息:

function prompt
{
    $prefix = ""
    if ((test-path ".git") -or (test-path ".gitdir") -or ((git rev-parse --is-inside-work-tree 2> $null) -eq "true")) {
        $prefix = "[git:" + (& git symbolic-ref --short HEAD) + "]"
    }

    write-host "PS $prefix $(get-location) >" -nonewline -foregroundcolor DarkMagenta
    return " "
}

但是問題是,當我不在git樹中時,即使我將錯誤重定向到$ null,檢查的git rev-parse部分也會在$error插入錯誤。

這意味着$ error會因以下錯誤而受到污染,因為每次提示呈現時都會生成該錯誤:

git : fatal: Not a git repository (or any of the parent directories): .git
At C:\temp\prompt.ps1:4 char:64
    + ... ".gitdir") -or ((git rev-parse --is-inside-work-tree 2> $null) -eq "t ...
    +                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (fatal: Not a gi...ectories): .git:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

交互式運行時,我注意到2> $null確實抑制了控制台的錯誤,但是該錯誤仍顯示在$ error中:

PS C:\temp> $error
PS C:\temp> git rev-parse --is-inside-work-tree 2> $null
PS C:\temp> $error
git : fatal: Not a git repository (or any of the parent directories): .git
At line:1 char:1
+ git rev-parse --is-inside-work-tree 2> $null
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (fatal: Not a gi...ectories): .git:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

PS C:\temp>

我試過將命令包裝在try {...} catch {} ,還使用了帶有ignore的erroraction的invoke-command ,但都沒有運氣:

PS c:\temp> $error.clear()
PS c:\temp> $error
PS c:\temp> invoke-command -scriptblock { git rev-parse --is-inside-work-tree 2> $null } -erroraction ignore
PS c:\temp> $error
git : fatal: Not a git repository (or any of the parent directories): .git
At line:1 char:31
+ ... d -scriptblock { git rev-parse --is-inside-work-tree 2> $null } -erro ...
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (fatal: Not a gi...ectories): .git:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

PS c:\temp> $error.clear()
PS c:\temp> $error
PS c:\temp> try { git rev-parse --is-inside-work-tree 2> $null } catch { }
PS c:\temp> $error
git : fatal: Not a git repository (or any of the parent directories): .git
At line:1 char:7
+ try { git rev-parse --is-inside-work-tree 2> $null } catch { }
+       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (fatal: Not a gi...ectories): .git:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

PS c:\temp>

如何抑制此錯誤添加到$error ,使其保持干凈?

不幸的是,從PowerShell Core 6.2.1 / Windows PowerShell v5.1開始,使用2>$null抑制來自外部程序的stderr輸出意外地仍然通過Powershell的錯誤流(流2 )繞行,因此輸出仍記錄在$Error GitHub問題中描述了此已知問題。

作為一種解決方法,您可以通過cmd /c (或在Unix上為sh -c )調用git ,並使其進行重定向:

# Windows
cmd /c 'git rev-parse --is-inside-work-tree 2>NUL'

# Linux, macOS
sh -c 'git rev-parse --is-inside-work-tree 2>/dev/null'

如您所述,這將正確傳遞git的退出代碼,因此您之后可以通過$LASTEXITCODE確定成功。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM