如何从gif diff中删除注释和信息行?

时间:2019-07-17 21:55:09

标签: git diff git-diff

当我发出如下命令时:

url.rewrite-once = ( "^(/api/.+)$" => "/api.fcgi$1" )

我得到的输出是:

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

如何在@@之前删除这些行?我还尝试了选项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> ,但没有更改输出。

为了摆脱C文件中的注释,我做了

--color-word

,如此处指定:How to make git diff ignore comments。但这是行不通的。它不会删除输出上的任何注释。如何解决这两个问题?

1 个答案:

答案 0 :(得分:1)

如果要比较一对文件,则第一行@@总是紧随第一行+++之后,无论输出的标题部分是否为四行:

$ 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

或更多:

$ 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

因此,如果您确实有sed,只需通过sed '1,/^+++ /d'进行传递。

如果您知道两个文件模式匹配(因此多余的行old mode ...new mode ...将不存在),则可以删除前四行。

(此外:您的git diff仅显示三个标头行似乎很奇怪。即使没有存储在Git中的文件也应该出现index ...行。)

关于删除评论:Git并不真正知道某个东西是否是评论。注释语法特定于源语言。例如,#在shell和Python中标记注释,而//在C ++和Go中标记注释。 (这要复杂得多,因为各种语言还存在其他类型的注释,并且存在块数据结构(例如Python中的三引号和Go中的反引号),这些东西可能使看起来评论,而不是评论。)因此,您需要为此发明自己的检测和删除。