[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

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 Join to answer
Community showcase
Jason Wong
Published 4 hours ago in Agility Beta

Welcome to agility

Every team in the world is unique, and so   Atlassian believes   that each and every team's best way of working  needs to  be molded to their unique circumstances  – ...

31 views 1 5
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot