Patches are great for small bugfixes since they're easy to create and maintain. You should follow magento's official documentation to install the required composer plugin. Don't forget to add the required patch section on your composer.json or composer will not be able to see your patches.
- Go inside the folder that the file you need to fix is.
- Execute the
git init .
command. - Execute the
git add -A .
command. - Commit something. Ex:
git commit -m "start of patch"
After modifying the file that you need to patch you should add (git add
) and commit (git commit
) the changes.
After commiting your changes you must generate a new patch. You can do that by using this command:
git format-patch -1 HEAD
where -<n>
is the number of commits to be included in the patch and HEAD
can be changed to any previous commit sha hash.
When the patch is generated by git, the resulting file will not have the correct paths to the file due to the way in wich we created the repository. After creating a patch, you sould open it with a text editor (vim or emacs) and change only the paths tha are preceded by the letter "a" or "b". Ex:
diff --git a/File.php b/File.php
index 6fb4944..f49ea5d 100644
--- a/File.php
+++ b/File.php
Should be changed to
diff --git a/vendor/vendor-name/module-name/path/to/File.php b/vendor/vendor-name/module-name/path/to/File.php
index 6fb4944..f49ea5d 100644
--- a/vendor/vendor-name/module-name/path/to/File
+++ b/vendor/vendor-name/module-name/path/to/File
You should remove all the changes that you made to the file and remove the git repository that we created on the beginning (rm -rf .git
on the folder that you created the patch). After that, you should move your patch to a folder at the root of your project (i use the magento cloud default one called m2-hotfixes).
cd root/of/your/project
- execute the following command
git apply m2-hotfixes/path-to-patch.patch
If everything went well, you should be abble to see the changes on the target file that you created the patch for.
Now that you know that your patch works, you should add it to your composer.json file to autommatically install it when you run the composer install command.
- Install the patch composer module by executing this command
composer require cweagans/composer-patches
- Open the composer.json file on a text editor
- at the end of the file add the following:
"extra": {
"composer-exit-on-patch-failure": true,
"patches": {
"vendor-name/module-name": {
"brief description": "m2-hotfixes/file.patch"
}
}
}
- Run the "composer install" command and watch the magic happen ;D