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

John Garcia
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 13, 2014

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
Answer accepted
evzijst
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 13, 2014

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.

1 vote
evzijst
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 13, 2014

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

1 vote
John Garcia
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 13, 2014

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.

0 votes
Seth
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 13, 2014

Thanks, guys, for documenting this on Answers!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events