It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

[Stash] PreCommit Hook accessing Files

We are planning to convert our SVN repo to Git via stash and we would like to convert all the existing shell-based hooks to Stash. Can someone tell what would be the best way to get these items in Stash for a Pre-commit hook?

AUTHOR= com.atlassian.stash.history.

HistoryService.getChangeset(context.getRepository(), refId).getAuthor()

I would greatly appreciate that.


10 answers

1 accepted

0 votes
Answer accepted

Hi Zeeshan,



REPO=passed into the hook



PARENT_BRANCH=(I'm not sure what you mean?)

BRANCH=passed into the hook

DIR=Not sure what you mean here? If you're talking about the Git directory you shouldn't need that if you're just trying to get the above information which you can from our APIs/

I have previously written a hook that checks the file size of everything in a push, which I think will cover most of these except the file content and how to wire them together.

Does that help?


Is it possible for you to paste your current scripts and/or provide a summary for what they do?

Hi Zeeshan,

Not quite sure what you've just pasted there. Are we talking a Java-based hook or git scripts? I'm assuming the former and you've added the Java methods that you already know?



Charles - You are correct, I am talking about Java-based hook in Stash. So, the idea is to convert our existing SVN-hooks written in shell/perl to Java but before we can start we need the variables to play with. The list that I pasted was the list of variables used by shell-scripts and if I get them in Java-based hook (for Stash) then I will be more than happy.

AUTHOR= com.atlassian.stash.history. HistoryService.getChangeset(context.getRepository(), refId).getAuthor()
DIR= ?

If you can provide info how to get variabes with '?' for a pre-commit Java-based hook that will work.


It seems that it will be a decent start, let me give it a quick try and see how it goes - thanks again

One more question, I am trying to access these(FILE_NAMES, FILE_CONTENTS ,...) values before the commit has happened to repository, is it possible in stash ? I think the API calls you gave me works for post-commit not pre-commit

The calls should work regardless of whether the hook is pre or post receive, the only difference is that the pushed refs haven't _moved_ yet. The fromHash and toHash will stay the same and will exist in the repository regardless.

Does that make sense?

When I am calling FILE_NAMES/FILE_CONTENTS/... and other variables I am trying to access the data which is getting committed from the developer in "THIS" commit which hasnt been committed yet (since we are in pre-rcv state), is that possible ?

I agree that calls will work in all cases but I can only access OLD commits, not the data related to CURRENT commit which hasnt been committed yet.

Hi Zeeshan,

Ok, just to make sure we're on the same page:

1. Developer makes changes locally

2. Developer commits changes locally

3. Developer pushes changes to Stash

4. Stash accepts or rejects changes basd on pre-receive commit

You said "hasnt been committed yet". The only time that happens is before step 2 - as the developer is committing. Stash obviously can't stop this from happening, as they're working on their machine. You can use Git pre-commit hooks but that requires some mechanism to install and manage them, something Stash can't help with. One of the advantages of using pre-receive hooks is that it's central and can be controlled in one place.

On the Stash team we use both types, and check the pre-commit hooks into a repository and ask developers to symlink to them once so that they get updated over time.

If you actually mean "hasn't been accepted in Stash yet" then we're still talking about a pre-receive commit. In that case the commits are technically _in_ the Git repository on the server, but the refs (branches) haven't been updated yet. This allows you to look at the contents of the commits as long as you don't expect the refs to have updated yet, which is what I was suggesting earlier with toHash and fromHash.

Let me know if that helps?


That worked like a charm - thanksss alot

On a different tangent, do you know how I can generate a separate log for my plugin, an example code would be highly appreciated.

Apologies, you've already done it... :)

Hi Zeeshan,

You mind creating a separate Question so we can track it more easily? Although maybe see if this helps first:


Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Bitbucket

Powering DevOps with Bitbucket Server & Data Center

Hi everyone, The Cloud team recently announced 12 new DevOps features that help developers ship better code, faster   ! While we’re all excited about the new improvements to Bitbucket ...

1,519 views 0 6
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you