首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用gitpython从特定的git提交中获取文件数据

如何使用gitpython从特定的git提交中获取文件数据
EN

Stack Overflow用户
提问于 2018-02-15 15:38:42
回答 2查看 2.3K关注 0票数 6

我正在尝试使用gitpython python-module从特定的提交中获取文件。

我能够从最近一次提交中获得文件(包含内容)。但是,我想从以前的git提交中获取文件(包含内容)。

代码语言:javascript
复制
repo = git.Repo("G:\myrespo")
obj = repo.git.get_object_data(x.a_blob)

我怎么才能得到它呢?

EN

回答 2

Stack Overflow用户

发布于 2019-02-27 16:23:52

以下是从特定提交中获取文件的一种方法:

代码语言:javascript
复制
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是一个标准的GitPython Object

代码语言:javascript
复制
>>> targetfile.name
'some_file.md'
>>> targetfile.type
'blob'
票数 7
EN

Stack Overflow用户

发布于 2019-03-06 21:25:23

您还可以使用PyDriller (GitPython的包装器),这使得它更容易:

代码语言:javascript
复制
for commit in RepositoryMining("repo", single="commit_hash").traverse_commits():
    # list of modified files of the commit
    commit.modifications
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48802091

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档