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?

Yogu
  • 9,165
  • 5
  • 37
  • 58

2 Answers2

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.

Yogu
  • 9,165
  • 5
  • 37
  • 58
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.

user3159253
  • 16,836
  • 3
  • 30
  • 56
  • That looks very promising, I'll give it a shot. – Yogu Apr 16 '14 at 11:20
  • It seems like the best solution, but unfortunately I can't get it work right now in node.js (there's [pty.js](https://github.com/chjj/pty.js), but that is not yet compatible with node v0.11). Thank you anyway! – Yogu Apr 16 '14 at 12:05
  • Well, I see :-). Nevertheless I guess you can issue a simple native call using e.g. [node-ffi](https://github.com/rbranson/node-ffi). Or extend a bit [node-posix](https://github.com/melor/node-posix). Afterall, you need a simple call which would return you a couple of `int`s – user3159253 Apr 16 '14 at 12:37