[英]pygit2 blob history
我正在尝试使用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)
另一个解决方案,懒惰地从给定的提交中产生文件的修订。 由于它是递归的,如果历史太大,它可能会破坏。
def revisions(commit, file, last=None):
try:
entry = commit.tree[file]
except KeyError:
return
if entry != last:
yield entry
last = entry
for parent in commit.parents:
for rev in revisions(parent, file, last):
yield rev
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.