[Libreoffice-commits] .: git-hooks/pre-commit

Cédric Bosdonnat cbosdo at kemper.freedesktop.org
Fri Apr 22 02:51:54 PDT 2011


 git-hooks/pre-commit |   55 +++++++++++++++++++++++++--------------------------
 1 file changed, 28 insertions(+), 27 deletions(-)

New commits:
commit 4233baef4b197ab6857afa370a24a168cc68ca13
Author: Maxime Côté <cote.maxime1 at gmail.com>
Date:   Mon Apr 18 21:39:50 2011 -0400

    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)

diff --git a/git-hooks/pre-commit b/git-hooks/pre-commit
index 6817990..6c6fe4e 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 :-)


More information about the Libreoffice-commits mailing list