From eb5949465793fa9f6ca3e0ed0c58d5a0d122c1e7 Mon Sep 17 00:00:00 2001 From: Eden Kirin Date: Fri, 1 Sep 2023 09:41:55 +0200 Subject: [PATCH] Add prepare-commit-msg hook --- intis-hooks/prepare-commit-msg | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100755 intis-hooks/prepare-commit-msg diff --git a/intis-hooks/prepare-commit-msg b/intis-hooks/prepare-commit-msg new file mode 100755 index 0000000..9ebb104 --- /dev/null +++ b/intis-hooks/prepare-commit-msg @@ -0,0 +1,40 @@ +#!/usr/bin/python3 + +# put this in .git/hooks and ensure it has exec permissions: "chmod +x prepare-commit-msg" +# use the following steps to use it globally +# - create ~/.git-templates +# - run the command: "git config --global init.templateDir '~/.git-templates'" +# - reinitialize existing git repos you wish to use this in: "git init" + +import re, subprocess, sys + +JIRA_ISSUE_URL = "https://smartvending.atlassian.net/browse/" +PREPEND_TICKET = True +APPEND_ISSUE_LINK = True + +commit_file = sys.argv[1] +regex = re.compile("^([A-Z]+-[0-9]+)", re.IGNORECASE) + +git_branch_name = subprocess.run( + ["git", "rev-parse", "--abbrev-ref", "HEAD"], + stdout=subprocess.PIPE, + encoding="utf-8", +) + +match = regex.match(git_branch_name.stdout) + +if match: + ticket = match.group() + ticket_url = f"{JIRA_ISSUE_URL}{ticket}" + + with open(commit_file, "r+") as f: + contents = f.read().strip() + + if PREPEND_TICKET and not contents.startswith(ticket): + contents = f"{ticket}: {contents}" + + if APPEND_ISSUE_LINK: + contents = f"{contents}\n\n{ticket_url}" + + f.seek(0) + f.write(contents)