Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

How to apply a patch: Difference between revisions

From MoodleDocs
(Improve documentation a bit)
(Added section on potential problems and how to deal with them)
Line 15: Line 15:


     cd \moodle
     cd \moodle
     c:\bin\patch.exe -p1 < mynewpatch.diff
     c:\bin\patch.exe --dry-run -p1 < mynewpatch.diff


:The number after <tt>'-p'</tt> option can vary depending on the patch file, as it depends on the way the patch file was generated. Have a look at the [http://www.rt.com/man/patch.1.html 'patch' utility manual page ] to see how the <tt>'-p'</tt> option works. You could also have a look at this [http://www.linuxtutorialblog.com/post/introduction-using-diff-and-patch-tutorial diff and patch tutorial].
:The number after <tt>'-p'</tt> option can vary depending on the patch file, as it depends on the way the patch file was generated. Have a look at the [http://www.rt.com/man/patch.1.html 'patch' utility manual page ] to see how the <tt>'-p'</tt> option works. You could also have a look at this [http://www.linuxtutorialblog.com/post/introduction-using-diff-and-patch-tutorial diff and patch tutorial].
Line 28: Line 28:
     Hunk #1 succeeded at 430 (offset 2 lines).
     Hunk #1 succeeded at 430 (offset 2 lines).


:If you get any <tt>'Hunk #n succeeded...'</tt> messages, the patch was applied correctly although at different line numbers than the original file. In this case the 'patch' command will have created an additional file for each of the files where the hunk was applied at a different offset, that is named like the original file with the additional extension <tt>.orig</tt>. You can delete those files safely.
:At this stage the patch <b>has not been applied</b>. We just simulated the application (with the <tt>'--dry-run'</tt> option), to see if we are going to find any problems with it. Before explaining how to actually apply the patch, we are going to talk about what could be wrong, and how to deal with it.
 
===Potential problems and how to deal with them===
====Potential problems====
If everything goes well, the patch will apply cleanly and life should be good! But sometimes the patch will not apply 100% cleanly due to a version mismatch between the original files used to produce the patch file and your local files. In this case, the 'patch' command will try to apply as many changes as it can, and will emit some diagnostics describing the problems it encounters.
 
* If you get any <tt>'Hunk #n succeeded...'</tt> messages, the patch would have been applied correctly although at different line numbers than the original file. If we had actually applied the patch, the 'patch' command would have created an additional file for each of the files where the hunk was applied at a different offset, that would be named like the original file with the additional extension <tt>.orig</tt>.
 
* If you get any <tt>'Hunk #n failed...'</tt> messages, the patch would have not applied correctly. In this case the 'patch' command would have created two additional files for each of the files where the hunk was not applied correctly, called:
** <tt>original-file-name.orig</tt>&nbsp; &nbsp; This would be the original file before the patch was applied, just like above.
** <tt>original-file-name.rej&nbsp;</tt>&nbsp; &nbsp; This file would contain the hunks that could not be applied correctly, so you could inspect them.
 
====Dealing with potential problems====
Dealing with the first problem (the offsetted hunks) is trivial: we just need to delete the .orig files once we actually apply the patch.
 
In the second case (failed hunks), unless you know how to fix the failed hunks by hand, you <b>should not apply</b> the patch, as that would corrupt your Moodle install. If you want to apply the patch and try to fix the failed hunks by hand, you should use the <tt>'-b'</tt> option. That option automatically makes a backup of every file the patch applies to, with the <tt>.orig</tt> extension. That would allow you go back to the original files state by simply overwritting the modified files with their <tt>.orig</tt> backups.
 
===Actually applying the patch===
Now that we know what could go wrong and how to deal with it, let's see how to apply the patch. We only need to remove the <tt>'--dry-run'</tt>:
 
    cd \moodle
    c:\bin\patch.exe -p1 < mynewpatch.diff


:If you get any <tt>'Hunk #n failed...'</tt> messages, the patch could not be applied correctly. In this case the 'patch' command will have created two additional files for each of the files where the hunk was not applied correctly, called:
and optionally use the <tt>'-b'</tt> option if we are going to try to fix the failed hunks by hand:
:* <tt>original-file-name.orig</tt>&nbsp; &nbsp; This is the original file before the patch was applied, just like above.
:* <tt>original-file-name.rej&nbsp;</tt>&nbsp; &nbsp; This file contains the hunks that could not be applied correctly, so you can inspect them.


: In this case, unless you know how to fix the failed hunks by hand, you should delete the partially patched file (the one with the original file name) and rename 'original-file-name.orig' to 'original-file-name', to restore the original file. You should also delete the .rej file.
    cd \moodle
    c:\bin\patch.exe -b -p1 < mynewpatch.diff


==Apply a Patch in Linux using "patch"==
==Apply a Patch in Linux using "patch"==

Revision as of 15:45, 28 August 2008

Introduction

This page explains how you can apply a patch file. Patch is a standard format, and there are many options for how to apply one. Pick the one that is easiest for you.

Apply a Patch in Windows using gnuwin32

  • Download and extract patch for windows from sourceforge I placed the patch.exe binary in C:\bin
  • Download and extract Moodle somewhere. eg: C:\moodle
  • Download the patch file and place it in the same directory you put Moodle (C:\moodle\password-policy-17.diff)
  • Open the patch file with Wordpad, and click 'File' >> 'Save as...', choose a different name for the file eg ('mynewpatch.diff') and "Save as type" >> 'Text Document - MS-DOS Format'
  • Open up a command text window, and type:
   cd \moodle
   c:\bin\patch.exe --dry-run -p1 < mynewpatch.diff
The number after '-p' option can vary depending on the patch file, as it depends on the way the patch file was generated. Have a look at the 'patch' utility manual page to see how the '-p' option works. You could also have a look at this diff and patch tutorial.
  • You should get an output similar to this (the names and quantity of patched files vary from patch to patch):
   patching file admin/settings/security.php
   patching file lang/en_utf8/admin.php
   patching file lib/moodlelib.php
   patching file login/change_password.php
   patching file login/signup.php
   patching file user/edit.php
   Hunk #1 succeeded at 430 (offset 2 lines).
At this stage the patch has not been applied. We just simulated the application (with the '--dry-run' option), to see if we are going to find any problems with it. Before explaining how to actually apply the patch, we are going to talk about what could be wrong, and how to deal with it.

Potential problems and how to deal with them

Potential problems

If everything goes well, the patch will apply cleanly and life should be good! But sometimes the patch will not apply 100% cleanly due to a version mismatch between the original files used to produce the patch file and your local files. In this case, the 'patch' command will try to apply as many changes as it can, and will emit some diagnostics describing the problems it encounters.

  • If you get any 'Hunk #n succeeded...' messages, the patch would have been applied correctly although at different line numbers than the original file. If we had actually applied the patch, the 'patch' command would have created an additional file for each of the files where the hunk was applied at a different offset, that would be named like the original file with the additional extension .orig.
  • If you get any 'Hunk #n failed...' messages, the patch would have not applied correctly. In this case the 'patch' command would have created two additional files for each of the files where the hunk was not applied correctly, called:
    • original-file-name.orig    This would be the original file before the patch was applied, just like above.
    • original-file-name.rej     This file would contain the hunks that could not be applied correctly, so you could inspect them.

Dealing with potential problems

Dealing with the first problem (the offsetted hunks) is trivial: we just need to delete the .orig files once we actually apply the patch.

In the second case (failed hunks), unless you know how to fix the failed hunks by hand, you should not apply the patch, as that would corrupt your Moodle install. If you want to apply the patch and try to fix the failed hunks by hand, you should use the '-b' option. That option automatically makes a backup of every file the patch applies to, with the .orig extension. That would allow you go back to the original files state by simply overwritting the modified files with their .orig backups.

Actually applying the patch

Now that we know what could go wrong and how to deal with it, let's see how to apply the patch. We only need to remove the '--dry-run':

   cd \moodle
   c:\bin\patch.exe -p1 < mynewpatch.diff

and optionally use the '-b' option if we are going to try to fix the failed hunks by hand:

   cd \moodle
   c:\bin\patch.exe -b -p1 < mynewpatch.diff

Apply a Patch in Linux using "patch"

use something like: patch -p1 < patchfile.diff see here for more details on using Patch in Linux

See Also