git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2] pre-commit hook should ignore carriage returns at EOL

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 26, 2008, 23:06 UTC
Message-ID
<7vlk0rokxb.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<7vprq3ol63.fsf@gitster.siamese.dyndns.org>
Junio C Hamano <gitster@pobox.com> writes:
> With these enhancements in place, I think the pre-commit hook to find
> problematic change would become essentially a one-liner, something like:
>
> 	git diff-index --check -M --cached
More specifically, it would be like this.
 templates/hooks--pre-commit.sample |   89 ++++++++----------------------------
 1 files changed, 19 insertions(+), 70 deletions(-)
diff --git a/templates/hooks--pre-commit.sample b/templates/hooks--pre-commit.sample
dissimilarity index 79%
index 71c10f2..c302ed1 100755
--- a/templates/hooks--pre-commit.sample
+++ b/templates/hooks--pre-commit.sample
@@ -1,70 +1,19 @@
-#!/bin/sh
-#
-# An example hook script to verify what is about to be committed.
-# Called by git-commit with no arguments.  The hook should
-# exit with non-zero status after issuing an appropriate message if
-# it wants to stop the commit.
-#
-# To enable this hook, rename this file to "pre-commit".
-
-# This is slightly modified from Andrew Morton's Perfect Patch.
-# Lines you introduce should not have trailing whitespace.
-# Also check for an indentation that has SP before a TAB.
-
-if git-rev-parse --verify HEAD 2>/dev/null
-then
-	git-diff-index -p -M --cached HEAD --
-else
-	# NEEDSWORK: we should produce a diff with an empty tree here
-	# if we want to do the same verification for the initial import.
-	:
-fi |
-perl -e '
-    my $found_bad = 0;
-    my $filename;
-    my $reported_filename = "";
-    my $lineno;
-    sub bad_line {
-	my ($why, $line) = @_;
-	if (!$found_bad) {
-	    print STDERR "*\n";
-	    print STDERR "* You have some suspicious patch lines:\n";
-	    print STDERR "*\n";
-	    $found_bad = 1;
-	}
-	if ($reported_filename ne $filename) {
-	    print STDERR "* In $filename\n";
-	    $reported_filename = $filename;
-	}
-	print STDERR "* $why (line $lineno)\n";
-	print STDERR "$filename:$lineno:$line\n";
-    }
-    while (<>) {
-	if (m|^diff --git a/(.*) b/\1$|) {
-	    $filename = $1;
-	    next;
-	}
-	if (/^@@ -\S+ \+(\d+)/) {
-	    $lineno = $1 - 1;
-	    next;
-	}
-	if (/^ /) {
-	    $lineno++;
-	    next;
-	}
-	if (s/^\+//) {
-	    $lineno++;
-	    chomp;
-	    if (/\s$/) {
-		bad_line("trailing whitespace", $_);
-	    }
-	    if (/^\s* \t/) {
-		bad_line("indent SP followed by a TAB", $_);
-	    }
-	    if (/^([<>])\1{6} |^={7}$/) {
-		bad_line("unresolved merge conflict", $_);
-	    }
-	}
-    }
-    exit($found_bad);
-'
+#!/bin/sh
+#
+# An example hook script to verify what is about to be committed.
+# Called by git-commit with no arguments.  The hook should
+# exit with non-zero status after issuing an appropriate message if
+# it wants to stop the commit.
+#
+# To enable this hook, rename this file to "pre-commit".
+
+if git-rev-parse --verify HEAD 2>/dev/null
+then
+	against=
+else
+	# Initial commit: diff against an empty tree object
+	against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
+fi
+
+exec git diff-index --check --cached $against
+
Previous: Junio C HamanoNext: Jeff King
Message 13 of 14 in “pre-commit hook should ignore carriage returns at EOL”
  1. pre-commit hook should ignore carriage returns at EOLChristian Holtje, Jun 24, 2008
  2. Alex RiesenJun 25, 2008
  3. Christian HoltjeJun 25, 2008
  4. Junio C HamanoJun 25, 2008
  5. Christian HoltjeJun 26, 2008
  6. Junio C HamanoJun 26, 2008
  7. 1/5 diff --check: explain why we do not care whether old side is binaryJunio C Hamano, Jun 26, 2008
  8. 2/5 check_and_emit_line(): rename and refactorJunio C Hamano, Jun 26, 2008
  9. 3/5 checkdiff: pass diff_options to the callbackJunio C Hamano, Jun 26, 2008
  10. 4/5 Teach "diff --check" about a new blank lines at endJunio C Hamano, Jun 26, 2008
  11. 5/5 diff --check: detect leftover conflict markersJunio C Hamano, Jun 26, 2008
  12. Junio C HamanoJun 26, 2008
  13. Junio C HamanoJun 26, 2008
  14. Jeff KingJun 27, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.