Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does git-diff generate hunk descriptions?

Tags:

git

python

diff

(git version 1.6.5.7)

When I run git diff the output has a nice scope hint after the line numbers for my Python scripts, e.g.:

diff --git a/file.py b/file.py
index 024f5bb..c3b5c56 100644
--- a/file.py
+++ b/file.py
@@ -14,6 +14,8 @@ TITF: Test Infrastructure Tags Format
...
@@ -1507,13 +1533,16 @@ class Tags( object ):
...

Note that the line numbers are followed by TITF: Test Infrastructure Tags Format and class Tags( object ):. The first patch applies to module scope and the description TITF: Test Infrastructure Tags Format is the module's description. The second patch applies to a method of the Tags class.

  1. How does git generate these descriptions?
  2. How can I tweak them to show the method name that the patch applies to?
like image 456
RobM Avatar asked May 06 '10 17:05

RobM


1 Answers

Git uses a regular expression to find a suitable line for the hunk headers. Python's is built-in, but you should be able to define your own expression in your ~/.gitconfig:

[diff "python"]
        xfuncname = "<regex goes here>"

More about this here.

Edit: The built-in python regex seems to be defined in userdiff.c (line 53), although my regex-fu is not good enough to actually understand exactly what it does...

PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$",
         /* -- */
         "[a-zA-Z_][a-zA-Z0-9_]*"
         "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
         "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"
         "|[^[:space:]|[\x80-\xff]+"),
         /* -- */
like image 154
DataWraith Avatar answered Sep 20 '22 00:09

DataWraith