Only use color on systems that support it.

Fixes bug 897381. We should test if git supports the --color argument, and
only use it if it does. Specifically, the version of git on lucid does not
support color output.

Change-Id: Id87958a10b970f9ccca9ad52a0bec1f6667cc121
This commit is contained in:
Monty Taylor
2011-11-28 12:48:47 -08:00
parent 332e15448d
commit 9d1f225b5c

View File

@@ -39,6 +39,7 @@ PYPI_URL = "http://pypi.python.org/pypi/git-review/json"
PYPI_CACHE_TIME = 60 * 60 * 24 # 24 hours PYPI_CACHE_TIME = 60 * 60 * 24 # 24 hours
_branch_name = None _branch_name = None
_has_color = None
def run_command(cmd, status=False): def run_command(cmd, status=False):
@@ -243,12 +244,30 @@ def git_config_get_value(section, option):
return run_command(cmd).strip() return run_command(cmd).strip()
def check_color_support():
global _has_color
if _has_color is None:
test_command = "git log --color=never --oneline HEAD^1..HEAD"
(status, output) = run_command_status(test_command)
if status == 0:
_has_color = True
else:
_has_color = False
return _has_color
def check_remote(remote): def check_remote(remote):
"""Check that a Gerrit Git remote repo exists, if not, set one.""" """Check that a Gerrit Git remote repo exists, if not, set one."""
has_color = check_color_support()
if has_color:
color_never = "--color=never"
else:
color_never = ""
if remote in run_command("git remote").split("\n"): if remote in run_command("git remote").split("\n"):
remotes = run_command("git branch -a --color=never").split("\n") remotes = run_command("git branch -a %s" % color_never).split("\n")
for current_remote in remotes: for current_remote in remotes:
if current_remote.strip() == "remotes/%s/master" % (remote) \ if current_remote.strip() == "remotes/%s/master" % (remote) \
and not UPDATE: and not UPDATE:
@@ -316,7 +335,12 @@ def get_branch_name(target_branch):
if _branch_name is not None: if _branch_name is not None:
return _branch_name return _branch_name
_branch_name = None _branch_name = None
for branch in run_command("git branch --color=never").split("\n"): has_color = check_color_support()
if has_color:
color_never = "--color=never"
else:
color_never = ""
for branch in run_command("git branch %s" % color_never).split("\n"):
if branch.startswith('*'): if branch.startswith('*'):
_branch_name = branch.split()[1].strip() _branch_name = branch.split()[1].strip()
if _branch_name == "(no": if _branch_name == "(no":
@@ -326,14 +350,19 @@ def get_branch_name(target_branch):
def assert_one_change(remote, branch, yes): def assert_one_change(remote, branch, yes):
branch_name = get_branch_name(branch) branch_name = get_branch_name(branch)
color = git_config_get_value("color", "ui") has_color = check_color_support()
if color == "": if has_color:
color = "auto" color = git_config_get_value("color", "ui")
elif color == "auto": if color == "":
# Python is not a tty, we have to force colors color = "auto"
color = "always" elif color == "auto":
cmd = "git log --color=%s --decorate --oneline %s --not remotes/%s/%s" % \ # Python is not a tty, we have to force colors
(color, branch_name, remote, branch) color = "always"
use_color = "--color=%s" % color
else:
use_color = ""
cmd = "git log %s --decorate --oneline %s --not remotes/%s/%s" % \
(use_color, branch_name, remote, branch)
(status, output) = run_command_status(cmd) (status, output) = run_command_status(cmd)
if status != 0: if status != 0:
print "Had trouble running %s" % cmd print "Had trouble running %s" % cmd