If I have a patch file in the unified patch format like the example below, how do I add lines to it so that those lines are then added by the (applied) patch?

--- lao 2002-02-21 23:30:39.942229878 -0800
+++ tzu 2002-02-21 23:30:50.442260588 -0800
@@ -1,7 +1,6 @@
-The Way that can be told of is not the eternal Way;
-The name that can be named is not the eternal name.
The Nameless is the origin of Heaven and Earth;
-The Named is the mother of all things.
+The named is the mother of all things.
Therefore let there always be non-being,
so we may see their subtlety,
And let there always be being,
@@ -9,3 +8,6 @@
The two are the same,
But after they are produced,
they have different names.
+They both may be called deep and profound.
+Deeper and more profound,
+The door of all subtleties!
  • What do you mean by “add lines”? What do you want to add? Do you want to change the patch into a patch that makes a different modification? How do you expect to describe the modifications? – Gilles 'SO- stop being evil' Jul 19 '16 at 23:42
  • I think I want to add a few more additions to a hunk. – Xofo Jul 20 '16 at 0:14
  • Do you want to edit the file manually? Or are you looking for a tool to modify the patch with instructions like “add three more lines after line 4”? – Gilles 'SO- stop being evil' Jul 20 '16 at 0:20

Short: you don't do this.

Long: Here's why

  • A unified-diff is a script, using line-numbers and counts which tie-in to the content of the diff.
  • It's possible to manually make simple changes to this script (I do...), but
  • A unified-diff is mainly useful with the patch program, and
  • patch checks for consistency between the parts of a diff, and
  • patch will reject the parts which it finds are inconsistent.

These lines contain line-numbers and counts:

@@ -1,7 +1,6 @@
@@ -9,3 +8,6 @@

and the counts must match the number of leading + or - marks in the remaining lines of the diff.

So you don't do this manually. Someone could write a program to do this. However, it's not in patchutils (someone's idea of useful applications for manipulating patch-files), though you might find its rediff and combinediff scripts helpful in hand-editing patch-files.

There are few applications which deal with patch-files in any case (perhaps someone has written one).

The usual practice here is to regenerate the diff by

  1. get the original sources (before the patch)
  2. make a patched version of the sources (applying the patch)
  3. make your additional changes
  4. construct a new patch (across both sets of changes), or incremental patch (for just your changes).

That's simple to do with diff and patch. Because it's simple, not many people will write tools to do this.

Further reading:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.