From 3f897ecb3db649ef37d3381b90df51aef8ccde13 Mon Sep 17 00:00:00 2001 From: Maxime Côté Date: Mon, 18 Apr 2011 21:39:50 -0400 Subject: Easy hack Improve git pre-commit hook Change of the function check_and_fix_whitespace() to check only file with the extension listed (c|cpp|cxx|h|hrc|hxx|idl|inl|java|map|mk|MK|pmk|pl|pm|sdi|sh|src|tab|xcu|xml) --- git-hooks/pre-commit | 55 ++++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 27 deletions(-) (limited to 'git-hooks') diff --git a/git-hooks/pre-commit b/git-hooks/pre-commit index 6817990eeb98..6c6fe4ee662f 100755 --- a/git-hooks/pre-commit +++ b/git-hooks/pre-commit @@ -18,11 +18,6 @@ $ENV{LC_ALL} = "C"; sub fix_whitespace($$) { my ( $file, $lines ) = @_; - # usually we have nothing to do ;-) - return if ( keys( %{$lines} ) == 0 || - $file eq "" || $file eq "GNUmakefile" || - !( $file =~ /\.(c|cpp|cxx|h|hrc|hxx|idl|inl|java|map|MK|pl|pm|pmk|py|sdi|sh|src|tab)/ ) ); - open( IN, "$file" ) || die "Cannot open $file for reading"; my ( $out, $tmpfile ) = mkstemp( "/tmp/whitespace-fixing-XXXXXX" ); @@ -66,36 +61,42 @@ sub check_and_fix_whitespace($) my $fd; ( $fd, $stash ) = mkstemp( "/tmp/unstaged-changes-XXXXXX" ); close( $fd ); + # this will keep the staged changes system( "git diff > $stash" ); system( "git checkout ." ); } - open( IN, "git diff-index -p --no-prefix --cached $head -- |" ) || die "Cannot get git diff-index"; - while ( my $line = ) { - if ( $line =~ /^\+\+\+ (.*)/ ) { + open( FILES, "git diff-index --cached --name-only $head |" ) || die "Cannot run git diff-index."; + while( my $file = ) { + chomp( $file ); + if ( $file ne "GNUmakefile" && + ( $file =~ /\.(c|cpp|cxx|h|hrc|hxx|idl|inl|java|map|MK|pl|pm|pmk|py|sdi|sh|src|tab)/ ) ) { + open( F, "git diff-index -p --cached $head -- $file |" ); + while ( my $line = ) { + if ( $line =~ /^\+\+\+ (.*)/ ) { + %lines = (); + $line_no = 0; + $line_max = -1; + } + elsif ( $line =~ /^@@ -[0-9]+,[0-9]+ \+([0-9]+),([0-9]+) @@/ ) { + $line_no = $1; + $line_max = $line_no + $2; + } + elsif ( ( $line_no < $line_max ) && ( $line =~ /^[ +]/ ) ) { + if ( $line =~ /^\+.*[ \t]$/ ) { + $lines{$line_no} = 1; + } + ++$line_no; + } + } fix_whitespace( $file, \%lines ); - $file = $1; - %lines = (); - $line_no = 0; - $line_max = -1; - } - elsif ( $line =~ /^@@ -[0-9]+,[0-9]+ \+([0-9]+),([0-9]+) @@/ ) { - $line_no = $1; - $line_max = $line_no + $2; - } - elsif ( ( $line_no < $line_max ) && ( $line =~ /^[ +]/ ) ) { - if ( $line =~ /^\+.*[ \t]$/ ) { - $lines{$line_no} = 1; + close( IN ); + if ($stash) { + system( "git apply < $stash" ); + unlink( $stash ); } - ++$line_no; } } - fix_whitespace( $file, \%lines ); - close( IN ); - if ($stash) { - system( "git apply < $stash" ); - unlink( $stash ); - } } # Do the work :-) -- cgit