Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging PHP changes with diff/patch

I have three PHP files:

  • 1.php - Version 1.0 of OSS software (vanilla)
  • 1a.php - Version 1.0 of OSS software (with customisations)
  • 2.php - Version 1.1 of OSS software (vanilla)

I would like to use diff/patch (or similar) to create a single patch file that will merge the modifications I made to version 1.0 with the upstream changes made in version 1.1. What is the best way to achieve this?

Many thanks.

like image 807
gjb Avatar asked Dec 10 '10 18:12

gjb


People also ask

What is git diff patch?

With the git diff command, you can create a record of how the file has changed, and the owner of a repository can use the patch command to "replay" those changes over the old version to bring it up to date with the new version.

How do I apply a diff patch?

The "diff" tool calculates the differences between two text files. That difference is called a patch. You can apply a patch to another file using the "patch" tool. diff and patch are intended to be used on text files.

How do I merge patch files?

Make a new branch starting from the revision just before the first changeset. In the new branch, merge each changeset of the issue, in order. Take a diff between the start of the new branch and the final result. (If you do issue-based branching, you'd get the above situation automatically).


1 Answers

Use this:

$ diff -u 1.php 1a.php > customizations.patch

Which will give you a unified diff of all the changes between your vanilla and customized copy of version 1.0 of the file.

You can try to apply these changes to the new file, 2.php, like this:

$ patch -p0 2.php customizations.patch

Note, however, that this will likely fail with lots of rejected hunks if the source code has changed too much. Still, just getting that unified diff might prove to be useful since you can then manually reintegrate your customizations to fit the new source code.

What I would do, if it is possible, is to get a local copy of the version control repository that the free software project uses. Then, make a branch of it and integrate your customizations, and then see if you can bring things forward by merging the upstream commits since that time into your branch. Without seeing all of the stuff involved, I can neither tell you that it will be a trivial nor complex process, but it might be easier than just using diff and patch.

Note that this won't work for multiple files; if you need to do this in the future with multiple files, you'll want to use diff against two different sets of files (with the same filename) in two different directory trees. I thought it might not be a bad idea to mention this now just in case it proves to be a situation that you encounter in the future.

like image 179
Michael Trausch Avatar answered Sep 25 '22 23:09

Michael Trausch