Get the name of the files in an online git repository

I would like to get a list of the entries in a git repository.

Say there is this repository with the structure:

- src
- Cargo.toml

How would I get an iterator over src, that will return [,] (assuming I also know which branch I want to be on) ? Do I need to clone the whole repository, or is there a better way ?

Check out the Github GraphQL API. What you're looking for is almost certainly possible with that.

Edit from the Future: Note that Github also has something they call the "V3 API", which may be what turns up first when looking up "Github API" on search services. This is the old API and is much less expressive/powerful/efficient than the GraphQL API.


Thanks !
Following this I also found a Rust crate that exposes the API:

That may be overkill for such a simple use-case though :sweat_smile:

Another option is to programmatically download the repository and inspect it.

The git2 crate has really good bindings to libgit2 and probably has the ability to do a shallow clone.

1 Like

From what I'm seeing of the documentation, it seems you have to clone the whole repository somewhere though, no ? There may be another option but I am not at all familiar with git2 :confused: ...

BTW here is the query I managed to come up with:

query Entries($owner: String!, $name: String!, $branch_and_path: String!) { 
	repository(owner: $owner, name: $name) {
    branch_and_directory: object(expression: $branch_and_path) {
      ... on Tree {
        entries {

Where $branch_and_path must be of the form <branch>:<path>.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.