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

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

From
IHIan Hilt <ian.hilt@gmx.com>
Date
Jun 24, 2008, 18:26 UTC
Message-ID
<alpine.LFD.1.10.0806241418360.32759@sys-0.hiltweb.site>
In-Reply-To
<53A5AFCF-94C7-465E-A181-1DA69F251F5B@gmail.com>
On Tue, 24 Jun 2008 at 12:23pm -0400, Christian Holtje wrote:
Show 55 quoted lines
> When commit files that use DOS style CRLF end-of-lines, the pre-commit
> hook would raise an error.  When combined with the fact that the hooks
> get activated by default on windows, it makes life difficult for
> people working with DOS files.
> 
> This patch causes the pre-commit hook to deal with crlf files
> correctly.
> 
> Signed-off-by: Christian Höltje <docwhat@gmail.com>
> ---
> t/t7503-template-hook--pre-commit.sh |   75 ++++++++++++++++++++++++++++++++++
> templates/hooks--pre-commit          |   10 ++++-
> 2 files changed, 83 insertions(+), 2 deletions(-)
> create mode 100755 t/t7503-template-hook--pre-commit.sh
> 
> diff --git a/t/t7503-template-hook--pre-commit.sh
> b/t/t7503-template-hook--pre-commit.sh
> new file mode 100755
> index 0000000..8f0c3c9
> --- /dev/null
> +++ b/t/t7503-template-hook--pre-commit.sh
> @@ -0,0 +1,75 @@
> +#!/bin/sh
> +#
> +# Copyright (c) 2008 Christian Höltje
> +#
> +
> +test_description='t7503 templates-hooks--pre-commit
> +
> +This test verifies that the pre-commit hook shipped with
> +git by default works correctly.
> +'
> +
> +. ./test-lib.sh
> +
> +test_expect_success 'verify that autocrlf is unset' '
> +   if git config core.autocrlf
> +   then
> +     false
> +   else
> +     test $? -eq 1
> +   fi
> +'
> +
> +test_expect_success 'lf without hook' '
> +
> +	echo "foo" > lf.txt &&
> +	git add lf.txt &&
> +	git commit -m "lf without hook" lf.txt
> +
> +'
> +
> +test_expect_success 'crlf without hook' '
> +
> +	echo "foo\r" > crlf.txt &&

Perhaps you want to use printf "foo\r" instead? echo "foo\r" does not produce a CR on my system.

Show 53 quoted lines
> +	git add crlf.txt &&
> +	git commit -m "crlf without hook" crlf.txt
> +
> +'
> +
> +# Set up the pre-commit hook.
> +HOOKDIR="$(git rev-parse --git-dir)/hooks"
> +mkdir -p "${HOOKDIR}"
> +cp -r "${HOOKDIR}-disabled/pre-commit" "${HOOKDIR}/pre-commit"
> +chmod +x "${HOOKDIR}/pre-commit"
> +
> +test_expect_success 'lf with hook' '
> +
> +	echo "bar" >> lf.txt &&
> +	git add lf.txt &&
> +	git commit -m "lf with hook" lf.txt
> +
> +'
> +test_expect_success 'crlf with hook' '
> +
> +	echo "bar\r" >> crlf.txt &&
> +	git add crlf.txt &&
> +	git commit -m "crlf with hook" crlf.txt
> +
> +'
> +
> +test_expect_success 'lf with hook white-space failure' '
> +
> +	echo "bar " >> lf.txt &&
> +	git add lf.txt &&
> +	! git commit -m "lf with hook" lf.txt
> +
> +'
> +test_expect_success 'crlf with hook white-space failure' '
> +
> +	echo "bar \r" >> crlf.txt &&
> +	git add crlf.txt &&
> +	! git commit -m "crlf with hook" crlf.txt
> +
> +'
> +
> +test_done
> diff --git a/templates/hooks--pre-commit b/templates/hooks--pre-commit
> index b25dce6..335ca09 100644
> --- a/templates/hooks--pre-commit
> +++ b/templates/hooks--pre-commit
> @@ -55,8 +55,14 @@ perl -e '
> 	if (s/^\+//) {
> 	    $lineno++;
> 	    chomp;
> -	    if (/\s$/) {
> -		bad_line("trailing whitespace", $_);
> +	    if (/\r$/) {

Wouldn't it be less redundant to do a test for \s\r$ here instead of testing for \r$ and then \s\r$?

Show 11 quoted lines
> +		if (/\s\r$/) {
> +		    bad_line("trailing whitespace", $_);
> +		}
> +	    } else {
> +		if (/\s$/) {
> +		    bad_line("trailing whitespace", $_);
> +		}
> 	    }
> 	    if (/^\s* \t/) {
> 		bad_line("indent SP followed by a TAB", $_);
> 
-- 
Ian Hilt
Ian.Hilt (at) gmx.com
GnuPG key: 0x4AFC1EE3
Previous: Alf ClementNext: Jakub Narebski
Message 3 of 21 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. Alf ClementJun 24, 2008
  3. Ian HiltJun 24, 2008
  4. Jakub NarebskiJun 24, 2008
  5. Ian HiltJun 24, 2008
  6. Jakub NarebskiJun 24, 2008
  7. Ian HiltJun 24, 2008
  8. Christian HoltjeJun 24, 2008
  9. Junio C HamanoJun 24, 2008
  10. Christian HoltjeJun 24, 2008
  11. Jakub NarebskiJun 24, 2008
  12. Junio C HamanoJun 24, 2008
  13. Christian HoltjeJun 25, 2008
  14. Junio C HamanoJun 24, 2008
  15. Ship sample hooks with .sample suffixJunio C Hamano, Jun 25, 2008
  16. Junio C HamanoJun 25, 2008
  17. Johannes SixtJun 25, 2008
  18. Junio C HamanoJun 25, 2008
  19. Johannes SixtJun 26, 2008
  20. Junio C HamanoJun 26, 2008
  21. Peter BaumannJun 25, 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.