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

Miklos Vajna vmiklos at kemper.freedesktop.org
Thu Mar 3 04:09:07 PST 2011


 git-hooks/pre-commit |   16 ++++++++++++++++
 1 file changed, 16 insertions(+)

New commits:
commit 1cea097585a1a413efd4803635384e8b7f803540
Author: Miklos Vajna <vmiklos at frugalware.org>
Date:   Thu Mar 3 01:30:27 2011 +0100

    git-hooks: don't stage unstaged hunks before commit
    
    We call 'git add' for files where we fixed whitespaces in. This is a
    problem in case the user staged only part of a file - as a result we do
    not just fix up whitespace in the patch but also stage other hunks of
    the file.
    
    Fix the problem by removing/restoring not staged changes before/after
    whitespace fixing. The operation is cheap enough, as it's a noop in case
    there are no unstaged changes.

diff --git a/git-hooks/pre-commit b/git-hooks/pre-commit
index cee3a7f..8192e58 100755
--- a/git-hooks/pre-commit
+++ b/git-hooks/pre-commit
@@ -58,6 +58,18 @@ sub check_and_fix_whitespace($)
     my $line_no = 0;
     my $line_max = -1;
 
+    my $stash = "";
+
+    # any not staged changes to stash away?
+    system( "git update-index -q --refresh" );
+    if ( `git diff --name-only --` ) {
+        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 =~ /^\+\+\+ (.*)/ ) {
@@ -80,6 +92,10 @@ sub check_and_fix_whitespace($)
     }
     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