How to access the commit message in pre-receive hook

Hi,

I am attempting to write a pre-recieve hook in Stash 2.2 where I check the formatting of the commit message. I have been unsuccessful at accessing the message and am hoping I could get some guidance or even an example.

Thanks,

-Jim

1 answer

1 accepted

This widget could not be displayed.
Seb Ruiz Atlassian Team Mar 06, 2013

Hi Jim,

These are the steps involved for what you need:

  1. Create a PreReceiveRepositoryHook as detailed here: https://developer.atlassian.com/stash/docs/latest/how-tos/repo-hook-examples/pre-receive-no-config.html
  2. Iterate over the RefChanges (as shown in the example above). The pre receive hook does not receive a collection of changesets, but rather a list of the refs that have changed. There can be multiple refs that have changed in one push (eg pushing multiple commits to multiple branches at once)
  3. For each RefChange, use the fromHash and toHash to extract the individual changesets: https://developer.atlassian.com/static/javadoc/stash/latest/api/reference/com/atlassian/stash/repository/RefChange.html
  4. Using HistoryService.getChangesetsBetween stream a list of changesets from RefChange.getFromHash and RefChange.getToHash
  5. For each of these changesets, validate the message as you need.

Hope that helps,

Seb

This solution works, but the use of HistoryService.getChangesetsBetween with fromHash and toHash is deprecated in between.

The recommended ChangeSetBetweenRequest does not seem to provide a similar 'fromHash' - 'toHash' mechanism. We added fromHash to builder.include(), but this included everything. Using toHash as builder.exclude() in turn excluded everything.

Any hints on how to proceed?

Hi Jan,

While the terminology might seem a little strange, you want to reverse those arguments. You want to exclude the fromHash and include the toHash. That is exactly what the deprecated method is doing. This gives you a might more powerful way to include/exclude multiple changesets, rather than just a single from/to pair.

Cheers,

Charles

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 21, 2018 in Bitbucket

Branch Management with Bitbucket

As a project manager, I have discovered that different developers want to bring their previous branching method with them when they join the team. Some developers are used to performing individual wo...

1,367 views 9 11
Read article

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