git-p4消息和作者编码

时间:2015-05-13 19:29:07

标签: python git bash perforce git-p4

今天我可以将一些非常古老的perforce存储库迁移到git。虽然这真的很有趣但有一件事引起了我的注意。提交消息中的所有特殊字符甚至作者名称都没有正确的编码。

所以我试着调查问题的来源。

  • 首先,perforce服务器不支持unicode,因此设置P4CHARSET无效,Unicode clients require a unicode enabled server.
  • 然后我检查了像p4 users这样的简单命令的输出,其中确实在ANSI(根据重定向输出的file -bi咨询notepad ++或ISO-8859-1)
  • locale命令说LANG = en_US.UTF-8 ...

毕竟我的猜测是所有p4客户端输出都在ISO-8859-1中,但是git-p4代替了UTF-8。

我尝试用

重写提交消息
git filter-branch --msg-filter 'iconv -f iso-8859-1 -t utf-8' -- --all

但这并不能解决问题,特别是因为它不打算重写作者名称。

任何人都猜测如何在git-p4接收之前强制将输出转换为UTF-8?

更新:

我试图用一个简单的shell脚本“覆盖”默认的p4命令输出,我将它添加到PATH

/usr/bin/p4 $@ | iconv -f iso-8859-1 -t utf-8

但这会消除明显使用的编组python对象:

  File "/usr/local/bin/git-p4", line 2467, in getBranchMapping
    for info in p4CmdList(command):
  File "/usr/local/bin/git-p4", line 480, in p4CmdList
    entry = marshal.load(p4.stdout)
ValueError: bad marshal data

UPDATE2:

如此处所见Changing default encoding of Python?我试图将python编码设置为ascii:

export export PYTHONIOENCODING="ascii"
python -c 'import sys; print(sys.stdin.encoding, sys.stdout.encoding)'

输出:

('ascii', 'ascii')

但仍未正确迁移所有邮件和作者。

更新3:

即使尝试修补git-p4.py def commit(self, details, files, branch, parent = "")函数也无济于事: 改变

self.gitStream.write(details["desc"])

其中一个

self.gitStream.write(details["desc"].encode('utf8', 'replace'))
self.gitStream.write(unicode(details["desc"],'utf8')

刚刚提出:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 29: ordinal not in range(128)

因为我不是python开发人员,所以我不知道下一步该尝试什么。

1 个答案:

答案 0 :(得分:1)

我怀疑details["desc"]的类型是字节字符串。 (str for python2)。

因此,在decode之前,您需要encode到Unicode。

print type(details["desc"])

找出类型。

details["desc"].decode("iso-8859-1").encode("UTF-8")

可能有助于将iso-8859-1转换为UTF-8。