gpt4 book ai didi

python - pygit2 Blob 历史

转载 作者:太空狗 更新时间:2023-10-29 21:58:52 26 4
gpt4 key购买 nike

我正在尝试使用 pygit2 在 git 裸存储库中执行与 git log filename 等效的操作。该文档仅解释了如何像这样执行 git log:

from pygit2 import GIT_SORT_TIME
for commit in repo.walk(oid, GIT_SORT_TIME):
print(commit.hex)

你有什么想法吗?

谢谢

编辑:

我现在有这样的事情,或多或少是精确的:

from pygit2 import GIT_SORT_TIME, Repository


repo = Repository('/path/to/repo')

def iter_commits(name):
last_commit = None
last_oid = None

# loops through all the commits
for commit in repo.walk(repo.head.oid, GIT_SORT_TIME):

# checks if the file exists
if name in commit.tree:
# has it changed since last commit?
# let's compare it's sha with the previous found sha
oid = commit.tree[name].oid
has_changed = (oid != last_oid and last_oid)

if has_changed:
yield last_commit

last_oid = oid
else:
last_oid = None

last_commit = commit

if last_oid:
yield last_commit


for commit in iter_commits("AUTHORS"):
print(commit.message, commit.author.name, commit.commit_time)

最佳答案

我建议您只使用 git 的命令行界面,它可以提供格式良好的输出,使用 Python 非常容易解析。例如,获取给定文件的作者姓名、日志消息和提交哈希值:

import subprocess
subprocess.check_output(['git','log','--pretty="%H,%cn%n----%B----"','some_git_file.py'])

有关可以传递给 --pretty 的格式说明符的完整列表,请查看 git log 的文档:https://www.kernel.org/pub/software/scm/git/docs/git-log.html

关于python - pygit2 Blob 历史,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13293052/

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