Rest API to get all the merged commits in a branch

amit raj August 3, 2017

Hello Friends,

I want to list all the JIRA#(commits)  those were merged in a branch. I can get list of all the commits using below rest api, but it gives me all the commits including merged commit details, is there any way to get only "merged" commits?

https://<URL>/rest/api/1.0/projects/<KEY>/repos/<REPO>/commits?until=release/2.0.5

 

Thank you

Amit Raj

4 answers

1 accepted

0 votes
Answer accepted
Mark A_
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 9, 2017

Hey Amit,

Have you looked at our REST documentation? REST API

You just need to add the merges parameter with a value of only like so:

/rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/commits?&merges=only

Adding the parameter will return only merge commits.

I hope this helps.

Cheers,

Mark 

Mike Stead September 4, 2017

@Mark A_ Thanks for the pointers. I was hoping to do similar but I've noticed that when I add the merges=only query parameter, it doesn't seem to pick up PR merges that had the squash option enabled. 

I was looking at this approach because the PR RESTful endpoints don't seem to return the commit hash a merged PR creates, only the to and from branch refs for each PR.

The only way I can seem to determine a commit a PR makes is by scanning the commit history of a branch and regex'ing the commit messages looking for the the merge commit msg pattern.

Can you let me know if there's a better approach I can take? 

Cheers,

Mike

Mark A_
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 12, 2017

Hey Mike,

I'm sorry that I originally missed your question.

When using the Squash merge strategy, what it does is rewrite history by combining the commits into a new non-merge commit, therefore, the merges=only query will not retrieve this commit since it is not a merge.

As I noted below, you may be able to use our REST endpoint to compare commits and filter based on the message as it will still contain a reference to "Merge pull request".

-Mark

Mike Stead September 13, 2017

Thanks Mark and no worries.

Harshil shah July 17, 2018

Hey mark,

 

I am using the Bitbucket 2.0 api and I need to do a similar thing of getting only the merged commits. I tried using the merges=only parameter but does not seem to work as it just returns all the commits on master(I only want the ones with label "M" and grayed out). 

I am currently using this endpoint -

https://api.bitbucket.org/2.0/repositories/<username>/<repo-name>/commits?merges=only.

 

Please let me know how to achieve this with the 2.0 API. I cannot find any documentation on that.

 

Thank you very much,

Harshil

akota January 23, 2019

Hi, I am also trying to get all commits without merge commits. I used merges=exclude query parameter but it doesn't work. i'm getting all commits.

 

please suggest me a solution how to achieve this. 

Julius Davies _bit-booster_com_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 23, 2019

The Bitbucket Server REST API can do this.   This means any on-prem Bitbucket instance can do this, including Bitbucket Data Center instances.

The Bitbucket Cloud REST API cannot.

And so I have some bad news for Amulya Kota and Harshil Shah:  api.bitbucket.org refers exclusively to Bitbucket Cloud (a.k.a. bitbucket.org), and its REST API does not support this kind of filtering.

You will need to process the JSON in your own code and ignore all results where {commit}.parents.length > 1.

Whenever a commit object's parents array has 2 or more entries you are looking at a merge.

Like akota likes this
akota January 23, 2019

Thanks for your reply. I'll try that. 

0 votes
Joel Ransom March 1, 2020

Revisiting the "merges=exclude" topic above, I'm using Bitbucket Server and trying merges=exclude as follows, and it has no effect. It 

...compare/commits?from=fromBranch&to=toBranch&limit=1000&merges=exclude

Also, "Whenever a commit object's parents array has 2 or more entries you are looking at a merge" doesn't seem to be correct. I'm looking at merge commits that only have one parent entry. 

I'm resorting to doing a string search on the commit message to exclude merge commits. 

Am I missing something? Should I be able to exclude merge commits? 

Thanks in advance. 

0 votes
Joel Ransom February 26, 2018

Sorry to comment on an old thread, but what Amit mentioned "comparing the release branch with master" and getting the commits back is exactly what I want to do, but I don't seem to understand what the syntax would be. Would someone be so kind as to provide an example? 

Julius Davies _bit-booster_com_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 26, 2018

 

If you're on Bitbucket Server, experiment with the compare commits rest api.

/REST/API/1.0/PROJECTS/{PROJECTKEY}/REPOS/{REPOSITORYSLUG}/COMPARE/COMMITS?FROM&TO&FROMREPO

