Skip to content

Commit 6fb1009

Browse files
committed
Add git-commit-msg-hook script
The script is verifying if the minimum body length contains 10 characters. Also raise a warning if there is no: "Signed-Off-By". Signed-off-by: Daniel Pawlik <[email protected]>
1 parent 975ab76 commit 6fb1009

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Check if commit message body is not too short
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, edited, reopened]
6+
7+
jobs:
8+
verify-body-length:
9+
runs-on: ubuntu-latest
10+
# set as non-voting for now.
11+
continue-on-error: true
12+
13+
permissions:
14+
contents: write
15+
pull-requests: write
16+
repository-projects: write
17+
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@v4
21+
with:
22+
fetch-depth: 0
23+
24+
- name: Run commit message check
25+
id: bodylength
26+
run: |
27+
set +e
28+
./scripts/git-check-commit-body-length.sh > result.log 2>&1
29+
EXIT_CODE=$?
30+
echo "exit_code=$EXIT_CODE" >> $GITHUB_OUTPUT
31+
cat result.log
32+
33+
- name: Comment on PR if body length check failed
34+
if: steps.bodylength.outputs.exit_code != '0'
35+
uses: peter-evans/create-or-update-comment@v5
36+
with:
37+
issue-number: ${{ github.event.pull_request.number }}
38+
body-path: ./result.log
39+
reactions: confused
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
3+
MSG_FILE="$1"
4+
MIN_BODY_LEN=10
5+
6+
# If no file provided, get latest commit message
7+
if [ -z "$MSG_FILE" ]; then
8+
TMP_FILE=$(mktemp)
9+
git log -1 --pretty=format:"%B" >"$TMP_FILE"
10+
MSG_FILE="$TMP_FILE"
11+
fi
12+
13+
# 0 = pass, 1 = fail
14+
FAIL_LENGTH=0
15+
FAIL_SIGNED_OFF_BY=0
16+
17+
BODY=$(tail -n +3 "$MSG_FILE" | sed '/^\s*#/d' | sed '/^\s*$/d')
18+
BODY_LEN=$(echo -n "$BODY" | wc -m)
19+
20+
if [ "$BODY_LEN" -lt "$MIN_BODY_LEN" ]; then
21+
echo -e "\n\n**WARNING: Commit message body is too short (has $BODY_LEN chars, minimum $MIN_BODY_LEN required).**\n" >&2
22+
echo "Please add a detailed explanation after the subject line." >&2
23+
FAIL_LENGTH=1
24+
fi
25+
26+
if ! grep -qi '^Signed-off-by:' "$MSG_FILE"; then
27+
echo -e "\n\n**WARNING: Missing 'Signed-off-by:' line in commit message.**\n" >&2
28+
echo "Add: Signed-off-by: Your Name <[email protected]>" >&2
29+
FAIL_SIGNED_OFF_BY=1
30+
fi
31+
32+
[ -n "$TMP_FILE" ] && rm -f "$TMP_FILE"
33+
34+
if [ "$FAIL_LENGTH" -eq 0 ] && [ "$FAIL_SIGNED_OFF_BY" -eq 0 ]; then
35+
echo "Commit message passes all checks."
36+
exit 0
37+
else
38+
echo -e "\nSome checks failed. See warnings above.\n"
39+
exit 1
40+
fi

0 commit comments

Comments
 (0)