1

I like to compare Python sources with diff util and I have seen that git diff shows me the function name (for some files, but also not for Python) in the hunk or chunk? header. Thats why I searched to do the same with normal diff.

Then I found that there is a -p, --show-c-function param, which don't work well with Python files (I only get the class name). So I also found in the man page the param: -F, --show-function-line=RE

Now I searched for a nice RE to match python functions to include them in my diff output without luck.

I know we have def myname(): or async def myname(): ... and maybe more?

Has someone a good RegEx for it?

I found this one, but it don't work with diff util (the output has no function names):

diff -Nru -F '(?P<function>\w+)\s?\((?P<arg>(?P<args>\w+(,\s?)?)+)\)' modules/websocket/__init__.py.old modules/websocket/__init__.py

Regards, Thomas

5
  • I'm surprised you say it does not work well for Python. I just did a git log -p, which uses the same diff as git diff, on my Python repo, and it's finding the class or function quite reliably for me, except for code that's outside those. What version of Git are you using? I've got 2.25.1 where I just tested this. Commented Apr 9, 2021 at 12:36
  • 1
    Oops, I read your question wrong, you're asking about doing this in diff, not in git diff. For what it's worth, if the git diff output happened to be what you liked better, you can do git diff path1 path2 to invoke git diff on files outside a repo context, or ignoring the repo they're in. Commented Apr 9, 2021 at 12:43
  • Problem is, that I don't have a git repository all the time under my fingers :) - but on my test with git diff - I only got the class name - not the function. Maybe because of async def funcname() syntax... dont know... Commented Apr 12, 2021 at 13:56
  • 1
    The thing just just discovered is that git diff file1 file2 will also work outside a Git repo -- all you need is the Git software installed. But if it doesn't output what you want, then nevermind! Commented Apr 12, 2021 at 16:16
  • "git diff shows me the function name (for some files, but also not for Python) ..." — Are you sure that it doesn't show the function name for Python? What version of Git are you using? This is the regular expression that Git uses to find Python functions: github.com/git/git/blob/v2.33.1/userdiff.c#L223 Commented Oct 27, 2021 at 6:23

1 Answer 1

0

Was comparing two big Python codebases across versions, and was missing the function names - so as per the hint in this question I used the -F parameter.

It was really as simple as -

diff -F '\Wdef '  old.py  new.py

Matches any def that is followed by a space and preceded by a non-word character (such as a new line, or space).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.