What are the Gitlab references listed under /refs?

Viewed 319

I am working on a script which collects information about commits via the Gitlab API. Among others, I need to know which tag(s) are pointing to a certain commit. I was using GET /repository/commits/<sha>/refs until I noticed that there are multiple tags listed in the response and different commits can have the same tags, according to this endpoint, which is impossible. So my questions are:

  1. What information is shown by this endpoint as type "branch" and "tag"?
  2. Which endpoint would show me the correct tag for a given commit?

Thank you!

1 Answers

1. From GitLab Docs, REST API, Commits:

Get references a commit is pushed to

Get all references (from branches or tags) a commit is pushed to. ...

GET /projects/:id/repository/commits/:sha/refs

So it shows all refs where given commit is included, and it works perfectly as documented, several tags could have particular commit in their history tree. By default it shows both branches and tags, but you can filter to include either branches or tags.

2. To find a tag with the tip at specific commit, you may enumerate all tags first, as (reference to tags endpoint doc):

Get a list of repository tags from a project, sorted by name in reverse alphabetical order.

GET /projects/:id/repository/tags

Then manually find the tags at the target commit by commit id (SHA). It's perfectly correct that several tags could have the same commit at their tip. The tags endpoint by default enumerates them from latest to oldest, so if there are many tags at the same commit, you may pick for example latest.

Related