Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

[Stash] PreCommit Hook accessing Files

Zeeshan Maqbool January 14, 2014

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?


FILE_NAMES=
FILE_CONTENT=
REPO=
LOG_MSG=
AUTHOR= com.atlassian.stash.history.

HistoryService.getChangeset(context.getRepository(), refId).getAuthor()
LAST_CHANGED_AUTHOR=
PARENT_BRANCH=com.atlassian.stash.pull.PullRequest.getFromRef()
BRANCH=
DIR=


I would greatly appreciate that.

thanks
Z

10 answers

1 accepted

0 votes
Answer accepted
cofarrell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 15, 2014

Hi Zeeshan,

FILE_NAMES=https://developer.atlassian.com/static/javadoc/stash/latest/api/reference/com/atlassian/stash/content/DetailedChangeset.html#getChanges%28%29

FILE_CONTENT=https://developer.atlassian.com/static/javadoc/stash/latest/api/reference/com/atlassian/stash/content/ContentService.html#streamFile%28com.atlassian.stash.repository.Repository,%20java.lang.String,%20java.lang.String,%20com.atlassian.stash.util.PageRequest,%20boolean,%20com.atlassian.stash.content.FileContentCallback%29

REPO=passed into the hook

LOG_MSG=https://developer.atlassian.com/static/javadoc/stash/latest/api/reference/com/atlassian/stash/content/Changeset.html#getMessage%28%29

LAST_CHANGED_AUTHOR=https://developer.atlassian.com/static/javadoc/stash/latest/api/reference/com/atlassian/stash/history/HistoryService.html#getChangeset%28com.atlassian.stash.repository.Repository,%20java.lang.String,%20java.lang.String%29

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.

https://bitbucket.org/atlassianlabs/stash-filesize-hook-plugin/src/b520fe77a2a5a2596d559f3358607721bcf6398a/src/main/java/com/atlassian/stash/plugin/filesize/FilesizeHook.java?at=master

Does that help?

Charles

0 votes
Zeeshan Maqbool January 29, 2014

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

cofarrell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 29, 2014

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

cofarrell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 29, 2014

Hi Zeeshan,

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

https://answers.atlassian.com/questions/188880/how-do-i-control-logging-in-an-atlassian-stash-plugin

Charles

0 votes
Zeeshan Maqbool January 29, 2014

That worked like a charm - thanksss alot

0 votes
Zeeshan Maqbool January 21, 2014

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.

cofarrell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 21, 2014

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?

Charles

0 votes
Zeeshan Maqbool January 21, 2014

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

cofarrell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 21, 2014

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?

0 votes
Zeeshan Maqbool January 15, 2014

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

0 votes
Zeeshan Maqbool January 15, 2014

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.

FILE_NAMES= ?
FILE_CONTENT= ?
REPO= ?
LOG_MSG= ?
AUTHOR= com.atlassian.stash.history. HistoryService.getChangeset(context.getRepository(), refId).getAuthor()
LAST_CHANGED_AUTHOR (for FILE_NAMES)= ?
PARENT_BRANCH=com.atlassian.stash.pull.PullRequest.getFromRef()
BRANCH= ?
DIR= ?

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

thanks

0 votes
cofarrell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 14, 2014

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?

Cheers,

Charles

0 votes
cofarrell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 14, 2014

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events