Ignore git diff multi-line comments

I am trying to find significant differences in the source code of C / C ++, in which only the source code is changed. I know you can use git diff -G<regex> , but it seems very limited in the form of regular expressions that you can run. For example, this is not like a way to ignore multiline comments in C / C ++.

Is there a way in git or preferably libgit2 to ignore comments (including multi-line), spaces, etc. before running diff? Or a way to determine if a line from the output of diff is a comment or not?

+6
source share
2 answers

git diff -w to ignore differences in spaces.

You cannot ignore multi-line comments, because git is a version control tool, not a language-dependent interpreter. He does not know what your C ++ code is. It does not analyze files for semantics, so it cannot interpret what a comment is and what is not. In particular, it relies on diff (or configured diffftool) to compare text files and expects a linear comparison.

I agree with @ andrew-c that what you are really asking for is to compare the two parts of the code without comment. More specifically useful, you are asking to compare lines of code where all multi-line comments have been turned into empty lines. You keep blank lines there so that you have the correct line numbers to link to a regular copy.

That way, you can manually convert the two states of the code to empty multi-line comments ... or you can see how to create your own diff wrapper that did the cleanup for you. But the latter is hardly worth the effort.

+2
source

You can achieve this by using the git attributes and filter filters, as described in Viewing git filters the output by using the meld command as a diff tool to invoke a sed script, which, however, is quite complex in itself if you want it to handle all cases such as comment delimiters inside string literals, etc.

0
source

Source: https://habr.com/ru/post/982537/


All Articles