Search code examples
pythongitpython-2.xgitpython

extracting git time recursivley for subfolders and files


I am trying to create a dictionary with elements in the format filename: timestamp in yy-mm-dd hh:mm:ss . This should recursively include all subfolders and files in the repo . I came across ths piece of code :

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)

However, this includes only the main sub folders. How to include sub folders and files recursively

Note: The following solution by Roland here does not include sub folders, only files.Also I need to be in the path where git repo is downloaded and then run the script by giving its absolute path

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


Solution

  • This works for me

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

    As per the doc As trees allow direct access to their intermediate child entries only, use the traverse method to obtain an iterator to retrieve entries recursively

    It creates a generator object which does the work

    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