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
Accepted answer

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 vote

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 Sign up to answer
Community showcase
Published Nov 06, 2018 in Bitbucket

Upgrade Best Practices

Hello! My name is Mark Askew and I am a Premier Support Engineer for products Bitbucket Server/Data Center, Fisheye & Crucible. Today, I want to bring the discussion that Jennifer, Matt, and ...

1,960 views 7 10
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