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

Re: Improving CRLF error message; also, enabling autocrlf and safecrlf by default

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 16, 2009, 03:08 UTC
Message-ID
<7vljs7f58a.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<loom.20090216T022524-78@post.gmane.org>
Jason Spiro <jasonspiro4@gmail.com> writes:
> One of the pre-commit hooks detects trailing whitespace:

All sample hooks are shipped disabled by default, so it shouldn't be triggering unless you enabled it yourself. The only known exception is the binary packaged one for Cygwin, which we do not have much control over here.

Show 10 quoted lines
> if (/\s$/) {
> bad_line("trailing whitespace", $_);
> }
>
> Unfortunately, when I try to check in a file with DOS (CR+LF) line endings, 
> this hook triggers on every line.  This happens on Cygwin.  I haven't checked, 
> but I bet it happens on other platforms as well, as long as this hook runs.
>
> But the error message "trailing whitespace" doesn't clearly tell me what's 
> wrong.

I and other people agreed with your analysis above wholeheartedly several months ago, and as a result, v1.6.0 and later version of git use a different implementation for this check in the sample hook. It does know your CRLF line endings and therefore it should behave much better.

The fix to your situation might be just the matter of taking a copy of templates/hooks--pre-commit.sample from the current git source code and replacing .git/hooks/pre-commit in your repository.

The sample hook looks like the attached one these days. It relies on an enhancement 346245a (hard-code the empty tree object, 2008-02-13) that appeared first in v1.5.5 so it may not work if your copy of git is older than that version.

-- >8 -- cut here -- >8 -- #!/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=HEAD
else
	# Initial commit: diff against an empty tree object
	against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
exec git diff-index --check --cached $against --
Previous: Jeff King
Message 7 of 7 in “Improving CRLF error message; also, enabling autocrlf and safecrlf by default”
  1. Jason SpiroFeb 16, 2009
  2. Jeff KingFeb 16, 2009
  3. Junio C HamanoFeb 16, 2009
  4. Jeff KingFeb 16, 2009
  5. Jason SpiroFeb 16, 2009
  6. Jeff KingFeb 16, 2009
  7. Junio C HamanoFeb 16, 2009

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.