通过Python获取Git存储库文件的最后提交时间?

Rin*_*lle 6 python git

我有一个包含数千个文件的Git存储库,并且想获取每个文件的最后一次提交的日期和时间。可以使用Python完成此操作(例如,使用os.path.getmtime(path))吗?

Mar*_*ian 8

使用GitPython,这将完成这项工作:

import git
repo = git.Repo("./repo")
tree = repo.tree()
for blob in tree:
    commit = repo.iter_commits(paths=blob.path, max_count=1).next()
    print(blob.path, commit.committed_date)
Run Code Online (Sandbox Code Playgroud)

请注意,这commit.committed_date是“自纪元以来的秒数”格式。