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 https://api.bitbucket.org/2.0/repositories/evzijst/emptygit/commits | 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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Jun 12, 2018 in Bitbucket

Do you use any Atlassian products for your personal projects?

After spinning my wheels trying to get organized enough to write a book for National Novel Writing Month (NaNoWriMo) I took my affinity for Atlassian products from my work life and decided to tr...

23,364 views 26 12
Join discussion

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