> Gets the commits accessible from the {@code from} commit but not in the {@code to} commit.

FROM and TO can be commit-ids or branch names (e.g., FROM=release, TO=master).

If you don't get the results you expected, try flipping the values (e.g., FROM=master, TO=release).

 

 

 

Joel Ransom February 26, 2018

thanks for the prompt reply, I think I'm getting somewhere. I also learned that branch names are case sensitive. 

Yaron Norani November 13, 2019

Hi @Julius Davies _bit-booster_com_  

Do you know if the compare commit is like the command:

git log commit .. commit2    ?

means that you get list all commits that are reachable from commit1 and NOT reachable form 2 ?

If not, is it supported command in REST API?

0 votes
rajanibs August 31, 2017

Hi Mark,

How do you specify the target branch here, if I have multiple branches in a repository? Does it work by adding "at=refs/heads/release/<branchname>" in the above query?

Thanks in advance,

Rajan

Mark A_
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 1, 2017

Hey Rajan,

You may be able to use PATH to help filter the commits by. You would not be able to use AT as that is not allowed. Only the following from the API is allowed for this endpoint:

/REST/API/1.0/PROJECTS/{PROJECTKEY}/REPOS/{REPOSITORYSLUG}/COMMITS?FOLLOWRENAMES&IGNOREMISSING&MERGES&PATH&SINCE&UNTIL&WITHCOUNTS

I looked around the API and tested but could not get exactly what you're looking for strictly with the API. You may have to additionally filter the JSON data to retrieve the first level ID and its message which may display as "Merge branch..." or "Merge Pull Request..."

# First Merge Commit with collapsed elements
{
"id": "10326411acaf5036bd4d525718cbd9e56305303b",
"displayId": "10326411aca",
"author": {
"name": "Mark Williams",
"emailAddress": ""
},
"authorTimestamp": 1467995573000,
"committer": {
"name": "Mark Williams",
"emailAddress": ""
},
"committerTimestamp": 1467995573000,
"message": "Merge branch 'test' of https://proxy:8443/bitbucket/scm/mir/mirror",
"parents": [
{
"id": "0ba3baf0cac25e297f3c391bed6ba0f126f90378",
"displayId": "0ba3baf0cac"
},
{
"id": "60a4b9b7fe505f9e049afa3f2625d884f0aea11e",
"displayId": "60a4b9b7fe5"
}
]
}

# Second Merge Commit with expanded elements
{
"id": "8e5b2bc030dd64edcd048fa873e159a80521c0ec",
"displayId": "8e5b2bc030d",
"author": {
"name": "admin",
"emailAddress": "",
"id": 402,
"displayName": "admin",
"active": true,
"slug": "admin",
"type": "NORMAL",
"links": {
"self": [
{
"href": "http://stash0:7990/bitbucket/users/admin"
}
]
}
},
"authorTimestamp": 1459285021000,
"committer": {
"name": "admin",
"emailAddress": "",
"id": 402,
"displayName": "admin",
"active": true,
"slug": "admin",
"type": "NORMAL",
"links": {
"self": [
{
"href": "http://stash0:7990/bitbucket/users/admin"
}
]
}
},
"committerTimestamp": 1459285021000,
"message": "Merge branch 'master' of https://proxy:8443/bitbucket/scm/mir/mirror",
"parents": [
{
"id": "4061f6ec24091ad552a01fd582748e17c269e637",
"displayId": "4061f6ec240"
},
{
"id": "ce216abf3393a46393d9031ffc1b5928ad7cbac6",
"displayId": "ce216abf339"
}
]
},

You can also play around with the compare commits endpoint 

/REST/API/1.0/PROJECTS/{PROJECTKEY}/REPOS/{REPOSITORYSLUG}/COMPARE/COMMITS?FROM&TO

Again, you will have to filter the JSON results to get the MESSAGE that references Merging.

Unfortunately, there is nothing at the moment that gets directly to the core of your request.

-Mark

amit raj September 3, 2017

Thank you very much Mark!

Compare commits endpoint worked for me, I am comparing the release branch with master and filtering all the merged commits.

Thank you.

Mark A_
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 12, 2017

Awesome to hear. I'm glad that is working for you.

Cheers,

Mark

rajanibs December 12, 2017

Thanks Mark, Yes, that is the only option currently!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events