aboutsummaryrefslogtreecommitdiffstats
path: root/.githooks/prepare-commit-msg
diff options
context:
space:
mode:
authorJakub Jirutka <jakub@jirutka.cz>2017-10-14 21:52:15 +0200
committerJakub Jirutka <jakub@jirutka.cz>2017-10-14 21:52:15 +0200
commit35d64d7e359cdc1382eaa8680c48ced61e05c01c (patch)
tree4ec9c9a0ae3ae98f756012daea2ffd7b6107716e /.githooks/prepare-commit-msg
parent097cf28fcb19221db7d7e87d18040439d63c0241 (diff)
downloadaports-35d64d7e359cdc1382eaa8680c48ced61e05c01c.tar.gz
aports-35d64d7e359cdc1382eaa8680c48ced61e05c01c.tar.bz2
aports-35d64d7e359cdc1382eaa8680c48ced61e05c01c.tar.xz
git: add hook prepare-commit-msg
Diffstat (limited to '.githooks/prepare-commit-msg')
-rwxr-xr-x.githooks/prepare-commit-msg45
1 files changed, 45 insertions, 0 deletions
diff --git a/.githooks/prepare-commit-msg b/.githooks/prepare-commit-msg
new file mode 100755
index 0000000000..44ae86d26a
--- /dev/null
+++ b/.githooks/prepare-commit-msg
@@ -0,0 +1,45 @@
+#!/bin/sh
+#
+# This hook adds prefix "<repo>/<pkgname>: " to the commit message when
+# committing changes of a single package.
+#
+MSG_FILE="$1"
+SOURCE="$2"
+
+longest_common_prefix() {
+ awk -F/ '
+ (NR == 1) { split($0, prefix); prefix_len = NF }
+ (NR > 1) {
+ for (i = 1; i <= prefix_len; i++) {
+ if (prefix[i] != $i) {
+ prefix_len = i - 1; break
+ }
+ }
+ }
+ (prefix_len == 0) { exit }
+ END {
+ res = prefix[1]
+ for (i = 2; i <= prefix_len; i++) {
+ res = res FS prefix[i]
+ }
+ print(res)
+ }'
+}
+
+prepend_msg() {
+ local prefix="$1"
+
+ printf '%s\n%s\n' "$prefix" "$(cat "$MSG_FILE")" > "$MSG_FILE"
+}
+
+
+# Do nothing if message has been given using -m, template, merge etc.
+[ -z "$SOURCE" ] || exit 0
+
+lcp=$(git diff-index --name-only --cached HEAD | longest_common_prefix)
+
+case "$lcp" in
+ [^.]*/*) prepend_msg "$(echo "$lcp" | cut -d/ -f1-2): ";;
+esac
+
+exit 0