问题描述
git clone 将其输出写入 stderr ,如。我可以用以下命令重定向它:git clone https:// myrepo c:\repo 2>& 1
但是,这会将所有输出,包括错误,从 stderr 到 stdout 。有没有办法将进度消息重定向到 stdout ,但仍有写入 stderr 的错误消息。
我使用这个脚本来运行git命令。由于git会写入stderr,即使成功(例如在同步时拉出),它会处理这些情况并写出第一行输出,这通常是您需要知道的。
<#
.Synopsis
调用git,处理其并非错误的古怪stderr
.Outputs
Git消息,最后退出代码
。示例
Invoke-Git推送
。示例
Invoke-Gitadd。
#>
函数Invoke-Git
{
param(
[Parameter(Mandatory)]
[string] $ Command)
try {
$ exit = 0
$ path = [System.IO.Path] :: GetTempFileName()
Invoke-Expressiongit $ Command 2> $ path
$ exit = $ LASTEXITCODE
if($ exit -gt 0)
{
Write-Error(Get-Content $ path).ToString()
}
else
{
Get-Content $ path | Select-Object -First 1
}
$ exit
}
catch
{
Write-Host错误:$ _`n $($ _ .ScriptStackTrace)
}
finally
{
if(Test-Path $ path)
{
Remove-Item $ path
}
}
}
git clone writes its output to stderr as documented here. I can redirect this with the following command:
git clone https://myrepo c:\repo 2>&1
But this will redirect all output, including errors, from stderrto stdout. Is there a way to redirect progress messages to stdout but have error messages still written to stderr.
I use this script to run git commands. Since git will write to stderr even if successful (e.g. pull when in sync), this handles those cases and writes out first line of output, which is usually what you need to know.
<# .Synopsis Invoke git, handling its quirky stderr that isn't error .Outputs Git messages, and lastly the exit code .Example Invoke-Git push .Example Invoke-Git "add ." #> function Invoke-Git { param( [Parameter(Mandatory)] [string] $Command ) try { $exit = 0 $path = [System.IO.Path]::GetTempFileName() Invoke-Expression "git $Command 2> $path" $exit = $LASTEXITCODE if ( $exit -gt 0 ) { Write-Error (Get-Content $path).ToString() } else { Get-Content $path | Select-Object -First 1 } $exit } catch { Write-Host "Error: $_`n$($_.ScriptStackTrace)" } finally { if ( Test-Path $path ) { Remove-Item $path } } }
这篇关于Git clone:将stderr重定向到标准输出,但将错误写入标准错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!