有没有办法让 git diff 显示方法名称而不是类名称?

发布于 2024-12-03 12:39:50 字数 1139 浏览 1 评论 0原文

这已经困扰我一段时间了,搜索也没有得到任何提示。

问题:

当我对包含 Class 的 PHP 文件执行 git diff 时,每个块描述都有类名,但没有提及方法姓名。

例如:

@@ -351,7 +351,7 @@ class Session
             $form->setError($field, $subpassValid['msg']);
       }

       // E-Mail address error checking
       $field = "email";  //Use field name for email
-       if(!isset($subemail) || strlen($subemail = trim($subemail)) == 0){
+       if(!$subemail || strlen($subemail = trim($subemail)) == 0){
          $form->setError($field, "* Email not entered");
@@ -373,7 +373,7 @@ class Session
...

问题:

有没有办法告诉 git 显示方法名称而不是/沿着类名称?

编辑:

其他信息:

diff version: 2.8.1
git version: 1.7.6 (from the git-core PPA)
OS: Ubuntu 9.10

我最初从 Ubuntu 存储库中获得了 git 版本 1.7.0.4,但更新了它,希望这是一个错误。不幸的是,这并没有解决问题。

Edit2:

经过更多测试,看来函数声明是否缩进(使用空格或制表符)git diff不获取函数名称。这似乎是 git 中的一个错误。

This has been bugging me for a while and searching has yielded no hints.

Issue:

When I do a git diff on a PHP file containing a Class, each hunk description has the class name with no mention of the method name.

For example:

@@ -351,7 +351,7 @@ class Session
             $form->setError($field, $subpassValid['msg']);
       }

       // E-Mail address error checking
       $field = "email";  //Use field name for email
-       if(!isset($subemail) || strlen($subemail = trim($subemail)) == 0){
+       if(!$subemail || strlen($subemail = trim($subemail)) == 0){
          $form->setError($field, "* Email not entered");
@@ -373,7 +373,7 @@ class Session
...

Question:

Is there a way to tell git to show method names instead of/along side of the class name?

Edit:

Additional Information:

diff version: 2.8.1
git version: 1.7.6 (from the git-core PPA)
OS: Ubuntu 9.10

I originally had git version 1.7.0.4 from the stock Ubuntu repos, but updated it hoping that it was a bug. Unfortunately that didn't solve the issue.

Edit2:

After more testing, it appears that if the function declaration is indented at all (with either spaces or tabs) git diff doesn't pick up the function name. This appears to be a bug in git.

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

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

发布评论

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

评论(3

饮湿 2024-12-10 12:39:50

如果您发现自己遇到此问题,可能是由于 gitdiff 定义 hunk 标头。如果您想强制 git 使用 PHP 的预定义属性,请将以下行放在 git 存储库根文件夹中的 .gitattributes.git 中存储库中的 /info/attributes 文件:(

*.php diff=php

类似地支持其他语言。内置模式的列表可在 文档。)

全局设置

应该可以通过在中执行以下操作来全局设置它>git version >= 1.4

  1. 将行放入 ~/.gitattributes
  2. 运行 git config --global core.attributesfile "~/.gitattributes"

或者,如果您想全局设置此值

  1. ,请将行放入 /etc/gitattributes

警告: 如果您个人全局设置此值,您的同事在运行 git diff 时可能会得到不同的结果,因为他们不会使用存储库检出 .gitattributes 的副本。

If you find yourself having this issue, it might be due to the fact that git lets diff define the hunk header. If you want to force git to use its predefined ones for PHP place the following line in either .gitattributes in the root folder of your git repository or the .git/info/attributes file in your repository:

*.php diff=php

(Other languages are similarly supported. A list of the built in patterns is available in the documentation.)

Setting this Globally

It should be possible to globally set this by doing the following in git version >= 1.4

  1. Put line(s) in ~/.gitattributes
  2. Run git config --global core.attributesfile "~/.gitattributes"

Or if you want to set this globally

  1. Put line(s) into /etc/gitattributes

Warning: If you personally set this globally, your peers may get different results when running git diff since they won't checkout a copy of .gitattributes with the repository.

奶气 2024-12-10 12:39:50

我不太明白为什么它不能按应有的方式工作

作为临时解决方案,请将以下内容添加到 .gitattributes

*.php   diff=phpf

并将这些行添加到 .git/config~/.gitconfig

[diff "phpf"]
        xfuncname = "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$"

对于永久解决方案解决方案直接询问 Git 开发人员 [电子邮件受保护]邮件列表

I don't quite understand why it doesn't work the way it should.

As a temporary solution, add the following to .gitattributes:

*.php   diff=phpf

And these lines to .git/config or ~/.gitconfig:

[diff "phpf"]
        xfuncname = "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$"

For a permanent solution ask Git developers directly at [email protected] mailing list.

关于从前 2024-12-10 12:39:50

使用 Git 2.19(2018 年第 3 季度)该 diff 可能会更好,因为 .php 的 userdiff 模式已更新。

请参阅 提交 1ab6316提交 9992fbd(2018 年 7 月 3 日),作者:假名夏野(假名
(由 Junio C Hamano -- gitster -- 合并于 提交 d3f0938,2018 年 7 月 24 日)

userdiff:支持 PHP hunk 标头中的新关键字

最新版本的 PHP 支持接口trait抽象类
最后一堂课
此补丁修复了 PHP hunk 标头正则表达式以支持所有这些关键字。



Git 2.30(2021 年第一季度)确实有所改进:

请参阅 commit aff9282(10 月 7 日 ) 2020)由 Javier Spagnoletti (phansys)
(由 Junio C Hamano -- gitster -- 合并于 提交 a8a49eb,2020 年 10 月 27 日)

userdiff:PHP:捕获“抽象”和“最终”功能

帮助者:Johannes Sixt
签字人:Javier Spagnoletti

PHP 允许这样定义函数

最终公共函数 foo() { }
抽象受保护函数 bar() { }  

但是我们的大块标题模式无法识别这些装饰。

将“final”和“abstract”添加到函数修饰符列表中。

That diff would likely be better with Git 2.19 (Q3 2018), since the userdiff pattern for .php has been updated.

See commit 1ab6316, commit 9992fbd (03 Jul 2018) by Kana Natsuno (kana).
(Merged by Junio C Hamano -- gitster -- in commit d3f0938, 24 Jul 2018)

userdiff: support new keywords in PHP hunk header

Recent version of PHP supports interface, trait, abstract class and
final class.
This patch fixes the PHP hunk header regexp to support all of these keywords.


And it does improve with Git 2.30 (Q1 2021):

See commit aff9282 (07 Oct 2020) by Javier Spagnoletti (phansys).
(Merged by Junio C Hamano -- gitster -- in commit a8a49eb, 27 Oct 2020)

userdiff: PHP: catch "abstract" and "final" functions

Helped-by: Johannes Sixt
Signed-off-by: Javier Spagnoletti

PHP permits functions to be defined like

final public function foo() { }
abstract protected function bar() { }  

but our hunk header pattern does not recognize these decorations.

Add "final" and "abstract" to the list of function modifiers.

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