Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pre-commit to use CI base image #1307

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 31 additions & 56 deletions tools/pre-commit
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
#!/usr/bin/env bash

# Copyright (c) 2019 The STE||AR-Group
# Copyright (c) 2024 ETH Zurich
#
# SPDX-License-Identifier: BSL-1.0
# Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand All @@ -11,67 +11,42 @@
# are correctly clang-formatted and that CMakeLists.txt and *.cmake
# files are cmake-formatted

# To use this hook, you must have clang-format and cmake-format
# installed on your system

# To install this hook, symlink this hook to your git hooks as follows
# (note that the extra ../../ in the path is because git runs the hook
# from the .git/hooks directory, so the symlink has to be redirected)
# ln -s -f ../../tools/pre-commit .git/hooks/pre-commit

# @todo : add support for the pika inspect tool to be run as well

CLANG_FORMAT_VERSION=clang-format-16

red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
normal=$(tput sgr0)

cxxfiles=()
for file in `git diff --cached --name-only --diff-filter=ACMRT | grep -E "\.(cpp|hpp)$"`; do
if ! cmp -s <(git show :${file}) <(git show :${file}|$CLANG_FORMAT_VERSION -style=file); then
cxxfiles+=("${file}")
fi
done
# To use this hook, you must have podman installed.

cmakefiles=()
for file in `git diff --cached --name-only --diff-filter=ACMRT | grep -E "(CMakeLists\.txt|\.cmake)$"`; do
tmpfile=$(mktemp /tmp/cmake-check.XXXXXX)
git show :${file} > $tmpfile
cmake-format -c $(pwd)/.cmake-format.py -i $tmpfile
if ! cmp -s <(git show :${file}) <(cat $tmpfile); then
cmakefiles+=("${file}")
fi
rm $tmpfile
done

returncode=0
full_list=

if [ -n "${cxxfiles}" ]; then
printf "# ${blue}clang-format ${red}error pre-commit${normal} : To fix run the following (use git commit ${yellow}--no-verify${normal} to bypass)\n"
for f in "${cxxfiles[@]}" ; do
rel=$(realpath --relative-to "./$GIT_PREFIX" $f)
printf "$CLANG_FORMAT_VERSION -i %s\n" "$rel"
full_list="${rel} ${full_list}"
done
returncode=1
fi
# @todo : add support for the pika inspect tool to be run as well

if [ -n "${cmakefiles}" ]; then
printf "# ${green}cmake-format ${red}error pre-commit${normal} : To fix run the following (use git commit ${yellow}--no-verify${normal} to bypass)\n"
for f in "${cmakefiles[@]}" ; do
rel=$(realpath --relative-to "./$GIT_PREFIX" $f)
printf "cmake-format -i %s\n" "$rel"
full_list="${rel} ${full_list}"
done
returncode=1
function error_msg() {
echo "$1 formatted files. run git add -u and git commit again."
echo "run git commit --no-verify to bypass."
git status
exit_code=1
}

cont_image=docker.io/pikaorg/pika-ci-base:27
git_dir=$(git rev-parse --show-toplevel)

echo "Run clang-format"
podman run -w /pika/source -v=${git_dir}:/pika/source ${cont_image} \
git-clang-format -f --extensions cpp,hpp,cu
result_clang_format=$?

echo "Run cmake-format"
checksum=$(git diff --cached --name-only | grep -E '(CMakeLists.txt|\.cmake)' | xargs -I {} sh -c 'md5sum {}')
podman run -w /pika/source -v=${git_dir}:/pika/source ${cont_image} \
bash -c "git diff --cached --name-only | grep -E '(CMakeLists.txt|\.cmake)' | xargs cmake-format -i"
checksum_after_format=$(git diff --cached --name-only | grep -E '(CMakeLists.txt|\.cmake)' | xargs -I {} sh -c 'md5sum {}')

exit_code=0
if [ "$checksum" != "$checksum_after_format" ]; then
error_msg "cmake-format"
fi

if [ ! -z "$full_list" ]; then
printf "\n# ${red}To commit the corrected files, run\n${normal}\ngit add ${full_list}\n"
if [ "$result_clang_format" == 1 ]; then
error_msg "clang-format"
fi

exit $returncode
exit $exit_code
Loading