Jump to content

Best way to patch files using only PHP


Eriksrocks

Recommended Posts

Before I get to the question let me explain what I am trying to do. I am working on a project that involves fixing bugs in a certain piece of web software and then distributing the fixes along with a script that will apply the fixes/patches to the user's installation of the software automatically. The core of this script will be the patching code that will take the fixes and apply them to the relevant files.

 

I am looking for opinions as to the best way to patch files using PHP. I was originally thinking of distributing the fixes as diff files. I've found a couple of pure-PHP implementations of the Unix utility patch online, and on *nix environments we could always use the shell execution functions to access patch directly. But then someone else on the project brought up an interesting point: Diff files are based on specific line numbers. It is easily conceivable that somebody wanting to use our patches could have already modified their files in some way, adding some custom code, fixing something else, whatever, and that would throw off the line numbers. I assume that in this case, patch would not work correctly because the line numbers would be different. We could always use simple find and replace functions, but I am wary of just doing a simple "find and replace" because of the possibility of replacing something we don't want to replace and the possibility of overlapping patches. In addition, diff files are easy to make, understand, and distribute.

 

He also said:

There are also natural search functions which let you allow a match if there is a 1% difference, or, say, up to X characters are different. I think we would write a custom function which uses a combination of str_replace(), isstr() which tells you the first occurrence of one string inside another, and maybe the natural search functions to make sure a good match is happening.

However I was unable to find what he was referring to either in the PHP manual or elsewhere online.

 

So, what do you think is the best way to patch files with PHP, keeping in mind my situation in which files could already be slightly modified? I could really use some help on this one. Thanks. :)

Link to comment
Share on other sites

I'm not sure there is any good way of doing this. Patches generally need to be applied to unmodified source. That's why any fixes should always be sent upstream, so they can be maintained buy the developers there.

 

As for modifications, as soon as you start down that path, its up to you to maintain your own work. If you want to be able to apply upstream patches along with your own work then you should likely keep your own work in patches too and apply them both (upstream and yours) to an unmodified source.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.