简体   繁体   English

没有 shell 转义序列的 git show/log,用于 python sh

[英]git show/log without shell escape sequences, for use with python sh

I'm using python's sh to script git commands.我正在使用 python 的 sh 来编写 git 命令。 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.然后我将 project_hash、project_branch 和 project_date 写入数据库等。

The trouble is git sometimes adds shell escape sequences to its output.问题是 git 有时会在其输出中添加 shell 转义序列。 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, eg asking git not to output any escape sequences?有什么办法可以摆脱这些,例如要求 git 不要输出任何转义序列?

I have tried the "--no-color" option with the git log command.我已经用 git log 命令尝试了“--no-color”选项。 That did not help.那没有帮助。

I would also be happy to strip them out in python itself, but I don't know how.我也很乐意在 python 中将它们剥离出来,但我不知道如何。 I tried s.encode('ascii') where s is the date string.我试过 s.encode('ascii') 其中 s 是日期字符串。 That did not make a difference.这并没有什么不同。

Print stdout in Python without shell escape sequences addresses the same issue. 在没有 shell 转义序列的 Python 中打印标准输出解决了同样的问题。 The recommendation there is to use python's subprocess rather than sh.那里的建议是使用 python 的子进程而不是 sh。 Eg, 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.但是,如果可能的话,我更愿意坚持使用 sh,因此想知道我是否可以使用 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.这表明git log正在通过您的寻呼机运行内容。 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).通常,当输出是管道时,git 会禁止使用寻呼机subprocess.Popen()至少与subprocess.Popen() ,我会考虑使用sh ,尽管我没有使用sh模块)。

(Pause to consult documentation...) (暂停以查阅文档...)

Aha!啊哈! Per sh module docs , by default, the output of an sh -module-run command goes through a pseudo-tty.根据sh module docs ,默认情况下, sh -module-run 命令的输出通过伪 tty。 This is fooling git into running your pager.这是在欺骗 git 运行你的寻呼机。

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 .作为一个稍微肮脏的解决方法,您可以运行git --no-pager log ...来禁止使用寻呼机,即使在使用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).或者,您可以尝试使用_tty_out=False参数(同样,我还没有使用sh模块,您将不得不尝试一下)。 Amusingly, one of the examples at the bottom of the sh module documentation is git!有趣的是,sh 模块文档底部的示例之一是 git!

It seems like sh does the right thing.似乎 sh 做了正确的事情。 In python 2.7, this:在python 2.7中,这个:

import sh
git = sh.git.bake(_cwd='/tmp/gittest/')

project_hash = git('rev-parse', 'HEAD')
project_branch = git('rev-parse', '--abbrev-ref', 'HEAD')
project_date = git('log', '-1', '--pretty=format:%ci')

print(repr(project_hash).strip())
print(repr(project_branch).strip())
print(repr(project_date).strip())

gives me:给我:

500ddad67203badced9a67170b42228ffa269f53
master
2013-11-22 00:05:59 +1100

If you really want to strip out escapes, use the decoder tools provided by python ( Process escape sequences in a string in Python )如果你真的想去掉转义,使用 python 提供的解码器工具( 在 Python 中处理字符串中的转义序列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM