Closed aperezdc closed 2 years ago
The suggestion of adding a CI build for this came from @donny-dont and I agree it is a very good idea.
The check-style
scripts and GitHub Actions config is shamelessly pulled from https://github.com/Igalia/cog/pull/279 (also by me) 📜
Introduce a script to check the coding style, and a CI worflow which runs it only on the changes introduced by a PR. The goal of checking only the changed code is to avoid having to run a full reformatting of the tree: instead, changes that modified existing code will slowly make the tree drift towards the desired status without introducing noise in the repository history.
There are is some trickery involved in getting this to work reliably:
The
clang-format-diff
helper script is installed at different locations depending on the distribution. Thecheck-style
script checks a few locations that cover popular GNU/Linux distributions (Ubuntu, Debian, Arch Linux, Fedora); it can be improved as we see fit in the future.The
actions/checkout
step defaults to shallow clones of the single commit which triggered a PR, which is enough to build, but not to find the differences between it and the base commit. To avoid forcing a full fetch of the history, the job uses a second instance of the action to fetch only the base tree in a separate directory.Even with both trees checked out, Git will refuse to calculate a diff because the commits are not linked to one another. But both trees are now checked out, so instead it is possible to use a plain
diff
and feed its output tocheck-style --diff
.As a bonus, the
check-style
script can be run by developers locally from the command line in order to verify their changes prior to filing merge requests, e.g:The output from the script is an unified diff, which can be piped back into
patch
orgit apply
to get the suggested changes applied to the tree: