Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) (as of version 0.1.0).

## [0.5.0] `2026-05-16`

### Changed
- Restructured the script template file. Developers now have to implement the 'before_run' and 'run' functions within their script.
- Function 'before_run' must be implemented. Should initialize certain variables.
- Function 'run' must be implemented. Should contain the check's logic. A typical use case is included in the template script file.
- Function 'after_run' may be implemented. May contain any post-check actions.
- Move more logic from within the scripts to the core gitcc script and refactored 'check' function.
- Update the README.md file.

### Fixed
- Fix a bug in the lib function parse_npm_audit, that was showing an error message to the user when the NPM audit report file either did not contain a valid JSON object or did not contain certain expected json keys.


## [0.4.0] `2026-05-11`

### Added
Expand Down Expand Up @@ -49,4 +63,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [0.1.0] `2025-04-20`

### Added
- A POSIX-compliant shell script for running configurable and extendable pre-commit checks on your local git repository.
- A POSIX-compliant shell script for running configurable and extendable pre-commit checks on your git repository.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
# Git Commit Check

#### A POSIX-compliant Shell tool for executing configurable and extendable pre-commit checks on your git repository.
#### A POSIX-compliant Shell tool for executing configurable pre-commit checks on your git repository.

---
May be used as a pre-commit git hook, as part of a CI/CD pipeline or as a standalone tool.
Tested with the following shells: `sh, bash, dash, zsh`.

##### How to use:
- Download the git-commit-check directory and place it within your repository.

- Run the following commands to copy the pre-commit hook script example and the config example files
and edit their values according to your project.

Let's assume you placed gitcc files under the bin directory of your repository:
Let's assume you placed the gitcc directory under the bin directory of your repository:
```
cd path/to/your/repo
cp bin/git-commit-check/hooks/pre-commit.example bin/git-commit-check/hooks/pre-commit
cp bin/git-commit-check/config.sh.example bin/git-commit-check/config.sh
cp bin/gitcc/hooks/pre-commit.example bin/gitcc/hooks/pre-commit
cp bin/gitcc/config.sh.example bin/gitcc/config.sh
```
- Edit the config.sh file to set the values for the checks you want to run.
- Set the ENABLED variable with the ',' separated script IDs that you want to enable.
- Create your own new script and add it to the `scripts` directory. Script naming conventions:
1. Prepend a two-digit script ID to the script name.
2. Append the .sh extension to the script name.
3. Copy the contents of the `scripts/template` script into the new script file.
4. Adjust the values in the script file to suit your needs, as described in the comments of the template script.
4. Follow the instructions within the template file.
a. Implement function 'before_run' and initialise the variables.
b. Implement function 'run' and add the check's logic. A typical use case can be found in the template file.
c. Optionally implement function 'after_run' to perform any cleanup or post-processing actions.
5. Try to keep your scripts POSIX-compliant so that they will work in all (most) SHELL. There are command line and online tools that can help you verify this.
- Alternatively, you may copy one of the prepared scripts under the `scripts/prepared` directory into the `scripts` directory.
- Make the pre-commit hook executable. Run the following command to instruct git to call `gitcc` before allowing the developer to create a new commit.

