Python版本3.11.3 当我使用 gitpython 时,我的代码如下
os.environ["PYTHONIOENCODING"] = "utf-8"
os.environ["PYTHONUFT8"] = "on"
repo = Repo(repo_path)
remote = repo.remote()
remote.fetch()
如果有类似“测试分支”这样的中文分支,会报错
git.exc.CommandError: Cmd('<stderr-pump>') failed due to: UnicodeDecodeError(''gbk' codec can't decode byte 0xad in position 54: illegal multibyte sequence')
我知道这是编码问题,我已经设置了
PYTHONUTF8=1
和PYTHONIOENCODING='utf-8'
,但是不起作用。
fetch
方法有像encoding='utf-8'
这样的参数吗?尝试了最新的Gitpython,fetch不再抛出错误。但remote().fetch().name等部分仍保持GBK编码。
到目前为止,我能尝试的,解决这个问题的最佳方法是: str(bytes(remote().fetch().name,'gbk'), 编码='utf-8')