Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent git commit if specific phrase in changes is detected?

I noticed some patterns, that developers tend to do - like committing javascript tests with fdescribe or fit left in them (which means only one test/suite will be running), usually that is found in review phase, but it would be nice to catch those small things earlier. Wondering if there is a way to configure git to prevent commit if some pattern is detected within the changes?

like image 602
Giedrius Avatar asked Sep 30 '15 13:09

Giedrius


People also ask

How do I exclude a commit?

Set “–assume-unchanged” to a path to exclude to check on git commit and it will exclude your file from git commit. You will need to use the git update-index and –assume-unchanged to exclude files from git commit.

How do I stop a pre-commit hook?

Quick tip if you want to skip the pre-commit validations and quickly want to get a commit out there. To get your commit through without running that pre-commit hook, use the --no-verify option. Voila, without pre-commit hooks running!


1 Answers

That's a classical job for git commit hooks (man githooks); for example, from the standard script samples that you get in .git/hooks, you can add a script pre-push to that folder (make it executable!).

This example script from the git project only checks the commit names, but if you replace the

git rev-list

line with something like

git diff $remote_sha $local_sha

and grep for your suspcious strings, e.g.

git diff $remote_sha $local_sha|grep -E '^\+.*(fdescribe|fit)'

you can make this happen for your case.

#!/bin/sh

# An example hook script to verify what is about to be pushed.  Called by "git
# push" after it has checked the remote status, but before anything has been
# pushed.  If this script exits with a non-zero status nothing will be pushed.
#
# This hook is called with the following parameters:
#
# $1 -- Name of the remote to which the push is being done
# $2 -- URL to which the push is being done
#
# If pushing without using a named remote those arguments will be equal.
#
# Information about the commits which are being pushed is supplied as lines to
# the standard input in the form:
#
#   <local ref> <local sha1> <remote ref> <remote sha1>
#
# This sample shows how to prevent push of commits where the log message starts
# with "WIP" (work in progress).

remote="$1"
url="$2"

z40=0000000000000000000000000000000000000000

while read local_ref local_sha remote_ref remote_sha
do
    if [ "$local_sha" = $z40 ]
    then
        # Handle delete
        :
    else
        if [ "$remote_sha" = $z40 ]
        then
            # New branch, examine all commits
            range="$local_sha"
        else
            # Update to existing branch, examine new commits
            range="$remote_sha..$local_sha"
        fi

        # Check for WIP commit
        commit=`git rev-list -n 1 --grep '^WIP' "$range"`
        if [ -n "$commit" ]
        then
            echo >&2 "Found WIP commit in $local_ref, not pushing"
            exit 1
        fi
    fi
done

exit 0
like image 137
Marcus Müller Avatar answered Nov 01 '22 13:11

Marcus Müller