0

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?

Gravity Mass
  • 605
  • 7
  • 13

1 Answers1

1

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.

torek
  • 448,244
  • 59
  • 642
  • 775
  • `'/^diff/,/^@@/d'` -> This worked well for sed as it also got rid off @@ line. For syntax, I am just trying to remove C programming comments. – Gravity Mass Jul 17 '19 at 23:09
  • You said you wanted to *keep* the `@@` line. As for deleting C comments, what if the entire diff section comes after `/*` and the concluding `*/` comes after the diff block? – torek Jul 17 '19 at 23:34
  • that is where i used regex `'(^[^\*# /])|(^#\w)|(^\s+[^\*#/])'` but it did not work. – Gravity Mass Jul 18 '19 at 03:45
  • That RE doesn't match `/*` type comments at all: it matches things that aren't backslash, `*`, `#`, space, or `/` at the beginning of the line, or `#` followed by a word character at the beginning of a line, or spaces followed by something that is not backslash, `*`, `#`, or `/`, all at the beginning of a line (though the details depend on whether this is a Perl RE or a POSIX basic or extended RE). – torek Jul 18 '19 at 04:13