summaryrefslogtreecommitdiff
path: root/.husky
diff options
context:
space:
mode:
authorChris Kay <chris.kay@arm.com>2020-12-09 14:35:46 +0000
committerChris Kay <chris.kay@arm.com>2021-04-19 14:06:25 +0100
commitc75ce067aa7c47c2ef78e2598ef7e46d6bbc9e24 (patch)
tree19ecfc1b75fe8d0fe2be065d4bfaf1b9a2ae1cf2 /.husky
parent4b7eee81e3593b66e7340699ea0dafe568cc003a (diff)
build(hooks): add Commitizen hook
This change adds Commitizen, an interactive tool for writing commit messages, to the package.json file. This installs Commitizen within the `node_modules` directory automatically when developers invoke `npm install` from the root repository directory. Additionally, this change adds a prepare-commit-msg Git hook which invokes Commitizen prior to generation of the default commit message. It may be exited with the standard ^C signal without terminating the commit process for those who desperately want to avoid using it, but otherwise should encourage developers to conform to the new commit style without running into post-commit linting errors. Change-Id: I8a1e268ed40b61af38519d13d62b116fce76a494 Signed-off-by: Chris Kay <chris.kay@arm.com>
Diffstat (limited to '.husky')
-rwxr-xr-x.husky/prepare-commit-msg6
-rwxr-xr-x.husky/prepare-commit-msg.cz28
2 files changed, 34 insertions, 0 deletions
diff --git a/.husky/prepare-commit-msg b/.husky/prepare-commit-msg
new file mode 100755
index 000000000..593dfa88b
--- /dev/null
+++ b/.husky/prepare-commit-msg
@@ -0,0 +1,6 @@
+#!/bin/sh
+
+# shellcheck source=./_/husky.sh
+. "$(dirname "$0")/_/husky.sh"
+
+"$(dirname "$0")/prepare-commit-msg.cz" "$@"
diff --git a/.husky/prepare-commit-msg.cz b/.husky/prepare-commit-msg.cz
new file mode 100755
index 000000000..724527d6f
--- /dev/null
+++ b/.husky/prepare-commit-msg.cz
@@ -0,0 +1,28 @@
+#!/bin/bash
+
+file="$1"
+type="$2"
+
+if [ -z "$type" ]; then # only run on new commits
+ #
+ # Save any commit message trailers generated by Git.
+ #
+
+ trailers=$(git interpret-trailers --parse "$file")
+
+ #
+ # Execute the Commitizen hook.
+ #
+
+ (exec < "/dev/tty" && npx --no-install git-cz --hook) || true
+
+ #
+ # Restore any trailers that Commitizen might have overwritten.
+ #
+
+ printf "\n" >> "$file"
+
+ while IFS= read -r trailer; do
+ git interpret-trailers --in-place --trailer "$trailer" "$file"
+ done <<< "$trailers"
+fi