Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git clone: Redirect stderr to stdout but keep errors being written to stderr

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.

like image 588
Pascal Berger Avatar asked Jan 15 '16 22:01

Pascal Berger


People also ask

Can you redirect stdout and stderr to the same file?

When saving the program's output to a file, it is quite common to redirect stderr to stdout so that you can have everything in a single file. > file redirect the stdout to file , and 2>&1 redirect the stderr to the current location of stdout .

How can I redirect stdout and stderr to separate files?

By default, both standard error messages and standard output messages of interactive tasks are written to stdout on the submission host. To separate stdout and stderr and redirect to separate files, set LSF_INTERACTIVE_STDERR=y in lsf.

Should warnings go to stderr or stdout?

If you're expecting the user to take some action as a result of the warning, it should go to STDERR. If some downstream script is likely to be tripped up by the warning, it should go to STDERR.

Can stderr be redirected?

The regular output is sent to Standard Out (STDOUT) and the error messages are sent to Standard Error (STDERR). When you redirect console output using the > symbol, you are only redirecting STDOUT. In order to redirect STDERR, you have to specify 2> for the redirection symbol.


2 Answers

A MingW update provide a new way to handle redirection with Git 2.15.x/2.16 (Q1 2018)

See commit b2f5571, commit 1a172e4, commit 3f94442 (01 Nov 2017) by Johannes Schindelin (dscho).
(Merged by Junio C Hamano -- gitster -- in commit 421f21c, 09 Nov 2017)

mingw: add experimental feature to redirect standard handles

Particularly when calling Git from applications, such as Visual Studio's Team Explorer, it is important that stdin/stdout/stderr are closed properly.
However, when spawning processes on Windows, those handles must be marked as inheritable if we want to use them, but that flag is a global flag and may very well be used by other spawned processes which then do not know to close those handles.

Let's introduce a set of environment variables (GIT_REDIRECT_STDIN and friends) that specify paths to files, or even better, named pipes (which are similar to Unix sockets) and that are used by the spawned Git process.
This helps work around above-mentioned issue: those named pipes will be opened in a non-inheritable way upon startup, and no handles are passed around (and therefore no inherited handles need to be closed by any spawned child).

This feature shipped with Git for Windows (marked as experimental) since v2.11.0(2), so it has seen some serious testing in the meantime.

The Git documentation now includes:

GIT_REDIRECT_STDIN: GIT_REDIRECT_STDOUT: GIT_REDIRECT_STDERR: 

Windows-only: allow redirecting the standard input/output/error handles to paths specified by the environment variables. This is particularly useful in multi-threaded applications where the canonical way to pass standard handles via CreateProcess() is not an option because it would require the handles to be marked inheritable (and consequently every spawned process would inherit them, possibly blocking regular Git operations).

The primary intended use case is to use named pipes for communication (e.g. \\.\pipe\my-git-stdin-123).

And it adds:

mingw: optionally redirect stderr/stdout via the same handle

The "2>&1" notation in Powershell and in Unix shells implies that stderr is redirected to the same handle into which stdout is already written.

Let's use this special value to allow the same trick with GIT_REDIRECT_STDERR and GIT_REDIRECT_STDOUT: if the former's value is 2>&1, then stderr will simply be written to the same handle as stdout.

The functionality was suggested by Jeff Hostetler.

Example of usage:$env:GIT_REDIRECT_STDERR = '2>&1'

like image 52
VonC Avatar answered Sep 20 '22 13:09

VonC


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         }     } } 
like image 39
Jim W Avatar answered Sep 17 '22 13:09

Jim W