git hook to reject bad emails on the server.
Posted on February 19th, 2012 in git, node | 2 Comments »
In my git workflow I use git-commit-notifier to email beautiful, color-coded, diff emails out to the dev group anytime there’s a commit. Problem is, if a new engineer didn’t set his git author email properly that email will get silently rejected from the email group.
I finally got around to creating a git commit hook to reject the commit from the start, and inform the developer to update his or her git author email.
To use, simply create a pre-receive
file in your remote repos hooks
folder and make it executable.
I wrote this in Node so you’ll need to have Node installed on your server if it’s not there already:
#! /bin/env node var fs = require('fs'), arguments = fs.readFileSync('/dev/stdin').toString().split(' '); exec = require('child_process').exec; var email = 'yourcompany.com'; exec('git log -1 ' + arguments[1] + ' --format=%ae', function(err, stdout, stderr){ if (stdout !== '' && stdout.indexOf(email) == -1){ console.log('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'); console.log('Author email must contain ' + email + '! Your email is currently set to: ' + stdout); console.log('Please update by running: git config --global user.email "name@' + email + '"'); console.log('And then: git commit --amend --reset-author'); console.log('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'); process.exit(1); } process.exit(0); });
2 Responses
Good to have, but it needs to be updated to handle stuff like deleting a remote branch via:
git push -f origin :heads/stable
Right now it always fails as it fails to parse an email address.
Thanks for the find Mark. I’ve amended the code with a fix.