0

I have the following git extension script:

#!/usr/bin/env python

import sys
from subprocess import Popen, PIPE

if len(sys.argv) <= 1:
    sys.argv = sys.argv + ['status']

git_cmd = 'git ' + ' '.join(sys.argv[1:])

print('--->', git_cmd)

#p = Popen(['git'] + sys.argv[1:], shell=True, stdout=PIPE)
p = Popen(git_cmd, shell=True, stdout=PIPE)
out, err = p.communicate()

print(out)
#print(str(out).encode('ascii'))
#print(out.decode('ascii'))

Basically I want to get the output of my Git command in a variable but I also want it to preserve the terminal color codes in the variable. So when I call print(out), it should print the colors too. Currently the string given back to me after calling communicate() does not have color information. If I remove stdout=PIPE from Popen(), then I do get colors, but the output goes directly to the output and I do not get it in a variable.

I'm running Python 3.4 rc2 on Windows through msysgit (not cmd.exe). So for example I will do this:

git index status --short

The command it runs is git status --short, the output contains no color information, but when I run the same command directly in msysgit terminal, it gives me colors.

Anyone know how I can get the color information back?

2
  • Note that the answer I gave doesn't take into account any Windows-specific behavior which may/may not exist. Commented Feb 27, 2014 at 16:34
  • Also, using a git_cmd string (which implies shell=True) is bad form. Pass in an explicit argument array, and don't set shell=True, if you want better-defined behavior (where you don't run the risk of cmd.exe parsing your command line incorrectly). Commented Feb 27, 2014 at 16:35

1 Answer 1

1

Set color.ui to always in your git configuration.

Note that this will break quite a lot of scripts (which, in general, don't expect color codes interspersed in content they're reading from git subcommands).

To update this temporarily, you can pass -c color.ui=always on the command line, or set the GIT_CONFIG environment variable to point to a config file with this setting only when your script is in operation.


Alternately, you can just stop using stdout=PIPE, output will be direct to the TTY instead of captured by your program (which is sensible, as your program is doing absolutely nothing with it other than printing), and git will colorize by default.

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

3 Comments

My script isn't done yet but ideally I plan to mutate the output a bit before I print it. That's why I wanted colors.
...well, then, the first part of this answer should be useful to you. Of course, your code doing the mutation work will need to know how to handle terminal codes. (If you're only ever targeting Windows, you may only need to know how to handle ANSI terminal codes; if you're targeting other platforms as well, that gets considerably more complicated).
You can also add a -c color.ui=always argument between git and the actual command (or two separate strings in the array, which as already noted, sidesteps parsing issues). That might be easier than adjusting config files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.