Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to ask git diff to show the method name instead of the class name?

Tags:

git

php

diff

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.

like image 814
Tarek Fadel Avatar asked Sep 08 '11 07:09

Tarek Fadel


People also ask

How do you tell the difference in files in git?

You can run the git diff HEAD command to compare the both staged and unstaged changes with your last commit. You can also run the git diff <branch_name1> <branch_name2> command to compare the changes from the first branch with changes from the second branch.

Why is git diff not showing?

In answer to the original question, git diff isn't showing anything because you have a brand new directory, with a newly added file, but there are zero changes in the file for git diff to show. git status is showing that you added a new file, but git diff is for showing changes within files.

What does M mean in git diff?

^M represents carriage return. This diff means something removed a Unicode BOM from the beginning of the line and added a CR at the end.

How does git diff work?

Comparing changes with git diffDiffing is a function that takes two input data sets and outputs the changes between them. git diff is a multi-use Git command that when executed runs a diff function on Git data sources. These data sources can be commits, branches, files and more.


3 Answers

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.

like image 80
4 revs, 4 users 68% Avatar answered Oct 08 '22 16:10

4 revs, 4 users 68%


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.

like image 38
sanmai Avatar answered Oct 08 '22 17:10

sanmai


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.

like image 1
VonC Avatar answered Oct 08 '22 15:10

VonC