|
1 | | -#!/usr/bin/env sh |
| 1 | +#!/usr/bin/env bash |
2 | 2 | # SPDX-FileCopyrightText: NONE |
3 | 3 | # SPDX-License-Identifier: CC0-1.0 |
4 | 4 |
|
|
8 | 8 | # status after issuing an appropriate message if it wants to stop the |
9 | 9 | # commit. The hook is allowed to edit the commit message file. |
10 | 10 |
|
11 | | -grep -m 1 -e '^Signed-off-by: ' -- "${1}" || { |
| 11 | +COMMIT_MSG_FILE="${1:?}" |
| 12 | + |
| 13 | +COMMIT_MSG="$(cat "${COMMIT_MSG_FILE:?}")" || exit 2 |
| 14 | +CONVENTIONAL_COMMIT_REGEX='^(feat|fix|docs|style|refactor|test|chore|build|ci|perf|revert)(\([a-zA-Z0-9_.-]+\))?(!)?:\s.*$' |
| 15 | + |
| 16 | +# Check if the commit message matches the regex. |
| 17 | +if ! [[ "${COMMIT_MSG?}" =~ ${CONVENTIONAL_COMMIT_REGEX:?} ]]; then |
| 18 | + { |
| 19 | + echo "ERROR: Commit message does not follow Conventional Commits format." |
| 20 | + echo "" |
| 21 | + echo "The commit message should be structured as follows:" |
| 22 | + echo "<type>(<optional scope>): <description>" |
| 23 | + echo "[optional body]" |
| 24 | + echo "[optional footer(s)]" |
| 25 | + echo "" |
| 26 | + echo "Valid types are:" |
| 27 | + echo " feat: A new feature." |
| 28 | + echo " fix: A bug fix." |
| 29 | + echo " docs: Documentation changes." |
| 30 | + echo " style: Code style changes (formatting, missing semicolons, etc.)." |
| 31 | + echo " refactor: Code refactoring (neither fixes a bug nor adds a feature)." |
| 32 | + echo " test: Adding or updating tests." |
| 33 | + echo " chore: Routine tasks like updating dependencies or build tools." |
| 34 | + echo " build: Changes affecting the build system or external dependencies." |
| 35 | + echo " ci: Changes to CI configuration files or scripts." |
| 36 | + echo " perf: Performance improvements." |
| 37 | + echo " revert: Reverting a previous commit." |
| 38 | + echo "" |
| 39 | + echo "Examples:" |
| 40 | + echo " feat(auth): add login functionality" |
| 41 | + echo " fix(api)!: resolve timeout issue" |
| 42 | + echo " docs(readme): update installation instructions" |
| 43 | + echo "" |
| 44 | + } 1>&2 |
| 45 | + exit 1 |
| 46 | +fi |
| 47 | + |
| 48 | +grep -m 1 -e '^Signed-off-by: ' -- "${COMMIT_MSG_FILE:?}" || { |
12 | 49 | echo 1>&2 "Missing Signed-off-by line." |
13 | 50 | exit 46 |
14 | 51 | } |
| 52 | + |
| 53 | +exit 0 |
0 commit comments