git diff custom hunk header 未显示正确的功能

git diff custom hunk header not showing correct function

我正在尝试为 Python 自定义 git diff hunk header。我目前正在使用此处定义的“python”正则表达式模式:https://github.com/git/git/blob/6d2f208c3dd39493f4d45ea67c55a1b7fe06626a/userdiff.c

hunk header 似乎能够正确显示 class 或大部分发生更改的函数。但是,当我在函数定义之后的几行中进行更改时,问题就来了,如下面的 func2 所示。

def func1():
   print('hello')

def func2():
   #some change here
   print('bye')

对于此更改,我的帅哥 header 看起来像

@@ -k,l +m,n @@ def func1():

所以即使更改发生在 func2 中,hunk header 错误地指示它发生在 func1 中。有什么方法可以更改正则表达式模式,以便 hunk header 显示正确的功能?我认为这可能与 git diff 上下文的长度有关。现在对于单行的更改,header 中字母“l”和“n”指示的空格显示 7,这意味着 git diff 显示 7 行。如何更改 hunk header 中的字母 k、l、m、n 并自定义 git diff 上下文的长度?

Git 人员并未将此定义为错误。 (不是每个人都同意这一点...)

如果您检查 diff hunk,您会发现上下文行向后 延伸到 func1。因此,上下文是 func1() 的上下文,而不是 func2 的上下文。 更改的行 专用于func2(),但上下文func1.

的上下文

I think it may have something to do with the length of the git diff context. Right now for a change to a single line, the spaces indicated by the letters "l" and "n" in the header display 7, meaning that the git diff shows 7 lines. How do I change the letters k,l,m,n in the hunk header and customize the length of the git diff context?

没错。显示的上下文数量由 git diff-U 参数决定。如果您不提供 -U(或 --unified,同一选项的长名称版本),则默认值为 3.

请注意,有许多额外的差异控制。