getFromHash() from RefChange class return '000000000000'

Hello,

I developped a plugin for our Bitbucket Instance. I use the getFromHash() method from the RefChange class.

When using it in my plugin, i, and it’s kind of logic, has the attached stackTrace

com.atlassian.bitbucket.commit.NoSuchCommitException: Commit '0000000000000000000000000000000000000000' does not exist in repository 'databases'.

I found a few links with people having the same Issue

Aside a simple and not so elegant correction such as this one

if (optionalRefChange.get().getFromHash().equals("0000000000000000000000000000000000000000")) {
	doSomething();
} else {
    doAnotherThing();
}

does anyone has an idea about why is the method bugged and how to solve it ?

The 0000000000000000000000000000000000000000 is used as the fromHash for newly created branches or tags to indicate ‘does not exist’. Likewise, the same zero-hash used as the toHash for branch/tag deletions.

The best way to prevent the NoSuchCommitException is to inspect refChange.getType():

if (optionalRefChange.get()getType() == RefChangeType.UPDATE) {
    doSomething();
} else {
    doSomethingElse();
}
1 Like

Thanks for the suggestion it worked.

RefChange and RefChangeType documentations could benefit from this additional information as it seems i am not the only one with the issue (see link above)

1 Like