Skip to content

Commit

Permalink
script: add pre-commit script to verify commits
Browse files Browse the repository at this point in the history
  • Loading branch information
xdelaruelle committed Mar 2, 2022
1 parent d38233c commit ff6a136
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
17 changes: 17 additions & 0 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,25 @@ Coding conventions
setState already_report 1
}

* `No trailing space nor misspelling <commit-hook_>`_

.. _Tcl minimal escaping style: https://wiki.tcl-lang.org/page/Tcl+Minimal+Escaping+Style

.. _commit-hook:

Commit hook
~~~~~~~~~~~

A :command:`pre-commit` hook script is provided in the :file:`script`
directory of the project to help you check that the contribution made is free
of misspellings and trailing spaces. It requires the `codespell`_ utility that
checks for typos. The :command:`pre-commit` could be enabled in your local
repository with following command::

ln -s ../../script/pre-commit .git/hooks/pre-commit

.. _codespell: https://github.com/codespell-project/codespell

Emacs settings for coding conventions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
2 changes: 2 additions & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ Modules 5.1.0 (not yet released)
completion.
* Fix the :subcmd:`sh-to-mod` and :mfcmd:`source-sh` mechanisms to correctly
detect empty function on fish shell.
* Script: add :command:`pre-commit` git hook script to help verify if commits
are free of misspellings and trailing spaces.

.. _Code of conduct: https://github.com/cea-hpc/modules/blob/master/CODE_OF_CONDUCT.md

Expand Down
38 changes: 38 additions & 0 deletions script/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/sh
#
# PRE-COMMIT, hook script to verify what is about to be committed
# 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 <http://www.gnu.org/licenses/>.

##########################################################################

# redirect output to stderr.
exec 1>&2

# fail if there are misspellings
git diff --cached --diff-filter=A HEAD | codespell -
if [ $? -ne 0 ]; then
exit 1
fi

# fail if there are whitespace errors
git diff-index --check --cached HEAD --
if [ $? -ne 0 ]; then
exit 1
fi

exit 0

# vim:set tabstop=3 shiftwidth=3 expandtab autoindent:

0 comments on commit ff6a136

Please sign in to comment.