Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

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

Bitbucket - pre-receive hook - How to get changes in the file between the commits (delta changes)

VKPuduran
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 28, 2020

Hi, I am writing bitbucket server side pre-receive hook. I need to verify certain @Annotation element is present in the java file.I am successful in getting the file contents.But, it is always returning the complete file content.I am interested in the delta changes, meaning the changes between the previous commit to current commit (similar to git diff).Not sure, how to get that using the SDK api's.

@Override
public boolean onReceive(RepositoryHookContext context, Collection<RefChange> refChanges, HookResponse hookResponse)
{
Repository repository = context.getRepository();

final Map<String, ByteArrayOutputStream> filesStreamsToHash = new HashMap();

final Map<String, String> fileContents = new HashMap();

boolean isValidCheckIn = true;

refChanges.stream().forEach(refChange -> {

final ChangesRequest changesreq = new ChangesRequest.Builder(repository, refChange.getToHash()).sinceId(refChange.getFromHash()).build();

commitService.streamChanges(changesreq, change -> {

String fileName = change.getPath().toString();

//get the changes for model classes only
if (fileName.startsWith(ModelPath)) {
//want to have changes between changesreq.getUntilId() and changesreq.getSinceId() ONLY
contentService.streamFile(repository, changesreq.getUntilId(), fileName, arg0 -> {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
filesStreamsToHash.put(fileName, baos);
return baos;
});


}

return true;
});
});

 

Any idea would be appreciated.

 

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events