git show / log没有shell转义序列,用于python sh [英] git show/log without shell escape sequences, for use with python sh

查看:182
本文介绍了git show / log没有shell转义序列,用于python sh的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用python的sh来编写脚本git命令。例如,我做的事情就像

  import sh 
git = sh.git.bake(_cwd ='/ some / dir /')

project_hash = git('rev-parse','HEAD')。stdout.strip()
project_branch = git('rev-parse',' - stdout.strip()
project_date = git('log','-1','--pretty = format:%ci')。stdout.strip()

然后我将project_hash,project_branch和project_date写入数据库等。



问题在于git有时会将shell转义序列添加到其输出中。例如,

  print(repr(project_hash))
print(repr(project_branch))
print (repr(project_date))

导致

 'e55595222076bd90b29e184b6ff6ad66ec8c3a03'
'master'
'\x1b [?1h\x1b = \r2012-03-26 01:07:40 -0500 \x1b [m\r\\\
\r\x1b [K\x1b [?1l\x1b>'

前两个字符串不是问题,但最后一个日期具有转义序列。



有什么方法可以解决这些问题吗?要求git不要输出任何转义序列?



我用git log命令尝试了--no-color选项。这没有帮助。

我也很乐意将它们在python中删除,但我不知道如何。我试过s.encode('ascii'),其中s是日期字符串。这没有什么区别。



在不使用shell转义序列的情况下在Python中打印stdout 可解决相同的问题。建议使用python的子进程而不是sh。例如,我可以做
$ b $ pre code project_date = subprocess.check_output([git,log,-1, --pretty = format:%ci],cwd ='/ some / dir /')

  print(repr(project_date))

给出

 '2012-03-26 01:07:40 -0500' 

当然,这正是我想要的。但是,如果有可能,我宁愿坚持sh,所以想知道我是否可以避免使用sh的转义序列。



有什么建议吗? 这些不是颜色序列,那些看起来像终端初始化序列。具体来说:

  ESC [? 1 h ESC = 

是打开功能键模式和

的序列

  ESC [? 1升ESC> 

是再次关闭它的顺序。这表明 git log 正在通过你的寻呼机运行。我不太清楚为什么;通常git会在输出为管道时抑制对分页器的使用(至少与 subprocess.Popen()>一样,我会认为使用尽管我没有使用 sh 模块)。



(Pause to 咨询文件...)



啊!根据默认,每个 sh模块文档的输出为 sh -module-run命令通过伪tty。这是欺骗git运行你的寻呼机。



作为一个稍微脏的解决方法,你可以运行 git --no-pager log .. 。即使在使用 sh 运行时也禁止使用该寻呼机。或者,您可以尝试 _tty_out = False 参数(同样,我没有使用 sh 模块,您将拥有试验一下)。有趣的是,sh模块文档底部的示例之一是git!


I'm using python's sh to script git commands. For example, I do things like

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()

and then I write the project_hash, project_branch and project_date into a database, etc.

The trouble is git sometimes adds shell escape sequences to its output. For example,

print(repr(project_hash))
print(repr(project_branch))
print(repr(project_date))

leads to

'e55595222076bd90b29e184b6ff6ad66ec8c3a03'
'master'
'\x1b[?1h\x1b=\r2012-03-26 01:07:40 -0500\x1b[m\r\n\r\x1b[K\x1b[?1l\x1b>'

The first two strings are not a problem, but the last one, the date, has escape sequences.

Is there any way I can get rid of these, e.g. asking git not to output any escape sequences?

I have tried the "--no-color" option with the git log command. That did not help.

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.

Print stdout in Python without shell escape sequences addresses the same issue. The recommendation there is to use python's subprocess rather than sh. E.g., I could do

project_date = subprocess.check_output(["git", "log", "-1", "--pretty=format:%ci"], cwd='/some/dir/')

and

print(repr(project_date))

gives

'2012-03-26 01:07:40 -0500'

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.

Any suggestions?

解决方案

Those are not color sequences, those look like terminal initialization sequences. Specifically:

ESC [ ? 1 h ESC =

is the sequence to turn on function-key-mode and

ESC [ ? 1 l ESC >

is the sequence to turn it off again. This suggests that git log is running things through your pager. I'm not quite sure why; normally git suppresses use of the pager when the output is a pipe (as it is with subprocess.Popen() at least, and I would think with sh, although I have not used the sh module).

(Pause to consult documentation...)

Aha! Per sh module docs, by default, the output of an sh-module-run command goes through a pseudo-tty. This is fooling git into running your pager.

As a slightly dirty work-around, you can run git --no-pager log ... to suppress the use of the pager, even when running with sh. Or, you can try the _tty_out=False argument (again, I have not used the sh module, you will have to experiment a bit). Amusingly, one of the examples at the bottom of the sh module documentation is git!

这篇关于git show / log没有shell转义序列,用于python sh的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