Development:How to apply a patch
Note: You are currently viewing documentation for Moodle 2.2. Up-to-date documentation for the latest stable version is available here: How to apply a patch.
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.
Perhaps most critical is the usage of the -p flag, which tells patch about the relationship between the directory where the patch file is located and the files that will be patched. See the references below for details and DO NOT assume anything.
Apply a Patch in Windows using gnuwin32
- Download and extract patch for windows from sourceforge I placed the patch.exe binary in C:\bin (NOTE: Things are a lot easier if you put it in a directory that doesn't have white spaces in it.)
- 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
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.
Sometimes, there will be a large difference in line numbers since a patch was generated and the patch will not apply. You can tell patch allow larger differences in line numbers by using the fuzz option '-F' to increase the number of lines difference there can be. For example patch -F 100 would allow 100 lines difference.
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 Windows using NetBeans
NetBeans comes with integrated tools for version control and developer collaboration which include powerful graphical Diff tools and easily applying (and creating) patches. You can apply any patch to file from the file's context menu.
Apply a Patch in Linux using "patch"
use something like: patch -p1 < patchfile.diff see here for more details on using Patch in Linux