diff --git a/tools/precommit/Dockerfile b/tools/precommit/Dockerfile index 2eea553e69..48daaea1f7 100644 --- a/tools/precommit/Dockerfile +++ b/tools/precommit/Dockerfile @@ -7,6 +7,7 @@ FROM ubuntu:20.04 # https://clang.llvm.org/docs/ClangFormat.html RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && \ apt-get update -qq && apt-get install -qq --no-install-recommends \ + ca-certificates \ clang-format \ git \ less \ @@ -17,6 +18,7 @@ RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && \ python3-wheel \ python3-setuptools \ shellcheck \ + wget \ && rm -rf /var/lib/apt/lists/* # Install markdownlint-cli and dependencies. @@ -32,6 +34,13 @@ RUN ln -s /opt/cp2k-precommit/node_modules/markdownlint-cli/markdownlint.js /usr COPY requirements.txt . RUN pip3 install --quiet -r requirements.txt +# Install shfmt. +# https://github.com/mvdan/sh +RUN wget -q https://github.com/mvdan/sh/releases/download/v3.2.2/shfmt_v3.2.2_linux_amd64 && \ + echo '3a32a69286a19491a81fcd854154f0d886c379ff28d99e32d5594490b8bbef4b shfmt_v3.2.2_linux_amd64' | sha256sum --check && \ + chmod +x shfmt_v3.2.2_linux_amd64 && \ + ln -s /opt/cp2k-precommit/shfmt_v3.2.2_linux_amd64 /usr/bin/shfmt + # Clone cp2k repository (needed for CI mode). RUN git clone --quiet --recursive --single-branch -b master https://github.com/cp2k/cp2k.git /workspace/cp2k diff --git a/tools/precommit/README.md b/tools/precommit/README.md index 4f90608acb..b922ccc9df 100644 --- a/tools/precommit/README.md +++ b/tools/precommit/README.md @@ -15,6 +15,8 @@ source code: to format C and Cuda files. - [black](https://github.com/psf/black) to format Python scripts. +- [shfmt](https://github.com/mvdan/sh) + to format Shell scripts. - [shellcheck](https://github.com/koalaman/shellcheck) to analyze Shell scripts. - [markdownlint](https://github.com/DavidAnson/markdownlint) diff --git a/tools/precommit/precommit.py b/tools/precommit/precommit.py index 39709fa4df..c1f81fd29c 100755 --- a/tools/precommit/precommit.py +++ b/tools/precommit/precommit.py @@ -106,7 +106,7 @@ def main(): # Find eligible files and sort by size as larger ones will take longer to process. eligible_file_pattern = re.compile( - r"(./data/.*POTENTIALS?)|(.*/PACKAGE)|(.*\.(F|fypp|c|cu|h|py|md))$" + r"(./data/.*POTENTIALS?)|(.*/PACKAGE)|(.*\.(F|fypp|c|cu|h|py|md|sh))$" ) file_list = [fn for fn in candidate_file_list if eligible_file_pattern.match(fn)] file_list.sort(reverse=True, key=lambda fn: os.path.getsize(fn)) @@ -189,7 +189,8 @@ def process_file(fn, allow_modifications): run_analyze_src(fn) elif re.match(r".*\.sh$", fn): - run_remote_tool("shellcheck", fn) + run_remote_tool("shfmt", fn) + # run_remote_tool("shellcheck", fn) elif re.match(r".*\.md$", fn): run_remote_tool("markdownlint", fn) diff --git a/tools/precommit/precommit_server.py b/tools/precommit/precommit_server.py index 9421846a93..720280d268 100755 --- a/tools/precommit/precommit_server.py +++ b/tools/precommit/precommit_server.py @@ -28,6 +28,12 @@ def black(): return run_tool(["black"]) +# ====================================================================================== +@app.route("/shfmt", methods=["POST"]) +def shfmt(): + return run_tool(["shfmt", "-i=2", "-ci", "-sr", "-w"]) + + # ====================================================================================== @app.route("/shellcheck", methods=["POST"]) def shellcheck():