When developing software, especially if working with other people, emotions may run high at times. Nonetheless, conversations should be kept as civil as possible. To help you in this regard, here’s a short git hook that performs basic sentiment analysis on your commit messages:

#!/usr/bin/env python
#
# Checks the input file for negative sentiments and exits with non-zero return
# status in case the message appears to be too negative.

from afinn import Afinn

import sys

with open(sys.argv[1]) as f:
    afinn = Afinn()
    text  = f.read()
    score = afinn.score(text)
    if score < -4.0:
        print(  "This commit seems to be written very negatively. You might want\n"
                "to overthink it and rewrite it. If you are really, really sure,\n"
                "use\n\n"
                "  git commit --no-verify\n\n"
                "to skip this check." )
        sys.exit(-1)

Installation

Install the script by copying it into the local .git/hooks directory under the name commmit-msg. Don’t forget to make the file executable:

$ cp sentiment_analysis.py .git/hooks/commit-msg
$ chmod +x .git/hooks/commit-msg

Usage

The hook will now be executed whenever you create or change a new commit message. If the sentiment of the message is considered too negative, the commit will be aborted. This is how an interaction may look like:

$ touch Foo
$ git add Foo
$ git commit -m "Your code is crap and you are dumb"
This commit seems to be written very negatively. You might want
to overthink it and rewrite it. If you are really, really sure,
use

  git commit --no-verify

to skip this check.
$ git commit -m "Your code is okayish and you are not completely stupid"
[master 0c97888] Your code is okayish and you are not completely stupid
 1 file changed, 0 insertions(+), 0 deletions(-)
    create mode 100644 Foo

The hook requires a working installation of the cool AFINN sentiment analysis library. Thanks for Finn Årup Nielsen for providing it!

Known issues & disclaimer

The hook cannot detect sarcasm. Also, cleverly-worded insults will not be understood either. This is obviously provided as a tongue-in-cheek script—above all, do no harm to your fellow developers.

Happy developing!