Add include-fixer to pre-commit

This commit is contained in:
Sameer Rahmani 2022-06-12 22:34:12 +01:00
parent 02b3674436
commit 3d31d01bc5
7 changed files with 69 additions and 62 deletions

15
.ci.yml
View File

@ -16,10 +16,15 @@ steps:
commands:
- cmake --version
- ./builder build
- name: Lint
- name: Clang Tidy
image: rg.fr-par.scw.cloud/serene/llvm:15-8
commands:
- pre-commit clang-tidy -a
- pre-commit cppcheck -a
- pre-commit include-what-you-use -a
- pre-commit shell-lint -a
- ./builder build-tidy
- name: Linters
image: rg.fr-par.scw.cloud/serene/llvm:15-8
commands:
- pre-commit run cppcheck -a
- pre-commit run include-what-you-use -a
- pre-commit run shell-lint -a

View File

@ -17,15 +17,21 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- id: mixed-line-ending
- repo: local
hooks:
- id: include-fixer
name: Fixing 'serene' includes
language: script
entry: ./scripts/include-fixer.sh
files: ".*.(h|cpp)"
- repo: https://github.com/pocc/pre-commit-hooks
rev: v1.3.5
hooks:
- id: clang-format
#- id: clang-tidy
#- id: oclint
- id: cppcheck
args: ['--project=compile_commands.json']
#- id: cpplint
args: ['--project=compile_commands.json', '-j 4']
- id: include-what-you-use
- repo: https://github.com/detailyang/pre-commit-shell
rev: 1.0.5

0
LICENSE Executable file → Normal file
View File

View File

@ -160,7 +160,7 @@ function build-20() { ## Builds the project using C++20 (will regenerate the bui
popd_build
}
function build-tidy { ## Builds the project using clang-tidy (It takes longer than usual)
function build-tidy() { ## Builds the project using clang-tidy (It takes longer than usual)
build "-DSERENE_ENABLE_TIDY=ON" "${@:2}"
}

47
scripts/include-fixer.sh Executable file
View File

@ -0,0 +1,47 @@
#! /bin/bash
set -e
# function get_changed_files() {
# local add_modified
# local renamed
# add_modified=$(git diff --cached --name-status |egrep '*\.(cpp|h|hpp|cpp.inc|h.inc)'|awk '($1 != "D" && $1 !~ /R.+/) { print $2 }')
# renamed=$(git diff --cached --name-status |egrep '*\.(cpp|h|hpp|cpp.inc|h.inc)'|awk '$1 ~ /^R.+/ { print $3 }')
# echo $add_modified | sed '/^[[:space:]]*$/d'
# echo $renamed | sed '/^[[:space:]]*$/d'
# }
function fix_includes(){
#local files=$(get_changed_files)
echo "Fixing '#include' syntax in '$1'"
sed -i -E '/^#include "(serene|\.)/!s/^.include \"(.*)\"/#include <\1>/g' "$1"
}
# function clang_format_staged() {
# local files=$(get_changed_files)
# if [[ "$files" ]];
# then
# for file in $(get_changed_files)
# do
# echo "Reformatting $file..."
# clang-format -i $file
# done
# fi
# }
# function git_add_changes() {
# local files=$(get_changed_files)
# if [[ "$files" ]];
# then
# git add $files
# fi
# }
fix_includes "$1"
# clang_format_staged
# git_add_changes

View File

@ -1,51 +0,0 @@
#! /bin/bash
set -e
function get_changed_files() {
local add_modified
local renamed
add_modified=$(git diff --cached --name-status |egrep '*\.(cpp|h|hpp|cpp.inc|h.inc)'|awk '($1 != "D" && $1 !~ /R.+/) { print $2 }')
renamed=$(git diff --cached --name-status |egrep '*\.(cpp|h|hpp|cpp.inc|h.inc)'|awk '$1 ~ /^R.+/ { print $3 }')
echo $add_modified | sed '/^[[:space:]]*$/d'
echo $renamed | sed '/^[[:space:]]*$/d'
}
function fix_includes(){
local files=$(get_changed_files)
if [[ "$files" ]];
then
echo "Fixing '#include' syntax...."
sed -i -E '/^#include "(serene|\.)/!s/^.include \"(.*)\"/#include <\1>/g' $files
fi
}
function clang_format_staged() {
local files=$(get_changed_files)
if [[ "$files" ]];
then
for file in $(get_changed_files)
do
echo "Reformatting $file..."
clang-format -i $file
done
fi
}
function git_add_changes() {
local files=$(get_changed_files)
if [[ "$files" ]];
then
git add $files
fi
}
fix_includes
clang_format_staged
git_add_changes

View File

@ -27,7 +27,7 @@ set_target_properties(serene-repl PROPERTIES
INTERPROCEDURAL_OPTIMIZATION TRUE)
if(SERENE_ENABLE_TIDY)
set_target_properties(serene PROPERTIES CXX_CLANG_TIDY ${CLANG_TIDY_PATH})
set_target_properties(serene-repl PROPERTIES CXX_CLANG_TIDY ${CLANG_TIDY_PATH})
endif()
if (CPP_20_SUPPORT)