In gitlab CI for merge requests I want to create file changes.diff that summarizes changes introduced by this merge request.
Some context:
I have CI that verifies (using clang-format-diff) if merge request (MR) introduces some formatting deviations from .clang-format style and looks for new #if 0. Reformatting and cleaning whole project is on wishlist, but won't happen soon. But when we are on the bottom, it is good idea to at least stop digging.
So far I have following script:
oldrev=$(git rev-list HEAD --grep="Merge branch '.*' into 'master'" |head -n1)
newrev=${CI_MERGE_REQUEST_SOURCE_BRANCH_SHA:-$(git rev-parse HEAD)}
# 1 Find what files changed, consider only added, copied, modified (ACM), not removed, moved
# 2 ignore files with extensions that suggest its not code
files=$(git diff-tree --no-commit-id --name-only --diff-filter=ACM -r $oldrev $newrev | egrep "\.(c|cpp|cxx|cc|h|hpp|ipp|tpp)$")
if [[ -z "${files}" ]]; then
echo "nothing interesting changed"
exit 0
fi
echo "Changed files:"
echo $files
echo $files | xargs git diff -U0 --no-color $oldrev $newrev -- > changes.diff
This works fine in simple cases:
- topic branch was forked from master
- there were no merges from master to topic (sometimes necessary for conflict resolution before merge to master)
If either of those don't hold, changes.diff starts holding changes introduced by other MRs. How can I improve this script?