I'm trying to do something which I think should be pretty simple but has turned into quite a rabbit hole and I think there must be a better way.
Imagine you have two consecutive patch files (which represent sequential changes) to a source file but you don't have the source file itself.
How would you combine both patches into a single patch which represents the combined set of changes. The result of applying the combined patch to the source file should be identical to applying the two patches in sequence. All context should be preserved
Is there a well-known algorithm for this?
Example. Take these two patches
@@ -1,1 +1,2 @@
+ add this first line
this line is just context
@@ -1,2 +1,2 @@
- add this first line
+ change the first line
this line is just context
@@ -7,2 +7,2 @@
context
- change this line
+ to this one
more context
The result would be:
@@ -1,1 +1,2 @@
+ change the first line
this line is just context
@@ -7,2 +7,2 @@
context
- change this line
+ to this one
more context
The only tool/library I've found for this use case is this one, but in testing it has more than a few bugs and the code is too dense for me to sort out what the underlying algorithm is: https://github.com/twaugh/patchutils