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 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