How does git-diff generate hunk descriptions?

With Git 2.25 (Q1 2020), the userdiff machinery has been taught that "async def" is another way to begin a "function" in Python.

See commit 077a1fd (19 Nov 2019) by Josh Holland (anowlcalledjosh).
(Merged by Junio C Hamano -- gitster -- in commit 9502b61, 05 Dec 2019)

userdiff: support Python async functions

Signed-off-by: Josh Holland
Acked-by: Johannes Sixt

Python's async functions (declared with "async def" rather than "def") were not being displayed in hunk headers.
This commit teaches Git about the async function syntax, and adds tests for the Python userdiff regex.


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

Tags:

Python

Git

Diff