From 51bcfb5a44ebedc319bb8dc66dfe814f936e5e83 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 27 Dec 2020 15:34:06 +0100 Subject: [PATCH] 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'. --- .pre-commit-config.yaml | 1 - Meta/lint-ci.sh | 12 +++++----- Meta/lint-clang-format.sh | 45 ++++++++++++++++++++++++-------------- Meta/lint-shell-scripts.sh | 41 +++++++++++++++++----------------- 4 files changed, 56 insertions(+), 43 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1763a5e4cb3..4d4f8bc8646 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,4 +5,3 @@ repos: name: Running Meta/lint-ci.sh to ensure changes will pass linting on CI entry: bash Meta/lint-ci.sh language: system - pass_filenames: false diff --git a/Meta/lint-ci.sh b/Meta/lint-ci.sh index d3ba23de2ea..bf4cbbedc70 100755 --- a/Meta/lint-ci.sh +++ b/Meta/lint-ci.sh @@ -20,17 +20,17 @@ for cmd in \ Meta/lint-executable-resources.sh \ Meta/lint-ipc-ids.sh \ Meta/lint-shell-scripts.sh; do - echo "Running $cmd... " - if $cmd; then - echo -e "[${GREEN}OK${NC}]: ${cmd}" + echo "Running ${cmd}... " + if "${cmd}" "$@"; then + echo -e "[${GREEN}OK${NC}]: ${cmd}" else - echo -e "[${RED}FAIL${NC}]: ${cmd}" - ((FAILURES+=1)) + echo -e "[${RED}FAIL${NC}]: ${cmd}" + ((FAILURES+=1)) fi done 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" else echo -e "[${RED}FAIL${NC}]: Meta/lint-clang-format.sh" diff --git a/Meta/lint-clang-format.sh b/Meta/lint-clang-format.sh index 6a849409699..2359639071f 100755 --- a/Meta/lint-clang-format.sh +++ b/Meta/lint-clang-format.sh @@ -20,7 +20,7 @@ else exit 1 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. else # Note that this branch also covers --help, -h, -help, -?, etc. @@ -31,19 +31,32 @@ fi echo "Using ${CLANG_FORMAT}" -{ - git ls-files -- \ - '*.cpp' \ - '*.h' \ - ':!:Base' \ - ':!:Kernel/Arch/i386/CPU.cpp' \ - ':!:Kernel/FileSystem/ext2_fs.h' \ - ':!:Libraries/LibC/getopt.cpp' \ - ':!:Libraries/LibC/syslog.h' \ - ':!:Libraries/LibCore/puff.h' \ - ':!:Libraries/LibCore/puff.cpp' \ - ':!:Libraries/LibELF/exec_elf.h' \ - || echo "'git ls-files failed!'" -} | xargs -d'\n' "${CLANG_FORMAT}" -style=file -i +if [ "$#" -eq "1" ]; then + mapfile -t files < <( + git ls-files -- \ + '*.cpp' \ + '*.h' \ + ':!:Base' \ + ':!:Kernel/Arch/i386/CPU.cpp' \ + ':!:Kernel/FileSystem/ext2_fs.h' \ + ':!:Libraries/LibC/getopt.cpp' \ + ':!:Libraries/LibC/syslog.h' \ + ':!:Libraries/LibCore/puff.h' \ + ':!:Libraries/LibCore/puff.cpp' \ + ':!:Libraries/LibELF/exec_elf.h' + ) +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 diff --git a/Meta/lint-shell-scripts.sh b/Meta/lint-shell-scripts.sh index e6ee774532b..836b906fede 100755 --- a/Meta/lint-shell-scripts.sh +++ b/Meta/lint-shell-scripts.sh @@ -10,24 +10,25 @@ if ! command -v shellcheck &>/dev/null ; then exit 1 fi -ERRORS=() - -while IFS= read -r f; do - if file "$f" | grep --quiet shell; then - { - shellcheck "$f" && echo -e "[\033[0;32mOK\033[0m]: successfully linted $f" - } || { - ERRORS+=("$f") - } - fi -done < <(git ls-files -- \ - '*.sh' \ - ':!:Toolchain' \ - ':!:Ports' \ - ':!:Shell/Tests' \ -) - -if (( ${#ERRORS[@]} )); then - echo "Files failing shellcheck: ${ERRORS[*]}" - exit 1 +if [ "$#" -eq "0" ]; then + mapfile -t files < <( + git ls-files -- \ + '*.sh' \ + ':!:Toolchain' \ + ':!:Ports' \ + ':!:Shell/Tests' + ) +else + files=() + for file in "$@"; do + if [[ "${file}" == *".sh" ]]; then + files+=("${file}") + fi + done +fi + +if (( ${#files[@]} )); then + shellcheck "${files[@]}" +else + echo "No .sh files to check." fi