4

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?

Bob van Luijt
  • 7,153
  • 12
  • 58
  • 101
  • This is a problem with SmartGit and other GUI tools too. – prabodhprakash Sep 29 '16 at 13:42
  • This sounds to me like a bug in Git Tower, as your hook looks fine. Since [they claim that hooks should work](https://www.git-tower.com/help/mac/faq-and-tips/faq/hook-scripts) (Albeit, messages should be printed to stderr), I'd contact [Git Tower Support](https://www.git-tower.com/support/contact) about this. – Hasturkun Sep 29 '16 at 13:46
  • Check, contacted the support team – Bob van Luijt Sep 29 '16 at 14:42

1 Answers1

2

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
Bob van Luijt
  • 7,153
  • 12
  • 58
  • 101