I didn't find any other option (other than the --function-context
or its -W
short option, already mentioned) able to restrict a diff output to a single function.
And even that -W
option isn't always enough, knowing that a 'function' can vary greatly from language to language, as illustrated in this blog post:
I’ve found this option rather unreliable, at least within a large PHP class.
My tests found --function-context
often results in displaying almost all of the original file, and git appears ignorant of PHP’s function boundaries.
The number of context lines before and after a change seem random, and the diff doesn’t necessarily always show all lines of the function, either.
The original patch message that introduced this change sheds some light:
This implementation has the same shortcoming as the one in grep, namely that there is no way to explicitly find the end of a function.
That means that a few lines of extra context are shown, right up to the next recognized function begins.
So it appears detecting a function’s boundaries is difficult for git.
It seems in this instance, git never detects a function boundary and gives us the context of the entire file.
As the OP Steve Bennett points out, a potential solution would be to define a git alias which would extract the function in the revision before and after modification, in order to diff on those 2 'temp' files.
Example in "Creating Git Aliases" and "Bash script to select a single Python function from a file".
This is an ad-hoc solution which will be able to parse the specific type of source the OP happens to work with in his repo.