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

How to get the upcoming/current ChangeSet in a PreReceiveHook?

Christoffer Karlsson February 5, 2015

Hi,

(I'm kind of new to Stash/Git, but have experience in JIRA plugin development).

Basically I have this PreReceiveHook in a Stash plugin which starts off with looping the RefChanges:

for (RefChange refChange : refChanges)
		{
			changeSet = commitService.getChangeset(currentRepo, refChange.getRefId());
		}

This changeSet seems to be reflecting the latest commit already pushed to the master.

I thought the whole point of a Pre-Hook was to check the commit(s) I'm about to push. I don't want to check what has already been pushed.

 

So, is there a way to get the "upcoming" changeSet?

 

Regards

Christoffer

 

 

2 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.
February 5, 2015

You can use this:

final ChangesetsBetweenRequest request = new ChangesetsBetweenRequest.Builder(repository)
        .exclude(refChange.getFromHash()) //this is all 0's for RefChangeType.ADD
        .include(refChange.getToHash())
        .build();
final Page<Changeset> cs = commitService.getChangesetsBetween(request, PageUtils.newRequest(0, 9999));

for(Changeset changeset: cs.getValues())
{
    //false for new commits, true for old ones
    if (!commitIndex.isMemberOf(changeset.getId(), repository))
    {
        ...
    }
}

to do things only for the newly pushed commits. (commitIndex is an instance of the CommitIndex class, passed into the constructor, commitService similarly.)

Christoffer Karlsson February 5, 2015

Thank you very much Balazs! Just tried it together with the rest of my code and this is exactly what I was looking for. Actually, I did stumble upon the CommitIndex earlier, but I ignored it when I saw isMemberOf was deprecated. Anyway, I guess this is as good as it gets for now. Best regards Christoffer

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.
February 5, 2015

It implements the DeprecatedChangesetIndex interface (there used to be a similar class called ChangesetIndex), where almost all of the methods are deprecated, but this one is not.

0 votes
Michael Heemskerk
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 5, 2015

Hi Christoffer,

The git pre-receive-hook gets triggered after the objects (commits, files and trees) are received but before any of the refs (branches / tags) are updated. If you return false, no branches are updated and the received objects are immediately orphaned and will be deleted after the next garbage collection run. The idea behind this decision is that you may want to inspect the commit message or the modified files in your hook. To be able to do this, you need the objects.

Stash's PreReceiveHook follows the same logic. You get the changes that are about to be made. If you return false, these changes will not be made.

Christoffer Karlsson February 5, 2015

Hi Michael, Thanks for clarifying. I understood the concept of returning true or false, but did not understand how to get commit I was looking for. Balazs answer below was the code specific answer I was looking for though. Regards Christoffer

TAGS
AUG Leaders

Atlassian Community Events