From d6db3333aa2c78851e0b1a37fd3125dbdfc78a2d Mon Sep 17 00:00:00 2001 From: Pavel Tikhomirov Date: Wed, 11 Jan 2023 11:15:37 +0300 Subject: [PATCH] clang-format: rework make indent to check specific commits Previousely "make indent" checked all files in criu source directory for codding style flaws. We have several problems with it: - clang-format default format sometimes changes in new versions of the package and we need to reformat all our code base each time it happens - on different systems we may have different versions of clang-format and on latest criu-dev "make indent" may be still unhappy on your system - when we want to update clang-format rules ourselves we need to update all our code base each time - sometimes clang-format rules are not fitting all our cases, (e.g.: an option IndentGotoLabels works nice for simple C code, but is a no go for assembler and C macros) and putting "clang-format off" everywhere is a mess - sometimes we intentionally want to break clang-format rules (e.g.: we want to put function arguments on a new line separating them "logically" not "mechanically" following 120-char rule like clang-format does). This adds a BASE option for "make indent" where all commits in range BASE..HEAD would be checked with git-clang-format for codding style flaws. For instance when developing on top of criu-dev, one can use "make BASE=origin/criu-dev indent" to check all their commits for compliance with the clang-format rules. Default base is HEAD~1 to make last commit checked when "make indent" is called. The closest thing to the old behaviour would then be "make indent BASE=init", note that only commited files would be checked. Extra options to git-clang-format may be passed through OPTS variable. Also reuse "make indent" in github lint workflow. Signed-off-by: Pavel Tikhomirov --- .github/workflows/lint.yml | 16 ++++++---------- Makefile | 4 +++- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 4c05285e6..0194c9393 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -23,16 +23,12 @@ jobs: - name: Run make indent run: > if [ -z "${{github.base_ref}}" ]; then - make indent + if ! make indent OPTS=--diff; then + exit 1 + fi else git fetch origin ${{github.base_ref}} && - git clang-format --style file --extensions c,h --quiet origin/${{github.base_ref}} - fi && - STATUS=$(git status --porcelain) && - if [ ! -z "$STATUS" ]; then - echo "FAIL: some files are not correctly formatted."; - echo "$STATUS" - git diff - echo "FAIL: please run 'make indent'"; - exit 1; + if ! make indent OPTS=--diff BASE=origin/${{github.base_ref}}; then + exit 1 + fi fi diff --git a/Makefile b/Makefile index 6bb1497b3..83ae7ca5a 100644 --- a/Makefile +++ b/Makefile @@ -461,8 +461,10 @@ fetch-clang-format: .FORCE $(E) ".clang-format" $(Q) scripts/fetch-clang-format.sh +BASE ?= "HEAD~1" +OPTS ?= "--quiet" indent: - find . -name '*.[ch]' -type f -print0 | xargs --null --max-args 128 --max-procs 4 clang-format -i + git clang-format --style file --extensions c,h $(OPTS) $(BASE) .PHONY: indent include Makefile.install