amazon

12.5.16

Get a list of tracked files of Git by GitPython


The code below is equivalent to git command, "git ls-tree -r master --name-only".

import git

def get_tracked_files(trees):
paths = []
for tree in trees:
for blob in tree.blobs:
paths.append(blob.abspath)
if tree.trees:
paths.extend(get_tracked_files(tree.trees))
return paths

if __name__=='__main__':
repo = git.Repo('.')
tracked_files = get_tracked_files([repo.tree()])
print tracked_files