Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git clone writes to sderr, fine but why can't I redirect to stdout

I figured that git clone uses STDERR.

I now want to redirect it to STDOUT to use this hack.

I'm having some issues (also, I use great stderred library to color STDERR red automatically).

You can see the problems in the attached image, doesn't make much sense to me... please clarify why this is happening and how to get all output to STDERR so I can use tee properly.

enter image description here

like image 983
davidhq Avatar asked Sep 21 '15 00:09

davidhq


2 Answers

Like many Unix utilities, git-clone will be quieter if it's redirected to a pipe. The assumption is the output is useful to a human and it will just get in the way of a program. tee breaks this assumption, but git can't know what's at the end of the pipe.

From the git-clone manual...

--progress

Progress status is reported on the standard error stream by default when it is attached to a terminal, unless -q is specified. This flag forces progress status even if the standard error stream is not directed to a terminal.

You have to specify git clone --progress to force it to do the full output.

like image 141
Schwern Avatar answered Nov 19 '22 09:11

Schwern


Here's a working example of redirecting stderr to stdout. My most recent use case is fixing a Github Actions bug that puts all stderr output at the top of the log, rather than putting it in sequence with stdout. When Microsoft fixes this bug I'll have a lot of unfixing to do on my end...

Proof of git clone using stderr:

$ (git clone --progress https://github.com/sindresorhus/ora.git 2>&1 | tee true) > cmd.stdout 2> cmd.stderr
$ cat cmd.stderr # Nothing here...
$ cat cmd.stdout
Cloning into 'ora'...
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 563 (delta 3), reused 7 (delta 2), pack-reused 551
Receiving objects: 100% (563/563), 652.32 KiB | 2.70 MiB/s, done.
Resolving deltas: 100% (346/346), done.

Kicking that stderr stream into stdout:

$ (git clone --progress https://github.com/sindresorhus/ora.git 2>&1 | tee true) > cmd.stdout 2> cmd.stderr
$ cat cmd.stderr # Nothing here now...
$ cat cmd.stdout
Cloning into 'ora'...
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 563 (delta 3), reused 7 (delta 2), pack-reused 551
Receiving objects: 100% (563/563), 652.32 KiB | 2.70 MiB/s, done.
Resolving deltas: 100% (346/346), done.
like image 34
Alex Atkinson Avatar answered Nov 19 '22 07:11

Alex Atkinson