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 https://github.com/owner/project with the structure:

- src
  - lib.rs
  - a.rs
- Cargo.toml
- README.md

How would I get an iterator over src, that will return [lib.rs, a.rs] (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.

2 Likes

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 {
          name
        }
      }
    }
  }
}

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