Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to list all files been tracked by git? #1277

Answered by Byron
shlomiLan asked this question in Q&A
Discussion options

Hey all,

How do I get a list of all file? using git I can run

git ls-tree HEAD -r

how can I do it with this library? the best I could find is:

for file in repo.commit().tree.blobs:
 print(file.abspath)

but that only lists files in the top dictionary.

Thanks

You must be logged in to vote

You are probably looking for this:

for entry in repo.commit().tree.traverse():
 print(entry)

There are fields or methods on the returned entries to obtain their name or path relative to the root tree.

Replies: 2 comments 1 reply

Comment options

You are probably looking for this:

for entry in repo.commit().tree.traverse():
 print(entry)

There are fields or methods on the returned entries to obtain their name or path relative to the root tree.

You must be logged in to vote
0 replies
Answer selected by Byron
Comment options

To complete this, if you want a file listing, you want:

for entry in repo.commit().tree.traverse():
 print(entry.path)

or

for entry in repo.commit().tree.traverse():
 print(entry.abspath)
You must be logged in to vote
1 reply
Comment options

I also realize now that even though this is a way to list files that will always work, it will be much faster to traverse index entries instead if one is present, i.e. do the equivalent of git ls-files. But then again, unless repo.git.ls_files() is used, any kind of traversal will be slow comparatively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
Converted from issue

This discussion was converted from issue #1273 on June 23, 2021 11:43.

AltStyle によって変換されたページ (->オリジナル) /