Githooks for Shell Scripts

June 26, 2023 ยท View on GitHub

Githooks for Shell Scripts

This repository contains shared repository Git hooks for shell scripts in githooks/* to be used with the Githooks Manager.

The following hooks are included:

  • Hook to format shell files with shfmt (pre-commit).
  • Hook to check shell files with shellcheck (pre-commit).
  • Hook to check shell mistakes (pre-commit).
Table of Content (click to expand)

Requirements

Run them containerized where only docker is required.

If you want to run them non-containerized, make the following installed on your system:

  • bash
  • shfmt
  • shellcheck
  • GNU grep
  • GNU sed
  • GNU find
  • GNU xargs
  • GNU parallel [optional]

This works with Windows setups too.

Installation

The hooks can be used by simply using this repository as a shared hook repository inside your repository. See further documentation.

You should configure the shared hook repository in your project to use this repos main branch by using the following .githooks/.shared.yaml :

version: 1
urls:
  - https://github.com/gabyx/githooks-shell.git@main`.

Hooks

Format Shell: pre-commit/1-format/format-shell.yaml

Formatting with shfmt.

Check Shell Mistakes: pre-commit/2-check/check-shell-mistakes.yaml

Mistakes such as wrong shellcheck ignore format and 'set -x' are checked.

Check Shell: pre-commit/2-check/check-shell.yaml

Linting file with shellcheck.

Scripts

The following scripts are provided:

  • format-shell-all.sh : Script to format all shell files in a directory recursively. See documentation.

  • check-shell-all.sh : Script to check all shell files in a directory recursively. See documentation.

They can be used in scripts by doing the following trick inside a repo which uses this hook:

shellHooks=$(git hooks shared root ns:githooks-shell)
"$shellHooks/githooks/scripts/<script-name>.sh"

Testing

The containerized tests in tests/* are executed by

tests/test.sh

or only special tests steps by

tests/test.sh --seq 001..010

For showing the output also in case of success use:

tests/test.sh --show-output [other-args]