Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to redirect 'git gc' output

I have a @daily job in the crontab that runs git gc on all the repos. I'm trying to set up a log file for the job but I/O redirection does not produce the results I am looking for; I get a blank file.

I've done all the usual >, 2>&1 and so on without any success.

Someone mentioned to me that git gc uses ncurses for its output procedures which throws output directly to the console, thus bypassing stdout/stderr (correct me if I'm wrong here).

Can some one point me in the right direction?

like image 665
s k Avatar asked Aug 01 '13 19:08

s k


2 Answers

It doesn't use ncurses (programs that use ncurses typically take over the entire screen).

Running

strace -p -o git-gc.strace git gc

shows that the progress messages are written to stderr (file descriptor 2) -- but they're disabled if stderr is not a tty. So if you run

git gc 2>some_file

then some_file will be empty, because git gc doesn't produce the progress messages at all.

Looking at the source code (builtin/gc.c), there's a quiet option that's set by the --quiet command-line option:

git gc --quiet

I haven't found the code that turns quiet on if stderr is not a tty, but I see similar code elsewhere in the git sources, such as:

quiet = !isatty(2);

There is no command-line option to turn the quiet option off.

Which means that if you want to capture the progress output of git gc, you'll need to convince it that it's running with stderr directed to a tty.

some guy's answer provides one way to do that.

But since the code goes out of its way to produce the progress messages only if it's writing to a terminal, you might consider whether you really need to capture those messages. They're not designed to be saved, and they might not be all that useful.

If you want to examine the git sources yourself (and, if you're sufficiently motivated, hack them to force the progress messages to be written):

git clone git://git.kernel.org/pub/scm/git/git.git
like image 190
Keith Thompson Avatar answered Oct 16 '22 03:10

Keith Thompson


You can try this:

script -q -c 'git gc' > log

Or this (with more readable output):

script -q -c 'git gc' | sed 's/\r.*//g' > log

like image 42
Nikolai Popov Avatar answered Oct 16 '22 04:10

Nikolai Popov