]> code.delx.au - dotfiles/commitdiff
git: git-template for Jira issues
authorJames Bunton <jbunton@atlassian.com>
Tue, 26 Feb 2019 22:54:59 +0000 (09:54 +1100)
committerJames Bunton <jbunton@atlassian.com>
Tue, 26 Feb 2019 22:54:59 +0000 (09:54 +1100)
.git-template/hooks/prepare-commit-msg [new file with mode: 0755]
.gitconfig

diff --git a/.git-template/hooks/prepare-commit-msg b/.git-template/hooks/prepare-commit-msg
new file mode 100755 (executable)
index 0000000..b5fe1d1
--- /dev/null
@@ -0,0 +1,21 @@
+#!/bin/bash
+
+set -eu
+
+FILENAME="$1"
+
+function is_new_commit {
+    test -z "$(head -n1 "$FILENAME")"
+}
+
+function insert_issue_key {
+    local issue_key
+    issue_key="$(git symbolic-ref -q HEAD | grep -o "[A-Z]\+-[0-9]\+" | head -n1 || true)"
+    if [ -n "$issue_key" ]; then
+        sed -i "1i${issue_key}: " "$FILENAME"
+    fi
+}
+
+if is_new_commit; then
+    insert_issue_key
+fi
index 9e79711b9de280a2038e77727d613e4d271f8ed5..4c0abc361c794f3b2e97d8b6c4530d1c129dfedf 100644 (file)
@@ -1,3 +1,5 @@
+[init]
+       templatedir = ~/.git-template
 [core]
        excludesfile = ~/.gitignore_global
 [pager]