gpt4 book ai didi

gitpython - 如何使用 gitpython 从特定的 git commit 中获取文件数据

转载 作者:行者123 更新时间:2023-12-04 12:06:50 25 4
gpt4 key购买 nike

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

我能够从最新提交中获取文件(包含内容)。但是我想从特定的先前 git commit 中获取文件(包含内容)。

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

我怎么才能得到它 ?

最佳答案

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

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 :
>>> targetfile.name
'some_file.md'
>>> targetfile.type
'blob'

关于gitpython - 如何使用 gitpython 从特定的 git commit 中获取文件数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48802091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com