0

I need to get git to abort commits containing todo except when in the commit message "work in progress" text is specified.

Isn't there a way to access the commit message into the pre-commit hook given that I always provide it through the -m option, which means it should be available for all the available git hooks?

If not, how do I do?

smarber
  • 4,829
  • 7
  • 37
  • 78
  • 1
    Not in `pre-commit` hook, but you could try `commit-msg` hook - see here: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks#Client-Side-Hooks – 1615903 May 12 '16 at 09:17

1 Answers1

1

As 1615903 noted in a comment, you will need to use the commit-msg hook.

I need to get git to abort commits containing todo except when in the commit message "work in progress" text is specified.

You have not defined "containing todo" here, but let's take a stab at it anyway:

#! /bin/sh
# commit-msg hook: if "work in progress" is included, allow anything,
# otherwise require that no line adds the word "todo" anywhere.
#
# Note that this will forbid a commit that includes a text file
# or comment saying that you should not include the word "todo"
# in your commits!
grep "work in progress" "$1" >/dev/null && exit 0 # allow
git diff --cached | grep '^+.*todo' >/dev/null && {
    echo "commit aborted: found \"todo\" without \"work in progress\"" 1>&2
    exit 1 # forbid
}
exit 0 # test passed, allow

Note: this is entirely untested.

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775