Reputation: 926
I have a series of .patch files I need to apply to some code, but I don't have the original source that the patches were based on top of.
I've been applying them using 'git apply' but it's not applying all the changes (this is a separate issue).
I would be a lot easier for me to debug the problems with the 'git apply' (or apply them manually) if all of my .patch files were squashed into one big patch. Unfortunately, all the answers I can find on this site assume you can run git commands on the original source the patches were based on, which I can't.
I know the order of the patches as well.
Is there a way to 'squash' multiple patch files into one patch without the original source? Let me know if you need any extra information.
Here's an example of two patchfiles that I feel could be squashed:
$ cat patch1.patch
diff --git a/foo.c b/foo.c
index ...
--- a/foo.c
--- b/foo.c
@@ -1,2 +1,3 @@
int main(){
+printf("1");
}
$ cat patch2.patch
diff --git a/foo.c b/foo.c
index ...
--- a/foo.c
--- b/foo.c
@@ -1,3 +1,4 @@
int main(){
printf("1");
+printf("2");
}
$ merge-patches patch1.patch patch2.patch > patch1and2.patch # this command doesn't exist, but I want it to
$ cat patch1and2.patch
diff --git a/foo.c b/foo.c
index ...
--- a/foo.c
--- b/foo.c
@@ -1,2 +1,4 @@
int main(){
+printf("1");
+printf("2");
}
Upvotes: 3
Views: 1769
Reputation: 488183
Is there a way to 'squash' multiple patch files into one patch without the original source?
In the fully general case, no. Lucky for you, you don't have that fully general case. 😀
A pretty simple "patch algebra" works for simple cases. Suppose, for instance, that we're worried about a single file. Patch A says "add two lines after line 3" and patch B says "add one line after line 10". If patch A comes before patch B, we can combine them into one patch that says "add these two lines after line 3, and this one other line after line 8" because we know that what was line 10 in patch B must have been line 8 in patch A, since patch A added two lines, all of which are before line 8.
If the two patches go in the other order, the combined patch needs to add the lines at positions 3 and 10, not 3 and 8. Things get considerably uglier when patches overlap, but you can still make some cases work even if you don't know the right order, as the context might provide the right order.
I know the order of the patches as well.
This helps a lot, because now we know that A comes before B, for sure: there is no need to guess based on context.
So, when later patches modify earlier patches, and we know they all apply to a fixed initial base, we can combine them, provided we know the order. This potentially drops information (i.e., before combining, you might be able to find the true base revision; after combining, you might no longer be able to do that).1 If you're OK with that, you can write a program that does this. I do not know of any existing programs that do it, though DARCS is built with the concept of patch algebra and is therefore a candidate. You might look at http://darcs.net/Theory (I haven't looked much myself).
1I have not constructed a proof for this, but it's intuitively obvious ... which means it could be completely wrong. 😀
Upvotes: 3