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

git diff function name
git diff file
git diff filename
git renormalize

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.

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.

Git Best Practices: Upgrading the Patch Process, How do you tell the difference in files in git? git diff [<options>] [--] [<path>…. This form is to view the changes you made relative to the index (staging area for the next commit). In other words, the differences are what you could tell Git to further add to the index but you still haven’t.

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 git@vger.kernel.org mailing list.

How to exit git log or git diff, a patch if you have other uncommitted changes in your code. The text after the closing pair of "@@" aims to clarify the context, again: Git tries to display a method name or other contextual information of where this chunk was taken from in the file. However, this greatly depends on the programming language and doesn't work in all scenarios.

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.

Git Diff, am · apply · format-patch · send-email · request-pull NAME. git-diff - Show changes between commits, commit and working tree, etc It affects the way a change that amounts to a total rewrite of a file not as a series of deletion In the above example output, the function signature was changed from both files (hence two  There it is. Git shows the exact change I made in the file. But, if you look at the diff command, you might wonder what HEAD is doing there! Well, it is there for a purpose. If you can recall, Git has an index between local repository and your working directory. So most of Git commands can either refer to index or the local repo.

git-diff Documentation, Let's take a detailed look at such a diff - and learn how to read it. "IDs") of our two files: Git saves every version not only of the project but also of each file as an object. again: Git tries to display a method name or other contextual information of where To understand how they were changed in detail, we can ask "git diff": Today, I discovered, git diff --stat, and it's quite helpful. I sometimes just need to find which files were modified and how many changes I made in each. However, the output doesn't just say "5 additions, 2 deletions", it says +++++--.

Inspecting Changes with Diffs, Saving changes in a repo: git add adds a change in the working directory to git diff is a multi-use Git command that when executed runs a diff function on Git If we execute git diff at this point, there will be no output. You will most likely not need this information. Some example refs are, HEAD , tags, and branch names​. The documentation for "diff-files" mode of "git diff" primarily talks about how changes in the files in the working tree are shown relative to the contents previously added to that index, and tucks explanation on how "--no-index" mode, which works in a quite different way, may be implicitly used instead.

Test-Driven Development with Python: Obey the Testing Goat: Using , We're using several of the methods that Selenium provides to examine web pages: We also use send_keys, which is Selenium's way of typing into input elements. You'll also see the Keys class (don't forget to import it), which lets us send your commits, the better: $ git diff # should show changes to functional_tests.py $. How to create a patch (git diff) with a specific name. I would like to create a patch (git diff) with custom name -- e.g., hello-world.diff. Emacs doesn't seem to be able to handle git diff > hello-world.diff, perhaps because of the greater-than sign that works in regular terminals.

Comments
  • Some more info here: stackoverflow.com/questions/2783086/…
  • Looks like we have a bug. It should work exactly like you want it to.
  • What is the exact filename you display the diff of?
  • @hakre The behavior I am referring to is uniform across the board. It happens when I call git diff session.php (from which the example is taken from) or git diff master or just git diff --staged.
  • Doing the above removes that part completely. Using the above example, class Session has now disappeared, replaced by nothing. I've added version numbers to the original question if that helps any.
  • I think the \n at the end breaks it; it is used somehow to separate regexes and you have only one here. Without that, it worked for me. (The other solution is more elegant though.)