3.Git hooks

以下提交 hooks 可能对您有所帮助,请自行斟酌使用。

pre-commit

#!/bin/bash

HAS_ISSUES=0
FIRST_FILE=1

colerr="$(tput setaf 9)"
colok="$(tput setaf 10)"
colreset="$(tput sgr0)"

for file in $(git diff --name-only --staged); do
    FMT_RESULT="$(rustfmt --edition 2021 --check --quiet $file 2>/dev/null || true)"
    if [ "$FMT_RESULT" != "" ]; then
        if [ $FIRST_FILE -eq 0 ]; then
            echo -n ", "
        fi
        echo -n "${colerr}${file}${colreset}"
        HAS_ISSUES=1
        FIRST_FILE=0
    fi
done

if [ $HAS_ISSUES -eq 0 ]; then
    exit 0
fi

echo "."
echo "Your code has formatting issues in the files listed above."
echo "Format your code with: ${colok}cargo fmt --all --${colreset}"
exit 1

commit-msg

最后更新于