#!/usr/bin/perl # # Check an svn transaction for leading tabs # # $Id$ # Author: Andy Grundman # # Inspired by a Python pre-commit script that I found here: # http://blog.wordaligned.org/articles/2006/08/09/a-subversion-pre-commit-hook # use strict; use constant DEBUG => 0; use constant SVNLOOK => '/usr/bin/svnlook'; my $test; my $repos = shift; if ( $repos eq '-r' ) { $test = 1; $repos = shift; } my $txn = shift; unless ( $repos && $txn ) { usage(); } # Get list of changed files my @files; my $cmd = sprintf "%s %s %s %s %s", SVNLOOK, 'changed', $repos, $test ? '--revision' : '--transaction', $txn; DEBUG && warn "$cmd\n"; open my $changed, "$cmd |" or die "Unable to run $cmd: $!\n"; while (<$changed>) { chomp; if ( /^[AU]\s+(.+)/ ) { my $path = $1; # Skip dirs if ( $path !~ m{/$} ) { push @files, $path; } } } close $changed; # Check each file for tabs my @has_tabs; for my $file ( @files ) { my $cmd = sprintf "%s %s %s %s %s %s", SVNLOOK, 'cat', $repos, $test ? '--revision' : '--transaction', $txn, $file; DEBUG && warn "$cmd\n"; open my $cat, "$cmd |" or die "Unable to run $cmd: $!\n"; my @lines = <$cat>; close $cat; my $line = 1; for ( @lines ) { if ( /^\t/ ) { push @has_tabs, { file => $file, line => $line, }; last; } $line++; } } if ( @has_tabs ) { warn "Leading tabs were found in the following file(s). Please correct and re-commit.\n"; for my $file ( @has_tabs ) { my $error = sprintf "Line: %4d %s\n", $file->{line}, $file->{file}; warn $error; } exit 1; } exit 0; sub usage { warn <