How to get the commits that were pushed?

alex1312 May 25, 2015

I'm attempting to write a pre-receive hook in Stash 3.9. I have some problems to retreive the commits that were pushed. This is what i've done so far:

CommitsBetweenRequest.Builder commitsBetweenBuilder = new CommitsBetweenRequest.Builder(context.getRepository()) ;
commitsBetweenBuilder.exclude(refChange.getFromHash());
commitsBetweenBuilder.include(refChange.getToHash());
PageRequest pageRequest = new PageRequestImpl(0,125);
CommitsBetweenRequest request =  commitsBetweenBuilder.build();

I'm having some problems with the commitService.

Please help me to understand how should proceed.

 

Thanks, 

Alex

 

 

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
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.
May 25, 2015

After this, you need to use getCommitsBetween.

 

Something like this:

final CommitsBetweenRequest request = new CommitsBetweenRequest.Builder(repository)
        .exclude(refChange.getFromHash()) //this is all 0's for RefChangeType.ADD
        .include(refChange.getToHash())
        .build();
final Page<Commit> cs = commitService.getCommitsBetween(request, PageUtils.newRequest(0, 9999));
for(Commit commit: cs.getValues())
{
    //false for new commits, true for old ones
    if (!commitIndex.isMemberOf(commit.getId(), repository))
    {
        //do stuff;
    }
}
alex1312 May 25, 2015

In which package can i find "PageUtils". By the way, what is the the commitIndex ? I don't really if the "cs" Page is showing to me the current commits that user is about to push or it's showing to me the whole history ?

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.
May 25, 2015

PageUtils is com.atlassian.stash.util.PageUtils (for some reason, I cannot find it here: https://developer.atlassian.com/static/javadoc/stash/latest/api/reference/classes.html) CommitIndex: https://developer.atlassian.com/static/javadoc/stash/latest/api/reference/com/atlassian/stash/idx/CommitIndex.html ; You need to pass it in as a parameter to the constructor, like this: public YourHook(final CommitService commitService, final CommitIndex commitIndex, final NavBuilder navBuilder) {this.commitService = commitService; this.commitIndex = commitIndex; this.navBuilder = navBuilder;} cs will contain more than the freshly pushed commits, because it has all the commits whose ref information has changed. eg. for a new branch with no unique commits, it will only contain pre-existing commits. (All the commits than now belong to this branch as well.) These are filtered out with the help of CommitIndex.

alex1312 May 26, 2015

Great! I have another question what can i do with the "NavBuilder" ? Last but not least, do you know how can i ammend a commit message, and also what is the process of getting fields from a specific issue on Jira, before we've started working with Stash, i've used simple bash scripts and just used the REST API of Jira, but i'm sure that there is a better way, to get some fields from Jira issues. Thanks, Alex

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.
May 27, 2015

NavBuilder is used for creating URL's pointing to Stash pages: https://developer.atlassian.com/static/javadoc/stash/latest/api/reference/com/atlassian/stash/nav/NavBuilder.html No idea about the Jira part.

TAGS
AUG Leaders

Atlassian Community Events