On the Gitlab webpage you have the possibility to check how many commits a branch is behind or ahead of the master branch. My question is if it is possible to get information about this with the "gitlab API"? I checked the docs but unfortunately couldn't find anything.
Asked
Active
Viewed 776 times
7
-
As in https://stackoverflow.com/a/51572162/6309? – VonC Jul 14 '20 at 08:42
-
@VonC i am searching for Gitlab API and not Github – Tessaiga Jul 14 '20 at 14:00
2 Answers
1
The specific Gitlab API that does the trick is
GET /projects/:id/repository/compare
More info at https://docs.gitlab.com/ee/api/repositories.html#compare-branches-tags-or-commits
Execution example to bring you number of commits that differentiates the 2 branches
curl -s --header "PRIVATE-TOKEN: <access_token>" "https://gitlab.com/api/v4/projects/<project_id>/repository/compare?from=<branch_name_behind_in_commits>&to=<branch_name_ahead_in_commits>&straight=true" | jq '. | .commits | length'
- If you want to see how many commits is
master
branchbehind
offeature
branch. You should add the master tofrom
field and feature branch atto
field - If you want to see how many commits is
master
branchahead
offeature
branch. You should add the feature branch tofrom
field and master branch atto
field

Tolis Gerodimos
- 3,782
- 2
- 7
- 14
0
Look for include_diverged_commits_count: "true" (default is false) in the MR API: https://docs.gitlab.com/ee/api/merge_requests.html#get-single-mr.
The only downer (IMHO) is that this seems to be only available via the MR API, and so you need to first create a MR.

Bollob
- 1