2

I have a bitbucket server which has 100's of branches. I would like to separate the branches which contain code already on the master branch, and those that dont.

The only way I've thought to get the info would be to get a list of branches:

https://<bitbucket host>/rest/api/1.0/projects/<project>/repos/<MyRepo>/branches

and get the head of each branch

and see whether the commit exists in the list of commits from master: https://<bitbucket host>/rest/api/1.0/projects/<project>/repos/<MyRepo>/commits?branch=master&limit=1000'

This is extremely slow. Can anybody think of a better way of doing this? Basically I'm hoping to identify all branches which have been added into master and so can be deleted somewhat safely.

Im thinking that I want the equivalent of:

for each branch:
  git rev-list --left-right --count origin/master...origin/FOO

Thanks in advance

DaveM
  • 25
  • 1
  • 3

1 Answers1

2

What you can do first is get the list of all branches available in that particular repository using API call and make it into a python list. Since you have 100's of branches and also there is paging, try to loop the curl by iterating the page value until the obtained response is empty.

curl --url "https://api.bitbucket.org/2.0/repositories/workspace/repository_name/refs/branches?pagelen=100&page={Iterate}" --user username:password --request GET --header "Accept: application/json"

Later there is an API available to check if all the commits in the source branch is available in destination branch.

curl --url "https://api.bitbucket.org/2.0/repositories/workspace/repository_name/commits/source_branch?exclude=destination_branch" --user username:password --request GET --header "Accept: application/json"

So, you could have your source as master and iterate all other branches as destination and get the response. Based on the response if its empty or not, you can categorize the branches that has all the contents in master and that don't have the content.

AkshayBadri
  • 504
  • 1
  • 10
  • 18
  • Sorry, I missed your reply to this, thanks for the help! I'll give this a go soon. – DaveM Jan 08 '21 at 09:28
  • I think this only works for bitbucket cloud which has rest API 2.0. Sadly, the bitbucket server do not have api version 2.0 – vpp Apr 07 '22 at 13:44