2

I am trying to use git diff --color=never in a python script, but i realized that there are escape sequences in the output of git diff, despite the usage of the --color=never option.

This is an example of the output:

[u'\x1b[?1h\x1b=\r\x1b[1mdiff --git a/file1 b/file1\x1b[m\x1b[m\r', u'\x1b[1mnew file mode 100644\x1b[m\x1b[m\r', u'\x1b[1mindex 0000000..e69de29\x1b[m\x1b[m\r', u'\x1b[1mdiff --git a/file2 b/file2\x1b[m\x1b[m\r', u'\x1b[1mdeleted file mode 100644\x1b[m\x1b[m\r', u'\x1b[1mindex e69de29..0000000\x1b[m\x1b[m\r', u'\r\x1b[K\x1b[?1l\x1b>']

To reproduce you can use this snippet (or something similar):

from sh import git


def main():
    changes = git.diff('--color=never', 'master', 'origin/master')
    changes = changes.split('\n')
    print(changes)


if __name__ == '__main__':
    main()

I have two questions:

Is there a way to avoid that these sequences are printed (e.g. a git option)?

Why are these sequences there and what do they do, because i do not see any colors when i invoke it from the command line.

Salo
  • 1,936
  • 15
  • 24
  • 3
    This might help: http://stackoverflow.com/questions/20121664/git-show-log-without-shell-escape-sequences-for-use-with-python-sh – AlG Oct 02 '15 at 11:13
  • thanks! gits `--no-pager` option worked for me, too! – Salo Oct 02 '15 at 11:23

0 Answers0