powershell - Git clone: Redirect stderr to stdout but keep errors being written to stderr -
git clone
writes output stderr
documented here. can redirect following command:
git clone https://myrepo c:\repo 2>&1
but redirect output, including errors, stderr
to stdout
. there way redirect progress messages stdout
have error messages still written stderr
.
i use script run git commands. since git write stderr if successful (e.g. pull when in sync), handles cases , writes out first line of output, need know.
<# .synopsis invoke git, handling quirky stderr isn't error .outputs git messages, , lastly 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)" } { if ( test-path $path ) { remove-item $path } } }
Comments
Post a Comment