Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git warn before committing if string appears in source (or diff)

Tags:

I would like to be prevented† when staging‡ in a git repo if the changes I am about to commit contain a certain string (say, @todo or @hack).

Can someone show me how to achieve this?

† or warned.
‡ or when committing.

like image 576
Alan H. Avatar asked Sep 03 '11 04:09

Alan H.


2 Answers

A simple pre-commit hook that checks if the string '@todo' is being added could look like:

#!/bin/sh  . git-sh-setup  # for die  if git-diff-index -p -M --cached HEAD -- \ | grep '^+' \ | grep @todo; then     die Blocking commit because string @todo detected in patch fi 

If this is the content of .git/hooks/pre-commit and is executable, any patch that adds the string @todo will be rejected.

like image 162
William Pursell Avatar answered Sep 28 '22 03:09

William Pursell


For a more general and thorough solution to this, take a look at Git Confirm:

Asciinema Git Confirm demo

like image 25
Tim Perry Avatar answered Sep 28 '22 03:09

Tim Perry