How does git-diff generate hunk descriptions?

不想你离开。 提交于 2019-11-30 13:00:20

问题


(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?

回答1:


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]+"),
         /* -- */


来源:https://stackoverflow.com/questions/2783086/how-does-git-diff-generate-hunk-descriptions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!