Commit Messages and it's Validation

Hi All,

I want to apply some validation on commit message.

How can I achieve this.

I got one sample in github i.e.,

there they used for jira services so I thought it not suited for my requirement because some of the classes I'm not able to import.

I'm using stash server and atlas-sdk. created pre repository hook. and I modified for getting commit message like below in my class.

public class MyPreReceiveRepositoryHook implements PreReceiveRepositoryHook

private CommitService commitService;

public MyPreReceiveRepositoryHook(CommitService commitService) {
this.commitService = commitService;

public boolean onReceive(RepositoryHookContext context, Collection<RefChange> refChanges, HookResponse hookResponse)

final ChangesetsBetweenRequest request = new ChangesetsBetweenRequest.Builder(context.getRepository()) .exclude(((RefChange)refChanges).getFromHash()).include(((RefChange)refChanges).getToHash()) .build();

final Page<Changeset> cs = commitService.getChangesetsBetween(request, new PageRequestImpl(0, PageRequest.MAX_PAGE_LIMIT));

String commitMessage;

for(Changeset changeset: cs.getValues())
commitMessage = changeset.getMessage();


return true;

With this I want to see the commit messages but I got this error on Git(client console).

remote: Hook com.konylabs.stash.plugin.hook.MyPreReceiveRepositoryHook failed. Error:
remote: cannot be cast to com.atlassian.stash.repository.RefChange



3 answers

1 accepted

This widget could not be displayed.

The reason for getting the exception is that here:
final ChangesetsBetweenRequest request = new ChangesetsBetweenRequest.Builder(context.getRepository()) .exclude(((RefChange)refChanges).getFromHash()).include(((RefChange)refChanges).getToHash()) .build();
You try to cast a Collection<RefChange> to a single RefChange.

Instead, you need to iterate over the collection like this:

for(RefChange refChange: refChanges)
	ChangesetsBetweenRequest request = new ChangesetsBetweenRequest
	[do the rest of the stuff here]

Thanks Balazs for correction.

Hi Anil,

I am the author of Yet Another Commit Checker. I just saw your question... what classes are you not able to import that would prevent you from using YACC? I am just wondering if there is something I can do to improve my plugin. Thanks!


Hi Sean,

Glad to got the reply from you.

Currently I'm not able to import this one


And one doubt is.

A)For suppose If I create a repo and pushed some changes.

B)Later I enabled this hook so here it will validate for current messge(Whether the user provides ticket number or not). Will it check the pushed changes of A or not.

Earlier when I used,

ChangesetsBetweenRequest request = new ChangesetsBetweenRequest.Builder

( context.getRepository()).exclude(refChange.getFromHash()) .include


Some times I found that, "refChange.getFromHash() given SHA-ID

0000000000000000000000000...00000". then it looked for all the changesets.

When I checkd the latest yacc plugin..I saw ChangesetsBetweenRequest request =

new ChangesetsBetweenRequest.Builder(repository).exclude(getBranches


So with latest plugin will my problemsolve..I dont want to check the earlier pushed changes.



This widget could not be displayed.
Timothy Chin Community Champion Mar 20, 2014

I am supprised that your code actually compiled with a cast exception.

You should also take a look at this example (

Thanks Timothy Chin, Provided sample helped.

This widget could not be displayed.


Is there any way to get only latest commit message. 


Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Friday in Bitbucket

What is your #1 piece of advice to new users getting started using Bitbucket?

Hello Community! My name is Claire Maynard, and I’m a Product Marketing Manager on the Bitbucket team. I’m interested in hearing what advice or tips you have for new users getting started on ...

169 views 9 2
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