Search code examples
gitdiffgit-diff

How to remove comments and information lines from gif diff?


When I issue a command like the following:

git diff -U10 C:\text1.c C:\text2.c 

I get an output with:

diff --git "a/C:\text1.c" "b/C:\text2.c"
--- "a/C:\text1.c"
+++ "a/C:\text2.c"
@@ -345,31 +456,32 @@ <content of the files>

How to get rid of these lines until @@? I also tried the option --color-word but to did not change the output.

And to get rid of the comments in C files, I did

git diff -G'(^[^\*# /])|(^#\w)|(^\s+[^\*#/])' -U10 C:\text1.c C:\text2.c 

as specified here: How to make git diff ignore comments . But it is not working. It is not removing any comments on the output. How to solve these two problems?


Solution

  • If you are diffing a single pair of files, the first @@ line always immediately follows the first +++ line, regardless of whether the header section of the output is four lines:

    $ git diff -U10 /tmp/[12]
    diff --git a/tmp/1 b/tmp/2
    index e4fbac4..624d841 100644
    --- a/tmp/1
    +++ b/tmp/2
    @@ -1,2 +1,2 @@
     this is file
    -one
    +two
    

    or more:

    $ chmod +x /tmp/2
    $ git diff -U10 /tmp/[12]
    diff --git a/tmp/1 b/tmp/2
    old mode 100644
    new mode 100755
    index e4fbac4..624d841
    --- a/tmp/1
    +++ b/tmp/2
    @@ -1,2 +1,2 @@
     this is file
    -one
    +two
    

    So, if you do have sed, simply pipe through sed '1,/^+++ /d'.

    If you know the two file modes match (so that the extra lines old mode ... and new mode ... will not exist), you can just cut away the first four lines.

    (Aside: it seems curious that your git diff is showing only three header lines. The index ... line should appear even for files not stored in Git.)

    As for removing comments: Git doesn't really know whether something is a comment. The comment syntax is specific to the source language. For instance, # marks comments in shell and Python, but // marks comments in C++ and Go. (It's more complicated than that, since there are other kinds of comments in various languages, and there are block data constructs—triple quotes in Python and backticks in Go for instance—that may make something that looks like a comment, not actually be a comment.) So you'll need to invent your own detection-and-removal for that.