For c/cpp code I want to add all my newly added changes inside a macro and removed changes in the else part.
e.g. for the below patch
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;
}
modified to
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;
}
Or
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;
}
I tried searching if there is some inbuilt tool for doing this. Unfortunately could not find one.
Can someone please help. TIA
This is built into diff
, at least on Linux and macOS (it is not in the POSIX specification for diff
). From https://linux.die.net/man/1/diff:
-D NAME
--ifdef=NAME
Output merged file to show '#ifdef NAME' diffs.
For a POSIX diff
without this feature, diff -U 99999999 FileA FileB
will produce the entire contents of reasonable files, and then a simple script can insert a #if defined !Name
where -
starts in the first column, a #else
where a -
changes to a +
, a #if defined Name
where +
starts without a preceding -
, and a #endif
where +
or -
ends, along with removing the headers and the leading character of each line (a space for unchanged lines). That leaves the groups of lines reversed from what you show, so it has the old lines first and then the new lines. If that is not acceptable, you could swap the order of the files on the diff
command line and change the senses of -
and +
.