Skip to content

Commit

Permalink
Only emit color messages when stdout is a tty
Browse files Browse the repository at this point in the history
Fixes #213.
  • Loading branch information
jengelh committed Nov 12, 2018
1 parent 7cb40ee commit 8a6c26a
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions TarSCM/scm/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

from TarSCM.scm.base import Scm

def color_red(s):
if not os.isatty(1):
return s
return "\033[0;31m" + s + "\033[0m"

class Git(Scm):
scm = 'git'
Expand Down Expand Up @@ -200,9 +204,8 @@ def _detect_parent_tag(self, args):

def _detect_version_parent_tag(self, parent_tag, versionformat): # noqa pylint: disable=no-self-use
if not parent_tag:
sys.exit("\033[31mNo parent tag present for the checked out "
"revision, thus @PARENT_TAG@ cannot be expanded."
"\033[0m")
sys.exit(color_red("No parent tag present for the checked out "
"revision, thus @PARENT_TAG@ cannot be expanded."))

versionformat = re.sub('@PARENT_TAG@', parent_tag,
versionformat)
Expand All @@ -218,17 +221,15 @@ def _detect_version_tag_offset(self, parent_tag, versionformat):
self.clone_dir
)
if rcode != 0:
sys.exit("\033[31mparent_tag is not an ancestor of HEAD. " +
"Cannot compute a (meaningful) distance.\033[0m")
sys.exit(color_red("parent_tag is not an ancestor of HEAD. " +
"Cannot compute a (meaningful) distance."))

cmd = self._get_scm_cmd()
cmd.extend(['rev-list', '--count', parent_tag + '..HEAD'])
rcode, out = self.helpers.run_cmd(cmd, self.clone_dir)

if rcode:
msg = "\033[31m@TAG_OFFSET@ can not be expanded: %s\033[0m"
msg.format(out)
sys.exit(msg)
sys.exit(color_red("@TAG_OFFSET@ can not be expanded: " + output))

tag_offset = out.strip()
versionformat = re.sub('@TAG_OFFSET@', tag_offset,
Expand Down

0 comments on commit 8a6c26a

Please sign in to comment.