How to disambiguate branches with "/" characters via the API

Consider the following URL:

There are two possibilities for what this means:

  1. this references the filepath bar/filename.txt on a branch foo
  2. this references the filepath filename.txt on a branch foo/bar

Git itself prevents this ambiguity from occurring within a given repo (e.g. if you have a branch named foo/bar you cannot create a new branch named foo, and vice-versa) but disambiguating the URL path above requires knowledge of the github repository contents.

What I would like is an API query that given a path, such as orgname/reponame/blob/XXX/YYY/ZZZ/filename.txt, will return the name of the branch within the repo that it refers to (here, either XXX or XXX/YYY or XXX/YYY/ZZZ or 404 Not Found). Currently, it seems the only options for this via the V3 REST API are:

  1. List all branches, with a potentially very large number of paginated results for repos with many branches

  2. Query for the contents of all possible branch names, which might hit rate limits in the case of a deeply-nested path where the branch does not exist.

Both have the potential to be problematic in corner cases. Is there a better option?

Hi @jakevdp!

That’s a really good question. To help me answer it more thoroughly, can you tell me what you’re trying to do that you find yourself in a situation where you have a URL like this to a file and you have to figure out what branch it’s in? Just trying to get a better picture of the situation.


1 Like