andilabs
andilabs

Reputation: 23351

how to create git pre hook for detecting 'TODO' or any certain string in commit?

E.g pycharm has nice feature of warning user before commiting code containing 'TODO'... I would like to have something like that in my standard command line used git. I heard it will be possible using git pre-hook. I will appreciate some hint how to accomplish that nice.

Upvotes: 3

Views: 2786

Answers (2)

mic4ael
mic4ael

Reputation: 8310

Something as simple as this would do the trick

#!/bin/sh

PWD=$("pwd")
if git grep TODO $PWD; then
    exit 1
else
    exit 0
fi

Upvotes: -1

sschuberth
sschuberth

Reputation: 29867

As a simple solution, you could save the following shell script as .git/hooks/pre-commit:

#!/bin/sh

files=$(git diff --cached --name-only --diff-filter=AM)

if [ -n "$files" ]; then
    if grep -H TODO $files; then
        echo "Blocking commit as TODO was found."
        exit 1
    fi
fi

Upvotes: 6

Related Questions