diff --git a/.github/scripts/clang-format-hook b/.github/scripts/clang-format-hook new file mode 100755 index 0000000..731de08 --- /dev/null +++ b/.github/scripts/clang-format-hook @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +# Thin wrapper around clang-format for easier to parse output from the +# pre-commit hook. +# +# Needs to work with multiple input files as pre-commit passes multiple files to +# the "executables" + +# Make sure that diff is actually recent enough (diffutils >= 3.4) to support +# colored output +COLOR_OUTPUT=$(diff --color=always <(echo) <(echo) > /dev/null 2>&1 && echo "--color=always") + +success=0 +for file in ${@}; do + if ! $(clang-format --style=file --Werror --dry-run ${file} > /dev/null 2>&1); then + echo "Necessary changes for: '${file}' (run 'clang-format --style=file -i ${file}' to fix it)" + diff ${COLOR_OUTPUT} -u ${file} <(clang-format --style=file ${file}) | tail -n +3 + success=1 + fi +done +exit ${success} diff --git a/.github/templates/apache-2.tmpl b/.github/templates/apache-2.tmpl new file mode 100644 index 0000000..f529936 --- /dev/null +++ b/.github/templates/apache-2.tmpl @@ -0,0 +1,16 @@ +Copyright (c) ${years} ${owner}. + +This file is part of ${projectname}. +See ${projecturl} for further info. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/.github/workflows/clang-format-check.yml b/.github/workflows/clang-format-check.yml deleted file mode 100644 index 0fe3a55..0000000 --- a/.github/workflows/clang-format-check.yml +++ /dev/null @@ -1,21 +0,0 @@ -name: clang-format-check - -on: [push, pull_request] - -jobs: - clang-format: - runs-on: ubuntu-latest - container: debian:stable-slim - steps: - - - name: Install clang-format - run: | - apt-get update - apt-get install -y curl git - curl -SL https://github.com/muttleyxd/clang-tools-static-binaries/releases/download/master-208096c1/clang-format-14_linux-amd64 > /usr/local/bin/clang-format - chmod +x /usr/local/bin/clang-format - - - uses: actions/checkout@v2 - - - name: Run format - run: ci/format.sh . diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml new file mode 100644 index 0000000..68a1db8 --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -0,0 +1,23 @@ +name: pre-commit + +on: [push, pull_request] + +jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: cvmfs-contrib/github-action-cvmfs@v3.1 + - uses: aidasoft/run-lcg-view@v4 + with: + release-platform: LCG_101/x86_64-centos7-clang12-opt + run: | + export PYTHONPATH=$(python -m site --user-site):$PYTHONPATH + export PATH=/root/.local/bin:$PATH + pip install --upgrade --user pip + pip install pre-commit --user + # Use virtualenv from the LCG release + pip uninstall --yes virtualenv + pre-commit run --show-diff-on-failure \ + --color=always \ + --all-files diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..3205f8e --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,25 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.4.0 + hooks: + - id: mixed-line-ending + - id: trailing-whitespace + exclude: (doc/ReleaseNotes.md) + - repo: local + hooks: + - id: clang-format + name: clang-format + entry: .github/scripts/clang-format-hook + types: [c++] + language: system + - repo: https://github.com/johann-petrak/licenseheaders.git + rev: 'v0.8.8' + hooks: + - id: licenseheaders + args: ["-t", ".github/templates/apache-2.tmpl", "-y2014-2023", + "-o", "Key4hep-Project", + "-n", "Key4hep", + "-u", "https://key4hep.github.io/key4hep-doc/", + "-x", ".github/*", ".pre-commit-config.yaml", "README.md", + "doc/ReleaseNotes.md", + "-f"] \ No newline at end of file diff --git a/ci/format.sh b/ci/format.sh deleted file mode 100755 index 4f68028..0000000 --- a/ci/format.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -files=$(find $1 -type f -name "*.cpp" -or -name "*.h" -or -name "*.hpp" -or -name "*.cxx") - -clang-format -Werror --verbose -i --style=file ${files} - -git diff --exit-code --stat