Skip to content

Instantly share code, notes, and snippets.

@mbork
Forked from seveas/git-list-all-files
Last active July 19, 2020 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbork/57f67f42bfa6efe472ce543a586c3472 to your computer and use it in GitHub Desktop.
Save mbork/57f67f42bfa6efe472ce543a586c3472 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
#
# List all files that have ever been committed in this repository, in any
# commit in any branch.
import pygit2
import stat
def list_all_files(repo_path):
repo = pygit2.Repository(repo_path)
walker = repo.walk(repo.head.target, pygit2.GIT_SORT_NONE)
files = {}
for ref in repo.listall_references():
if not ref.startswith('refs/heads'):
continue
walker.push(repo.lookup_reference(ref).target)
for ref in walker:
add_tree(repo, ref.tree, '', files)
files = files.keys()
return sorted(files)
def add_tree(repo, tree, prefix, files):
for file in tree:
fullname = '%s/%s' % (prefix, file.name)
if stat.S_ISDIR(file.filemode):
add_tree(repo, file, fullname, files)
else:
files[fullname] = 1
print("\n".join(list_all_files('.')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment