Hacking Thy Fearful Symmetry

Hacker, hacker coding bright

Git's the nightclub, Perltidy's the bouncer

February 9, 2008
perl git perl::tidy

I finally wrapped my CVS-encrusted mind around Git's hooks. Huzzah! The biggest hurdle, really, was realizing that there is no spoon.

Anyway, as I'm an unsalvageable slob who always forget to run perltidy before committing changes, I've written a pre-commit hook that makes sure that all Perl files to be committed are all clean, neat and tidy (and aborts the commit and chides me if they are not):

#!/usr/bin/perl
 
use Perl6::Slurp;
 
my $status = slurp '-|', 'git-status';
 
# only want what is going to be commited
$status =~ s/Changed but not updated.*$//s;
 
my @dirty =
  grep { !file_is_tidy($_) }                   # not tidy
  grep {/\.(pl|pod|pm|t)$/ }                  # perl file
  map  {/(?:modified|new file):\s+(\S+)/ }    # to be commited
  split "\n", $status;
 
exit 0 unless @dirty;                          # Alles gut
 
warn "following files are not tidy, aborting commit\n",
     map "\t$_\n" => @dirty;
 
exit 1;
 
### utility functions ###############################################
 
sub file_is_tidy {
    my $file = shift;
 
    my $original = slurp $file;
    my $tidied = slurp '-|', 'perltidy', $file, '-st';
 
    return $original eq $tidied;
}
comments powered by Disqus

About the author

Yanick Champoux
Perl necrohacker , ACP writer, orchid lover. Slightly bonker all around. he/him/his