I have created a repository hook on Bitbucket that automatically creates a pull request from one branch to another (and merge it) when some specific conditions are fulfilled. I would like to detect the case where the result of the merge is that no files will be modified (empty merge).
What I tried so far:
To check if there is commits on source branch that are not on target branch :
CommitsBetweenRequest commitsBetweenRequest = new CommitsBetweenRequest .Builder(repository) .include(sourceBranch) .exclude(targetBranch) .build();Boolean anyChanges = commitService .getCommitsBetween(commitsBetweenRequest, new PageRequestImpl(0, 1)) .stream() .anyMatch();
This works great for simple cases. However, some "empty merges" are still undetected. This occurs when files modified on source branch have also been modified on target branch the same way, but in different commits.
FB1 -- FB2 (source) / / ---- A ---- B ---- C (target)
getCommitsBetween()
will return FB1 and FB2.However, if files modified in FB1 and FB2 have been modified in C as well (the same way) the result of the merge is no changes at all.
Checking the difference between branches in terms of file changes :
//I have tried to invert sourceBranch and targetBranch, without successChangesetsRequest changesRequest = new ChangesetsRequest .Builder(request.getRepository(), sourceBranch) .sinceId(targetBranch) .build();commitService.getChangesets(changesRequest, new PageRequestImpl(0, 1));
It does not give expected results. It give me the list of files modified in both source and target branch (not the files modified as the result of the merge).