Git 提交因不存在的行的尾随空格而停止

发布于 2024-10-06 08:23:42 字数 564 浏览 8 评论 0原文

我尝试在 Git 中进行提交,但收到以下错误:

$ 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:

很好。我将删除空白。唯一的问题是什么?这些线不存在。

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

该文件只有两行长。第 28 行和第 44 行不能有空格。

这是怎么回事?

我使用的是 Git 版本 1.5.4,除了默认安装的钩子之外,没有任何钩子。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢欲 2024-10-13 08:23:42

Git 1.5.4 是一个相当旧的 Git 版本(从 2008 年 2 月开始);我建议您更新到较新的版本。较新的版本(我相信自 1.6 起)默认情况下不启用示例挂钩(我相信旧版本也不应该默认启用示例挂钩,但它是通过禁用可执行位来完成的,有时会意外地得到在某些平台上进行设置;现在通过将它们命名为 .sample 来完成,这样 Git 甚至不会查看它们,直到您重命名它们)。请注意,如果您升级,所有现有存储库仍将具有旧挂钩,因此您必须手动禁用它们(将 .git/hooks 中的所有内容重命名为以 结尾。示例),或者只是将您的存储库克隆到新的存储库中以获得不遗留旧垃圾的新存储库。

至于您的具体问题,如果没有更多信息,我不完全确定为什么会发生这种情况( git-diff-index -p -M --cached HEAD 的输出可能会有所帮助),但它是可能是因为示例钩子检查尾随空格的方式是生成补丁,然后解析补丁,查找 diff 标头以获取文件名,查找 diff 行号以找出行,以空格开头的行进行计数未更改的行,以及以 + 开头的行以查找已更改的行。如果有什么东西设法混淆了这个相当简单的补丁解析器,它可能会得到一个完全错误的文件名和它报告的错误的行号。

对尾随空格的检查甚至不再包含在示例预提交挂钩中,可能是因为它非常脆弱。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文