Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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.

like image 364
andilabs Avatar asked Oct 24 '25 15:10

andilabs


1 Answers

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
like image 65
sschuberth Avatar answered Oct 26 '25 04:10

sschuberth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!