Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to find the branch name of a commit by using API?

Taras March 6, 2019

The ultimate task I need to perform is to extract the the commits that are made to a specific branch between two time points. This is quite easy to achieve with git CLI but I am limited to Bitbucket API v1.

Of course it's not a problem to get all commits:

<stash-url>/rest/api/1.0/projects/<proj_name>/repos/<repo_name>/commits?since=<start>&until=<end>

But then I have no branch context.

The only query that I can made successfully is to retrieve the latest commit of the branch:


<stash-url>/rest/api/1.0/projects/<proj_name>/repos/<repo_name>/commits?until=<branch_name>

 

Basically what I am missing is the ability to make a connection between a branch and a commit.



Any help will be appreciated. 


  

2 answers

1 vote
Poorni S
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 1, 2022

Please try this. It gives the branch of a commit. 

 

rest/branch-utils/1.0/projects/{projectKey}/repos/{repositorySlug}/branches/info/{commitId} 

jianbo qu August 10, 2023

this api response only return the latest branch.

how can I get all the branches which contain this commit?

1 vote
Stephen Sifers
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 8, 2019

Hello Taras and welcome to the Community!

I research a bit on your question about linking commits to a specific Bitbucket branch and found there is a possible solution within the v1 API for Bitbucket Server. While the documentation is not exactly clear on how to accomplish this, I did find a community post where someone figured out how to do this along with a StackOverflow thread on a similar topic.

The StackOverflow thread reports the following may work for your needs:

https://<stash-url>/stash/rest/api/latest/projects/<project-ID>/repos/<repo-slug>/commits?until=<my-target-feature-branch>&limit=0&start=0

Source thread: I want to get commits on a branch in bitbucket stash via rest API

Along with this, the Community post reports the following also worked for their requirements (This is similar to your API call on getting the last commit):

http://<stashurl>/stash/rest/api/latest/projects/<projectname>/repos/<myrepo>/commits?until=deploy&limit=0&start=0

Source post: Querying the last commit on a particular branch in Bitbucket using the Rest API

From my understanding on this, you should be able to use the StackOverflow response to call against the feature branch within Bitbucket and have it return commits for the said branch. Please give this a try and let us know the results.

We look forward to hearing back from you so we may find a way to link Commits to Branches within Bitbucket Server.

Regards,
Stephen Sifers

Taras March 11, 2019

Hi Stephen,

Unfortunately both solutions (which are in fact identical) produce the same outcome - the latest commit of the desired branch. Meaning that it leavens me with nothing to retrieve the other commits from the same branch.

I kind of found a partial solution by reverse engineering. Thus master branch commits always have their previous commit as a parent. Thus it is only a matter of traversing "parent->parent->parent..." until the  required depth is reached. All these "parent" items are the commits of the master branch.

But this only works because in my case we have a strict policy of not committing to master directly and only merging feature branches instead.

Meaning that this problem cannot be solved reliably. Unfortunately.

Thank you Stephen anyway.
 

Like constantinosm likes this
constantinosm
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 25, 2019

Did you ever find a solution to this? I am facing a similar problem

Taras November 25, 2019

Nope :o(

I am afraid this can only be achieved with the new/latest API but not with the legacy version.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events