|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# |
| 4 | +# Distributed Linear Algebra with Future (DLAF) |
| 5 | +# |
| 6 | +# Copyright (c) 2018-2023, ETH Zurich |
| 7 | +# All rights reserved. |
| 8 | +# |
| 9 | +# Please, refer to the LICENSE file in the root directory. |
| 10 | +# SPDX-License-Identifier: BSD-3-Clause |
| 11 | +# |
| 12 | + |
| 13 | +# ---------------------------------------------------------------- |
| 14 | +# simple pre commit hook script to check that *.cpp and *.hpp files |
| 15 | +# are correctly clang-formatted and that CMakeLists.txt and *.cmake |
| 16 | +# files are cmake-formatted |
| 17 | + |
| 18 | +# To use this hook, you must have clang-format and cmake-format |
| 19 | +# installed on your system |
| 20 | + |
| 21 | +# To install this hook, symlink this hook to your git hooks as follows |
| 22 | +# (note that the extra ../../ in the path is because git runs the hook |
| 23 | +# from the .git/hooks directory, so the symlink has to be redirected) |
| 24 | +# ln -s -f ../../tools/pre-commit .git/hooks/pre-commit |
| 25 | +# ---------------------------------------------------------------- |
| 26 | + |
| 27 | +CLANG_FORMAT_VERSION=clang-format-12 |
| 28 | + |
| 29 | +red=$(tput setaf 1) |
| 30 | +green=$(tput setaf 2) |
| 31 | +yellow=$(tput setaf 3) |
| 32 | +blue=$(tput setaf 4) |
| 33 | +normal=$(tput sgr0) |
| 34 | + |
| 35 | +cxxfiles=() |
| 36 | +for file in `git diff --cached --name-only --diff-filter=ACMRT | grep -E "\.(cpp|hpp)$"`; do |
| 37 | + if ! cmp -s <(git show :${file}) <(git show :${file}|$CLANG_FORMAT_VERSION -style=file); then |
| 38 | + cxxfiles+=("${file}") |
| 39 | + fi |
| 40 | +done |
| 41 | + |
| 42 | +cmakefiles=() |
| 43 | +for file in `git diff --cached --name-only --diff-filter=ACMRT | grep -E "(CMakeLists\.txt|\.cmake)$"`; do |
| 44 | + tmpfile=$(mktemp /tmp/cmake-check.XXXXXX) |
| 45 | + git show :${file} > $tmpfile |
| 46 | + cmake-format -c $(pwd)/.cmake-format.py -i $tmpfile |
| 47 | + if ! cmp -s <(git show :${file}) <(cat $tmpfile); then |
| 48 | + cmakefiles+=("${file}") |
| 49 | + fi |
| 50 | + rm $tmpfile |
| 51 | +done |
| 52 | + |
| 53 | +returncode=0 |
| 54 | +full_list= |
| 55 | + |
| 56 | +if [ -n "${cxxfiles}" ]; then |
| 57 | + printf "# ${blue}clang-format ${red}error pre-commit${normal} : To fix run the following (use git commit ${yellow}--no-verify${normal} to bypass)\n" |
| 58 | + for f in "${cxxfiles[@]}" ; do |
| 59 | + rel=$(realpath --relative-to "./$GIT_PREFIX" $f) |
| 60 | + printf "$CLANG_FORMAT_VERSION -i %s\n" "$rel" |
| 61 | + full_list="${rel} ${full_list}" |
| 62 | + done |
| 63 | + returncode=1 |
| 64 | +fi |
| 65 | + |
| 66 | +if [ -n "${cmakefiles}" ]; then |
| 67 | + printf "# ${green}cmake-format ${red}error pre-commit${normal} : To fix run the following (use git commit ${yellow}--no-verify${normal} to bypass)\n" |
| 68 | + for f in "${cmakefiles[@]}" ; do |
| 69 | + rel=$(realpath --relative-to "./$GIT_PREFIX" $f) |
| 70 | + printf "cmake-format -i %s\n" "$rel" |
| 71 | + full_list="${rel} ${full_list}" |
| 72 | + done |
| 73 | + returncode=1 |
| 74 | +fi |
| 75 | + |
| 76 | +if [ ! -z "$full_list" ]; then |
| 77 | + printf "\n# ${red}To commit the corrected files, run\n${normal}\ngit add ${full_list}\n" |
| 78 | +fi |
| 79 | + |
| 80 | +exit $returncode |
0 commit comments