我遇到一个问题,即使用带有git的sh模块获取的字符串和写入文件的字符串不匹配
我得到提交字符串如下
from sh import stat, git
repo = git.bake(_cwd=git_dir)
current_commit=git.log("-n1", "--format='%T'", git_file)
current_commit显示如下
print current_commit
'89a848eb9ea98bfd6770301dce848052ec8ef63f'
类型如下
type(current_commit)
<class 'sh.RunningCommand'>
我可以将其更改为字符串,其长度为66
type(str(current_commit))
<type 'str'>
len(str(current_commit))
66
与实际字符串的长度不同
len('89a848eb9ea98bfd6770301dce848052ec8ef63f')
40
current_commit被写入文件,如下所示
with open(commit_store_file, 'w+') as f:
f.write(str(current_commit))
当我用vi打开这个文件时,我看到以下
^[[?1h^[=^M'89a848eb9ea98bfd6770301dce848052ec8ef63f'^[[m
^M^[[K^[[?1l^[>
我用以下内容阅读了
with open(commit_store_file, 'r+') as f:
stored_commit = f.readline()
此处的类型为字符串
type(stored_commit)
<type 'str'>
长度为55
print len(stored_commit)
55
如何使用sh检索实际字符串并正确存储以使它们匹配?
由于
答案 0 :(得分:0)
这些角色看起来像是寻呼机的东西。
尝试将--no-pager
添加到命令的前面:
git --no-pager log -n 1 --format='%T' --color=never
该选项需要先于log
命令,而不是之后,因此您必须bake
进入。另一种获得相同效果的方法是使用{{1环境变量或配置设置。
获取机器可读树形哈希的另一种方法是GIT_PAGER
。树将出现在输出的第一行,以“tree”为前缀。