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

如何使用GitPython在提交中获取文件的源代码?,python,git,file,commit,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')) 但我得到了这个错误: Trac

我需要获得提交中所有文件的源代码。目前我正在使用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更改为拉丁语-1也无济于事


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

正如第一条评论所建议的,对于我建议您使用的这些东西,它要简单得多:

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

它还负责解码、重命名等。在提交之前,您还拥有源代码(
修改的文件。source\u code\u之前

PyDriller
使用
GitPython
,因此只需稍加搜索,我认为您就可以找到快乐。对于/foo.bar的相对路径路径,请尝试
repo.git.show('%s:%s')(commit.hexsha,'path/to/foo.bar')
。正如我在问题中所说,我需要找到Pydriller的替代品。我目前正在使用它,但我需要更好的性能