Skip to content

Instantly share code, notes, and snippets.

@danpal
Last active December 24, 2015 14:48
Show Gist options
  • Save danpal/6814795 to your computer and use it in GitHub Desktop.
Save danpal/6814795 to your computer and use it in GitHub Desktop.
Authy Pre-commit hook
#!/bin/sh
# Check commits for trailing spaces, tabs at the begging of file
# and bad merges
# Save on .git/hooks/pre-commit
# Make file executable
if git rev-parse --verify HEAD 2>/dev/null
then
git diff-index -p -M --cached HEAD
else
:
fi |
perl -e '
my $found_bad = 0;
my $filename;
my $reported_filename = "";
my $lineno;
sub bad_line {
my ($why, $line) = @_;
if (!$found_bad) {
print STDERR "*\n";
print STDERR "* You have some suspicious patch lines:\n";
print STDERR "*\n";
$found_bad = 1;
}
if ($reported_filename ne $filename) {
print STDERR "* In $filename\n";
$reported_filename = $filename;
}
print STDERR "* $why (line $lineno)\n";
print STDERR "$filename:$lineno:$line\n";
}
while (<>) {
if (m|^diff --git a/(.*) b/\1$|) {
$filename = $1;
next;
}
if (/^@@ -\S+ \+(\d+)/) {
$lineno = $1 - 1;
next;
}
if (/^ /) {
$lineno++;
next;
}
if (s/^\+//) {
$lineno++;
chomp;
if (/^\t/) {
bad_line("Line starts with TAB", $_);
}
if (/\s$/) {
bad_line("Trailing whitespace", $_);
}
if (/^\s* /) {
bad_line("Indented SPACE followed by a TAB", $_);
}
if (/^(?:[<>=]){7}/) {
bad_line("Unresolved merge conflict", $_);
}
}
}
exit($found_bad);
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment