T.J. Schuck
T.J. Schuck

Reputation: 3645

Git commit stalls on trailing whitespace on lines that don't exist

I'm trying to make a commit in Git, but receive the following error:

$ git commit -m "Changed model name from Employee to Person for abstraction"
*
* You have some suspicious patch lines:
*
* In app/helpers/people_helper.rb
* trailing whitespace (line 28)
app/helpers/people_helper.rb:28:
* trailing whitespace (line 44)
app/helpers/people_helper.rb:44:

Fine. I'll remove the whitespace. The only problem? Those lines don't exist.

$ cat app/helpers/people_helper.rb
module PeopleHelper
end
$ 

The file is only two lines long. There can't be white space on lines 28 and 44.

What gives?

I'm on Git version 1.5.4 with no hooks except for the ones installed by default.

Upvotes: 2

Views: 751

Answers (1)

Brian Campbell
Brian Campbell

Reputation: 332846

Git 1.5.4 is quite an old version of Git (from February 2008); I would recommend you update to a newer version. Newer versions (since 1.6, I believe) do not enable the sample hooks by default (I believe that older versions weren't supposed to enable the sample hooks by default either, but it was done by disabling the executable bit which would sometimes accidentally get set on certain platforms; now it's done by naming them .sample so Git won't even look at them until you rename them). Note that if you upgrade, all of your existing repos will still have the old hooks, so you'll either have to disable them manually (rename everything in .git/hooks to end in .sample), or just clone your repos into fresh ones to get new repositories with no old junk left over.

As to your exact problem, I'm not entirely sure without more information why it would happen (the output of git-diff-index -p -M --cached HEAD would probably help), but it's likely because the way that the sample hook checked for trailing whitespace was by generating a patch, and then parsing the patch, looking for diff headers to get the file name, diff line numbers to figure out the line, lines beginning with a space to count unchanged lines, and lines beginning with a + to find changed lines. If something managed to confuse this fairly simple patch parser, it might wind up with a completely wrong filename and line number for the errors it reports.

This check for trailing whitespace is no longer even included in the sample pre-commit hook, likely because of how fragile it was.

Upvotes: 1

Related Questions