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

Stash PreReceiveRepositoryHook, difference between branch add and commit add

Hugh McManus June 30, 2015

Hi everyone,

I'm attempting run some validation of files which have been added/changed in the repository and reject the push if the files are not valid. I have a working plugin, everything is fine however when creating a new branch all my files are attempted to be validated, regardless if they are changed on branch or not.

I am looking for a performance improvement and to only to check the files that have changed on the branch. The problem I'm facing is that when a branch is created and pushed to the repository, all the files seem to be of the ChangeType.ADD. I can't seem to differentiate between files which have been added on the branch and pushed and those which are just a branch create.

I'm using the following classes to determine if the the change types:

https://developer.atlassian.com/static/javadoc/stash.old-perms-pre-feb4/1.3.1/api/reference/com/atlassian/stash/content/Change.html#getType()

and the refChange which is passed to the onReceive method of the PreReceiveRepositoryHook.java:

https://developer.atlassian.com/static/javadoc/stash.old-perms-pre-feb4/2.0.1/api/reference/com/atlassian/stash/repository/RefChange.html

Has anyone any ideas how to do this?

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Balázs Szakmáry
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.
July 1, 2015

First of all, the ChangeType is about the Ref Change (i.e. branch/tag added/moved/deleted), not the commits or files in commits, but I guess you have figured this out already.

For making sure you only process the newly pushed commits, check out this recursive implementation. Lines 97-100 have the check where CommitIndex (newer verson of the now deprecated ChangesetIndex) is used to determine if a commit is newly pushed or not. The recursion goes from refChange.getToHash() backwards until it hits the first commit(s) which are already indexed. This ensures that only the newly pushed commits (and their parents) are visited in the graph.

Hugh McManus July 6, 2015

Hey Balazs, Good answer. I was able to complete what I wanted by following the code that you have posted. Thanks very much for your answer. I'm not sure how to accept your answer, but I think it was the best one. Thanks again, Hugh

Balázs Szakmáry
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.
July 6, 2015

Next to my name above on the left you should see a number with two arrows above/below and a grey checkmark below the lower arrow. Click that to accept the answer. (You need to be logged in to do this.)

Hugh McManus July 6, 2015

Done.

1 vote
JamieA
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.
June 30, 2015

IMHO the answer to this is in @Michael Heemskerk 's comment to my answer here: https://answers.atlassian.com/questions/15696071

 

Hugh McManus June 30, 2015

Hey Jamie, hmmm, I've found that this might be the better answer: https://answers.atlassian.com/questions/223143/how-to-get-only-new-changesets-in-pre-receive-hook-on-new-branch-push Sorry for not seeing this answer prior to posting this. Should I accept your answer or how should I close this question?

JamieA
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.
June 30, 2015

My understanding is that you can't use ChangesetIndex in a pre-receive hook.

Hugh McManus July 1, 2015

Yep, I understand that as the index hasn't yet been updated. However, is it possible to get the list of ref's on a pre-receive hook with the normal Stash API instead of mucking around with org.eclipse.jgit.lib.Repository? You can see an example of this here: https://github.com/sford/yet-another-commit-checker/blob/master/src/main/java/com/isroot/stash/plugin/ChangesetsServiceImpl.java#L84 On the method getBranches() I got all this from one of the comments on that link I posted previously by Bradley Baetz...

Hugh McManus July 1, 2015

What I'm getting at is that you could then exclude all the ref's that as the following: final ChangesetsBetweenRequest request = new ChangesetsBetweenRequest.Builder(repository) .exclude(getBranches(repository)) .include(refChange.getToHash()) .build(); Does that make any sense?

0 votes
Hugh McManus June 30, 2015

Oh yes, and I've also tried to use getPercentUnchanged to check if the file has been 100% changed or something but it seems to only work on ChangeType.MOVE, when it's ChangeType.ADD it seems to be -1. Shame, thought I had it with that one.

TAGS
AUG Leaders

Atlassian Community Events