2

I'm writing a tool that runs git commands and captures the output, and I'd like to have the output colored. Git notices that the tool isn't a terminal, so the color.ui would need to be set to always. I don't really want to set this in the global/repo configuration file, as it would mess with other programs using git. The git book explicitly discourages that, too:

You’ll rarely want color.ui = always. In most scenarios, if you want color codes in your redirected output, you can instead pass a --color flag to the Git command to force it to use color codes. The color.ui = true setting is almost always what you’ll want to use.

Sadly, not all git commands support the --color flag, most notably git status and git pull.

So, how do I force colors for one git command? Is there a configuration variable I can set, e.g. for a custom git config file?

2 Answers 2

4

You can use the -c option to override config values. This works for all git commands. For example:

git -c color.ui=always status > status_file 

outputs the color codes into the file. It does not see a pty, so it will not try to paginate.

Sign up to request clarification or add additional context in comments.

Comments

1

I think you should use a master/slave PTY to communicate with git subcommand instead of a pipe. This makes git think that it runs in an interactive session, not in a pipeline.

3 Comments

That looks very promising, I'll give it a shot.
It seems like the best solution, but unfortunately I can't get it work right now in node.js (there's pty.js, but that is not yet compatible with node v0.11). Thank you anyway!
Well, I see :-). Nevertheless I guess you can issue a simple native call using e.g. node-ffi. Or extend a bit node-posix. Afterall, you need a simple call which would return you a couple of ints

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.