Skip to content
Snippets Groups Projects
Commit 57379798 authored by Marco Clemencic's avatar Marco Clemencic
Browse files

Use pre-commit to check C++ and Python formatting

See merge request !1261
parents 9197c46a 2ccc1d60
No related branches found
No related tags found
1 merge request!1261Use pre-commit to check C++ and Python formatting
Pipeline #3121772 passed
......@@ -112,4 +112,8 @@ SpacesInParentheses: true
Standard: Cpp11
TabWidth: 8
UseTab: Never
---
Language: ObjC
# Don't format Objective-C, Objective-C++ files.
DisableFormat: true
...
......@@ -226,14 +226,44 @@ x86_64-centos7-gcc10-opt:check-unused:
- ci-utils/check-sources build/build.ninja
allow_failure: true
check-formatting:
pre-commit-checks:
stage: pre-build-checks
image: gitlab-registry.cern.ch/gaudi/gaudi/format-checker:latest
image: registry.cern.ch/docker.io/library/python:3.8
variables:
PRE_COMMIT_HOME: ${CI_PROJECT_DIR}/.cache/pre-commit
cache:
paths:
- ${PRE_COMMIT_HOME}
before_script:
- |
python -m venv ${CI_PROJECT_DIR}/.cache/pre-commit-venv
. ${CI_PROJECT_DIR}/.cache/pre-commit-venv/bin/activate
pip install pre-commit
git fetch origin $TARGET_BRANCH
git config user.name "Gitlab CI"
git config user.email "noreply@cern.ch"
script:
- ci-utils/check-formatting
- |
if ! pre-commit run --show-diff-on-failure --from-ref FETCH_HEAD --to-ref HEAD ; then
echo ""
echo "Generating patch file..."
git commit -a -m "pre-commit fixes
patch generated by ${CI_JOB_URL}" > /dev/null
git format-patch HEAD~
cat <<EOF
=======================================
You can apply these changes with:
curl ${CI_JOB_URL}/artifacts/raw/0001-pre-commit-fixes.patch | git am
=======================================
EOF
exit 1
fi
artifacts:
paths:
- apply-formatting.patch
- 0001-pre-commit-fixes.patch
when: on_failure
expire_in: 1 week
allow_failure: true
......
repos:
- repo: https://github.com/ssciwr/clang-format-precommit
rev: v13.0.0
hooks:
- id: clang-format
- id: clang-format
name: clang-format (non-standard extensions)
types_or: [file]
files: \.(icc|icpp)$
- repo: https://github.com/pre-commit/mirrors-yapf
rev: v0.24.0
hooks:
- id: yapf
name: Format Python files
#!/bin/bash -e
#####################################################################################
# (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations #
# #
# This software is distributed under the terms of the Apache version 2 licence, #
# copied verbatim in the file "LICENSE". #
# #
# In applying this licence, CERN does not waive the privileges and immunities #
# granted to it by virtue of its status as an Intergovernmental Organization #
# or submit itself to any jurisdiction. #
#####################################################################################
if [ -n "${CI}" ] ; then
job_id="${CI_JOB_URL}"
else
job_id="standalone job"
fi
# make sure we have the right versions of the commands
YAPF_VERSION=0.24.0
if [ "$(yapf --version)" != "yapf $YAPF_VERSION" ] ; then
echo "invalid version of yapf: $(yapf --version) ($YAPF_VERSION required)"
exit 1
fi
CLANG_FORMAT_VERSION=8
if ! which clang-format-$CLANG_FORMAT_VERSION >/dev/null ; then
echo "clang-format-$CLANG_FORMAT_VERSION command not available"
exit 1
fi
if [ -n "${TARGET_BRANCH:=$1}" ] ; then
git fetch https://gitlab.cern.ch/gaudi/Gaudi.git ${TARGET_BRANCH}
files_to_check() {
git diff --name-only --no-renames --diff-filter MA FETCH_HEAD...HEAD
}
else
files_to_check() {
git ls-files
}
fi
files_to_check | \
grep -E '\.(i?[ch](pp|xx|c)?|cc|hh)$' | xargs --no-run-if-empty --max-procs=$(nproc) --max-args=100 clang-format-$CLANG_FORMAT_VERSION -i --style=file || true
files_to_check | \
grep -E '\.py$' | xargs --no-run-if-empty --max-procs=$(nproc) --max-args=100 yapf -i || true
files_to_check | grep -v -E '\.(i?[ch](pp|xx|c)?|cc|hh|py)$' | \
xargs --no-run-if-empty awk 'FNR>1{nextfile} /^#!.*python/{print FILENAME; nextfile}' | \
xargs --no-run-if-empty --max-procs=$(nproc) --max-args=100 yapf -i || true
if git diff --exit-code --quiet ; then
echo "no change"
else
echo "From: Gitlab CI <noreply@cern.ch>" > apply-formatting.patch
echo "Date: $(date -R)" >> apply-formatting.patch
echo "Subject: [PATCH] Fixed formatting" >> apply-formatting.patch
echo "" >> apply-formatting.patch
echo "patch generated by ${job_id}" >> apply-formatting.patch
echo "" >> apply-formatting.patch
echo "" >> apply-formatting.patch
git diff >> apply-formatting.patch
if [ -n "${CI}" ] ; then
cat <<EOF
=======================================
You can fix formatting with:
curl ${CI_JOB_URL}/artifacts/raw/apply-formatting.patch | git am
=======================================
EOF
else
echo "Changes made to files and recorded as 'apply-formatting.patch'."
echo "You can commit them with:"
echo " git commit -a -m 'Fixed formatting'"
echo "or discard all non staged changes with:"
echo " git checkout ."
fi
exit 1
fi
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment