Trying to redirect 'git gc' output

前端 未结 2 1364
心在旅途
心在旅途 2021-01-18 04:14

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 pro

相关标签:
2条回答
  • 2021-01-18 04:47

    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
    
    0 讨论(0)
  • 2021-01-18 04:59

    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

    0 讨论(0)
提交回复
热议问题