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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root


1 badge earned


Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!


Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.


Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!


Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

Script runner pre commit hook reading change content


Hi, I am writing a script runner pre commit hook and need to validate that the file being committed contains certain string. and reject the push if it does not. 

I know how to stream a file using ContentService if the file has already been committed into the repository but it doesn’t seem to work with newly added files. 

When I add new file to but bucket I don’t see how to read the content of newly added file. 

// When iterating through changes as below, I get to the change where new file that needs to be validated got added 

def collector = { Iterable<Change> changes -> changes.each { change -> validate(change.path.toString()) } }

pathsMatchExcludingDeletes(“glob:**.txt”, collector)

// and them validate in that file 

def validate(path) {

   contentService.streamFile(repo, contentId, path, supplier) // this will throw “The path xxx does not exist at revision yyy


If I attempt to read the file using path provided by the change.path it will throw the exception “The path xxx does not exist at revision yyy” 

I assume this is because file is only getting added and does not yet exist in the repository at the time pre hook runs.

How can I read content of the change for the newly added files?

1 answer

2 votes
Max Lim _Adaptavist_
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.
Dec 08, 2021

I think, as I have tested previously, ContentService only read the current content of the file, instead of the new changes being pushed.

So, you can use CompareService for checking the changes (diff's). Following snippet will also work for newly added file:

import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.bitbucket.content.AbstractDiffContentCallback
import com.atlassian.bitbucket.content.DiffSegmentType
import com.atlassian.bitbucket.content.ConflictMarker

def compareService = ComponentLocator.getComponent(CompareService)

refChanges.each { refChange ->
        new CompareDiffRequest.Builder()
            .fromRef(new CompareRef(refChange.toHash, repository))
            .toRef(new CompareRef(refChange.fromHash, repository))
        new AbstractDiffContentCallback() {
            boolean isLineChanged = false
            void onSegmentStart(DiffSegmentType type) {
                isLineChanged = type == DiffSegmentType.ADDED
            void onSegmentLine(String line, ConflictMarker marker, boolean truncated) {
                if (isLineChanged) {
//check the contents of the line log.warn "changed or new line is: " + line } } } ) }

This snippet will also soon be available in our library, be sure to check out for more useful example there 😀.

Robert Giddings [Adaptavist]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jan 19, 2022

Hi @Mark ,

Further to @Max Lim _Adaptavist_ previous answer, we have now published a Script to our Adaptavist Library that shows an example of blocking commits based on file content.

The Script can be found here:

Kind regards,

Robert Giddings,

Product Manager,

ScriptRunner for Bitbucket

Max, Robert - thank you for your answers. 

I have used Compare service as suggested but it all turns out quite tricky to use. 

To meet the requirement of my pre commit hook I need to be able to take a file that is being pushed (added or edited) and then validate every line of this file as it would be seen in the repository if the push was successful. 


The problem with using CompareService and looking at changes instead of the whole file is that I don't actually get every line in the streamed diff. Lines that haven't changed are not included. Changes include added and deleted lines, deleted files also need special handling etc.. It's doable but makes it complicated to use for my need. 

After struggling with CompareService I came back to this post and saw response from Robert, this looks promising but I have a question

When you do this: 

contentService.streamFile(repository, filePaths[filePath], filePath) { fileStream }


Isn't it the case that contentService.streamFile will only look at the state of the file as it is currently in the repository, hence ignoring all changes brought by the commits being pushed? Which is my main problem and what Max also mentioned at the beginning of his response. 

Thanks in advance for looking into this, I find ScriptRunner very powerful, apart from some beginner's issues like this. 



Max Lim _Adaptavist_
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.
Apr 05, 2022

Hi Mark,

The library script that uses ContentService does work.

When I tested it previously, I passed:

contentService.streamFile(repository, branchRefId, filePath)

So, it only retrieve the current content.

With the new library script, filePaths[filePath] returns the commit id. So, it will retrieve the pushed content.

Sorry for the mixed up earlier.

Suggest an answer

Log in or Sign up to answer
AUG Leaders

Atlassian Community Events