Skip to content

Instantly share code, notes, and snippets.

@dalbrekt
Last active August 17, 2021 13:05
Show Gist options
  • Save dalbrekt/dcd80bce48c3f3021df4 to your computer and use it in GitHub Desktop.
Save dalbrekt/dcd80bce48c3f3021df4 to your computer and use it in GitHub Desktop.
Git hooks

1. Enable git templates:

$ git config --global init.templatedir '~/.git-templates'

This tells git to copy everything in ~/.git-templates to your per-project .git/ directory when you run git init

2. Create a directory to hold the global hooks:

$ mkdir -p ~/.git-templates/hooks

3. Write your hooks in ~/.git-templates/hooks.

4. Make sure the hook is executable.

$ chmod 755 ~/.git-templates/hooks/*

5. Re-initialize git in each existing repo you'd like to use this in:

$ git init

NOTE if you already have a hook defined in your local git repo, this will not overwrite it.

#!/bin/sh
#
# Stopping you from committing stuff you don't want in your repo
#
# Add to .git/hooks/pre-commit
#
# Override with --no-verify
#
# See http://git-scm.com/docs/githooks for more info.
#
count=`grep -iR 'console\.log\|console\.table' *.js | wc -l | awk '{print $1}'`
if [[ "$count" -ge 1 ]]; then
echo " Remove any config.log() or config.table() statements in Javascript sources"
exit 1
fi
count=`grep -iR 'println' *.scala | wc -l | awk '{print $1}'`
if [[ "$count" -ge 1 ]]; then
echo " Remove any println() statements in Scala sources"
exit 1
fi
count=`grep -iR 'System\.out\.println\|System\.err\.println' *.java | wc -l | awk '{print $1}'`
if [[ "$count" -ge 1 ]]; then
echo " Remove any System.out.println() or System.err.println() statements in Java sources"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment