Bob van Luijt Bob van Luijt - 1 month ago 8
Git Question

How to get the Git Commit message in Git Tower?

I am using this script in the githook

commit-msg
.

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


The problem is that Git Tower doesn't seem to include any arguments inside
argv
. 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

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

    commit_regex='(gh-\d+|merge)'
    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
    fi
Comments