python - 如何使用 GitPython 获取提交中文件的源代码?

标签 python git file commit gitpython

我需要获取提交中所有文件的源代码。目前我正在使用 Pydriller,它运行良好。但出于性能原因,我需要使用 GitPython。 我尝试过这个解决方案:

repo = Repo('path to repo') )
    commit = repo.commit('my hash')
with io.BytesIO(target_file.data_stream.read()) as f: 
    print(f.read().decode('utf-8'))

但我收到此错误:

Traceback (most recent call last):
File "D:\Programmi\Python36\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
File "D:\Programmi\Python36\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
File "D:/Workspaces/PythonProjects/fixing- 
    commit/crop_data_preparing_gitpython.py", line 82, in 
get_commit_data_gitpython
print(f.read().decode('utf-8'))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9f in position 18: invalid start byte

我认为这可能是一个编码问题,但即使将编码从 utf-8 更改为 latin-1 也没有帮助。

是否存在另一种策略可以帮助我使用 GitPython 获取这些文件的代码?

最佳答案

正如第一条评论所建议的,对于这些事情,我建议您使用 PyDriller ,这更容易:

for commit in RepositoryMining("repo").traverse_commits():
    for modified_file in commit.modifications:
        modified_file.source_code

它还负责解码、重命名等。您还可以在提交之前获得 source_code (modified_file.source_code_before)

关于python - 如何使用 GitPython 获取提交中文件的源代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56647723/

相关文章:

python - 如何在初始化时创建和存储类的实例?

python - 使用 Python 进行 Digram 列表操作

Python输入/输出解释

.net - .NET 下的 File.Delete() 是原子的吗

git - 使用 git rebase -i 重新排序提交时出错

python - 遍历多个文件并计算多个字符串

python - 基于 Django 类的 View 出现错误 : 'get_context_data() keywords must be strings'

python - 如何使用 Python 和 Plotly 创建不显示异常值的箱线图?

git - Dropbox 和 Git 协同工作

git - 如何对 Excel 文件和 SQL 模式文件执行更好的文档版本控制