Reputation: 38619
My question is similar to Interactive program to selectively exclude parts of a diff file - but somewhat different...
Say I have the following "original" file:
$ cat test-orig.h
int varA;
int varB;
int varC;
int varD;
int varE;
... and say I've made these changes into a "new" file:
$ cat test-newmodified.h
int varA;
int varB;
int varC;
// a couple of lines
// of useless comments
int var_extra1;
int var_extra2;
int varD;
int varE;
Then the diff between the two will be:
$ diff -Naur test-orig.h test-newmodified.h | tee test.patch
--- test-orig.h 2013-07-18 19:21:25.741027138 +0200
+++ test-newmodified.h 2013-07-18 19:21:19.916998200 +0200
@@ -1,5 +1,9 @@
int varA;
int varB;
int varC;
+// a couple of lines
+// of useless comments
+int var_extra1;
+int var_extra2;
int varD;
int varE;
Let's say then, I have a patch file thus obtained, (with many hunks), and I'd want to delete the "lines of useless comments". It's usually easy to delete a whole hunk from a diff
generated patch file - but if I want to delete just some lines, then the diff counters (above in @@ -1,5 +1,9 @@
) would have to be modified too. Say, if the comments line were erased in the file, the diff would then be:
$ diff -Naur test-orig.h test-newmodified-nc.h
--- test-orig.h 2013-07-18 19:21:25.741027138 +0200
+++ test-newmodified-nc.h 2013-07-18 19:26:30.898540270 +0200
@@ -1,5 +1,7 @@
int varA;
int varB;
int varC;
+int var_extra1;
+int var_extra2;
int varD;
int varE;
... that is, the counters with comments, @@ -1,5 +1,9 @@
- now became, without comments, @@ -1,5 +1,7 @@
.
If I just delete the comment lines from the patch test.patch
(and save that edit as test-edit.patch
), and do not update the counters, then I get:
$ patch -p0 <test-edit.patch
patching file test-orig.h
patch: **** malformed patch at line 10:
... and such a patch doesn't get applied. If, then, I just change the +1,9
to +1.7
in the test-edit.patch
, then it applies cleanly:
$ patch -p0 <test-edit.patch
patching file test-orig.h
... and test-orig.h
is changed as expected (without the comment lines).
So, given a patch file, where all hunks related to a file are contained - is there a (GUI) (text) editor which is diff
aware (at least, unified diff), such that: when lines from a hunk (or entire hunks) are deleted, it will automatically update the diff counters - so that the edited patch file will still apply cleanly to the original file?
Upvotes: 4
Views: 2210
Reputation: 38619
Well, I now know I can at least use emacs
for this, thanks to this answer: #9740489 How to edit a diff/patch file cleanly ? Are there any patch file editors?:
If you open a diff file in emacs and put the editor in "diff" mode you can actually edit patches and it will update the hunk markers in a smart way
Note that on my system (Ubuntu Lucid), sudo apt-get install emacs
will, after installation, make the emacs
command eventually symlink to emacs23-x
, which runs in its own window (not in terminal). So I can just call:
$ emacs test.patch
... and emacs will automatically start in "diff" mode:
Then you can select lines using the mouse, but you cannot delete them using Del/Backspace - you have to cut a selection using Ctrl-W (see In Emacs, how do I cut, paste, and copy a block of text? - Knowledge Base). As it can be seen from the screenshot, markers are automatically updated. Also, do not go in to "diff" mode using M-x diff
(see Comparing Files - GNU Emacs Manual; on my machine, M-x is Alt-X) - it will then start asking about "original" and "new" input file paths to perform diff
on them, which is not what we want (given the patch file is already loaded). Finally, the GUI menu has File/Save As (and C-x C-w
shortcut for it) - clicking on the GUI menu raises the usual file dialog, where the edited file can be renamed on save.
Still, would like to know if there are alternatives to this...
Upvotes: 2