gitpython fetch 获取中文命名分支获取编码错误

问题描述 投票:0回答:1

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'
,但是不起作用。

  1. fetch
    方法有像
    encoding='utf-8'
    这样的参数吗?
  2. 如何将 fetch 与 utf-8 一起使用?
  3. 为什么自动选择gbk?
python gitpython
1个回答
0
投票

尝试了最新的Gitpython,fetch不再抛出错误。但remote().fetch().name等部分仍保持GBK编码。

到目前为止,我能尝试的,解决这个问题的最佳方法是: str(bytes(remote().fetch().name,'gbk'), 编码='utf-8')

© www.soinside.com 2019 - 2024. All rights reserved.