I need to know the date of the first commit for a branch in multiple repos to know the creation time of the repo, I'm using the REST API V2.
i need something like :
curl -D- -X GET "https://api.bitbucket.org/2.0/repositories/user/repo/commits/master?pagelen=100"
But i need to sort the answer from the last page, since some repos have more than 100 pages of commits but i don't know the total amount of pages, and i need the first historical commit in a master branch for each one.
Hello @maximiliano.venesio,
I'm afraid this is not easily possible. There's no direct link to the very first commit, and to find it the whole commit tree traversal is required. This is why Bitbucket doesn't have this option in the commit list API.
You can get repo creation date by hitting repository endpoint, potentially narrowing it down to just the created_on attribute (I also included slug in this example):
https://api.bitbucket.org/2.0/repositories/atlassian/atlaskit-mk-2?fields=slug,created_on
However this will show when the repo has been created in Bitbucket which doesn't necessarily mean when the first commit was pushed to it or when that commit was created. The latter can be both before and after the repo creation date.
If you really need to find the date of the first commit, the only way to do that without cloning every repository is to traverse commit list till the end – until there's no next attribute in the response:
# Pseudocode
page = fetch('https://api.bitbucket.org/2.0/repositories/atlassian/atlaskit-mk-2/commits?pagelen=100&fields=next,values.date')
while(page.next) {
page = fetch(page.next)
}
first_commit_date = page.values.last().date
If cloning the repo is an option, you can run this command locally (assuming your repo is under Git):
git rev-list --max-parents=0 HEAD | xargs git log --pretty=format:'%ad'
Hope this helps. Let me know if you have any questions.
Cheers,
Daniil
Hi Daniil, thanks for your quick answer.
In my case, this call does not bring any data
https://api.bitbucket.org/2.0/repositories/atlassian/atlaskit-mk-2?fields=slug,created_on
Do you know if i have to add something else in my call ?
the creation date should be enough in this case.
Best,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I guess the problem is that you need to authenticate the request. That particular repository in my example is public, so you can just paste the URL into the browser, and it'll work (even in incognito mode). But for a private repository you need to authenticate the request (api.bitbucket.org doesn't allow session auth).
If that's not the issue, can you elaborate what do you actually get back?
Cheers,
Daniil
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Daniil Penkin, I have 10+ open branches in a repo, and if I want the first commit date for each branch, how can we get this using REST API?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.