```
git config core.hooksPath bin/git-commit-check/hooks`
git config core.hooksPath bin/gitcc/hooks
```

- Run `git commit` to test the pre-commit hook. If your application is containerized (docker/podman), gitcc will automatically run within your container, regardless whether you do `git commit` from the host machine or the container.
This way the pre-commit hook and all the checks will be performed in the same environment as the application.

- Done!
120 changes: 81 additions & 39 deletions gitcc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/sh
# Git Commit Check (gitcc)
# Version: 0.4.0
# Version: 0.5.0
# Github repo: https://github.com/angtheod/git-commit-check
# Author: Angelos Theodorakopoulos <angtheod@gmail.com>

# Change to the directory of the script and source the required scripts.
Expand All @@ -10,64 +11,105 @@ cd "$(dirname "$0")" || exit 1
. ./lib.sh

init() {
START=$(date +%s)
HAS_ERRORS=0
HAS_SYNTAX_ERRORS=0
START=$(date +%s)
HAS_ERRORS=0
HAS_SYNTAX_ERRORS=0
}

header() {
line3 "╔" "╗" $((WIDTH+8)) "═"
printf " %s${CYAN}Load\n" "$SECTION_SYMBOL"
line "Configuration" "${1:-./config.sh}"
line3 "╔" "╗" $((WIDTH+8)) "═"
printf " %s${CYAN}Load\n" "$SECTION_SYMBOL"
line "Configuration" "${1:-./config.sh}"
}

check() {
[ -n "$ENABLED" ] && printf "\n %s${CYAN}Check${NC}\n" "$SECTION_SYMBOL"

for script in $(find "$GITCC_SCRIPTS_PATH" -type f -name '*.sh' | sort); do
_name=$(basename "$script")
_id="${_name%%_*}"
# Check if the script is enabled in the configuration and then source the script
# so that it runs in the same shell process and the variables are accessible within it.
if contains_string "$_id" "$ENABLED"; then
. "$script"
[ -n "$ENABLED" ] && printf "\n %s${CYAN}Check${NC}\n" "$SECTION_SYMBOL"

for script in $(find "$GITCC_SCRIPTS_PATH" -type f -name '*.sh' | sort); do
_name=$(basename "$script")
_id="${_name%%_*}"

# Check if the script is enabled in the configuration and then source the script
# so that it runs in the same shell process and the variables are accessible within it.
if contains_string "$_id" "$ENABLED"; then
before_run_check "$script"
run_check
after_run_check
fi
done
}

before_run_check() {
if [ $# -eq 0 ]; then
printf "(Unknown)\n"
return 1
fi

script=$1

# Initialise variables.
HEADER=""
COMMAND_NAME=""
COMMAND_VERSION=""

unset -f after_run # Clear the 'after_run' shell function before sourcing the script.

. "$script"

before_run # Call user's before run function.

line2 "$HEADER" "$LOAD_SYMBOL"
}

run_check() {
run
}

after_run_check() {
if [ "$COMMAND_NAME" != "" ] && [ "$COMMAND_VERSION" != "" ]; then
add_about_info "$COMMAND_NAME" "$COMMAND_VERSION"
fi

if type after_run > /dev/null 2>&1; then # Call the function 'after_run' only if the script implements it.
after_run
fi
done
}

about() {
[ "$SHOW_ABOUT" = 0 ] && return
if [ "$SHOW_ABOUT" = 0 ] || [ -z "$ABOUT" ]; then
return
fi

printf "\n %s${CYAN}About\n" "$SECTION_SYMBOL"
printf "\n %s${CYAN}About\n" "$SECTION_SYMBOL"

split_about_info | while IFS='|' read -r left right; do
line "$left" "$(extract_version "$right")"
done
split_about_info | while IFS='|' read -r left right; do
line "$left" "$(extract_version "$right")"
done
}

footer() {
line3 "╚" "╝" $((WIDTH+8)) "═"
line3 "╚" "╝" $((WIDTH+8)) "═"

END=$(date +%s)
time=$((END-START))
END=$(date +%s)
time=$((END-START))

if [ "$SHOW_LOG_URL" -eq 1 ]; then
line3 "$LOG_SYMBOL $LOG_URL" "$TIME_SYMBOL ${time}sec" "$((WIDTH+7))" " "
else
line3 "" "$TIME_SYMBOL ${time}sec" "$((WIDTH+7))" " "
fi
if [ "$SHOW_LOG_URL" -eq 1 ]; then
line3 "$LOG_SYMBOL $LOG_URL" "$TIME_SYMBOL ${time}sec" "$((WIDTH+7))" " "
else
line3 "" "$TIME_SYMBOL ${time}sec" "$((WIDTH+7))" " "
fi
}

main() {
init
header
check
about
footer

if [ "$HAS_ERRORS" -ne 0 ]; then
exit 1
fi
init
header
check
about
footer

if [ "$HAS_ERRORS" -ne 0 ]; then
exit 1
fi
}

main
Expand Down
2 changes: 1 addition & 1 deletion hooks/pre-commit.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# [pre-commit]
PROJECT_PATH="/path/to/project" # Absolute path to the project, e.g. /var/www/my-project
GITCC_PATH="path/to/gitcc" # Relative path to the project, e.g. "bin/git-commit-check"
GITCC_PATH="path/to/gitcc" # Relative path to the project, e.g. "bin/gitcc"
CONTAINER_ENGINE=$(which docker 2>/dev/null || which podman 2>/dev/null)
CONTAINER_NAME="container_name"
SHELL="/bin/sh"
Expand Down
Loading