Git stop words

If you add development code lines to your file which must not be committed to the code base (e.g. temporarily disabled code, fixated variables, noisy outputs), mark them with the word NOCOMMIT by putting it in a comment, a variable name, …

if(true) {//NOCOMMIT counter > 5 && testThis)
    myNewFeatureToTest();
} else {
    someOtherCode();
}
int NOCOMMIT = 5;
int myVariable = 1;
myVariable = 5; //NOCOMMIT

To activate instant rejection by git as soon as you try to commit this code, do the following:

1. Put this helper script in the file your repository checkout/.git/hooks/showlinenum.awk
2. and the following hook in the file your repository checkout/.git/hooks/pre-commit:

#!/bin/sh
#
# Dismisses the commit if it adds illegal statements (see variable $searchPhrase)

if git rev-parse --verify HEAD >/dev/null 2>&1
then
    against=HEAD
else
    # Initial commit: diff against an empty tree object
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# Redirect output to stderr.
exec 1>&2
# ""
# Test for NOCOMMIT and other typical debugging lines
searchPhrase='NOCOMMIT\|(true ||\|(true||\|(false &&\|(false&&'

DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

. git-sh-setup  # for die
git-diff-index -p -M --cached $against -- | $DIR/showlinenum.awk show_path=1 | grep -E '^.+[0-9]+:\+' |
grep "$searchPhrase" && echo "" && die Rejected commit since the above lines contain illegal statements. Use git commit -n to ignore

# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --

Tip1: best add the stop word to a line which will cause a compile failure if you would commit the lines around it but forgot the one with the stop word (cf. the if condition above).

Tip2: if you want to ignore this hook for one commit, use git commit -n -m "[tag] Your commit message".

Leave a Reply

Your comment will be moderated.

seven × = forty two