Bob van Luijt Bob van Luijt - 1 year ago 113
Git Question

How to get the Git Commit message in Git Tower?

I am using this script in the githook


import sys
import re
ret = 1
with open(sys.argv[1]) as msg:
res = re.match("^fix gh-[0-9]+.*$", msg.readline())
if res != None:
ret = 0
if (ret != 0):
print("Wrong commit message. Example: 'fix gh-1234 foo bar'")

The problem is that Git Tower doesn't seem to include any arguments inside
. How to solve this in a way that I can use git both from the command line as in a GUI like Git Tower?

Answer Source

Figured this out with the help of the Tower support team.

In my example I wasn't able to grab the argument (ie: #!/usr/bin/python) by changing this to #!/usr/bin/env bash I was able to get it. Now $1 contains the argument.

Complete example:

#!/usr/bin/env bash

# regex to validate in commit msg

    error_msg="Aborting commit. Your commit message is missing either a Github Issue ('GH-xxxx') or 'Merge'"

    if ! grep -iqE "$commit_regex" "$1"; then
        echo "$error_msg" >&2
        exit 1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download