Patching Files In Unix
Ever needed to create or apply a patch? diff, cvs diff, svn diff and patch make this very simple.
For this faux 'demonstration' lets say we have two files, 'their' file and 'mine' which contains our recent changes. Perhaps their.php contains an unfinished script of:
<?php
if ($foo) {
// @todo
}Our new document might contain:
<?php
if ($foo) {
echo 'Finished';
}Creating A Patch From Two Files
We simply pass the original filename, then our file, finally redirecting the output to a patch name of our choice which is 'mypatch.patch' in this case. the -up options output the diff in a format which provides context lines which are useful for merging, by default this provides 3 lines of surrounding context.
I should mention that a good convention to following when patching Drupal or Drupal contrib is the following syntax of
$ diff -up theirs.php mine.php > mypatch.patchApplying The Patch
To apply our simple patch we pass the original filename, then our patch's filename. We can then verify the patch and remove the patch file.
$ patch theirs.php mypatch.patchReversing A Patch
If necessary we can reverse the patch using the -R option.
$ patch -R theirs.php mypatch.patch
Delicious
Digg
StumbleUpon
Reddit
Facebook