How to make 'git diff' ignore comments

前端 未结 6 1478
闹比i
闹比i 2020-12-05 02:38

I am trying to produce a list of the files that were changed in a specific commit. The problem is, that every file has the version number in a comment at the top of the file

6条回答
  •  暖寄归人
    2020-12-05 02:58

    Using 'grep' on the 'git diff' output,

    git diff -w | grep -c -E "(^[+-]\s*(\/)?\*)|(^[+-]\s*\/\/)"
    

    comment line changes alone can be calculated. (A)

    Using 'git diff --stat' output,

    git diff -w --stat
    

    all line changes can be calculated. (B)

    To get non comment source line changes (NCSL) count, subtract (A) from (B).

    Explanation:

    In the 'git diff ' output (in which whitespace changes are ignored),

    • Look out for a line which start with either '+' or '-', which means modified line.
    • There can be optional white-space characters following this. '\s*'
    • Then look for comment line pattern '/*' (or) just '*' (or) '//'.
    • Since, '-c' option is given with grep, just print the count. Remove '-c' option to see the comments alone in the diffs.

    NOTE: There can be minor errors in the comment line count due to following assumptions, and the result should be taken as a ballpark figure.

    • 1.) Source files are based on the C language. Makefile and shell script files have a different convention, '#', to denote the comment lines and if they are part of diffset, their comment lines won't be counted.

    • 2.) The Git convention of line change: If a line is modified, Git sees it as that particular line is deleted and a new line is inserted there and it may look like two lines are changed whereas in reality one line is modified.

       In the below example, the new definition of 'FOO' looks like a two-line change.
      
       $  git diff --stat -w abc.h
       ...
       -#define FOO 7
       +#define FOO 105
       ...
       1 files changed, 1 insertions(+), 1 deletions(-)
       $
      
    • 3.) Valid comment lines not matching the pattern (or) Valid source code lines matching the pattern can cause errors in the calculation.

    In the below example, the "+ blah blah" line which doesn't start with '*' won't be detected as a comment line.

               + /*
               +  blah blah
               + *
               + */
    

    In the below example, the "+ *ptr" line will be counted as a comment line as it starts with *, though it is a valid source code line.

                + printf("\n %p",
                +         *ptr);
    

提交回复
热议问题