summaryrefslogtreecommitdiff
path: root/Meta/lint-commit.sh
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-05-02 14:16:21 +0300
committerAndreas Kling <kling@serenityos.org>2021-05-02 16:28:01 +0200
commit028dad6e87b6728a20a30f3951c3f330eaf1395d (patch)
tree1b2698ad87ede2b109c502f8c023d185ef07b89d /Meta/lint-commit.sh
parenteffdd76bb25fc51353832ed56c43af9e673eee31 (diff)
downloadserenity-028dad6e87b6728a20a30f3951c3f330eaf1395d.zip
Meta: Add a post-commit commit message linter hook
This should help with getting commit messages tidy before they pass through CI's commit linter :^) For this hook to work pre-commit has to be explicitly installed via: `pre-commit install --hook-type commit-msg`
Diffstat (limited to 'Meta/lint-commit.sh')
-rwxr-xr-xMeta/lint-commit.sh35
1 files changed, 35 insertions, 0 deletions
diff --git a/Meta/lint-commit.sh b/Meta/lint-commit.sh
new file mode 100755
index 0000000000..d9360e98cb
--- /dev/null
+++ b/Meta/lint-commit.sh
@@ -0,0 +1,35 @@
+#!/usr/bin/env bash
+
+# the file containing the commit message is passed as the first argument
+commit_file="$1"
+commit_message=$(cat "$commit_file")
+
+error() {
+ echo -e "\033[0;31m$1:\033[0m"
+ echo "$commit_message"
+ exit 1
+}
+
+# fail if the commit message contains windows style line breaks (carriage returns)
+if grep -q -U $'\x0D' "$commit_file"; then
+ error "Commit message contains CRLF line breaks (only unix-style LF linebreaks are allowed)"
+fi
+
+line_number=0
+while read -r line; do
+ # ignore comment lines
+ [[ "$line" =~ ^#.* ]] && continue
+
+ ((line_number += 1))
+ line_length=${#line}
+
+ category_pattern="^\S.*?: .+"
+ if [[ $line_number -eq 1 ]] && (echo "$line" | grep -P -v -q "$category_pattern"); then
+ error "Missing category in commit title (if this is a fix up of a previous commit, it should be squashed)"
+ fi
+
+ if [[ $line_length -gt 72 ]]; then
+ error "Commit message lines are too long (maximum allowed is 72 characters)"
+ fi
+done <"$commit_file"
+exit 0