4

I need to make a commit-msg hook to check if the commit message contains "app.asana" in any part of this. I searched some references and documentation and I know I need to use the commit-msg for this. I have to make this using Perl or Bash.

Does anybody has a clue about this or somewhere I can look more examples to learn how to do it??

Thank you.

lol
  • 403
  • 4
  • 12
  • [This](https://www.kernel.org/pub/software/scm/git/docs/githooks.html) might help. – devnull Oct 09 '13 at 13:13
  • I already been to this page but thanks. And after some more research I did it... It was actually really simple. – lol Oct 09 '13 at 15:02

1 Answers1

10

I found an answer to my question. In case it may help somebody...

I just created a commit-msg file in .git/hooks containing

#!/bin/sh

test -n "$(grep 'app.asana.com/' ${1})" || {
        echo >&2 "ERROR: Commit message is missing Asana's task link.\n\nPlease append the Asana's task link relative to this commit into the commit message."
        exit 1
}

Each user needs to have a commit-msg file inside .git/hooks. Then, as a solution, I added commit-msg to the project folder (so I can pull this) with another file called commit-msg-hook.sh containing:

#!/bin/sh

cp commit-msg .git/hooks/commit-msg
chmod +x .git/hooks/commit-msg
echo "\nThe commit-msg hook was added to git hooks"

I welcome any advice to improve what I've done. Thanks.

mustaccio
  • 18,234
  • 16
  • 48
  • 57
lol
  • 403
  • 4
  • 12
  • 1
    Is it just me or is the 2nd line in the first code snippet not needed/not going to work. `chmod a+x .git/hooks/commit-msg` is to make the commit-msg executable, in order for it to run with git. But, the file (and thus that command) would never be able to execute if it did not already have `+x` so including this command in the actual file would not make sense. This post is from 2013, so anyone please correct me if im wrong. Surely someone else noticed this? – redfox05 Jan 21 '15 at 15:41
  • @Russell You are correct. I fixed it, it is being peer reviewed atm. – greduan May 12 '15 at 16:23