diff options
author | Maxime Côté <cote.maxime1@gmail.com> | 2011-04-18 21:39:50 -0400 |
---|---|---|
committer | Cédric Bosdonnat <cedric.bosdonnat.ooo@free.fr> | 2011-04-22 11:19:31 +0200 |
commit | 3f897ecb3db649ef37d3381b90df51aef8ccde13 (patch) | |
tree | 591ccc53b5dd875990005a91e5010010aaca29b5 | |
parent | 38b89de95843f72ccd9ee03e052eca5ae1ecdcdd (diff) |
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)
-rwxr-xr-x | git-hooks/pre-commit | 55 |
1 files changed, 28 insertions, 27 deletions
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 = <IN> ) { - if ( $line =~ /^\+\+\+ (.*)/ ) { + open( FILES, "git diff-index --cached --name-only $head |" ) || die "Cannot run git diff-index."; + while( my $file = <FILES> ) { + 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 = <F> ) { + 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 :-) |