From 3253853bde745d5474a2c5519e75c56bca7a77cc Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Thu, 22 Jun 2023 23:47:31 +0800 Subject: [PATCH 1/2] feat: introduce pre-commit to lint code --- .github/workflows/build-test-publish.yml | 2 +- .hadolint.yaml | 4 ---- .pre-commit-config.yaml | 23 +++++++++++++++++++++++ 13/sources.list.jammy | 2 +- 14/sources.list.jammy | 2 +- 15/sources.list.kinetic | 2 +- Makefile | 7 +++---- README.md | 4 ++-- all/Dockerfile | 4 ++-- all/sources.list.jammy | 2 +- demo/compile_commands.json | 2 +- 11 files changed, 36 insertions(+), 18 deletions(-) delete mode 100644 .hadolint.yaml create mode 100644 .pre-commit-config.yaml diff --git a/.github/workflows/build-test-publish.yml b/.github/workflows/build-test-publish.yml index fd11bc8..e657e66 100644 --- a/.github/workflows/build-test-publish.yml +++ b/.github/workflows/build-test-publish.yml @@ -40,7 +40,7 @@ jobs: elif [[ "$tag" == *"alpine"* ]]; then docker run clang-tools:$tag clang-format --version | grep -E "clang-format version" docker run clang-tools:$tag clang-tidy --version | grep "LLVM version" - else + else docker run clang-tools:$tag clang-format --version | grep -E "clang-format version $tag" docker run clang-tools:$tag clang-tidy --version | grep "LLVM version $tag" fi diff --git a/.hadolint.yaml b/.hadolint.yaml deleted file mode 100644 index 002e0bc..0000000 --- a/.hadolint.yaml +++ /dev/null @@ -1,4 +0,0 @@ -ignored: - - DL3008 - - DL3018 - - SC2086 \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..2d7241d --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,23 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.3.0 + hooks: + - id: check-yaml + - id: end-of-file-fixer + - id: trailing-whitespace + - id: check-added-large-files + - id: detect-private-key + - repo: https://github.com/hadolint/hadolint + rev: v2.12.0 + hooks: + - id: hadolint-docker + args: + [ + --ignore,DL3008, + --ignore,DL3018, + --ignore,SC2086, + ] + - repo: https://github.com/shellcheck-py/shellcheck-py + rev: v0.9.0.5 + hooks: + - id: shellcheck diff --git a/13/sources.list.jammy b/13/sources.list.jammy index 789252a..f9510d3 100644 --- a/13/sources.list.jammy +++ b/13/sources.list.jammy @@ -39,4 +39,4 @@ deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted deb http://security.ubuntu.com/ubuntu/ jammy-security universe # deb-src http://security.ubuntu.com/ubuntu/ jammy-security universe deb http://security.ubuntu.com/ubuntu/ jammy-security multiverse -# deb-src http://security.ubuntu.com/ubuntu/ jammy-security multiverse \ No newline at end of file +# deb-src http://security.ubuntu.com/ubuntu/ jammy-security multiverse diff --git a/14/sources.list.jammy b/14/sources.list.jammy index 789252a..f9510d3 100644 --- a/14/sources.list.jammy +++ b/14/sources.list.jammy @@ -39,4 +39,4 @@ deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted deb http://security.ubuntu.com/ubuntu/ jammy-security universe # deb-src http://security.ubuntu.com/ubuntu/ jammy-security universe deb http://security.ubuntu.com/ubuntu/ jammy-security multiverse -# deb-src http://security.ubuntu.com/ubuntu/ jammy-security multiverse \ No newline at end of file +# deb-src http://security.ubuntu.com/ubuntu/ jammy-security multiverse diff --git a/15/sources.list.kinetic b/15/sources.list.kinetic index 0795ff7..647ae45 100644 --- a/15/sources.list.kinetic +++ b/15/sources.list.kinetic @@ -39,4 +39,4 @@ deb http://security.ubuntu.com/ubuntu/ kinetic-security main restricted deb http://security.ubuntu.com/ubuntu/ kinetic-security universe # deb-src http://security.ubuntu.com/ubuntu/ kinetic-security universe deb http://security.ubuntu.com/ubuntu/ kinetic-security multiverse -# deb-src http://security.ubuntu.com/ubuntu/ kinetic-security multiverse \ No newline at end of file +# deb-src http://security.ubuntu.com/ubuntu/ kinetic-security multiverse diff --git a/Makefile b/Makefile index 31fbbf2..3153602 100644 --- a/Makefile +++ b/Makefile @@ -70,8 +70,7 @@ prune: ## clean all that is not actively used @docker system prune -af @echo "== Prune ✅ image(s) Succeeded" -lint: check-file ## Lint the $(DOCKERFILE) content - @echo "== Linting $(DOCKERFILE)..." - @echo "Output Lint results" - @docker run --rm -i -v $$PWD/.hadolint.yaml:/.config/hadolint.yaml hadolint/hadolint hadolint - < $(DOCKERFILE) +lint: ## Lint code + @echo "== Running pre-commit against all-files..." + @pre-commit run --all-files @echo "== Lint ✅ Succeeded" diff --git a/README.md b/README.md index f7efe87..7f12045 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ $ docker run -v $PWD:/src xianpengshen/clang-tools:12 clang-format --dry-run -i $ docker run xianpengshen/clang-tools:12 clang-tidy --version LLVM (http://llvm.org/): LLVM version 12.0.0 - + Optimized build. Default target: x86_64-pc-linux-gnu Host CPU: cascadelake @@ -83,7 +83,7 @@ $ docker run clang-tools clang-tidy helloworld.c \ -checks=boost-*,bugprone-*,performance-*,readability-*,portability-*,modernize-*,clang-analyzer-cplusplus-*,clang-analyzer-*,cppcoreguidelines-* ``` -### Use specific tag in `Dockerfile` +### Use specific tag in `Dockerfile` If you want to use a specific tag of clang-tools, you can build image by passing `--build-arg` diff --git a/all/Dockerfile b/all/Dockerfile index bba1bf7..b643bfe 100644 --- a/all/Dockerfile +++ b/all/Dockerfile @@ -10,7 +10,7 @@ RUN set -ex \ && apt-get update \ && for CLANG_VERSION in $CLANG_VERSIONS; do \ apt-get --no-install-recommends -y install clang-format-$CLANG_VERSION clang-tidy-$CLANG_VERSION; done \ - && rm -rf /var/lib/apt/lists/* + && rm -rf /var/lib/apt/lists/* ENV CLANG_VERSIONS="16 15 14 13" @@ -26,7 +26,7 @@ RUN set -ex \ ./llvm.sh $CLANG_VERSION \ && apt-get --no-install-recommends -y install clang-format-$CLANG_VERSION clang-tidy-$CLANG_VERSION; done \ && rm llvm.sh \ - && rm -rf /var/lib/apt/lists/* + && rm -rf /var/lib/apt/lists/* # Integrity testing diff --git a/all/sources.list.jammy b/all/sources.list.jammy index 789252a..f9510d3 100644 --- a/all/sources.list.jammy +++ b/all/sources.list.jammy @@ -39,4 +39,4 @@ deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted deb http://security.ubuntu.com/ubuntu/ jammy-security universe # deb-src http://security.ubuntu.com/ubuntu/ jammy-security universe deb http://security.ubuntu.com/ubuntu/ jammy-security multiverse -# deb-src http://security.ubuntu.com/ubuntu/ jammy-security multiverse \ No newline at end of file +# deb-src http://security.ubuntu.com/ubuntu/ jammy-security multiverse diff --git a/demo/compile_commands.json b/demo/compile_commands.json index bd90fb2..d3d5c7b 100644 --- a/demo/compile_commands.json +++ b/demo/compile_commands.json @@ -4,4 +4,4 @@ "command": "/usr/bin/g++ -Wall -Werror helloworld.cpp", "file": "/helloworld.cpp" } -] \ No newline at end of file +] From d50a1012bb8e9084db6f0e666132c4feec8278a1 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Thu, 22 Jun 2023 23:51:48 +0800 Subject: [PATCH 2/2] fix: install pre-commit pkg and hook --- .github/workflows/build-test-publish.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build-test-publish.yml b/.github/workflows/build-test-publish.yml index e657e66..ca257cc 100644 --- a/.github/workflows/build-test-publish.yml +++ b/.github/workflows/build-test-publish.yml @@ -20,6 +20,7 @@ jobs: uses: tj-actions/changed-files@v18.4 - name: Build & Test (Publish) run: | + pip install pre-commit && pre-commit install docker login -u '${{ secrets.DOCKER_USERNAME }}' -p '${{ secrets.DOCKER_PASSWORD }}' for file in ${{ steps.changed-files.outputs.all_changed_files }}; do if [[ "$file" == *"Dockerfile" ]]; then