[Libreoffice-commits] .: Branch 'libreoffice-3-3' - bin/fix-trailing-space.sh bin/fix-trailing-whitespace.pl git-hooks/pre-commit
Jan Holesovsky
kendy at kemper.freedesktop.org
Tue Nov 9 08:16:24 PST 2010
bin/fix-trailing-space.sh | 12 ------
bin/fix-trailing-whitespace.pl | 75 +++++++++++++++++++++++++++++++++++++++++
git-hooks/pre-commit | 16 ++------
3 files changed, 79 insertions(+), 24 deletions(-)
New commits:
commit 659a9f08b73d21218ccf163cf11ece292c876cba
Author: Jan Holesovsky <kendy at suse.cz>
Date: Tue Nov 9 17:03:58 2010 +0100
Remove trailing whitespace only on the added/changed lines.
Previously we were removing all the trailing whitespace in every changed file,
which was considered too intrusive.
diff --git a/bin/fix-trailing-space.sh b/bin/fix-trailing-space.sh
deleted file mode 100755
index ac070bf..0000000
--- a/bin/fix-trailing-space.sh
+++ /dev/null
@@ -1,12 +0,0 @@
-#!/bin/sh
-
-# when git commit gives the following, feed this error output as input to this script
-# l10ntools/java/jpropex/java/JPropEx.java:28: trailing whitespace.
-# +
-# l10ntools/scripts/makefile.mk:4: trailing whitespace.
-# +#
-
-files=`cat - | grep ': trailing whitespace\.$' | sed 's/:[0-9]*: trailing whitespace.$//' | sort -u`
-for file in $files; do
- sed -i 's/\s*$//' $file
-done
diff --git a/bin/fix-trailing-whitespace.pl b/bin/fix-trailing-whitespace.pl
new file mode 100755
index 0000000..06614dd
--- /dev/null
+++ b/bin/fix-trailing-whitespace.pl
@@ -0,0 +1,75 @@
+#!/usr/bin/perl -w
+
+# Reads a patch, and according to what it sees there, it fixes trailing
+# whitespace in the files it touches. To be used in conjuction with
+# git diff-index, so it assumes that the patch is 'correct' (but has some
+# basic checks to avoid shooting into the leg)
+#
+# The patch has to be ready for -p0 application
+#
+# Usage: git diff-index -p --no-prefix --cached HEAD | fix-trailing-whitespace.pl
+
+use strict;
+use File::Temp qw/ :mktemp /;
+use File::Copy;
+
+my $patch = $1;
+my $file = "";
+my %lines = ();
+my $line_no = 0;
+my $line_max = -1;
+
+# $1 - file to fix
+# $2 - list of lines containing whitespace errors
+sub fix_whitespace($%) {
+ my ( $file, $lines ) = @_;
+
+ # usually we have nothing to do ;-)
+ return if ( keys( %lines ) == 0 ||
+ $file eq "" ||
+ !( $file =~ /\.(c|cpp|cxx|h|hrc|hxx|idl|inl|java|map|mk|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" );
+
+ my $line_no = 1;
+ while ( my $line = <IN> ) {
+ if ( $lines{$line_no} && $line =~ /^(.*[^ \t])[ \t]+$/ ) {
+ print $out "$1\n";
+ }
+ elsif ( $lines{$line_no} && $line =~ /^[ \t]+$/ ) {
+ print $out "\n";
+ }
+ else {
+ print $out $line;
+ }
+ ++$line_no;
+ }
+ close( $out );
+ close( IN );
+
+ move( $tmpfile, $file ) or die "Cannot move '$tmpfile' to '$file'";
+ print "$file\n"
+}
+
+# go through the patch and collect lines to fix
+while ( my $line = <STDIN> ) {
+ if ( $line =~ /^\+\+\+ (.*)/ ) {
+ 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;
+ }
+ ++$line_no;
+ }
+}
+fix_whitespace( $file, \%lines );
diff --git a/git-hooks/pre-commit b/git-hooks/pre-commit
index 0b53fbf..6d3cf3a 100755
--- a/git-hooks/pre-commit
+++ b/git-hooks/pre-commit
@@ -85,18 +85,10 @@ if test -n "$ERRORS" ; then
fi
# fix whitespace in code
-git diff-index --check --cached $against -- | \
- grep ': trailing whitespace\.$' | \
- sed 's/:[0-9]*: trailing whitespace.$//' | sort -u |
- while read FILE ; do
- if echo "$FILE" | grep -qs '\.\(c\|cpp\|cxx\|h\|hrc\|hxx\|idl\|java\)' ; then
- # unfortunately, git stripspace works only as a filter :-(
- TEMPFILE=`mktemp`
- git stripspace < "$FILE" > "$TEMPFILE"
- mv "$TEMPFILE" "$FILE"
- git add "$FILE"
- echo "Fixed whitespace in '$FILE'."
- fi
+git diff-index -p --no-prefix --cached $against -- | bin/fix-trailing-whitespace.pl |
+ while read F ; do
+ git add $F
+ echo "Fixed whitespace in '$F'."
done
# check the rest of the files
More information about the Libreoffice-commits
mailing list