This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[HOOK] Fix leading and trailing whitespace
- From: OndÅej BÃlka <neleai at seznam dot cz>
- To: libc-alpha at sourceware dot org
- Date: Wed, 5 Jun 2013 23:34:20 +0200
- Subject: [HOOK] Fix leading and trailing whitespace
Hi,
As I noticed problem of patches with trailing whitespaces I
decided to use a pre commit hook to solve this problem once for all.
I wrote a simplified c parser below:
If you also want to use it add
./scripts/fix_leading_trailing_space_git to .git/hooks/pre-commit
As regenerated files need to remove these would it be possible to
automaticaly run this script after generation?
Comments?
---
scripts/fix_leading_trailing_space.c | 101 ++++++++++++++++++++++++++++++++
scripts/fix_leading_trailing_space_git | 21 +++++++
2 files changed, 122 insertions(+)
create mode 100644 scripts/fix_leading_trailing_space.c
create mode 100755 scripts/fix_leading_trailing_space_git
diff --git a/scripts/fix_leading_trailing_space.c b/scripts/fix_leading_trailing_space.c
new file mode 100644
index 0000000..de48156
--- /dev/null
+++ b/scripts/fix_leading_trailing_space.c
@@ -0,0 +1,101 @@
+/* Copyright (C) 1991-2013 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <http://www.gnu.org/licenses/>. */
+
+#include <stdio.h>
+#include <ctype.h>
+#include <string.h>
+#define isalnum_(x) (isalnum (x) || x == '_')
+#define cmp(x, y) memcmp (x, y, strlen (y))
+int
+main ()
+{
+ int i, j, k, len;
+ int incomment = 0, incomment2, insquote = 0, indquote = 0;
+ char _buffer[100000], *buffer = _buffer + 1;
+ char buffer2[100000];
+ _buffer[0] = ' ';
+ while (fgets (buffer, 100000, stdin))
+ {
+ i = 0;
+ j = 0;
+ len = 0;
+ if (!insquote && !indquote)
+ {
+ while (buffer[i] && isspace (buffer[i]))
+ {
+ if (buffer[i] == ' ')
+ len++;
+ if (buffer[i] == '\t')
+ len = 8 * (len / 8) + 8;
+ i++;
+ }
+ for (k = 0; k < len / 8; k++)
+ buffer2[j++] = '\t';
+ for (k = 0; k < len % 8; k++)
+ buffer2[j++] = ' ';
+ }
+ while (buffer[i])
+ {
+ if (!cmp (buffer + i, "/*"))
+ incomment = 1;
+ if (!cmp (buffer + i, "//"))
+ incomment2 = 1;
+
+ if (incomment)
+ {
+ if (!cmp (buffer + i, "*/"))
+ incomment = 0;
+ }
+ else if (incomment2)
+ {
+ if (buffer[i] != '\\' && buffer[i + 1] == '\n')
+ incomment2 = 0;
+ }
+ else
+ {
+ if (insquote)
+ {
+ if (buffer[i - 1] != '\\' && buffer[i] == '\'')
+ insquote = 0;
+ }
+ else if (indquote)
+ {
+ if (buffer[i - 1] != '\\' && buffer[i] == '"')
+ indquote = 0;
+ }
+ else
+ {
+ if (buffer[i] == '\'')
+ insquote = 1;
+ if (buffer[i] == '"')
+ indquote = 1;
+ }
+ }
+ buffer2[j++] = buffer[i++];
+ }
+ if (!insquote && !indquote)
+ {
+ while (--j >= 0 && isspace (buffer2[j]))
+ ;
+ buffer2[j + 1] = '\n';
+ j += 2;
+ }
+ buffer2[j] = 0;
+ printf ("%s", buffer2);
+ }
+ return 0;
+}
diff --git a/scripts/fix_leading_trailing_space_git b/scripts/fix_leading_trailing_space_git
new file mode 100755
index 0000000..b654189
--- /dev/null
+++ b/scripts/fix_leading_trailing_space_git
@@ -0,0 +1,16 @@
+#!/bin/bash
+
+DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+
+for CUP in fix_leading_trailing_space; do
+
+ gcc $DIR/$CUP.c -o $DIR/$CUP
+
+ for I in `git diff --name-only HEAD`; do
+ if [ ${I: -2} == ".c" ] || [ ${I: -2} == ".h" ]; then
+
+ $DIR/$CUP <$I >$I.tmp
+ mv $I.tmp $I
+ fi
+ done
+done
--
1.7.10.4