为子文件夹和文件提取git time recursivley

时间:2018-05-03 15:02:47

标签: python git python-2.x gitpython

我正在尝试使用格式为filename的元素创建一个字典:timestamp in yy-mm-dd hh:mm:ss。这应该递归地包括repo中的所有子文件夹和文件。我遇到了一段代码:

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)

但是,这仅包括主要子文件夹。如何递归地包含子文件夹和文件

注意:Roland的以下解决方案不包括子文件夹,只包含文件。我还需要在下载git repo的路径中,然后通过给出其绝对路径来运行脚本

Get time of last commit for Git repository files via Python?

1 个答案:

答案 0 :(得分:1)

这对我有用

http://gitpython.readthedocs.io/en/stable/tutorial.html#the-tree-object

根据文档由于树只允许直接访问它们的中间子条目,使用遍历方法获取迭代器以递归方式检索条目

它创建一个执行工作的生成器对象

print tree.traverse()
<generator object traverse at 0x0000000004129DC8>




  d=dict()
  for blob in tree.traverse():
        commit=repo.iter_commits(paths=blob.path).next()
           d[blob.path]=commit.committed_date