Meta: Update lint-{clang-format,shell-scripts}.sh to take a list of files

This should speed up pre-commit a bit as only files that are staged will
be processed, and clang-format and shellcheck are only invoked once, not
for every file. When no arguments are given (e.g. on CI), it still uses
'git ls-files'.
This commit is contained in:
Linus Groh 2020-12-27 15:34:06 +01:00 committed by Andreas Kling
parent a56b3cbf7c
commit 51bcfb5a44
4 changed files with 56 additions and 43 deletions

View file

@ -5,4 +5,3 @@ repos:
name: Running Meta/lint-ci.sh to ensure changes will pass linting on CI name: Running Meta/lint-ci.sh to ensure changes will pass linting on CI
entry: bash Meta/lint-ci.sh entry: bash Meta/lint-ci.sh
language: system language: system
pass_filenames: false

View file

@ -20,17 +20,17 @@ for cmd in \
Meta/lint-executable-resources.sh \ Meta/lint-executable-resources.sh \
Meta/lint-ipc-ids.sh \ Meta/lint-ipc-ids.sh \
Meta/lint-shell-scripts.sh; do Meta/lint-shell-scripts.sh; do
echo "Running $cmd... " echo "Running ${cmd}... "
if $cmd; then if "${cmd}" "$@"; then
echo -e "[${GREEN}OK${NC}]: ${cmd}" echo -e "[${GREEN}OK${NC}]: ${cmd}"
else else
echo -e "[${RED}FAIL${NC}]: ${cmd}" echo -e "[${RED}FAIL${NC}]: ${cmd}"
((FAILURES+=1)) ((FAILURES+=1))
fi fi
done done
echo "Running Meta/lint-clang-format.sh" echo "Running Meta/lint-clang-format.sh"
if Meta/lint-clang-format.sh --overwrite-inplace && git diff --exit-code; then if Meta/lint-clang-format.sh --overwrite-inplace "$@" && git diff --exit-code; then
echo -e "[${GREEN}OK${NC}]: Meta/lint-clang-format.sh" echo -e "[${GREEN}OK${NC}]: Meta/lint-clang-format.sh"
else else
echo -e "[${RED}FAIL${NC}]: Meta/lint-clang-format.sh" echo -e "[${RED}FAIL${NC}]: Meta/lint-clang-format.sh"

View file

@ -20,7 +20,7 @@ else
exit 1 exit 1
fi fi
if [ "$#" -eq "1" ] && [ "x--overwrite-inplace" = "x$1" ] ; then if [ "$#" -gt "0" ] && [ "x--overwrite-inplace" = "x$1" ] ; then
true # The only way to run this script. true # The only way to run this script.
else else
# Note that this branch also covers --help, -h, -help, -?, etc. # Note that this branch also covers --help, -h, -help, -?, etc.
@ -31,19 +31,32 @@ fi
echo "Using ${CLANG_FORMAT}" echo "Using ${CLANG_FORMAT}"
{ if [ "$#" -eq "1" ]; then
git ls-files -- \ mapfile -t files < <(
'*.cpp' \ git ls-files -- \
'*.h' \ '*.cpp' \
':!:Base' \ '*.h' \
':!:Kernel/Arch/i386/CPU.cpp' \ ':!:Base' \
':!:Kernel/FileSystem/ext2_fs.h' \ ':!:Kernel/Arch/i386/CPU.cpp' \
':!:Libraries/LibC/getopt.cpp' \ ':!:Kernel/FileSystem/ext2_fs.h' \
':!:Libraries/LibC/syslog.h' \ ':!:Libraries/LibC/getopt.cpp' \
':!:Libraries/LibCore/puff.h' \ ':!:Libraries/LibC/syslog.h' \
':!:Libraries/LibCore/puff.cpp' \ ':!:Libraries/LibCore/puff.h' \
':!:Libraries/LibELF/exec_elf.h' \ ':!:Libraries/LibCore/puff.cpp' \
|| echo "'git ls-files failed!'" ':!:Libraries/LibELF/exec_elf.h'
} | xargs -d'\n' "${CLANG_FORMAT}" -style=file -i )
else
files=()
for file in "${@:2}"; do
if [[ "${file}" == *".cpp" || "${file}" == *".h" ]]; then
files+=("${file}")
fi
done
fi
echo "Maybe some files have changed. Sorry, but clang-format doesn't indicate what happened." if (( ${#files[@]} )); then
"${CLANG_FORMAT}" -style=file -i "${files[@]}"
echo "Maybe some files have changed. Sorry, but clang-format doesn't indicate what happened."
else
echo "No .cpp or .h files to check."
fi

View file

@ -10,24 +10,25 @@ if ! command -v shellcheck &>/dev/null ; then
exit 1 exit 1
fi fi
ERRORS=() if [ "$#" -eq "0" ]; then
mapfile -t files < <(
while IFS= read -r f; do git ls-files -- \
if file "$f" | grep --quiet shell; then '*.sh' \
{ ':!:Toolchain' \
shellcheck "$f" && echo -e "[\033[0;32mOK\033[0m]: successfully linted $f" ':!:Ports' \
} || { ':!:Shell/Tests'
ERRORS+=("$f") )
} else
fi files=()
done < <(git ls-files -- \ for file in "$@"; do
'*.sh' \ if [[ "${file}" == *".sh" ]]; then
':!:Toolchain' \ files+=("${file}")
':!:Ports' \ fi
':!:Shell/Tests' \ done
) fi
if (( ${#ERRORS[@]} )); then if (( ${#files[@]} )); then
echo "Files failing shellcheck: ${ERRORS[*]}" shellcheck "${files[@]}"
exit 1 else
echo "No .sh files to check."
fi fi