Search code examples
bitbucketbitbucket-api

Download file at specific commit with Bitbucket REST API


We are trying to find a way to download a single file from a Bitbucket project using the REST API at a specific commit. Currently, we have the ability to download a file at a specific branch:

https://stash.domain.com:8443/rest/api/1.0/projects/our_project/src/main/java/com/SomeFile.java?at=refs%2Fheads%2Fmaster

Note that the end of the URL, when decoded, contains the query parameter at=refs/heads/master, which refers to the master branch. This also works for specific tags:

https://stash.domain.com:8443/rest/api/1.0/projects/our_project/src/main/java/com/SomeFile.java?at=refs%2Ftags%2Ftesttag1

Here the query parameter at=refs/tags/testtag1 refers to the tag (commit) testtag1.

But because of the nature of our implementation, we would like to refer directly to a commit SHA-1 hash via the Bitbucket REST API. Is this possible?

Obviously, one ugly workaround would be to just add a tag to every commit. But this could bloat the repository and it also feels like an unnecessary hack.


Solution

  • With the help of this SO question, I found one of the answers which tipped me off to the correct syntax. Use this:

    <URL>?at=commit_hash
    

    For example:

    https://stash.domain.com:8443/rest/api/1.0/projects/our_project/src/main/java/com/SomeFile.java?at=bed2dda5

    Here is a table of three main endpoint types with the Bitbucket REST API:

    query parameter   | role
    ---------------------------------------------
    refs/heads/master | specify master branch
    refs/tags/someTag | specify 'someTag' tag
    at=bed2dda5       | specify commit #bed2dda5