From 024fc61bfabd09659e9f2de951d43e173dd5c551 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sat, 5 Mar 2022 09:36:12 +0100 Subject: [PATCH] script: add commit-msg hook script to check commit msg --- CONTRIBUTING.rst | 7 +++++ INSTALL.rst | 3 +++ NEWS.rst | 2 ++ script/commit-msg | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+) create mode 100755 script/commit-msg diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 717c6251..e0e7e58a 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -157,6 +157,13 @@ local repository with following command:: ln -s ../../script/pre-commit .git/hooks/pre-commit +A :command:`commit-msg` hook script is also provided in the :file:`script` +directory of the project to help you check that your commit messages are free +of misspellings. It requires the `Aspell`_ utility and could be enabled in +your local repository with following command:: + + ln -s ../../script/commit-msg .git/hooks/commit-msg + .. _codespell: https://github.com/codespell-project/codespell .. _Aspell: http://aspell.net/ diff --git a/INSTALL.rst b/INSTALL.rst index d136c681..9b555fe6 100644 --- a/INSTALL.rst +++ b/INSTALL.rst @@ -1,5 +1,8 @@ .. _INSTALL: + + + Installing Modules on Unix ========================== diff --git a/NEWS.rst b/NEWS.rst index f21114be..c36069d2 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -165,6 +165,8 @@ Modules 5.1.0 (not yet released) :ref:`modulefile(4)` man page. (fix issues #431 and #433) * Script: update :command:`pre-commit` git hook script to spell check documentation files with `Aspell`_ tool. +* Script: add :command:`commit-msg` git hook script to spell check commit + message with `Aspell`_ tool. .. _Code of conduct: https://github.com/cea-hpc/modules/blob/master/CODE_OF_CONDUCT.md .. _codespell: https://github.com/codespell-project/codespell diff --git a/script/commit-msg b/script/commit-msg new file mode 100755 index 00000000..ebfee411 --- /dev/null +++ b/script/commit-msg @@ -0,0 +1,68 @@ +#!/bin/bash +# +# COMMIT-MSG, hook script to verify commit message +# Copyright (C) 2022 Xavier Delaruelle +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +########################################################################## + +# redirect output to stderr. +exec 1>&2 + +# apply SGR code to message if output channel is attached to terminal +sgr() { + local code=$1 + local msg=$2 + if [ -t 2 ]; then + echo "\033[${code}m${msg}\033[0m" + else + echo $msg + fi +} + +echo_warning() { + echo -e "$(sgr 43 WARNING): $1" +} + +echo_error() { + echo -e "$(sgr 41 ERROR): $1" +} + +echo_hint() { + echo -e "$(sgr 44 HINT): $1" +} + +# check misspellings in commit message +command -v aspell >/dev/null +if [ $? -eq 0 ]; then + ASPELL_OPTS='-l en -x --home-dir=. --personal=.aspell.en.pws' + # use sed to extract commit message from full commit details + words=$(sed '/^# /Q' "$1" | aspell $ASPELL_OPTS list) + # abort if misspelled words found + if [ -n "$words" ]; then + befmsg='# commit message was ---------------- >8 -------------' + aftmsg='# ------------------------ 8< ------------------------' + echo_error "misspellings found in commit message\n\n$words\n" + echo -e "$befmsg\n$(sed '/^# /Q' "$1")\n$aftmsg\n" + echo_hint "correct message or skip checks with '--no-verify' git option" + exit 1 + fi +else + echo_warning "aspell command not found" +fi + +exit 0 + +# vim:set tabstop=3 shiftwidth=3 expandtab autoindent: