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
Community moderators have prevented the ability to post new answers.
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.)
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.