在gitpython中获取特定文件的所有修订

Ran*_*ana 5 python git gitpython

我正在使用gitpython库执行git操作,从python代码中检索git信息。我想检索特定文件的所有修订。但是在文档中找不到对此的具体参考。

谁能在这方面提供些帮助的线索?谢谢。

Byr*_*ron 5

没有这样的功能,但是很容易实现:

import git
repo = git.Repo()
path = "dir/file_you_are_looking_for"

commits_touching_path = list(repo.iter_commits(paths=path))
Run Code Online (Sandbox Code Playgroud)

即使涉及多个路径,性能也会适中。可以在github上的问题中找到基准和更多相关代码。


Eri*_*ric 5

后续阅读每个文件:

import git
repo = git.Repo()
path = "file_you_are_looking_for"

revlist = (
    (commit, (commit.tree / path).data_stream.read())
    for commit in repo.iter_commits(paths=path)
)

for commit, filecontents in revlist:
    ...
Run Code Online (Sandbox Code Playgroud)