Skip to content

Conversation

@aperezdc
Copy link
Contributor

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. The check-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 to check-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:

git switch -c my-feature
$EDITOR src/loader.c
git commit -m'Awesome improvements'
scripts/check-style master

The output from the script is an unified diff, which can be piped back into patch or git apply to get the suggested changes applied to the tree:

scripts/check-style master | git apply -p0 -
git add -p && git commit --amend

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. The "check-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 to "check-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:

  % git co -b my-feature
  % $EDITOR cog.c
  % git ci -m'Awesome improvements'
  % scripts/check-style master

The output from the script is an unified diff, which can be piped back
into "patch" or "git apply" to get the suggested changes applied to the
tree:

  % scripts/check-style master | git apply -p0 -
  % git add -p && git commit --amend
@aperezdc
Copy link
Contributor Author

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 Igalia/cog#279 (also by me) 📜

@aperezdc aperezdc mentioned this pull request Mar 24, 2022
@aperezdc aperezdc merged commit 4a760fa into master Mar 24, 2022
@aperezdc aperezdc deleted the aperezdc/clangformat branch March 24, 2022 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants