As an API Consumer, I would like to know if a Repository is Empty

Recently, the accounting of Bitbucket resources has changed a bit, so that the first 33kB of an empty repository is reflected in the size reported by the repositories call. A customer has the following to say:

We’ve previously been using the repositories API endpoint to fetch details about repositories and specifically their size and if they’re empty, until recently empty repositories used to return 0 as their size however recently they’ve been returning sizes - whats changed?

How would you now suggest one detects if a repository is actually empty or not? Any plans to implement an `empty?` boolean?

4 answers

1 accepted

2 votes

While the disk size (bytes) does have a correlation to the repo's content, it is too simple to assume that an empty repo takes up zero bytes.

Even an empty repo contains the basic SCM files and so is not 0 bytes. Nor is it necessarily deterministic as a newer version of the SCM might format these files slightly differently and so show a slightly different size.

@john-garcia-atlassian makes good suggestions. An empty repo has no commits and so I'd just check for that:

$ curl -s | jsonify
    "page": 1,
    "pagelen": 30,
    "values": []

If the "values" element is empty, the repo contains no commits.

There's actually a few different ways to determine if a repository is empty.

- The branches resource can be used; if it returns anything other than empty set, the repo is not empty.

- The main-branch endpoint returns No main branch set

- The changesets endpoint will return 404

I'm sure there are others, but we hope this list will get you started.

1 votes

For extra credit, add "?pagelen=1" to reduce the amount of response data in case the repo is not empty ;-)

Thanks, guys, for documenting this on Answers!

Suggest an answer

Log in or Join to answer
Community showcase
Piotr Plewa
Published Dec 27, 2017 in Bitbucket

Recipe: Deploying AWS Lambda functions with Bitbucket Pipelines

Bitbucket Pipelines helps me manage and automate a number of serverless deployments to AWS Lambda and this is how I do it. I'm building Node.js Lambda functions using node-lambda&nbsp...

661 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot