git-p4 消息和作者编码

dag*_*dag 5 python git bash perforce git-p4

今天我可以将一些相当旧的 perforce 存储库迁移到 git。虽然这确实很有趣,但有一件事引起了我的注意。提交消息中的所有特殊字符甚至作者姓名的编码都不正确。

所以我试图调查问题出在哪里。

  • 首先perforce服务器不支持unicode,所以设置P4CHARSET没有效果,但是Unicode clients require a unicode enabled server.
  • 然后我检查了简单命令的输出,例如p4 userswich 实际上在 ANSI 中(咨询 notepad++,或根据file -bi重定向输出的 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
Run Code Online (Sandbox Code Playgroud)

但这并不能解决问题,特别是因为它无意重写作者姓名。

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

更新:

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

/usr/bin/p4 $@ | iconv -f iso-8859-1 -t utf-8
Run Code Online (Sandbox Code Playgroud)

但这会破坏明显使用的编组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
Run Code Online (Sandbox Code Playgroud)

更新2:

如此处所示更改 Python 的默认编码?我尝试将 python 编码设置为 ascii:

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

输出:

('ascii', 'ascii')
Run Code Online (Sandbox Code Playgroud)

但所有消息和作者仍然没有正确迁移。

更新3:

即使尝试修补 git-p4.pydef commit(self, details, files, branch, parent = "")功能也没有帮助:更改

self.gitStream.write(details["desc"])
Run Code Online (Sandbox Code Playgroud)

到其中之一

self.gitStream.write(details["desc"].encode('utf8', 'replace'))
self.gitStream.write(unicode(details["desc"],'utf8')
Run Code Online (Sandbox Code Playgroud)

刚刚提出:

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

因为我不是 python 开发人员,所以我不知道接下来要尝试什么。

Dou*_*der 1

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

因此,您需要decode先将其转换为 Unicode encode

print type(details["desc"])
Run Code Online (Sandbox Code Playgroud)

找出类型。

details["desc"].decode("iso-8859-1").encode("UTF-8")
Run Code Online (Sandbox Code Playgroud)

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