How to get file data from a specific git commit using gitpython

别等时光非礼了梦想. 提交于 2021-02-10 14:23:02

问题


I am trying get a file from a specific commit using gitpython python-module.

I'm able to get the file (with content) from the latest commit. However I want to get the file (with content) from a specific previous git commit.

repo = git.Repo("G:\myrespo")
obj = repo.git.get_object_data(x.a_blob)

How can I get it ?


回答1:


Here's one way to get a file from a specific commit:

import io

repo = Repo('G:\myrespo')

# Retrieve specific commit from repo
# The revision specifier must be one of the specifiers defined in
# https://git-scm.com/docs/git-rev-parse#_specifying_revisions
# In this example, we'll use a SHA-1

commit = repo.commit('7ba4789adf73c0555fbffad3b62d61e411c3b1af')

# Retrieve a file from the commit tree
# You can use the path helper to get the file by filename 

targetfile = commit.tree / 'some_file.md'

# Retrieve contents of targetfile

with io.BytesIO(targetfile.data_stream.read()) as f:
    print(f.read().decode('utf-8'))

targetfile is a standard GitPython Object:

>>> targetfile.name
'some_file.md'
>>> targetfile.type
'blob'



回答2:


You could also use PyDriller (a wrapper around GitPython), that makes it easier:

for commit in RepositoryMining("repo", single="commit_hash").traverse_commits():
    # list of modified files of the commit
    commit.modifications


来源:https://stackoverflow.com/questions/48802091/how-to-get-file-data-from-a-specific-git-commit-using-gitpython

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!