Compare branch via rest api
Asked by Vishal Gaurav:

There is a feature as "Compare branches or tags" which I am using to compare the branches for any merge conflict(screenshot attached). Now I want to do this via the bitbucket REST api. I tried searching to docs but was unable to find the endpoint. Can somebody please help with this?

![IMAGE 2023-05-09 11_35_06.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/255970i3F17A24F580027C4/image-size/large?v=v2&px=999 "IMAGE 2023-05-09 11_35_06.jpg")![IMAGE 2023-05-09 11_35_31.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/255971i9DB3745E84006540/image-size/large?v=v2&px=999 "IMAGE 2023-05-09 11_35_31.jpg")


Answers:
author: Theodora Boudale
body: Hi [@Vishal Gaurav](/t5/user/viewprofilepage/user-id/5214591) and welcome to the community!

You can use the following API endpoint to compare two branches as well:

* <https://developer.atlassian.com/cloud/bitbucket/rest/api-group-commits/#api-repositories-workspace-repo-slug-diff-spec-get>

An example with curl:

```
curl -u username:app_password --request GET \
--url 'https://api.bitbucket.org/2.0/repositories/workspace-id/repo-slug/diffstat/main..feature' \
--header 'Accept: application/json'
```

For the conflicted files, you will see in the output the field **"status": "merge conflict"**.

Please feel free to let me know if this works for you and if you have any questions.

Kind regards,  
Theodora

comments: author: Vishal Gaurav
body: Thanks [@Theodora Boudale](/t5/user/viewprofilepage/user-id/818086) for the answer, it works. Now I can see the "status": "merge conflict" in the response. However, I have a doubt.

I just went through the diffstat API documentation and found out about the "topic" parameter. By default, when I don't include this parameter (as shown in the example above), I only get diffstats of type "merge conflict" (which is exactly what I'm looking for). However, if I set it to false or true, I get a lot more diffstats, and they don't seem to include diffstats of type "merge conflict".

Omitting the topic parameter altogether works for me, but I wanted to be aware of what is actually happening. Can you please help me with this confusion?

Regards,  
Vishal Gaurav


author: Theodora Boudale
body: Hi Vishal,

Please allow me to give some context about diffs first. Git has two types of diff:

**(A)** ++The two-dot diff++

```
git diff --stat main..feature
```

This will show the diffstat for file changes in both branches, main and feature, since their common ancestor.

**(B)** ++The three-dot dif++f

```
git diff --stat main...feature
```

This will show the diffstat for files changed in *feature* branch only, since the common ancestor of main and feature branches. Therefore, this may include less diffstats than (A), since it won't include changes in main branch.

<br />

The example I shared in my previous example is using the three-dot diff **(B)** and it will show changes in the main branch only. If you want to see changes in the feature branch instead, you will need to reverse the order of the branches

```
https://api.bitbucket.org/2.0/repositories/workspace-id/repo-slug/diffstat/feature..main
```

When the topic parameter is absent, and also when it is true, the three-dot diff **(B)** is used.

When the topic parameter is false, the two dot diff is used **(A)** and this may show more diffstats since it will include changes in both branches.

<br />

Regarding your questions:

**1.** Getting more diffstats with topic=false is expected. However, I cannot reproduce a case where topic=true shows more diffstats than if omitted. Are you able to reproduce this in a public test repo, with some dummy content, so I can take a look?

**2.** When topic=true is used, merge conflicts are indeed not indicated. I will reach out to the development team for more info on why this occurs, and I will get back to you when I have an update.

<br />

Kind regards,  
Theodora


author: Theodora Boudale
body: Hi Vishal,

I reached out to the development team regarding question 2:
> 2. When topic=true is used, merge conflicts are indeed not indicated. I will reach out to the development team for more info on why this occurs, and I will get back to you when I have an update.

The **topic** query parameter was introduced last year when we made changes in the diff algorithm we use:

* <https://bitbucket.org/blog/improving-performance-on-complex-diffs>

The status **"merge conflict"** was not added as a status when the **topic** query is used, because we will eventually remove it. When we do, we will provide a public conflict API endpoint for customers who use this status for that purpose, to ensure continuity.

Any changes in the API are announced here:

* [https://developer.atlassian.com/cloud/bitbucket/?utm_source=%2Fbitbucket\&utm_medium=302](https://developer.atlassian.com/cloud/bitbucket/?utm_source=%2Fbitbucket&utm_medium=302)

Kind regards,  
Theodora


Tags: bitbucket-cloud

cloud