This directory contains scripts that can be used as pre-commit hooks or CI checks to maintain code quality in the repository.
The hooks are designed to automatically check and fix common issues in bash scripts, such as:
- Removing carriage return characters (Windows line endings)
- Ensuring files end with exactly one empty line
- Removing trailing whitespaces
- Formatting and linting bash scripts
To run all hooks in check mode (without modifying files):
./hooks/_run_all.shThis will execute all hooks with the --check option on the src directory. It will exit with status 1 if any checks fail.
Each hook can be run individually with the following syntax:
./hooks/<hook_name>.sh [--check] <path>--check: Only check if changes are needed, do not modify files<path>: File or directory to process
Examples:
# Check a single file for carriage returns
./hooks/remove_carriage_return.sh --check src/my_script.sh
# Remove trailing whitespaces from all files in src directory
./hooks/remove_trailing_whitespaces.sh src
# Check if all files end with exactly one empty line
./hooks/last_line_empty.sh --check srcFormats shell scripts using Beautysh and analyzes them with ShellCheck.
Requirements:
beautysh- Install withpip3 install beautyshshellcheck- Install withapt-get install shellcheck(Debian/Ubuntu)
Removes carriage return characters (\r) from files. This is useful for files that may have been edited on Windows systems.
Ensures that all files end with exactly one empty line. This is a common convention in many projects.
Removes trailing whitespace characters (spaces or tabs) from the end of lines in files.
These hooks are automatically run in the CI pipeline (see .github/workflows/ci.yml). The CI will fail if any checks don't pass.
To run these checks before every commit, you can create a git pre-commit hook:
cat > .git/hooks/pre-commit << 'EOF'
#!/usr/bin/env bash
./hooks/_run_all.sh
EOF
chmod +x .git/hooks/pre-commitThis will automatically run all hooks before each commit. If any checks fail, the commit will be aborted.
The scripts in this directory are actually symbolic links to the corresponding scripts in the src directory. This allows the same scripts to be used both as utility scripts and as hooks.
The _run_all.sh script:
- Finds all symbolic links in the
hooksdirectory (excluding files starting with_) - Executes each script with the
--checkflag - Collects the exit status of all scripts
- Exits with status 1 if any check failed
If a hook fails in CI or locally:
- Read the error message to understand which check failed
- Run the specific hook without
--checkto automatically fix the issue:./hooks/<hook_name>.sh src
- Commit the changes and try again
All hooks respect the --check flag. When used with --check, they will only report issues without modifying files. Without this flag, they will automatically fix the issues.