Note that there are some explanatory texts on larger screens.

plurals
  1. POgit show/log without shell escape sequences, for use with python sh
    primarykey
    data
    text
    <p>I'm using python's sh to script git commands. For example, I do things like </p> <pre><code>import sh git = sh.git.bake(_cwd='/some/dir/') project_hash = git('rev-parse', 'HEAD').stdout.strip() project_branch = git('rev-parse', '--abbrev-ref', 'HEAD').stdout.strip() project_date = git('log', '-1', '--pretty=format:%ci').stdout.strip() </code></pre> <p>and then I write the project_hash, project_branch and project_date into a database, etc. </p> <p>The trouble is git sometimes adds shell escape sequences to its output. For example,</p> <pre><code>print(repr(project_hash)) print(repr(project_branch)) print(repr(project_date)) </code></pre> <p>leads to</p> <pre><code>'e55595222076bd90b29e184b6ff6ad66ec8c3a03' 'master' '\x1b[?1h\x1b=\r2012-03-26 01:07:40 -0500\x1b[m\r\n\r\x1b[K\x1b[?1l\x1b&gt;' </code></pre> <p>The first two strings are not a problem, but the last one, the date, has escape sequences. </p> <p>Is there any way I can get rid of these, e.g. asking git not to output any escape sequences? </p> <p>I have tried the "--no-color" option with the git log command. That did not help. </p> <p>I would also be happy to strip them out in python itself, but I don't know how. I tried s.encode('ascii') where s is the date string. That did not make a difference.</p> <p><a href="https://stackoverflow.com/questions/13611061/print-stdout-in-python-without-shell-escape-sequences">Print stdout in Python without shell escape sequences</a> addresses the same issue. The recommendation there is to use python's subprocess rather than sh. E.g., I could do</p> <pre><code>project_date = subprocess.check_output(["git", "log", "-1", "--pretty=format:%ci"], cwd='/some/dir/') </code></pre> <p>and </p> <pre><code>print(repr(project_date)) </code></pre> <p>gives</p> <pre><code>'2012-03-26 01:07:40 -0500' </code></pre> <p>That is what I want, of course. However, if it is possible I would prefer to stick with sh, and so would like to know if I can avoid the escape sequences using sh. </p> <p>Any suggestions?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload