How to format the output of git show in python

Viewed 168

I want to manupulate the output of the git show in python.

I use the follow to get the output of the git show.

sh.git.show('b24f6f825324fb939210077c02a8944b59399510')
out.stdout

But the out is in one line.

b'\x1b[33mcommit b24f6f825324fb939210077c02a8944b59399510\x1b[m\nAuthor: Nuno Lopes <nlopess@php.net>\nDate:   Sun Jul 6 15:23:44 2008 +0000\n\n    MFB: fix CVE-2008-2371\n\n\x1b[1mdiff --git a/ext/pcre/pcrelib/pcre_compile.c b/ext/pcre/pcrelib/pcre_compile.c\x1b[m\n\x1b[1mindex 51a51e1253..0f3ebf93fd 100644\x1b[m\n\x1b[1m--- a/ext/pcre/pcrelib/pcre_compile.c\x1b[m\n\x1b[1m+++ b/ext/pcre/pcrelib/pcre_compile.c\x1b[m\n\x1b[36m@@ -4929,7 +4929,7 @@\x1b[m \x1b[mwe set the flag only if there is a literal "\\r" or "\\n" in the class. */\x1b[m\n                (lengthptr == NULL || *lengthptr == 2 + 2*LINK_SIZE))\x1b[m\n             {\x1b[m\n             cd->external_options = newoptions;\x1b[m\n\x1b[31m-            options = newoptions;\x1b[m\n\x1b[32m+\x1b[m\x1b[32m            options = *optionsptr = newoptions;\x1b[m\n             }\x1b[m\n          else\x1b[m\n             {\x1b[m\n'

So is there any way to get the format output just like command output in shell. So that I can get the any line I would like.

commit b24f6f825324fb939210077c02a8944b59399510
Author: Nuno Lopes <nlopess@php.net>
Date:   Sun Jul 6 15:23:44 2008 +0000

    MFB: fix CVE-2008-2371

diff --git a/ext/pcre/pcrelib/pcre_compile.c b/ext/pcre/pcrelib/pcre_compile.c
index 51a51e1253..0f3ebf93fd 100644
--- a/ext/pcre/pcrelib/pcre_compile.c
+++ b/ext/pcre/pcrelib/pcre_compile.c
@@ -4929,7 +4929,7 @@ we set the flag only if there is a literal "\r" or "\n" in the class. */
                (lengthptr == NULL || *lengthptr == 2 + 2*LINK_SIZE))
             {
             cd->external_options = newoptions;
-            options = newoptions;
+            options = *optionsptr = newoptions;
             }
          else
             {
2 Answers

The garbage text you see in your output:

\x1b[33m

seems to be console color escape codes. In other words, what git uses to color your text.

If you don't want those console codes in your output, then you can just tell git to not print the command results in color. According to the documentation, the --no-color or --color=never flags will accomplish this, so the corresponding sh.git command should be:

sh.git.show('b24f6f825324fb939210077c02a8944b59399510', color='never')

Note that the output will still be a bytestring, of course, and \n and other 'normal' control characters will still be present in that output (you don't really want the newlines gone, do you?). You can use bytes.decode() to turn the bytestring into a regular string, and the control characters should be properly processed if you use print() to output the string.

(I'd expect the colors to even work properly, if you just print()ed the result of what you were doing, but if you intend to do something else with the output then you might not want the color escape codes muddying up the output)

In addition to @GreenCloakGuy's answer, you can customize the format of what is returned from the git show command with the --format=<format> flag. See here.

Related