对于 c/cpp 代码,我想将所有新添加的更改添加到宏内,并删除 else 部分中的更改。
例如对于下面的补丁
diff --git a/test.c b/test.c
index a35d23a..5ee498a 100644
--- a/test.c
+++ b/test.c
@@ -1,6 +1,6 @@
#include <stdio.h>
int main() {
- printf("Old line");
+ printf("New line");
return 0;
}
修改为
diff --git a/test.c b/test.c
index a35d23a..668d2e0 100644
--- a/test.c
+++ b/test.c
@@ -1,6 +1,10 @@
#include <stdio.h>
int main() {
+#ifdef MODIFIED
+ printf("New line");
+#else
printf("Old line");
+#endif
return 0;
}
或者
diff --git a/test.c b/test.c
index a35d23a..d05f7d1 100644
--- a/test.c
+++ b/test.c
@@ -1,6 +1,11 @@
#include <stdio.h>
int main() {
+#ifdef MODIFIED
+ printf("New line");
+#endif
+#ifndef MODIFIED
printf("Old line");
+#endif
return 0;
}
我尝试搜索是否有一些内置工具可以执行此操作。可惜没找到。
有人可以帮忙吗? TIA
这是内置于
diff
中的,至少在 Linux 和 macOS 上是如此(它不在 diff
的 POSIX 规范中)。来自https://linux.die.net/man/1/diff:
-D NAME
--ifdef=NAME
输出合并文件以显示“#ifdef NAME”差异。